blob: 9262e5e834c85c27f41565a0783e3dd1a2c88e08 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +0000132 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Sunil Ravi036cec52023-03-29 11:35:17 -0700168static int wpas_p2p_disallowed_freq(struct wpa_global *global,
169 unsigned int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170
171
Hai Shalomc1a21442022-02-04 13:43:00 -0800172static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
173{
174 int he_capab = 0;
175
176 if (mode)
177 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
178 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
179 return he_capab;
180}
181
182
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700183/*
184 * Get the number of concurrent channels that the HW can operate, but that are
185 * currently not in use by any of the wpa_supplicant interfaces.
186 */
187static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
188{
189 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800190 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700191
192 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
193 if (!freqs)
194 return -1;
195
196 num = get_shared_radio_freqs(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000197 wpa_s->num_multichan_concurrent, false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198 os_free(freqs);
199
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800200 unused = wpa_s->num_multichan_concurrent - num;
201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
202 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700203}
204
205
206/*
207 * Get the frequencies that are currently in use by one or more of the virtual
208 * interfaces, and that are also valid for P2P operation.
209 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210static unsigned int
211wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
212 struct wpa_used_freq_data *p2p_freqs,
213 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700215 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700216 unsigned int num, i, j;
217
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700218 freqs = os_calloc(wpa_s->num_multichan_concurrent,
219 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700220 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700221 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700222
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700223 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000224 wpa_s->num_multichan_concurrent,
225 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700226
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700227 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228
229 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700230 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700231 p2p_freqs[j++] = freqs[i];
232 }
233
234 os_free(freqs);
235
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700236 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800237
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700238 return j;
239}
240
241
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700242static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
243 int freq)
244{
245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
246 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700247
248 /* Use the wpa_s used to control the P2P Device operation */
249 wpa_s = wpa_s->global->p2p_init_wpa_s;
250
251 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800252 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
253 wpas_p2p_num_unused_channels(wpa_s) > 0) {
254 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
255 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700256 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800257 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700258 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
259}
260
261
Hai Shalom60840252021-02-19 19:02:11 -0800262static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
263{
264 unsigned int delay = wpas_p2p_search_delay(wpa_s);
265
266 /* In case of concurrent P2P and external scans, delay P2P search. */
267 if (external_scan_running(wpa_s->radio)) {
268 delay = wpa_s->conf->p2p_search_delay;
269 wpa_printf(MSG_DEBUG,
270 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
271 delay);
272 }
273
274 p2p_scan_res_handled(wpa_s->global->p2p, delay);
275}
276
277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
279 struct wpa_scan_results *scan_res)
280{
281 size_t i;
282
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 if (wpa_s->p2p_scan_work) {
284 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
285 wpa_s->p2p_scan_work = NULL;
286 radio_work_done(work);
287 }
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
290 return;
291
292 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
293 (int) scan_res->num);
294
295 for (i = 0; i < scan_res->num; i++) {
296 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800297 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700298 const u8 *ies;
299 size_t ies_len;
300
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800301 time_tmp_age.sec = bss->age / 1000;
302 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800303 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700304
305 ies = (const u8 *) (bss + 1);
306 ies_len = bss->ie_len;
307 if (bss->beacon_ie_len > 0 &&
308 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
309 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
310 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
311 MACSTR, MAC2STR(bss->bssid));
312 ies = ies + ies_len;
313 ies_len = bss->beacon_ie_len;
314 }
315
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800318 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700319 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 break;
321 }
322
Hai Shalom60840252021-02-19 19:02:11 -0800323 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324}
325
326
Hai Shalom60840252021-02-19 19:02:11 -0800327static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700328{
Hai Shalom60840252021-02-19 19:02:11 -0800329 if (wpa_s->p2p_scan_work) {
330 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700331
Hai Shalom60840252021-02-19 19:02:11 -0800332 wpa_s->p2p_scan_work = NULL;
333 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700334 }
335
Hai Shalom60840252021-02-19 19:02:11 -0800336 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
337 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700338
Hai Shalom60840252021-02-19 19:02:11 -0800339 wpa_dbg(wpa_s, MSG_DEBUG,
340 "P2P: Failed to get scan results - try to continue");
341 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700342}
343
344
Sunil Ravi77d572f2023-01-17 23:58:31 +0000345void wpas_p2p_scan_freqs(struct wpa_supplicant *wpa_s,
346 struct wpa_driver_scan_params *params,
347 bool include_6ghz)
348{
349 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
350 params, false, false, false);
351 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
352 params, false, false, false);
353 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD,
354 params, false, false, false);
355 if (!wpa_s->conf->p2p_6ghz_disable &&
356 is_p2p_allow_6ghz(wpa_s->global->p2p) && include_6ghz)
357 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
358 params, true, true, false);
359}
360
361
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800362static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
363{
364 struct wpa_supplicant *wpa_s = work->wpa_s;
365 struct wpa_driver_scan_params *params = work->ctx;
366 int ret;
367
368 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800369 if (!work->started) {
370 wpa_scan_free_params(params);
371 return;
372 }
373
374 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 return;
376 }
377
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800378 if (wpa_s->clear_driver_scan_cache) {
379 wpa_printf(MSG_DEBUG,
380 "Request driver to clear scan cache due to local BSS flush");
381 params->only_new_results = 1;
382 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700383
Sunil Ravi77d572f2023-01-17 23:58:31 +0000384 if (!params->freqs)
385 wpas_p2p_scan_freqs(wpa_s, params, params->p2p_include_6ghz);
386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800387 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800388 if (ret == 0)
389 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 wpa_scan_free_params(params);
391 work->ctx = NULL;
392 if (ret) {
393 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800394 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800395 return;
396 }
397
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800398 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800399 os_get_reltime(&wpa_s->scan_trigger_time);
400 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800401 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800402 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800403 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800404 wpa_s->p2p_scan_work = work;
405}
406
407
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
409 int freq)
410{
411 if (wpa_s->global->p2p_24ghz_social_channels &&
412 (freq == 2412 || freq == 2437 || freq == 2462)) {
413 /*
414 * Search all social channels regardless of whether these have
415 * been disabled for P2P operating channel use to avoid missing
416 * peers.
417 */
418 return 1;
419 }
420 return p2p_supported_freq(wpa_s->global->p2p, freq);
421}
422
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
425 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800426 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
427 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428{
429 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800430 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700432 unsigned int num_channels = 0;
433 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700435 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800436 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437
438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
439 return -1;
440
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800441 if (wpa_s->p2p_scan_work) {
442 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
443 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700444 }
445
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800446 params = os_zalloc(sizeof(*params));
447 if (params == NULL)
448 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449
450 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800451 params->num_ssids = 1;
452 n = os_malloc(P2P_WILDCARD_SSID_LEN);
453 if (n == NULL)
454 goto fail;
455 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
456 params->ssids[0].ssid = n;
457 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458
459 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700460 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
461 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 num_req_dev_types, req_dev_types);
463 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800464 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000465
466 /*
467 * In case 6 GHz channels are requested as part of the P2P scan, only
468 * the PSCs would be included as P2P GOs are not expected to be
469 * collocated, i.e., they would not be announced in the RNR element of
470 * other APs.
471 */
Hai Shalomc1a21442022-02-04 13:43:00 -0800472 if (!wpa_s->conf->p2p_6ghz_disable)
473 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 switch (type) {
475 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
477 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800478 if (params->freqs == NULL)
479 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700480 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481 if (wpas_p2p_search_social_channel(
482 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700483 params->freqs[num_channels++] =
484 social_channels_freq[i];
485 }
486 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 break;
488 case P2P_SCAN_FULL:
489 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800490 case P2P_SCAN_SPECIFIC:
491 params->freqs = os_calloc(2, sizeof(int));
492 if (params->freqs == NULL)
493 goto fail;
494 params->freqs[0] = freq;
495 params->freqs[1] = 0;
496 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700498 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
499 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800500 if (params->freqs == NULL)
501 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700502 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 if (wpas_p2p_search_social_channel(
504 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700505 params->freqs[num_channels++] =
506 social_channels_freq[i];
507 }
508 if (p2p_supported_freq(wpa_s->global->p2p, freq))
509 params->freqs[num_channels++] = freq;
510 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 break;
512 }
513
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800514 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
515 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
516 if (ies == NULL) {
517 wpabuf_free(wps_ie);
518 goto fail;
519 }
520 wpabuf_put_buf(ies, wps_ie);
521 wpabuf_free(wps_ie);
522
523 bands = wpas_get_bands(wpa_s, params->freqs);
524 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
525
526 params->p2p_probe = 1;
527 n = os_malloc(wpabuf_len(ies));
528 if (n == NULL) {
529 wpabuf_free(ies);
530 goto fail;
531 }
532 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
533 params->extra_ies = n;
534 params->extra_ies_len = wpabuf_len(ies);
535 wpabuf_free(ies);
536
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800537 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800538 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
539 params) < 0)
540 goto fail;
541 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543fail:
544 wpa_scan_free_params(params);
545 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546}
547
548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
550{
551 switch (p2p_group_interface) {
552 case P2P_GROUP_INTERFACE_PENDING:
553 return WPA_IF_P2P_GROUP;
554 case P2P_GROUP_INTERFACE_GO:
555 return WPA_IF_P2P_GO;
556 case P2P_GROUP_INTERFACE_CLIENT:
557 return WPA_IF_P2P_CLIENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000558 default:
559 return WPA_IF_P2P_GROUP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561}
562
563
564static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
565 const u8 *ssid,
566 size_t ssid_len, int *go)
567{
568 struct wpa_ssid *s;
569
570 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
571 for (s = wpa_s->conf->ssid; s; s = s->next) {
572 if (s->disabled != 0 || !s->p2p_group ||
573 s->ssid_len != ssid_len ||
574 os_memcmp(ssid, s->ssid, ssid_len) != 0)
575 continue;
576 if (s->mode == WPAS_MODE_P2P_GO &&
577 s != wpa_s->current_ssid)
578 continue;
579 if (go)
580 *go = s->mode == WPAS_MODE_P2P_GO;
581 return wpa_s;
582 }
583 }
584
585 return NULL;
586}
587
588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
590{
591 struct wpa_supplicant *wpa_s = eloop_ctx;
592 wpa_printf(MSG_DEBUG,
593 "P2P: Complete previously requested removal of %s",
594 wpa_s->ifname);
595 wpas_p2p_disconnect(wpa_s);
596}
597
598
599static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
600 struct wpa_supplicant *calling_wpa_s)
601{
602 if (calling_wpa_s == wpa_s && wpa_s &&
603 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
604 /*
605 * The calling wpa_s instance is going to be removed. Do that
606 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700607 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608 * control interface responses on the per-interface socket.
609 */
610 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
611 wpa_s, NULL) < 0)
612 return -1;
613 return 0;
614 }
615
616 return wpas_p2p_disconnect(wpa_s);
617}
618
619
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800620/* Determine total number of clients in active groups where we are the GO */
621static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
622{
623 unsigned int count = 0;
624 struct wpa_ssid *s;
625
626 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
627 for (s = wpa_s->conf->ssid; s; s = s->next) {
628 wpa_printf(MSG_DEBUG,
629 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
630 wpa_s, s, s->disabled, s->p2p_group,
631 s->mode);
632 if (!s->disabled && s->p2p_group &&
633 s->mode == WPAS_MODE_P2P_GO) {
634 count += p2p_get_group_num_members(
635 wpa_s->p2p_group);
636 }
637 }
638 }
639
640 return count;
641}
642
643
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800644static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
645{
646 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
647 !wpa_s->current_ssid->disabled &&
648 wpa_s->current_ssid->p2p_group &&
649 wpa_s->current_ssid->p2p_persistent_group;
650}
651
652
653static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
654{
655 return p2p_is_active_persistent_group(wpa_s) &&
656 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
657}
658
659
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660/* Find an interface for a P2P group where we are the GO */
661static struct wpa_supplicant *
662wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
663{
664 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665
666 if (!wpa_s)
667 return NULL;
668
669 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800670 if (!p2p_is_active_persistent_go(wpa_s))
671 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800672
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800673 /* Prefer a group with connected clients */
674 if (p2p_get_group_num_members(wpa_s->p2p_group))
675 return wpa_s;
676 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677 }
678
679 /* No group with connected clients, so pick the one without (if any) */
680 return save;
681}
682
683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800684static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800685{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 return p2p_is_active_persistent_group(wpa_s) &&
687 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
688}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800689
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800690
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800691/* Find an interface for a P2P group where we are the P2P Client */
692static struct wpa_supplicant *
693wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
694{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800695 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800696 if (p2p_is_active_persistent_cli(wpa_s))
697 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800698 }
699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800700 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800701}
702
703
704/* Find a persistent group where we are the GO */
705static struct wpa_ssid *
706wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
707{
708 struct wpa_ssid *s;
709
710 for (s = wpa_s->conf->ssid; s; s = s->next) {
711 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
712 return s;
713 }
714
715 return NULL;
716}
717
718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
720 unsigned int *force_freq,
721 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800722{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800723 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 struct wpa_ssid *s;
725 u8 conncap = P2PS_SETUP_NONE;
726 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800727 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800728 struct wpa_ssid *persistent_go;
729 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000730 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
731 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732
733 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
734
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800735 if (force_freq)
736 *force_freq = 0;
737 if (pref_freq)
738 *pref_freq = 0;
739
740 size = P2P_MAX_PREF_CHANNELS;
741 if (force_freq && pref_freq &&
742 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
743 (int *) pref_freq, 0, pref_freq_list, &size))
744 wpas_p2p_set_own_freq_preference(wpa_s,
745 *force_freq ? *force_freq :
746 *pref_freq);
747
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800748 /*
749 * For non-concurrent capable devices:
750 * If persistent_go, then no new.
751 * If GO, then no client.
752 * If client, then no GO.
753 */
754 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800755 if (go_wpa_s)
756 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800757 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800758 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
759 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800760
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800761 wpa_printf(MSG_DEBUG,
762 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
763 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800764
765 /* If not concurrent, restrict our choices */
766 if (p2p_no_group_iface) {
767 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
768
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800769 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800770 return P2PS_SETUP_NONE;
771
772 if (go_wpa_s) {
773 if (role == P2PS_SETUP_CLIENT ||
774 incoming == P2PS_SETUP_GROUP_OWNER ||
775 p2p_client_limit_reached(go_wpa_s->p2p_group))
776 return P2PS_SETUP_NONE;
777
778 return P2PS_SETUP_GROUP_OWNER;
779 }
780
781 if (persistent_go) {
782 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
783 if (!incoming)
784 return P2PS_SETUP_GROUP_OWNER |
785 P2PS_SETUP_CLIENT;
786 if (incoming == P2PS_SETUP_NEW) {
787 u8 r;
788
789 if (os_get_random(&r, sizeof(r)) < 0 ||
790 (r & 1))
791 return P2PS_SETUP_CLIENT;
792 return P2PS_SETUP_GROUP_OWNER;
793 }
794 }
795 }
796 }
797
798 /* If a required role has been specified, handle it here */
799 if (role && role != P2PS_SETUP_NEW) {
800 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
802 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
803 /*
804 * Peer has an active GO, so if the role allows it and
805 * we do not have any active roles, become client.
806 */
807 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
808 !cli_wpa_s)
809 return P2PS_SETUP_CLIENT;
810
811 /* fall through */
812
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 case P2PS_SETUP_NONE:
814 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 conncap = role;
816 goto grp_owner;
817
818 case P2PS_SETUP_GROUP_OWNER:
819 /*
820 * Must be a complimentary role - cannot be a client to
821 * more than one peer.
822 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800823 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800824 return P2PS_SETUP_NONE;
825
826 return P2PS_SETUP_CLIENT;
827
828 case P2PS_SETUP_CLIENT:
829 /* Must be a complimentary role */
830 if (incoming != role) {
831 conncap = P2PS_SETUP_GROUP_OWNER;
832 goto grp_owner;
833 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700834 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800835
836 default:
837 return P2PS_SETUP_NONE;
838 }
839 }
840
841 /*
842 * For now, we only will support ownership of one group, and being a
843 * client of one group. Therefore, if we have either an existing GO
844 * group, or an existing client group, we will not do a new GO
845 * negotiation, but rather try to re-use the existing groups.
846 */
847 switch (incoming) {
848 case P2PS_SETUP_NONE:
849 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800850 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800851 conncap = P2PS_SETUP_GROUP_OWNER;
852 else if (!owned_members)
853 conncap = P2PS_SETUP_NEW;
854 else if (incoming == P2PS_SETUP_NONE)
855 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
856 else
857 conncap = P2PS_SETUP_CLIENT;
858 break;
859
860 case P2PS_SETUP_CLIENT:
861 conncap = P2PS_SETUP_GROUP_OWNER;
862 break;
863
864 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800865 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800866 conncap = P2PS_SETUP_CLIENT;
867 break;
868
869 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
870 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800871 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800872 conncap = P2PS_SETUP_GROUP_OWNER;
873 else {
874 u8 r;
875
876 if (os_get_random(&r, sizeof(r)) < 0 ||
877 (r & 1))
878 conncap = P2PS_SETUP_CLIENT;
879 else
880 conncap = P2PS_SETUP_GROUP_OWNER;
881 }
882 break;
883
884 default:
885 return P2PS_SETUP_NONE;
886 }
887
888grp_owner:
889 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
890 (!incoming && (conncap & P2PS_SETUP_NEW))) {
891 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
892 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800893
894 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800895 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800896 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800897 wpa_s->p2p_mgmt ?
898 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800899 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800900 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800901 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800902 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800903 wpa_printf(MSG_ERROR,
904 "P2P: Failed to allocate a new interface for the group");
905 return P2PS_SETUP_NONE;
906 }
907 wpa_s->global->pending_group_iface_for_p2ps = 1;
908 p2p_set_intended_addr(wpa_s->global->p2p,
909 wpa_s->pending_interface_addr);
910 }
911 }
912
913 return conncap;
914}
915
916
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700917static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
918 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919{
920 struct wpa_ssid *ssid;
921 char *gtype;
922 const char *reason;
923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 ssid = wpa_s->current_ssid;
925 if (ssid == NULL) {
926 /*
927 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700928 * pending P2P group interface waiting for provisioning or a
929 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 */
931 ssid = wpa_s->conf->ssid;
932 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700933 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 break;
935 ssid = ssid->next;
936 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300937 if (ssid == NULL &&
938 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
939 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
941 "not found");
942 return -1;
943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 }
945 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
946 gtype = "GO";
947 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
948 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
949 wpa_s->reassociate = 0;
950 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 gtype = "client";
952 } else
953 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700954
955 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
956 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
957
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700959 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
961 wpa_s, NULL)) {
962 wpa_printf(MSG_DEBUG,
963 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
964 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
965 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
966 wpa_s, NULL);
967 }
968 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 if (wpa_s->cross_connect_in_use) {
971 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800972 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700973 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
974 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700976 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 case P2P_GROUP_REMOVAL_REQUESTED:
978 reason = " reason=REQUESTED";
979 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
981 reason = " reason=FORMATION_FAILED";
982 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
984 reason = " reason=IDLE";
985 break;
986 case P2P_GROUP_REMOVAL_UNAVAILABLE:
987 reason = " reason=UNAVAILABLE";
988 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
990 reason = " reason=GO_ENDING_SESSION";
991 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700992 case P2P_GROUP_REMOVAL_PSK_FAILURE:
993 reason = " reason=PSK_FAILURE";
994 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800995 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
996 reason = " reason=FREQ_CONFLICT";
997 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 default:
999 reason = "";
1000 break;
1001 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001002 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001003 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001004 P2P_EVENT_GROUP_REMOVED "%s %s%s",
1005 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001008 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
1009 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
1011 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001012 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001013 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001014 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
1015 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001016 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001018 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019
Dmitry Shmidt15907092014-03-25 10:42:57 -07001020 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001021 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001022 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1023 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001024
Dmitry Shmidt96571392013-10-14 12:54:46 -07001025 /*
1026 * Make sure wait for the first client does not remain active after the
1027 * group has been removed.
1028 */
1029 wpa_s->global->p2p_go_wait_client.sec = 0;
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1032 struct wpa_global *global;
1033 char *ifname;
1034 enum wpa_driver_if_type type;
1035 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1036 wpa_s->ifname);
1037 global = wpa_s->global;
1038 ifname = os_strdup(wpa_s->ifname);
1039 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001041 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s = global->ifaces;
1043 if (wpa_s && ifname)
1044 wpa_drv_if_remove(wpa_s, type, ifname);
1045 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001046 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 }
1048
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001049 /*
1050 * The primary interface was used for P2P group operations, so
1051 * need to reset its p2pdev.
1052 */
1053 wpa_s->p2pdev = wpa_s->parent;
1054
Dmitry Shmidt96571392013-10-14 12:54:46 -07001055 if (!wpa_s->p2p_go_group_formation_completed) {
1056 wpa_s->global->p2p_group_formation = NULL;
1057 wpa_s->p2p_in_provisioning = 0;
1058 }
1059
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001060 wpa_s->show_group_started = 0;
1061 os_free(wpa_s->go_params);
1062 wpa_s->go_params = NULL;
1063
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001064 os_free(wpa_s->p2p_group_common_freqs);
1065 wpa_s->p2p_group_common_freqs = NULL;
1066 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001067 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001068 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001069
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001070 wpa_s->waiting_presence_resp = 0;
1071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1073 if (ssid && (ssid->p2p_group ||
1074 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1075 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1076 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001077 if (ssid == wpa_s->current_ssid) {
1078 wpa_sm_set_config(wpa_s->wpa, NULL);
1079 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001081 }
1082 /*
1083 * Networks objects created during any P2P activities are not
1084 * exposed out as they might/will confuse certain non-P2P aware
1085 * applications since these network objects won't behave like
1086 * regular ones.
1087 *
1088 * Likewise, we don't send out network removed signals for such
1089 * network objects.
1090 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 wpa_config_remove_network(wpa_s->conf, id);
1092 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001093 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 } else {
1095 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1096 "found");
1097 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001098 if (wpa_s->ap_iface)
1099 wpa_supplicant_ap_deinit(wpa_s);
1100 else
1101 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001103 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1104
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001105 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106}
1107
1108
1109static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1110 u8 *go_dev_addr,
1111 const u8 *ssid, size_t ssid_len)
1112{
1113 struct wpa_bss *bss;
1114 const u8 *bssid;
1115 struct wpabuf *p2p;
1116 u8 group_capab;
1117 const u8 *addr;
1118
1119 if (wpa_s->go_params)
1120 bssid = wpa_s->go_params->peer_interface_addr;
1121 else
1122 bssid = wpa_s->bssid;
1123
1124 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001125 if (bss == NULL && wpa_s->go_params &&
1126 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1127 bss = wpa_bss_get_p2p_dev_addr(
1128 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 if (bss == NULL) {
1130 u8 iface_addr[ETH_ALEN];
1131 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1132 iface_addr) == 0)
1133 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1134 }
1135 if (bss == NULL) {
1136 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1137 "group is persistent - BSS " MACSTR " not found",
1138 MAC2STR(bssid));
1139 return 0;
1140 }
1141
1142 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001143 if (p2p == NULL)
1144 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1145 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 if (p2p == NULL) {
1147 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1148 "group is persistent - BSS " MACSTR
1149 " did not include P2P IE", MAC2STR(bssid));
1150 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001151 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001153 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 bss->beacon_ie_len);
1155 return 0;
1156 }
1157
1158 group_capab = p2p_get_group_capab(p2p);
1159 addr = p2p_get_go_dev_addr(p2p);
1160 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1161 "group_capab=0x%x", group_capab);
1162 if (addr) {
1163 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1164 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1165 MAC2STR(addr));
1166 } else
1167 os_memset(go_dev_addr, 0, ETH_ALEN);
1168 wpabuf_free(p2p);
1169
1170 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1171 "go_dev_addr=" MACSTR,
1172 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1173
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001174 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175}
1176
1177
Jouni Malinen75ecf522011-06-27 15:19:46 -07001178static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1179 struct wpa_ssid *ssid,
1180 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181{
1182 struct wpa_ssid *s;
1183 int changed = 0;
1184
1185 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1186 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1187 for (s = wpa_s->conf->ssid; s; s = s->next) {
1188 if (s->disabled == 2 &&
1189 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1190 s->ssid_len == ssid->ssid_len &&
1191 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1192 break;
1193 }
1194
1195 if (s) {
1196 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1197 "entry");
1198 if (ssid->passphrase && !s->passphrase)
1199 changed = 1;
1200 else if (ssid->passphrase && s->passphrase &&
1201 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1202 changed = 1;
1203 } else {
1204 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1205 "entry");
1206 changed = 1;
1207 s = wpa_config_add_network(wpa_s->conf);
1208 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001209 return -1;
1210
1211 /*
1212 * Instead of network_added we emit persistent_group_added
1213 * notification. Also to keep the defense checks in
1214 * persistent_group obj registration method, we set the
1215 * relevant flags in s to designate it as a persistent group.
1216 */
1217 s->p2p_group = 1;
1218 s->p2p_persistent_group = 1;
1219 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_config_set_network_defaults(s);
1221 }
1222
1223 s->p2p_group = 1;
1224 s->p2p_persistent_group = 1;
1225 s->disabled = 2;
1226 s->bssid_set = 1;
1227 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1228 s->mode = ssid->mode;
1229 s->auth_alg = WPA_AUTH_ALG_OPEN;
1230 s->key_mgmt = WPA_KEY_MGMT_PSK;
1231 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001232 s->pbss = ssid->pbss;
1233 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 s->export_keys = 1;
1235 if (ssid->passphrase) {
1236 os_free(s->passphrase);
1237 s->passphrase = os_strdup(ssid->passphrase);
1238 }
1239 if (ssid->psk_set) {
1240 s->psk_set = 1;
1241 os_memcpy(s->psk, ssid->psk, 32);
1242 }
1243 if (s->passphrase && !s->psk_set)
1244 wpa_config_update_psk(s);
1245 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1246 os_free(s->ssid);
1247 s->ssid = os_malloc(ssid->ssid_len);
1248 }
1249 if (s->ssid) {
1250 s->ssid_len = ssid->ssid_len;
1251 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1252 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001253 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1254 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1255 wpa_s->global->add_psk = NULL;
1256 changed = 1;
1257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 if (changed && wpa_s->conf->update_config &&
1260 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1261 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1262 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001263
1264 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265}
1266
1267
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001268static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1269 const u8 *addr)
1270{
1271 struct wpa_ssid *ssid, *s;
1272 u8 *n;
1273 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001274 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001275 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001276
1277 ssid = wpa_s->current_ssid;
1278 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1279 !ssid->p2p_persistent_group)
1280 return;
1281
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001282 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001283 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1284 continue;
1285
1286 if (s->ssid_len == ssid->ssid_len &&
1287 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1288 break;
1289 }
1290
1291 if (s == NULL)
1292 return;
1293
1294 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001295 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001296 ETH_ALEN) != 0)
1297 continue;
1298
1299 if (i == s->num_p2p_clients - 1)
1300 return; /* already the most recent entry */
1301
1302 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001303 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1304 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1305 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001306 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001307 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1308 ETH_ALEN);
1309 os_memset(s->p2p_client_list +
1310 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1311 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001312 found = 1;
1313 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001314 }
1315
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001316 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1317 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001318 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001319 if (n == NULL)
1320 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001321 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1322 ETH_ALEN);
1323 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1324 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001325 s->p2p_client_list = n;
1326 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001327 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001328 /* Not enough room for an additional entry - drop the oldest
1329 * entry */
1330 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001331 s->p2p_client_list + 2 * ETH_ALEN,
1332 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001333 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001334 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001335 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001336 os_memset(s->p2p_client_list +
1337 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1338 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001339 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001340
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001341 if (p2p_wpa_s->conf->update_config &&
1342 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001344}
1345
1346
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001347static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1348 int go, struct wpa_ssid *ssid, int freq,
1349 const u8 *psk, const char *passphrase,
1350 const u8 *go_dev_addr, int persistent,
1351 const char *extra)
1352{
1353 const char *ssid_txt;
1354 char psk_txt[65];
1355
1356 if (psk)
1357 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1358 else
1359 psk_txt[0] = '\0';
1360
1361 if (ssid)
1362 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1363 else
1364 ssid_txt = "";
1365
1366 if (passphrase && passphrase[0] == '\0')
1367 passphrase = NULL;
1368
1369 /*
1370 * Include PSK/passphrase only in the control interface message and
1371 * leave it out from the debug log entry.
1372 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001373 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001374 P2P_EVENT_GROUP_STARTED
1375 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1376 MACSTR "%s%s",
1377 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1378 psk ? " psk=" : "", psk_txt,
1379 passphrase ? " passphrase=\"" : "",
1380 passphrase ? passphrase : "",
1381 passphrase ? "\"" : "",
1382 MAC2STR(go_dev_addr),
1383 persistent ? " [PERSISTENT]" : "", extra);
1384 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1385 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1386 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1387 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1388 extra);
1389}
1390
1391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001393 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394{
1395 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 int client;
1397 int persistent;
1398 u8 go_dev_addr[ETH_ALEN];
1399
1400 /*
1401 * This callback is likely called for the main interface. Update wpa_s
1402 * to use the group interface if a new interface was created for the
1403 * group.
1404 */
1405 if (wpa_s->global->p2p_group_formation)
1406 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001407 if (wpa_s->p2p_go_group_formation_completed) {
1408 wpa_s->global->p2p_group_formation = NULL;
1409 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001410 } else if (wpa_s->p2p_in_provisioning && !success) {
1411 wpa_msg(wpa_s, MSG_DEBUG,
1412 "P2P: Stop provisioning state due to failure");
1413 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001414 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001415 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001416 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001417 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418
1419 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001420 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001421 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001422 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001423 if (already_deleted)
1424 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001425 wpas_p2p_group_delete(wpa_s,
1426 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 return;
1428 }
1429
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001430 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001431 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432
1433 ssid = wpa_s->current_ssid;
1434 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1435 ssid->mode = WPAS_MODE_P2P_GO;
1436 p2p_group_notif_formation_done(wpa_s->p2p_group);
1437 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1438 }
1439
1440 persistent = 0;
1441 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 client = ssid->mode == WPAS_MODE_INFRA;
1443 if (ssid->mode == WPAS_MODE_P2P_GO) {
1444 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001445 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1446 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001447 } else {
1448 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 persistent = wpas_p2p_persistent_group(wpa_s,
1450 go_dev_addr,
1451 ssid->ssid,
1452 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 client = wpa_s->p2p_group_interface ==
1456 P2P_GROUP_INTERFACE_CLIENT;
1457 os_memset(go_dev_addr, 0, ETH_ALEN);
1458 }
1459
1460 wpa_s->show_group_started = 0;
1461 if (client) {
1462 /*
1463 * Indicate event only after successfully completed 4-way
1464 * handshake, i.e., when the interface is ready for data
1465 * packets.
1466 */
1467 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001469 wpas_p2p_group_started(wpa_s, 1, ssid,
1470 ssid ? ssid->frequency : 0,
1471 ssid && ssid->passphrase == NULL &&
1472 ssid->psk_set ? ssid->psk : NULL,
1473 ssid ? ssid->passphrase : NULL,
1474 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475 wpas_p2p_cross_connect_setup(wpa_s);
1476 wpas_p2p_set_group_idle_timeout(wpa_s);
1477 }
1478
1479 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001480 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1481 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001482 else {
1483 os_free(wpa_s->global->add_psk);
1484 wpa_s->global->add_psk = NULL;
1485 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001486
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001487 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001488 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001489 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001490 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491}
1492
1493
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001494struct send_action_work {
1495 unsigned int freq;
1496 u8 dst[ETH_ALEN];
1497 u8 src[ETH_ALEN];
1498 u8 bssid[ETH_ALEN];
1499 size_t len;
1500 unsigned int wait_time;
1501 u8 buf[0];
1502};
1503
1504
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001505static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1506{
1507 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1508
1509 wpa_printf(MSG_DEBUG,
1510 "P2P: Free Action frame radio work @%p (freq=%u dst="
1511 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1512 wpa_s->p2p_send_action_work, awork->freq,
1513 MAC2STR(awork->dst), MAC2STR(awork->src),
1514 MAC2STR(awork->bssid), awork->wait_time);
1515 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1516 awork->buf, awork->len);
1517 os_free(awork);
1518 wpa_s->p2p_send_action_work->ctx = NULL;
1519 radio_work_done(wpa_s->p2p_send_action_work);
1520 wpa_s->p2p_send_action_work = NULL;
1521}
1522
1523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1525 void *timeout_ctx)
1526{
1527 struct wpa_supplicant *wpa_s = eloop_ctx;
1528
1529 if (!wpa_s->p2p_send_action_work)
1530 return;
1531
1532 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001533 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001534}
1535
1536
Dmitry Shmidt03658832014-08-13 11:03:49 -07001537static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001539 if (wpa_s->p2p_send_action_work) {
1540 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001541
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001542 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001543 wpa_printf(MSG_DEBUG,
1544 "P2P: Clear Action TX work @%p (wait_time=%u)",
1545 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001546 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001547 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001548 } else {
1549 /*
1550 * In theory, this should not be needed, but number of
1551 * places in the P2P code is still using non-zero wait
1552 * time for the last Action frame in the sequence and
1553 * some of these do not call send_action_done().
1554 */
1555 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1556 wpa_s, NULL);
1557 eloop_register_timeout(
1558 0, awork->wait_time * 1000,
1559 wpas_p2p_send_action_work_timeout,
1560 wpa_s, NULL);
1561 }
1562 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001563}
1564
1565
1566static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1567 unsigned int freq,
1568 const u8 *dst, const u8 *src,
1569 const u8 *bssid,
1570 const u8 *data, size_t data_len,
1571 enum offchannel_send_action_result
1572 result)
1573{
1574 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1575
1576 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1579 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581 switch (result) {
1582 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1583 res = P2P_SEND_ACTION_SUCCESS;
1584 break;
1585 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1586 res = P2P_SEND_ACTION_NO_ACK;
1587 break;
1588 case OFFCHANNEL_SEND_ACTION_FAILED:
1589 res = P2P_SEND_ACTION_FAILED;
1590 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 }
1592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001595 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1596 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001597 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001598 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1599 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1602 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001603 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001604 P2P_EVENT_FALLBACK_TO_GO_NEG
1605 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001606 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1607 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 }
1609}
1610
1611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001612static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1613{
1614 struct wpa_supplicant *wpa_s = work->wpa_s;
1615 struct send_action_work *awork = work->ctx;
1616
1617 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001618 if (work->started) {
1619 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1620 wpa_s, NULL);
1621 wpa_s->p2p_send_action_work = NULL;
1622 offchannel_send_action_done(wpa_s);
1623 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001624 os_free(awork);
1625 return;
1626 }
1627
1628 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1629 awork->bssid, awork->buf, awork->len,
1630 awork->wait_time,
1631 wpas_p2p_send_action_tx_status, 1) < 0) {
1632 os_free(awork);
1633 radio_work_done(work);
1634 return;
1635 }
1636 wpa_s->p2p_send_action_work = work;
1637}
1638
1639
1640static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1641 unsigned int freq, const u8 *dst,
1642 const u8 *src, const u8 *bssid, const u8 *buf,
1643 size_t len, unsigned int wait_time)
1644{
1645 struct send_action_work *awork;
1646
Hai Shalom81f62d82019-07-22 12:10:00 -07001647 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001648 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1649 return -1;
1650 }
1651
1652 awork = os_zalloc(sizeof(*awork) + len);
1653 if (awork == NULL)
1654 return -1;
1655
1656 awork->freq = freq;
1657 os_memcpy(awork->dst, dst, ETH_ALEN);
1658 os_memcpy(awork->src, src, ETH_ALEN);
1659 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1660 awork->len = len;
1661 awork->wait_time = wait_time;
1662 os_memcpy(awork->buf, buf, len);
1663
Hai Shalom81f62d82019-07-22 12:10:00 -07001664 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001665 wpas_send_action_cb, awork) < 0) {
1666 os_free(awork);
1667 return -1;
1668 }
1669
1670 return 0;
1671}
1672
1673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1675 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001676 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677{
1678 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001679 int listen_freq = -1, send_freq = -1;
1680
Hai Shalom021b0b52019-04-10 11:17:58 -07001681 if (scheduled)
1682 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001683 if (wpa_s->p2p_listen_work)
1684 listen_freq = wpa_s->p2p_listen_work->freq;
1685 if (wpa_s->p2p_send_action_work)
1686 send_freq = wpa_s->p2p_send_action_work->freq;
1687 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001688 int res;
1689
1690 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1691 listen_freq, send_freq, freq);
1692 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1693 len, wait_time);
1694 if (res == 0 && scheduled)
1695 *scheduled = 1;
1696 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001697 }
1698
1699 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001700 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1701 wait_time,
1702 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703}
1704
1705
1706static void wpas_send_action_done(void *ctx)
1707{
1708 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001709
1710 if (wpa_s->p2p_send_action_work) {
1711 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1712 wpa_s, NULL);
1713 os_free(wpa_s->p2p_send_action_work->ctx);
1714 radio_work_done(wpa_s->p2p_send_action_work);
1715 wpa_s->p2p_send_action_work = NULL;
1716 }
1717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719}
1720
1721
1722static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1723 struct p2p_go_neg_results *params)
1724{
1725 if (wpa_s->go_params == NULL) {
1726 wpa_s->go_params = os_malloc(sizeof(*params));
1727 if (wpa_s->go_params == NULL)
1728 return -1;
1729 }
1730 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1731 return 0;
1732}
1733
1734
1735static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1736 struct p2p_go_neg_results *res)
1737{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001738 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001739 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1740 " dev_addr " MACSTR " wps_method %d",
1741 MAC2STR(res->peer_interface_addr),
1742 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1744 res->ssid, res->ssid_len);
1745 wpa_supplicant_ap_deinit(wpa_s);
1746 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001747 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001748 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001749#ifdef CONFIG_WPS_NFC
1750 } else if (res->wps_method == WPS_NFC) {
1751 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1752 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001753 wpa_s->p2pdev->p2p_oob_dev_pw,
1754 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1755 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001756 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001757 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001758 NULL,
1759 NULL, 0, 0);
1760#endif /* CONFIG_WPS_NFC */
1761 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001763 if (wpa_s->p2p_wps_method == WPS_P2PS)
1764 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1766 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1767 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1768 wpa_s->p2p_pin, 1, dev_pw_id);
1769 }
1770}
1771
1772
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001773static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1774 struct wpa_ssid *ssid)
1775{
1776 struct wpa_ssid *persistent;
1777 struct psk_list_entry *psk;
1778 struct hostapd_data *hapd;
1779
1780 if (!wpa_s->ap_iface)
1781 return;
1782
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001783 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001784 ssid->ssid_len);
1785 if (persistent == NULL)
1786 return;
1787
1788 hapd = wpa_s->ap_iface->bss[0];
1789
1790 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1791 list) {
1792 struct hostapd_wpa_psk *hpsk;
1793
1794 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1795 MACSTR " psk=%d",
1796 MAC2STR(psk->addr), psk->p2p);
1797 hpsk = os_zalloc(sizeof(*hpsk));
1798 if (hpsk == NULL)
1799 break;
1800 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1801 if (psk->p2p)
1802 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1803 else
1804 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1805 hpsk->next = hapd->conf->ssid.wpa_psk;
1806 hapd->conf->ssid.wpa_psk = hpsk;
1807 }
1808}
1809
1810
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001811static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1812{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001813 char buf[20 + P2P_MAX_CHANNELS * 6];
1814 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001815 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001816 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001817
Roshan Pius3a1667e2018-07-03 15:17:14 -07001818 pos = buf;
1819 end = pos + sizeof(buf);
1820 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1821 res = os_snprintf(pos, end - pos, " %d",
1822 wpa_s->p2p_group_common_freqs[i]);
1823 if (os_snprintf_error(end - pos, res))
1824 break;
1825 pos += res;
1826 }
1827 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001828
Roshan Pius3a1667e2018-07-03 15:17:14 -07001829 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001830}
1831
1832
1833static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1834 struct p2p_go_neg_results *params)
1835{
1836 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1837
1838 wpa_s->p2p_group_common_freqs_num = 0;
1839 os_free(wpa_s->p2p_group_common_freqs);
1840 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1841 if (!wpa_s->p2p_group_common_freqs)
1842 return;
1843
1844 for (i = 0; i < len; i++) {
1845 if (!wpa_s->go_params->freq_list[i])
1846 break;
1847 wpa_s->p2p_group_common_freqs[i] =
1848 wpa_s->go_params->freq_list[i];
1849 }
1850 wpa_s->p2p_group_common_freqs_num = i;
1851}
1852
1853
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001854static void p2p_config_write(struct wpa_supplicant *wpa_s)
1855{
1856#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001857 if (wpa_s->p2pdev->conf->update_config &&
1858 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001859 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1860#endif /* CONFIG_NO_CONFIG_WRITE */
1861}
1862
1863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864static void p2p_go_configured(void *ctx, void *data)
1865{
1866 struct wpa_supplicant *wpa_s = ctx;
1867 struct p2p_go_neg_results *params = data;
1868 struct wpa_ssid *ssid;
1869
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001870 wpa_s->ap_configured_cb = NULL;
1871 wpa_s->ap_configured_cb_ctx = NULL;
1872 wpa_s->ap_configured_cb_data = NULL;
1873 if (!wpa_s->go_params) {
1874 wpa_printf(MSG_ERROR,
1875 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1876 return;
1877 }
1878
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001879 p2p_go_save_group_common_freqs(wpa_s, params);
1880 p2p_go_dump_common_freqs(wpa_s);
1881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 ssid = wpa_s->current_ssid;
1883 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1884 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1885 if (wpa_s->global->p2p_group_formation == wpa_s)
1886 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001887 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1888 params->passphrase[0] == '\0' ?
1889 params->psk : NULL,
1890 params->passphrase,
1891 wpa_s->global->p2p_dev_addr,
1892 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001894
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001895 if (wpa_s->p2pdev->p2ps_method_config_any) {
1896 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001897 wpa_dbg(wpa_s, MSG_DEBUG,
1898 "P2PS: Setting default PIN for ANY");
1899 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1900 "12345670", NULL, 0,
1901 0);
1902 } else {
1903 wpa_dbg(wpa_s, MSG_DEBUG,
1904 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001905 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001906 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001907 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001908 "12345670", NULL, 0, 0);
1909 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001910 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001911 }
1912
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001913 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001914 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001915 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001916 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001917 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001918 wpas_p2p_add_psk_list(wpa_s, ssid);
1919 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001920
1921 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001922 params->persistent_group, 0,
1923 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 wpas_p2p_cross_connect_setup(wpa_s);
1925 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001926
1927 if (wpa_s->p2p_first_connection_timeout) {
1928 wpa_dbg(wpa_s, MSG_DEBUG,
1929 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1930 wpa_s->p2p_first_connection_timeout);
1931 wpa_s->p2p_go_group_formation_completed = 0;
1932 wpa_s->global->p2p_group_formation = wpa_s;
1933 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001934 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001935 eloop_register_timeout(
1936 wpa_s->p2p_first_connection_timeout, 0,
1937 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001938 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001939 }
1940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 return;
1942 }
1943
1944 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1945 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1946 params->peer_interface_addr)) {
1947 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1948 "filtering");
1949 return;
1950 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001951 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001953 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001954#ifdef CONFIG_WPS_NFC
1955 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001956 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001957 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001958 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001959 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1960 return;
1961 }
1962 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001963 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1964 wpa_s->p2pdev->p2p_oob_dev_pw,
1965 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1966 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001967#endif /* CONFIG_WPS_NFC */
1968 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001970 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 os_free(wpa_s->go_params);
1972 wpa_s->go_params = NULL;
1973}
1974
1975
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001976/**
1977 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1978 * @freq: Frequency (MHz) to convert
1979 * @op_class: Buffer for returning operating class
1980 * @op_edmg_channel: Buffer for returning channel number
1981 * Returns: 0 on success, -1 on failure
1982 *
1983 * This can be used to find the highest channel bonding which includes the
1984 * specified frequency.
1985 */
1986static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1987 unsigned int freq,
1988 u8 *op_class, u8 *op_edmg_channel)
1989{
1990 struct hostapd_hw_modes *hwmode;
1991 struct ieee80211_edmg_config edmg;
1992 unsigned int i;
1993 enum chan_width chanwidth[] = {
1994 CHAN_WIDTH_8640,
1995 CHAN_WIDTH_6480,
1996 CHAN_WIDTH_4320,
1997 };
1998
1999 if (!wpa_s->hw.modes)
2000 return -1;
2001
2002 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08002003 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002004 if (!hwmode) {
2005 wpa_printf(MSG_ERROR,
2006 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2007 return -1;
2008 }
2009
2010 /* Find the highest EDMG channel bandwidth to start the P2P GO */
2011 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2012 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2013 op_class,
2014 op_edmg_channel) < 0)
2015 continue;
2016
2017 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2018 if (edmg.channels &&
2019 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2020 wpa_printf(MSG_DEBUG,
2021 "Freq %u to EDMG channel %u at opclass %u",
2022 freq, *op_edmg_channel, *op_class);
2023 return 0;
2024 }
2025 }
2026
2027 return -1;
2028}
2029
2030
2031int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2032 struct p2p_go_neg_results *params)
2033{
2034 u8 op_channel, op_class;
2035 int freq;
2036
2037 /* Try social channel as primary channel frequency */
2038 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2039
2040 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2041 &op_channel) == 0) {
2042 wpa_printf(MSG_DEBUG,
2043 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2044 freq, op_channel, op_class);
2045 params->freq = freq;
2046 return 0;
2047 }
2048
2049 return -1;
2050}
2051
2052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2054 struct p2p_go_neg_results *params,
2055 int group_formation)
2056{
2057 struct wpa_ssid *ssid;
2058
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2060 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2061 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2062 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002064 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065
2066 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002067 if (ssid == NULL) {
2068 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002072 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002073 wpa_s->p2p_go_group_formation_completed = 0;
2074 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002075 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 wpa_config_set_network_defaults(ssid);
2078 ssid->temporary = 1;
2079 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002080 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2082 WPAS_MODE_P2P_GO;
2083 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002084 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002085 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002086 ssid->max_oper_chwidth = params->max_oper_chwidth;
2087 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002088 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002089 if (params->edmg) {
2090 u8 op_channel, op_class;
2091
2092 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2093 &op_class, &op_channel)) {
2094 ssid->edmg_channel = op_channel;
2095 ssid->enable_edmg = params->edmg;
2096 } else {
2097 wpa_dbg(wpa_s, MSG_DEBUG,
2098 "P2P: Could not match EDMG channel, freq %d, for GO",
2099 params->freq);
2100 }
2101 }
2102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 ssid->ssid = os_zalloc(params->ssid_len + 1);
2104 if (ssid->ssid) {
2105 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2106 ssid->ssid_len = params->ssid_len;
2107 }
2108 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2109 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002110 if (is_6ghz_freq(ssid->frequency) &&
2111 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2112 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2113 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2114 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002115 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
Hai Shalomc1a21442022-02-04 13:43:00 -08002116 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2117 } else {
2118 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 ssid->proto = WPA_PROTO_RSN;
2121 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002122 ssid->group_cipher = WPA_CIPHER_CCMP;
2123 if (params->freq > 56160) {
2124 /*
2125 * Enable GCMP instead of CCMP as pairwise_cipher and
2126 * group_cipher in 60 GHz.
2127 */
2128 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2129 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002130 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2131 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002132 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002133 if (os_strlen(params->passphrase) > 0) {
2134 ssid->passphrase = os_strdup(params->passphrase);
2135 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002136 wpa_msg_global(wpa_s, MSG_ERROR,
2137 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002138 wpa_config_remove_network(wpa_s->conf, ssid->id);
2139 return;
2140 }
2141 } else
2142 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002143 ssid->psk_set = params->psk_set;
2144 if (ssid->psk_set)
2145 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002146 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002147 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002148 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149
2150 wpa_s->ap_configured_cb = p2p_go_configured;
2151 wpa_s->ap_configured_cb_ctx = wpa_s;
2152 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002153 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002154 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 wpa_s->reassociate = 1;
2156 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002157 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2158 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 wpa_supplicant_req_scan(wpa_s, 0, 0);
2160}
2161
2162
2163static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2164 const struct wpa_supplicant *src)
2165{
2166 struct wpa_config *d;
2167 const struct wpa_config *s;
2168
2169 d = dst->conf;
2170 s = src->conf;
2171
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002172#define C(n) \
2173do { \
2174 if (s->n && !d->n) \
2175 d->n = os_strdup(s->n); \
2176} while (0)
2177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 C(device_name);
2179 C(manufacturer);
2180 C(model_name);
2181 C(model_number);
2182 C(serial_number);
2183 C(config_methods);
2184#undef C
2185
2186 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2187 os_memcpy(d->sec_device_type, s->sec_device_type,
2188 sizeof(d->sec_device_type));
2189 d->num_sec_device_types = s->num_sec_device_types;
2190
2191 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002192 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002194 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002195 d->max_num_sta = s->max_num_sta;
2196 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002197 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002198 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002199 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002200 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002201 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002202 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002203 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002204 d->pmf = s->pmf;
2205 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002206 d->sae_pwe = s->sae_pwe;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002207
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002208 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2209 !d->wps_nfc_pw_from_config) {
2210 wpabuf_free(d->wps_nfc_dh_privkey);
2211 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002212 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2213 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2214 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002215 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002216 d->go_interworking = s->go_interworking;
2217 d->go_access_network_type = s->go_access_network_type;
2218 d->go_internet = s->go_internet;
2219 d->go_venue_group = s->go_venue_group;
2220 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002221 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222}
2223
2224
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002225static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2226 char *ifname, size_t len)
2227{
2228 char *ifname_ptr = wpa_s->ifname;
2229
2230 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2231 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2232 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2233 }
2234
2235 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2236 if (os_strlen(ifname) >= IFNAMSIZ &&
2237 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002238 int res;
2239
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002240 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002241 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2242 if (os_snprintf_error(len, res) && len)
2243 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002244 }
2245}
2246
2247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2249 enum wpa_driver_if_type type)
2250{
2251 char ifname[120], force_ifname[120];
2252
2253 if (wpa_s->pending_interface_name[0]) {
2254 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2255 "- skip creation of a new one");
2256 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2257 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2258 "unknown?! ifname='%s'",
2259 wpa_s->pending_interface_name);
2260 return -1;
2261 }
2262 return 0;
2263 }
2264
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002265 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 force_ifname[0] = '\0';
2267
2268 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2269 ifname);
2270 wpa_s->p2p_group_idx++;
2271
2272 wpa_s->pending_interface_type = type;
2273 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2274 wpa_s->pending_interface_addr, NULL) < 0) {
2275 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2276 "interface");
2277 return -1;
2278 }
2279
Jimmy Chen36c21992018-11-29 16:46:43 +08002280 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002281 random_mac_addr(wpa_s->pending_interface_addr);
2282 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2283 " for the group",
2284 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002285 }
2286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287 if (force_ifname[0]) {
2288 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2289 force_ifname);
2290 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2291 sizeof(wpa_s->pending_interface_name));
2292 } else
2293 os_strlcpy(wpa_s->pending_interface_name, ifname,
2294 sizeof(wpa_s->pending_interface_name));
2295 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2296 MACSTR, wpa_s->pending_interface_name,
2297 MAC2STR(wpa_s->pending_interface_addr));
2298
2299 return 0;
2300}
2301
2302
2303static void wpas_p2p_remove_pending_group_interface(
2304 struct wpa_supplicant *wpa_s)
2305{
2306 if (!wpa_s->pending_interface_name[0] ||
2307 is_zero_ether_addr(wpa_s->pending_interface_addr))
2308 return; /* No pending virtual interface */
2309
2310 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2311 wpa_s->pending_interface_name);
2312 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2313 wpa_s->pending_interface_name);
2314 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2315 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002316 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002317}
2318
2319
2320static struct wpa_supplicant *
2321wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2322{
2323 struct wpa_interface iface;
2324 struct wpa_supplicant *group_wpa_s;
2325
2326 if (!wpa_s->pending_interface_name[0]) {
2327 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2328 if (!wpas_p2p_create_iface(wpa_s))
2329 return NULL;
2330 /*
2331 * Something has forced us to remove the pending interface; try
2332 * to create a new one and hope for the best that we will get
2333 * the same local address.
2334 */
2335 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2336 WPA_IF_P2P_CLIENT) < 0)
2337 return NULL;
2338 }
2339
2340 os_memset(&iface, 0, sizeof(iface));
2341 iface.ifname = wpa_s->pending_interface_name;
2342 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002343 if (wpa_s->conf->ctrl_interface == NULL &&
2344 wpa_s->parent != wpa_s &&
2345 wpa_s->p2p_mgmt &&
2346 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2347 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2348 else
2349 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002351 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 if (group_wpa_s == NULL) {
2353 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2354 "wpa_supplicant interface");
2355 return NULL;
2356 }
2357 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2359 P2P_GROUP_INTERFACE_CLIENT;
2360 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002361 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362
2363 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2364
Jimmy Chen36c21992018-11-29 16:46:43 +08002365 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002366 if (wpa_drv_set_mac_addr(group_wpa_s,
2367 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002368 wpa_msg(group_wpa_s, MSG_INFO,
2369 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002370 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2371 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002372 return NULL;
2373 }
2374
2375 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2376 wpa_msg(group_wpa_s, MSG_INFO,
2377 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002378 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2379 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002380 return NULL;
2381 }
2382
Hai Shalom74f70d42019-02-11 14:42:39 -08002383 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2384 " for the group",
2385 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002386 }
2387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 return group_wpa_s;
2389}
2390
2391
2392static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2393 void *timeout_ctx)
2394{
2395 struct wpa_supplicant *wpa_s = eloop_ctx;
2396 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002397 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002398}
2399
2400
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002401static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2402 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002403{
2404 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002405 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406 if (wpa_s->global->p2p)
2407 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002408 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409}
2410
2411
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002412static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2413{
2414 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2415 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002416 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002417 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002418 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002419 wpa_s->global->p2p_fail_on_wps_complete = 0;
2420}
2421
2422
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002423void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2424{
2425 if (wpa_s->global->p2p_group_formation != wpa_s)
2426 return;
2427 /* Speed up group formation timeout since this cannot succeed */
2428 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002429 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002430 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002431 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002432}
2433
2434
Matthew Wang06b42472022-11-10 06:56:31 +00002435bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2436{
2437 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002438 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
Matthew Wang06b42472022-11-10 06:56:31 +00002439 return false;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002440
Matthew Wang06b42472022-11-10 06:56:31 +00002441 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2442 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2443 wpa_s->p2pdev, NULL);
2444 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2445 wpa_s->p2pdev, NULL);
2446 return true;
2447}
2448
2449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002450static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451{
2452 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002453 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454
2455 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2456 wpa_drv_cancel_remain_on_channel(wpa_s);
2457 wpa_s->off_channel_freq = 0;
2458 wpa_s->roc_waiting_drv_freq = 0;
2459 }
2460
2461 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002462 wpa_msg_global(wpa_s, MSG_INFO,
2463 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2464 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002465 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 wpas_p2p_remove_pending_group_interface(wpa_s);
2467 return;
2468 }
2469
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002470 if (!res->role_go) {
2471 /* Inform driver of the operating channel of GO. */
2472 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2473 }
2474
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002475 if (wpa_s->p2p_go_ht40)
2476 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002477 if (wpa_s->p2p_go_vht)
2478 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002479 if (wpa_s->p2p_go_he)
2480 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002481 if (wpa_s->p2p_go_edmg)
2482 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002483 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2484 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002485
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002486 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2487 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2488 " wps_method=%s",
2489 res->role_go ? "GO" : "client", res->freq, res->ht40,
2490 MAC2STR(res->peer_device_addr),
2491 MAC2STR(res->peer_interface_addr),
2492 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002493 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494
Dmitry Shmidt04949592012-07-19 12:16:46 -07002495 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2496 struct wpa_ssid *ssid;
2497 ssid = wpa_config_get_network(wpa_s->conf,
2498 wpa_s->p2p_persistent_id);
2499 if (ssid && ssid->disabled == 2 &&
2500 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2501 size_t len = os_strlen(ssid->passphrase);
2502 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2503 "on requested persistent group");
2504 os_memcpy(res->passphrase, ssid->passphrase, len);
2505 res->passphrase[len] = '\0';
2506 }
2507 }
2508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002510 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2512 if (group_wpa_s == NULL) {
2513 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002514 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2515 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002516 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 return;
2518 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2520 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002522 group_wpa_s = wpa_s->parent;
2523 wpa_s->global->p2p_group_formation = group_wpa_s;
2524 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002525 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002526 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002528 group_wpa_s->p2p_in_provisioning = 1;
2529 group_wpa_s->p2pdev = wpa_s;
2530 if (group_wpa_s != wpa_s) {
2531 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2532 sizeof(group_wpa_s->p2p_pin));
2533 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2534 }
2535 if (res->role_go) {
2536 wpas_start_wps_go(group_wpa_s, res, 1);
2537 } else {
2538 os_get_reltime(&group_wpa_s->scan_min_time);
2539 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 }
2541
Hai Shalomfdcde762020-04-02 11:19:20 -07002542 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2544
2545 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2546 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2547 (res->peer_config_timeout % 100) * 10000,
2548 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2549}
2550
2551
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002552static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2553 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554{
2555 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002556 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002557 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2558 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002560 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561}
2562
2563
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002564static void wpas_dev_found(void *ctx, const u8 *addr,
2565 const struct p2p_peer_info *info,
2566 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002568 u8 *wfd_dev_info = NULL;
2569 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002570 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002571 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002572#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 struct wpa_supplicant *wpa_s = ctx;
2574 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002575 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002576 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002577
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002578#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002579 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2580 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002581 if (wfd_dev_info_hex) {
2582 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2583 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2584 // Only used for notification, so not handling error.
2585 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2586 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002587
2588 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2589 WFD_SUBELEM_R2_DEVICE_INFO);
2590 if (wfd_r2_dev_info_hex) {
2591 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2592 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2593 // Only used for notification, so not handling error.
2594 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2595 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002596#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002597
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002598 if (info->p2ps_instance) {
2599 char str[256];
2600 const u8 *buf = wpabuf_head(info->p2ps_instance);
2601 size_t len = wpabuf_len(info->p2ps_instance);
2602
2603 while (len) {
2604 u32 id;
2605 u16 methods;
2606 u8 str_len;
2607
2608 if (len < 4 + 2 + 1)
2609 break;
2610 id = WPA_GET_LE32(buf);
2611 buf += sizeof(u32);
2612 methods = WPA_GET_BE16(buf);
2613 buf += sizeof(u16);
2614 str_len = *buf++;
2615 if (str_len > len - 4 - 2 - 1)
2616 break;
2617 os_memcpy(str, buf, str_len);
2618 str[str_len] = '\0';
2619 buf += str_len;
2620 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2621
2622 wpa_msg_global(wpa_s, MSG_INFO,
2623 P2P_EVENT_DEVICE_FOUND MACSTR
2624 " p2p_dev_addr=" MACSTR
2625 " pri_dev_type=%s name='%s'"
2626 " config_methods=0x%x"
2627 " dev_capab=0x%x"
2628 " group_capab=0x%x"
2629 " adv_id=%x asp_svc=%s%s",
2630 MAC2STR(addr),
2631 MAC2STR(info->p2p_device_addr),
2632 wps_dev_type_bin2str(
2633 info->pri_dev_type,
2634 devtype, sizeof(devtype)),
2635 info->device_name, methods,
2636 info->dev_capab, info->group_capab,
2637 id, str,
2638 info->vendor_elems ?
2639 " vendor_elems=1" : "");
2640 }
2641 goto done;
2642 }
2643
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002644 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2645 " p2p_dev_addr=" MACSTR
2646 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002647 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002648 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2649 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2650 sizeof(devtype)),
2651 info->device_name, info->config_methods,
2652 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002653 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002654 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002655 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2656 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002657 info->vendor_elems ? " vendor_elems=1" : "",
2658 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002660done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002661 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002662 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002663#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002664
Roshan Piusfd2fd662017-01-23 13:41:57 -08002665 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002666 wfd_dev_info_len, wfd_r2_dev_info,
2667 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002668 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669}
2670
2671
2672static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2673{
2674 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002675
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002676 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2677 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2680}
2681
2682
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002683static void wpas_find_stopped(void *ctx)
2684{
2685 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002686
2687 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2688 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2689
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002690 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002691 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002692}
2693
2694
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002695struct wpas_p2p_listen_work {
2696 unsigned int freq;
2697 unsigned int duration;
2698 struct wpabuf *probe_resp_ie;
2699};
2700
2701
2702static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2703{
2704 if (lwork == NULL)
2705 return;
2706 wpabuf_free(lwork->probe_resp_ie);
2707 os_free(lwork);
2708}
2709
2710
2711static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2712{
2713 struct wpas_p2p_listen_work *lwork;
2714
2715 if (!wpa_s->p2p_listen_work)
2716 return;
2717
2718 lwork = wpa_s->p2p_listen_work->ctx;
2719 wpas_p2p_listen_work_free(lwork);
2720 radio_work_done(wpa_s->p2p_listen_work);
2721 wpa_s->p2p_listen_work = NULL;
2722}
2723
2724
2725static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2726{
2727 struct wpa_supplicant *wpa_s = work->wpa_s;
2728 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002729 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002730
2731 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002732 if (work->started) {
2733 wpa_s->p2p_listen_work = NULL;
2734 wpas_stop_listen(wpa_s);
2735 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002736 wpas_p2p_listen_work_free(lwork);
2737 return;
2738 }
2739
2740 wpa_s->p2p_listen_work = work;
2741
2742 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2743
2744 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2745 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2746 "report received Probe Request frames");
2747 wpas_p2p_listen_work_done(wpa_s);
2748 return;
2749 }
2750
2751 wpa_s->pending_listen_freq = lwork->freq;
2752 wpa_s->pending_listen_duration = lwork->duration;
2753
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002754 duration = lwork->duration;
2755#ifdef CONFIG_TESTING_OPTIONS
2756 if (wpa_s->extra_roc_dur) {
2757 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2758 duration, duration + wpa_s->extra_roc_dur);
2759 duration += wpa_s->extra_roc_dur;
2760 }
2761#endif /* CONFIG_TESTING_OPTIONS */
2762
2763 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002764 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2765 "to remain on channel (%u MHz) for Listen "
2766 "state", lwork->freq);
2767 wpas_p2p_listen_work_done(wpa_s);
2768 wpa_s->pending_listen_freq = 0;
2769 return;
2770 }
2771 wpa_s->off_channel_freq = 0;
2772 wpa_s->roc_waiting_drv_freq = lwork->freq;
2773}
2774
2775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002776static int wpas_start_listen(void *ctx, unsigned int freq,
2777 unsigned int duration,
2778 const struct wpabuf *probe_resp_ie)
2779{
2780 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002781 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002783 if (wpa_s->p2p_listen_work) {
2784 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 return -1;
2786 }
2787
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002788 lwork = os_zalloc(sizeof(*lwork));
2789 if (lwork == NULL)
2790 return -1;
2791 lwork->freq = freq;
2792 lwork->duration = duration;
2793 if (probe_resp_ie) {
2794 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2795 if (lwork->probe_resp_ie == NULL) {
2796 wpas_p2p_listen_work_free(lwork);
2797 return -1;
2798 }
2799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002801 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2802 lwork) < 0) {
2803 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 return -1;
2805 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806
2807 return 0;
2808}
2809
2810
2811static void wpas_stop_listen(void *ctx)
2812{
2813 struct wpa_supplicant *wpa_s = ctx;
2814 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2815 wpa_drv_cancel_remain_on_channel(wpa_s);
2816 wpa_s->off_channel_freq = 0;
2817 wpa_s->roc_waiting_drv_freq = 0;
2818 }
2819 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002820
2821 /*
2822 * Don't cancel Probe Request RX reporting for a connected P2P Client
2823 * handling Probe Request frames.
2824 */
2825 if (!wpa_s->p2p_cli_probe)
2826 wpa_drv_probe_req_report(wpa_s, 0);
2827
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002828 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829}
2830
2831
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002832static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2833 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834{
2835 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002836 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002837 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838}
2839
2840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2842 const u8 *peer, const char *params,
2843 unsigned int generated_pin)
2844{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002845 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2846 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847}
2848
2849
2850static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2851 const u8 *peer, const char *params)
2852{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002853 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2854 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855}
2856
2857
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002858static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2859 const u8 *dev_addr, const u8 *pri_dev_type,
2860 const char *dev_name, u16 supp_config_methods,
2861 u8 dev_capab, u8 group_capab, const u8 *group_id,
2862 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863{
2864 struct wpa_supplicant *wpa_s = ctx;
2865 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002866 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 u8 empty_dev_type[8];
2868 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002869 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002870 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002871
2872 if (group_id) {
2873 for (group = wpa_s->global->ifaces; group; group = group->next)
2874 {
2875 struct wpa_ssid *s = group->current_ssid;
2876 if (s != NULL &&
2877 s->mode == WPAS_MODE_P2P_GO &&
2878 group_id_len - ETH_ALEN == s->ssid_len &&
2879 os_memcmp(group_id + ETH_ALEN, s->ssid,
2880 s->ssid_len) == 0)
2881 break;
2882 }
2883 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884
2885 if (pri_dev_type == NULL) {
2886 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2887 pri_dev_type = empty_dev_type;
2888 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002889 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2890 " pri_dev_type=%s name='%s' config_methods=0x%x "
2891 "dev_capab=0x%x group_capab=0x%x%s%s",
2892 MAC2STR(dev_addr),
2893 wps_dev_type_bin2str(pri_dev_type, devtype,
2894 sizeof(devtype)),
2895 dev_name, supp_config_methods, dev_capab, group_capab,
2896 group ? " group=" : "",
2897 group ? group->ifname : "");
2898 if (os_snprintf_error(sizeof(params), res))
2899 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002903 if (wps_generate_pin(&generated_pin) < 0) {
2904 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2905 wpas_notify_p2p_provision_discovery(
2906 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002907 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002908 return;
2909 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 wpas_prov_disc_local_display(wpa_s, peer, params,
2911 generated_pin);
2912 } else if (config_methods & WPS_CONFIG_KEYPAD)
2913 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2914 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002915 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2916 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002917
2918 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2919 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002920 config_methods, generated_pin,
2921 group ? group->ifname : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922}
2923
2924
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002925static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926{
2927 struct wpa_supplicant *wpa_s = ctx;
2928 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002929 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002931 if (wpa_s->pending_pd_before_join &&
2932 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2933 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2934 wpa_s->pending_pd_before_join = 0;
2935 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2936 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002937 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002938 return;
2939 }
2940
Dmitry Shmidt04949592012-07-19 12:16:46 -07002941 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002942 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2943 int res;
2944
2945 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2946 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2947 if (os_snprintf_error(sizeof(params), res))
2948 params[sizeof(params) - 1] = '\0';
2949 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002950 params[0] = '\0';
2951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002953 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002955 if (wps_generate_pin(&generated_pin) < 0) {
2956 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2957 wpas_notify_p2p_provision_discovery(
2958 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002959 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002960 return;
2961 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002962 wpas_prov_disc_local_display(wpa_s, peer, params,
2963 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002965 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2966 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967
Jouni Malinen75ecf522011-06-27 15:19:46 -07002968 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2969 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002970 config_methods, generated_pin,
2971 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972}
2973
2974
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002975static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002976 enum p2p_prov_disc_status status,
2977 u32 adv_id, const u8 *adv_mac,
2978 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002979{
2980 struct wpa_supplicant *wpa_s = ctx;
2981
Dmitry Shmidt04949592012-07-19 12:16:46 -07002982 if (wpa_s->p2p_fallback_to_go_neg) {
2983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2984 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002985 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002986 P2P_EVENT_FALLBACK_TO_GO_NEG
2987 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002988 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2989 return;
2990 }
2991
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002992 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002993 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002994 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2995 "join-existing-group operation (no ACK for PD "
2996 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002997 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002998 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002999 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003000
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003001 if (adv_id && adv_mac && deferred_session_resp) {
3002 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3003 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3004 " deferred_session_resp='%s'",
3005 MAC2STR(peer), status, adv_id,
3006 deferred_session_resp);
3007 } else if (adv_id && adv_mac) {
3008 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3009 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3010 MAC2STR(peer), status, adv_id);
3011 } else {
3012 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3013 " p2p_dev_addr=" MACSTR " status=%d",
3014 MAC2STR(peer), status);
3015 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003016
Jouni Malinen75ecf522011-06-27 15:19:46 -07003017 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003018 status, 0, 0, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003019}
3020
3021
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003022static int freq_included(struct wpa_supplicant *wpa_s,
3023 const struct p2p_channels *channels,
3024 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003025{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003026 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3027 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3028 return 1;
3029 return 0;
3030}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003031
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003032
3033static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3034{
3035 unsigned int num = P2P_MAX_CHANNELS;
3036 int *common_freqs;
3037 int ret;
3038
3039 p2p_go_dump_common_freqs(wpa_s);
3040 common_freqs = os_calloc(num, sizeof(int));
3041 if (!common_freqs)
3042 return;
3043
3044 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3045 if (ret < 0) {
3046 wpa_dbg(wpa_s, MSG_DEBUG,
3047 "P2P: Failed to get group common freqs");
3048 os_free(common_freqs);
3049 return;
3050 }
3051
3052 os_free(wpa_s->p2p_group_common_freqs);
3053 wpa_s->p2p_group_common_freqs = common_freqs;
3054 wpa_s->p2p_group_common_freqs_num = num;
3055 p2p_go_dump_common_freqs(wpa_s);
3056}
3057
3058
3059/*
3060 * Check if the given frequency is one of the possible operating frequencies
3061 * set after the completion of the GO Negotiation.
3062 */
3063static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3064{
3065 unsigned int i;
3066
3067 p2p_go_dump_common_freqs(wpa_s);
3068
3069 /* assume no restrictions */
3070 if (!wpa_s->p2p_group_common_freqs_num)
3071 return 1;
3072
3073 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3074 if (wpa_s->p2p_group_common_freqs[i] == freq)
3075 return 1;
3076 }
3077 return 0;
3078}
3079
3080
3081static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3082 struct sta_info *sta, void *ctx)
3083{
3084 int *ecsa_support = ctx;
3085
3086 *ecsa_support &= sta->ecsa_supported;
3087
3088 return 0;
3089}
3090
3091
3092/* Check if all the peers support eCSA */
3093static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3094{
3095 int ecsa_support = 1;
3096
3097 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3098 &ecsa_support);
3099
3100 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003101}
3102
3103
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003104/**
3105 * Pick the best frequency to use from all the currently used frequencies.
3106 */
3107static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3108 struct wpa_used_freq_data *freqs,
3109 unsigned int num)
3110{
3111 unsigned int i, c;
3112
3113 /* find a candidate freq that is supported by P2P */
3114 for (c = 0; c < num; c++)
3115 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3116 break;
3117
3118 if (c == num)
3119 return 0;
3120
3121 /* once we have a candidate, try to find a 'better' one */
3122 for (i = c + 1; i < num; i++) {
3123 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3124 continue;
3125
3126 /*
3127 * 1. Infrastructure station interfaces have higher preference.
3128 * 2. P2P Clients have higher preference.
3129 * 3. All others.
3130 */
3131 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3132 c = i;
3133 break;
3134 }
3135
3136 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3137 c = i;
3138 }
3139 return freqs[c].freq;
3140}
3141
3142
Sunil Ravi036cec52023-03-29 11:35:17 -07003143/**
3144 * Pick the best frequency the driver suggests.
3145 *
3146 * num_pref_freq is used as both input and output
3147 * - input: the max size of pref_freq_list,
3148 * - output: the valid size of pref_freq_list filled with data.
3149 */
3150static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3151 struct weighted_pcl *pref_freq_list,
3152 unsigned int *num_pref_freq)
3153{
3154 int best_freq = 0;
3155 unsigned int max_pref_freq, i;
3156 int res;
3157 enum wpa_driver_if_type iface_type;
3158
3159 max_pref_freq = *num_pref_freq;
3160 *num_pref_freq = 0;
3161
3162 if (go)
3163 iface_type = WPA_IF_P2P_GO;
3164 else
3165 iface_type = WPA_IF_P2P_CLIENT;
3166
3167 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3168 pref_freq_list);
3169 if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3170 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3171 max_pref_freq);
3172 if (res || !max_pref_freq) {
3173 wpa_printf(MSG_DEBUG,
3174 "P2P: No preferred frequency list available");
3175 return 0;
3176 }
3177
3178 *num_pref_freq = max_pref_freq;
3179 i = 0;
3180 while (i < *num_pref_freq &&
3181 (!p2p_supported_freq(wpa_s->global->p2p,
3182 pref_freq_list[i].freq) ||
3183 wpas_p2p_disallowed_freq(wpa_s->global,
3184 pref_freq_list[i].freq) ||
3185 !p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3186 wpa_printf(MSG_DEBUG,
3187 "P2P: preferred_freq_list[%d]=%d is disallowed",
3188 i, pref_freq_list[i].freq);
3189 i++;
3190 }
3191 if (i != *num_pref_freq) {
3192 best_freq = pref_freq_list[i].freq;
3193 wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3194 i, best_freq);
3195 } else {
3196 wpa_printf(MSG_DEBUG,
3197 "P2P: All driver preferred frequencies are disallowed for P2P use");
3198 *num_pref_freq = 0;
3199 }
3200
3201 return best_freq;
3202}
3203
3204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3206 const u8 *go_dev_addr, const u8 *ssid,
3207 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003208 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003209 const struct p2p_channels *channels,
3210 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211{
3212 struct wpa_supplicant *wpa_s = ctx;
3213 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003214 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003216 int best_freq;
Sunil Ravi036cec52023-03-29 11:35:17 -07003217 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3218 unsigned int num_pref_freq;
3219 int res;
3220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221
3222 if (!persistent_group) {
3223 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003224 " to join an active group (SSID: %s)",
3225 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3227 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3228 == 0 ||
3229 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3230 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3231 "authorized invitation");
3232 goto accept_inv;
3233 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003234
3235#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003236 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3237 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003238 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003239 wpa_s->p2p_wps_method = WPS_NFC;
3240 wpa_s->pending_join_wps_method = WPS_NFC;
3241 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003242 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003243 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003244 bssid, ETH_ALEN);
3245 goto accept_inv;
3246 }
3247#endif /* CONFIG_WPS_NFC */
3248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 /*
3250 * Do not accept the invitation automatically; notify user and
3251 * request approval.
3252 */
3253 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3254 }
3255
3256 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3257 if (grp) {
3258 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3259 "running persistent group");
3260 if (*go)
3261 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3262 goto accept_inv;
3263 }
3264
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003265 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3266 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3267 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3268 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003269 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003270 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3272
3273 for (s = wpa_s->conf->ssid; s; s = s->next) {
3274 if (s->disabled == 2 &&
3275 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3276 s->ssid_len == ssid_len &&
3277 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3278 break;
3279 }
3280
3281 if (!s) {
3282 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3283 " requested reinvocation of an unknown group",
3284 MAC2STR(sa));
3285 return P2P_SC_FAIL_UNKNOWN_GROUP;
3286 }
3287
3288 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3289 *go = 1;
3290 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3291 wpa_printf(MSG_DEBUG, "P2P: The only available "
3292 "interface is already in use - reject "
3293 "invitation");
3294 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3295 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003296 if (wpa_s->p2p_mgmt)
3297 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3298 ETH_ALEN);
3299 else
3300 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 } else if (s->mode == WPAS_MODE_P2P_GO) {
3302 *go = 1;
3303 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3304 {
3305 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3306 "interface address for the group");
3307 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3308 }
3309 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3310 ETH_ALEN);
3311 }
3312
3313accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003314 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3315
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003316 best_freq = 0;
3317 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3318 sizeof(struct wpa_used_freq_data));
3319 if (freqs) {
3320 int num_channels = wpa_s->num_multichan_concurrent;
3321 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3322 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3323 os_free(freqs);
3324 }
3325
Sunil Ravi036cec52023-03-29 11:35:17 -07003326 num_pref_freq = P2P_MAX_PREF_CHANNELS;
3327 res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3328 &num_pref_freq);
3329 if (res > 0)
3330 best_freq = res;
3331
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003332 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003333 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003334 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003335 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003336
3337 if (wpa_s->num_multichan_concurrent < 2 ||
3338 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3339 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 -07003340 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003341 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 }
3343
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003344 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3345 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003346 if (*go == 0) {
3347 /* We are the client */
3348 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3349 "running a GO but we are capable of MCC, "
3350 "figure out the best channel to use");
3351 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003352 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003353 /* We are the GO, and *force_freq is not in the
3354 * intersection */
3355 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3356 "in intersection but we are capable of MCC, "
3357 "figure out the best channel to use",
3358 *force_freq);
3359 *force_freq = 0;
3360 }
3361 }
3362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 return P2P_SC_SUCCESS;
3364}
3365
3366
3367static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3368 const u8 *ssid, size_t ssid_len,
3369 const u8 *go_dev_addr, u8 status,
3370 int op_freq)
3371{
3372 struct wpa_supplicant *wpa_s = ctx;
3373 struct wpa_ssid *s;
3374
3375 for (s = wpa_s->conf->ssid; s; s = s->next) {
3376 if (s->disabled == 2 &&
3377 s->ssid_len == ssid_len &&
3378 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3379 break;
3380 }
3381
3382 if (status == P2P_SC_SUCCESS) {
3383 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003384 " was accepted; op_freq=%d MHz, SSID=%s",
3385 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003387 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003388 if (go) {
3389 wpa_msg_global(wpa_s, MSG_INFO,
3390 P2P_EVENT_INVITATION_ACCEPTED
3391 "sa=" MACSTR
3392 " persistent=%d freq=%d",
3393 MAC2STR(sa), s->id, op_freq);
3394 } else {
3395 wpa_msg_global(wpa_s, MSG_INFO,
3396 P2P_EVENT_INVITATION_ACCEPTED
3397 "sa=" MACSTR
3398 " persistent=%d",
3399 MAC2STR(sa), s->id);
3400 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003401 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003402 wpa_s, s, go, 0, op_freq, 0,
3403 wpa_s->conf->p2p_go_ht40,
3404 wpa_s->conf->p2p_go_vht,
3405 0,
3406 wpa_s->conf->p2p_go_he,
3407 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003408 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003409 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003410 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003412 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003413 wpa_msg_global(wpa_s, MSG_INFO,
3414 P2P_EVENT_INVITATION_ACCEPTED
3415 "sa=" MACSTR " go_dev_addr=" MACSTR
3416 " bssid=" MACSTR " unknown-network",
3417 MAC2STR(sa), MAC2STR(go_dev_addr),
3418 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003420 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003421 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422 }
3423 return;
3424 }
3425
3426 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3427 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3428 " was rejected (status %u)", MAC2STR(sa), status);
3429 return;
3430 }
3431
3432 if (!s) {
3433 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003434 wpa_msg_global(wpa_s, MSG_INFO,
3435 P2P_EVENT_INVITATION_RECEIVED
3436 "sa=" MACSTR " go_dev_addr=" MACSTR
3437 " bssid=" MACSTR " unknown-network",
3438 MAC2STR(sa), MAC2STR(go_dev_addr),
3439 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003441 wpa_msg_global(wpa_s, MSG_INFO,
3442 P2P_EVENT_INVITATION_RECEIVED
3443 "sa=" MACSTR " go_dev_addr=" MACSTR
3444 " unknown-network",
3445 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003447 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3448 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 return;
3450 }
3451
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003452 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003453 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3454 "sa=" MACSTR " persistent=%d freq=%d",
3455 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003456 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003457 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3458 "sa=" MACSTR " persistent=%d",
3459 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003460 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003461 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3462 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463}
3464
3465
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003466static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3467 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003468 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003469{
3470 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003471 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003472
3473 if (ssid == NULL)
3474 return;
3475
3476 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003477 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003478 ETH_ALEN) == 0)
3479 break;
3480 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003481 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003482 if (ssid->mode != WPAS_MODE_P2P_GO &&
3483 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3484 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3485 "due to invitation result", ssid->id);
3486 wpas_notify_network_removed(wpa_s, ssid);
3487 wpa_config_remove_network(wpa_s->conf, ssid->id);
3488 return;
3489 }
3490 return; /* Peer not found in client list */
3491 }
3492
3493 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003494 "group %d client list%s",
3495 MAC2STR(peer), ssid->id,
3496 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003497 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3498 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3499 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003500 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003501 if (p2p_wpa_s->conf->update_config &&
3502 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003503 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003504}
3505
3506
3507static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3508 const u8 *peer)
3509{
3510 struct wpa_ssid *ssid;
3511
3512 wpa_s = wpa_s->global->p2p_invite_group;
3513 if (wpa_s == NULL)
3514 return; /* No known invitation group */
3515 ssid = wpa_s->current_ssid;
3516 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3517 !ssid->p2p_persistent_group)
3518 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003519 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003520 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003521 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003522}
3523
3524
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003525static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003526 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003527 const u8 *peer, int neg_freq,
3528 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529{
3530 struct wpa_supplicant *wpa_s = ctx;
3531 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003532 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533
3534 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003535 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3536 "status=%d " MACSTR,
3537 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003539 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3540 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 }
3542 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3543
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003544 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3545 status, MAC2STR(peer));
3546 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003547 struct wpa_supplicant *group_if =
3548 wpa_s->global->p2p_invite_group;
3549
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003550 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3551 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003552
3553 /*
3554 * Invitation to an active group. If this is successful and we
3555 * are the GO, set the client wait to postpone some concurrent
3556 * operations and to allow provisioning and connection to happen
3557 * more quickly.
3558 */
3559 if (status == P2P_SC_SUCCESS &&
3560 group_if && group_if->current_ssid &&
3561 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3562 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3563#ifdef CONFIG_TESTING_OPTIONS
3564 if (group_if->p2p_go_csa_on_inv) {
3565 wpa_printf(MSG_DEBUG,
3566 "Testing: force P2P GO CSA after invitation");
3567 eloop_cancel_timeout(
3568 wpas_p2p_reconsider_moving_go,
3569 wpa_s, NULL);
3570 eloop_register_timeout(
3571 0, 50000,
3572 wpas_p2p_reconsider_moving_go,
3573 wpa_s, NULL);
3574 }
3575#endif /* CONFIG_TESTING_OPTIONS */
3576 }
3577 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003578 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003580 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3581 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3582 "invitation exchange to indicate readiness for "
3583 "re-invocation");
3584 }
3585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003587 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3588 ssid = wpa_config_get_network(
3589 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003590 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592 wpas_p2p_remove_pending_group_interface(wpa_s);
3593 return;
3594 }
3595
3596 ssid = wpa_config_get_network(wpa_s->conf,
3597 wpa_s->pending_invite_ssid_id);
3598 if (ssid == NULL) {
3599 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3600 "data matching with invitation");
3601 return;
3602 }
3603
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003604 /*
3605 * The peer could have missed our ctrl::ack frame for Invitation
3606 * Response and continue retransmitting the frame. To reduce the
3607 * likelihood of the peer not getting successful TX status for the
3608 * Invitation Response frame, wait a short time here before starting
3609 * the persistent group so that we will remain on the current channel to
3610 * acknowledge any possible retransmission from the peer.
3611 */
3612 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3613 "starting persistent group");
3614 os_sleep(0, 50000);
3615
Dmitry Shmidt15907092014-03-25 10:42:57 -07003616 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003617 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003618 freq = neg_freq;
3619 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003620 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003621 freq = peer_oper_freq;
3622 else
3623 freq = 0;
3624
3625 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3626 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003628 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003629 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003630 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003631 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003632 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003633 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003634 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003635 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003636 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003637 ssid->mode == WPAS_MODE_P2P_GO ?
3638 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003639 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00003640 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003641 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003642}
3643
3644
Dmitry Shmidt04949592012-07-19 12:16:46 -07003645static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3646 unsigned int freq)
3647{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003648 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3649 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003650 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003651}
3652
3653
3654static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3655{
3656 reg->channel[reg->channels] = chan;
3657 reg->channels++;
3658}
3659
3660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003661static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003662 struct p2p_channels *chan,
3663 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664{
3665 int i, cla = 0;
3666
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003667 wpa_s->global->p2p_24ghz_social_channels = 1;
3668
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003669 os_memset(cli_chan, 0, sizeof(*cli_chan));
3670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3672 "band");
3673
3674 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3675 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003676 chan->reg_class[cla].channels = 0;
3677 for (i = 0; i < 11; i++) {
3678 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3679 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3680 }
3681 if (chan->reg_class[cla].channels)
3682 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683
3684 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3685 "band");
3686
3687 /* Operating class 115 - 5 GHz, channels 36-48 */
3688 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003689 chan->reg_class[cla].channels = 0;
3690 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3691 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3692 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3693 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3694 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3695 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3696 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3697 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3698 if (chan->reg_class[cla].channels)
3699 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700
3701 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3702 "band");
3703
3704 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3705 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003706 chan->reg_class[cla].channels = 0;
3707 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3708 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3709 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3710 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3711 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3712 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3713 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3714 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3715 if (chan->reg_class[cla].channels)
3716 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717
3718 chan->reg_classes = cla;
3719 return 0;
3720}
3721
3722
Hai Shalomc1a21442022-02-04 13:43:00 -08003723static enum chan_allowed has_channel(struct wpa_global *global,
3724 struct hostapd_hw_modes *mode, u8 op_class,
3725 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726{
3727 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003728 unsigned int freq;
3729
Hai Shalomc1a21442022-02-04 13:43:00 -08003730 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003731 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003732 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003733
3734 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003735 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 if (flags)
3737 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003738 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003739 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003740 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3741 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003742 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3743 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003744 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 }
3746 }
3747
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003748 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749}
3750
3751
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003752static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3753 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003754 u8 channel, const u8 *center_channels,
3755 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003756{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003757 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003758
3759 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3760 return 0;
3761
Hai Shalomc1a21442022-02-04 13:43:00 -08003762 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003763 /*
3764 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3765 * so the center channel is 6 channels away from the start/end.
3766 */
3767 if (channel >= center_channels[i] - 6 &&
3768 channel <= center_channels[i] + 6)
3769 return center_channels[i];
3770
3771 return 0;
3772}
3773
3774
Hai Shalomc1a21442022-02-04 13:43:00 -08003775static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3776 155, 171 };
3777static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3778 119, 135, 151, 167, 183, 199,
3779 215 };
3780
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003781static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3782 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003783 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003784{
3785 u8 center_chan;
3786 int i, flags;
3787 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003788 const u8 *chans;
3789 size_t num_chans;
3790 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003791
Hai Shalomc1a21442022-02-04 13:43:00 -08003792 if (is_6ghz) {
3793 chans = center_channels_6ghz_80mhz;
3794 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3795 } else {
3796 chans = center_channels_5ghz_80mhz;
3797 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3798 }
3799 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3800 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003801 if (!center_chan)
3802 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003803 if (!wpa_s->p2p_go_allow_dfs &&
3804 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003805 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3806
3807 /* check all the channels are available */
3808 for (i = 0; i < 4; i++) {
3809 int adj_chan = center_chan - 6 + i * 4;
3810
Hai Shalomc1a21442022-02-04 13:43:00 -08003811 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3812 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003813 if (res == NOT_ALLOWED)
3814 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003815 if (res == RADAR)
3816 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003817 if (res == NO_IR)
3818 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003819 if (!is_6ghz) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003820 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003821 return NOT_ALLOWED;
3822 } else if (is_6ghz &&
3823 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3824 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003825 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003826 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003827 }
3828
3829 return ret;
3830}
3831
3832
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003833static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3834 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003835 u8 channel, const u8 *center_channels,
3836 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003837{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003838 unsigned int i;
3839
3840 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3841 return 0;
3842
Hai Shalomc1a21442022-02-04 13:43:00 -08003843 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003844 /*
3845 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3846 * so the center channel is 14 channels away from the start/end.
3847 */
3848 if (channel >= center_channels[i] - 14 &&
3849 channel <= center_channels[i] + 14)
3850 return center_channels[i];
3851
3852 return 0;
3853}
3854
3855
Hai Shalomc1a21442022-02-04 13:43:00 -08003856static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3857static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3858 207 };
3859
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003860static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3861 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003862 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003863{
3864 u8 center_chan;
3865 int i, flags;
3866 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003867 const u8 *chans;
3868 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003869
Hai Shalomc1a21442022-02-04 13:43:00 -08003870 if (is_6ghz_op_class(op_class)) {
3871 chans = center_channels_6ghz_160mhz;
3872 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3873 } else {
3874 chans = center_channels_5ghz_160mhz;
3875 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3876 }
3877 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3878 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003879 if (!center_chan)
3880 return NOT_ALLOWED;
3881 /* VHT 160 MHz uses DFS channels in most countries. */
3882
3883 /* Check all the channels are available */
3884 for (i = 0; i < 8; i++) {
3885 int adj_chan = center_chan - 14 + i * 4;
3886
Hai Shalomc1a21442022-02-04 13:43:00 -08003887 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3888 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003889 if (res == NOT_ALLOWED)
3890 return NOT_ALLOWED;
3891
Hai Shalomc1a21442022-02-04 13:43:00 -08003892 if (res == RADAR)
3893 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003894 if (res == NO_IR)
3895 ret = NO_IR;
3896
Hai Shalomc1a21442022-02-04 13:43:00 -08003897 if (!is_6ghz_op_class(op_class)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003898 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
3899 !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003900 return NOT_ALLOWED;
3901 } else if (is_6ghz_op_class(op_class) &&
3902 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3903 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003904 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003905 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003906 }
3907
3908 return ret;
3909}
3910
3911
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003912static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3913 struct hostapd_hw_modes *mode,
3914 u8 channel)
3915{
3916 struct ieee80211_edmg_config edmg;
3917
3918 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3919 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3920 return ALLOWED;
3921
3922 return NOT_ALLOWED;
3923}
3924
3925
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003926static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3927 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003928 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003929{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003930 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003931 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003932
Sunil Ravi89eba102022-09-13 21:04:37 -07003933 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3934 p2p_channel_to_freq(op_class, channel)))
3935 return NOT_ALLOWED;
3936
Hai Shalomc1a21442022-02-04 13:43:00 -08003937 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003938 if (bw == BW40MINUS) {
3939 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3940 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003941 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3942 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003943 } else if (bw == BW40PLUS) {
3944 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3945 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003946 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3947 NULL);
3948 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3949 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3950 return NOT_ALLOWED;
3951 if (get_6ghz_sec_channel(channel) < 0)
3952 res2 = has_channel(wpa_s->global, mode, op_class,
3953 channel - 4, NULL);
3954 else
3955 res2 = has_channel(wpa_s->global, mode, op_class,
3956 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003957 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003958 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3959 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003960 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003961 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3962 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003963 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3964 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003965 }
3966
3967 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3968 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003969 if (res == NO_IR || res2 == NO_IR)
3970 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003971 if (res == RADAR || res2 == RADAR)
3972 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003973 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003974}
3975
3976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003978 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003979 struct p2p_channels *cli_chan,
3980 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003982 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003983 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003985 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3987 "of all supported channels; assume dualband "
3988 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003989 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990 }
3991
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003992 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003994 for (op = 0; global_op_class[op].op_class; op++) {
3995 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003996 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003997 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Shuibing Daie2fad412023-05-05 14:08:11 -07003998 bool check_dfs_supported = (is_p2p_dfs_chan_enabled(wpa_s->global->p2p)
Shuibing Dai64a8a892023-03-08 10:26:22 -08003999 && is_dfs_global_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000
Shuibing Dai64a8a892023-03-08 10:26:22 -08004001 if ((!check_dfs_supported && o->p2p == NO_P2P_SUPP) ||
Hai Shalom60840252021-02-19 19:02:11 -08004002 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004003 continue;
4004
Hai Shalomfdcde762020-04-02 11:19:20 -07004005 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4006 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007 if (mode == NULL)
4008 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004009 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4010 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004012 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07004013
4014 /* Check for non-continuous jump in channel index
4015 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08004016 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07004017 ch < 149 && ch + o->inc > 149)
4018 ch = 149;
4019
Hai Shalomc1a21442022-02-04 13:43:00 -08004020 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4021 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004022 if (res == ALLOWED) {
4023 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004024 if (cla == P2P_MAX_REG_CLASSES)
4025 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004026 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4027 o->op_class);
4028 reg = &chan->reg_class[cla];
4029 cla++;
4030 reg->reg_class = o->op_class;
4031 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004032 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4033 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004034 reg->channel[reg->channels] = ch;
4035 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004036 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004037 wpa_s->conf->p2p_add_cli_chan) {
4038 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004039 if (cli_cla == P2P_MAX_REG_CLASSES)
4040 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004041 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4042 o->op_class);
4043 cli_reg = &cli_chan->reg_class[cli_cla];
4044 cli_cla++;
4045 cli_reg->reg_class = o->op_class;
4046 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004047 if (cli_reg->channels ==
4048 P2P_MAX_REG_CLASS_CHANNELS)
4049 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004050 cli_reg->channel[cli_reg->channels] = ch;
4051 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 }
4054 if (reg) {
4055 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4056 reg->channel, reg->channels);
4057 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004058 if (cli_reg) {
4059 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4060 cli_reg->channel, cli_reg->channels);
4061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 }
4063
4064 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004065 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067 return 0;
4068}
4069
4070
Hai Shalomc1a21442022-02-04 13:43:00 -08004071int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4072 struct hostapd_hw_modes *mode,
4073 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004074{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004075 int op;
4076 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004077
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004078 for (op = 0; global_op_class[op].op_class; op++) {
4079 const struct oper_class_map *o = &global_op_class[op];
Sunil Ravi77d572f2023-01-17 23:58:31 +00004080 u16 ch = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004081
Hai Shalomc1a21442022-02-04 13:43:00 -08004082 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4083 * driver offloaded DFS. */
4084 if ((o->p2p == NO_P2P_SUPP &&
4085 (!is_dfs_global_op_class(o->op_class) ||
4086 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004087 (is_6ghz_op_class(o->op_class) &&
4088 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004089 continue;
4090
Sunil Ravi77d572f2023-01-17 23:58:31 +00004091 /* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4092 * set up the BSS with a primary 20 MHz channel that coincides
4093 * with a preferred scanning channel (PSC)."
4094 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4095 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4096 * match the PSC.
4097 */
Hai Shalomc1a21442022-02-04 13:43:00 -08004098 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4099 get_6ghz_sec_channel(channel) < 0)
Sunil Ravi77d572f2023-01-17 23:58:31 +00004100 ch = 4;
Hai Shalomc1a21442022-02-04 13:43:00 -08004101
Sunil Ravi77d572f2023-01-17 23:58:31 +00004102 for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004103 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004104 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4105 o->bw != BW40) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00004106 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004107 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004108 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4109 ch, o->bw);
4110 if (ret == ALLOWED) {
4111 if (is_6ghz_op_class(o->op_class) &&
4112 o->bw == BW40)
4113 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004114 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004115 }
4116 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4117 /* Allow RADAR channels used for driver
4118 * offloaded DFS */
4119 return (o->bw == BW40MINUS) ? -1 : 1;
4120 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004121 }
4122 }
4123 return 0;
4124}
4125
4126
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004127int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004128 struct hostapd_hw_modes *mode, u8 channel,
4129 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004130{
Hai Shalomc1a21442022-02-04 13:43:00 -08004131 const u8 *chans;
4132 size_t num_chans;
4133 enum chan_allowed ret;
4134
4135 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4136 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004137 return 0;
4138
Hai Shalomc1a21442022-02-04 13:43:00 -08004139 if (is_6ghz_op_class(op_class)) {
4140 chans = center_channels_6ghz_80mhz;
4141 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4142 } else {
4143 chans = center_channels_5ghz_80mhz;
4144 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4145 }
4146 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4147 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004148}
4149
4150
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004151int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004152 struct hostapd_hw_modes *mode, u8 channel,
4153 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004154{
Hai Shalomc1a21442022-02-04 13:43:00 -08004155 const u8 *chans;
4156 size_t num_chans;
4157 enum chan_allowed ret;
4158
4159 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4160 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004161 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004162 if (is_6ghz_op_class(op_class)) {
4163 chans = center_channels_6ghz_160mhz;
4164 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4165 } else {
4166 chans = center_channels_5ghz_160mhz;
4167 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4168 }
4169 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4170 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004171}
4172
4173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4175 size_t buf_len)
4176{
4177 struct wpa_supplicant *wpa_s = ctx;
4178
4179 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4180 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4181 break;
4182 }
4183 if (wpa_s == NULL)
4184 return -1;
4185
4186 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4187}
4188
4189
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004190struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4191 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004192{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004193 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4194 struct wpa_ssid *s = wpa_s->current_ssid;
4195 if (s == NULL)
4196 continue;
4197 if (s->mode != WPAS_MODE_P2P_GO &&
4198 s->mode != WPAS_MODE_AP &&
4199 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4200 continue;
4201 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004202 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004203 continue;
4204 return wpa_s;
4205 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004206
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004207 return NULL;
4208
4209}
4210
4211
4212struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4213 const u8 *peer_dev_addr)
4214{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004215 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4216 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004217 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004218 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004219 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4220 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004221 }
4222
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004223 return NULL;
4224}
4225
4226
4227static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4228{
4229 struct wpa_supplicant *wpa_s = ctx;
4230
4231 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004232}
4233
4234
Dmitry Shmidt18463232014-01-24 12:29:41 -08004235static int wpas_is_concurrent_session_active(void *ctx)
4236{
4237 struct wpa_supplicant *wpa_s = ctx;
4238 struct wpa_supplicant *ifs;
4239
4240 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4241 if (ifs == wpa_s)
4242 continue;
4243 if (ifs->wpa_state > WPA_ASSOCIATED)
4244 return 1;
4245 }
4246 return 0;
4247}
4248
4249
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004250static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4251{
4252 struct wpa_supplicant *wpa_s = ctx;
4253 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4254}
4255
4256
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004257int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4258 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004259{
4260 struct wpa_interface iface;
4261 struct wpa_supplicant *p2pdev_wpa_s;
4262 char ifname[100];
4263 char force_name[100];
4264 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004265 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004266
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004267 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4268 wpa_s->ifname);
4269 if (os_snprintf_error(sizeof(ifname), ret))
4270 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004271 /* Cut length at the maximum size. Note that we don't need to ensure
4272 * collision free names here as the created interface is not a netdev.
4273 */
4274 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004275 force_name[0] = '\0';
4276 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004277
4278 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4279 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4280 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4281
4282 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004283 force_name, wpa_s->pending_interface_addr, NULL);
4284 if (ret < 0) {
4285 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4286 return ret;
4287 }
4288 os_strlcpy(wpa_s->pending_interface_name, ifname,
4289 sizeof(wpa_s->pending_interface_name));
4290
4291 os_memset(&iface, 0, sizeof(iface));
4292 iface.p2p_mgmt = 1;
4293 iface.ifname = wpa_s->pending_interface_name;
4294 iface.driver = wpa_s->driver->name;
4295 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004296
4297 /*
4298 * If a P2P Device configuration file was given, use it as the interface
4299 * configuration file (instead of using parent's configuration file.
4300 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004301 if (conf_p2p_dev) {
4302 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004303 iface.ctrl_interface = NULL;
4304 } else {
4305 iface.confname = wpa_s->confname;
4306 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4307 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004308
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004309 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004310 if (!p2pdev_wpa_s) {
4311 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4312 return -1;
4313 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004314
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004315 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004316 wpa_s->pending_interface_name[0] = '\0';
4317 return 0;
4318}
4319
4320
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004321static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4322 const u8 *noa, size_t noa_len)
4323{
4324 struct wpa_supplicant *wpa_s, *intf = ctx;
4325 char hex[100];
4326
4327 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4328 if (wpa_s->waiting_presence_resp)
4329 break;
4330 }
4331 if (!wpa_s) {
4332 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4333 return;
4334 }
4335 wpa_s->waiting_presence_resp = 0;
4336
4337 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4338 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4339 " status=%u noa=%s", MAC2STR(src), status, hex);
4340}
4341
4342
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004343static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4344 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004345 u8 *ret_ssid, size_t *ret_ssid_len,
4346 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004347{
4348 struct wpa_supplicant *wpa_s = ctx;
4349 struct wpa_ssid *s;
4350
4351 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4352 if (s) {
4353 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4354 *ret_ssid_len = s->ssid_len;
4355 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004356
4357 if (s->mode != WPAS_MODE_P2P_GO) {
4358 os_memset(intended_iface_addr, 0, ETH_ALEN);
4359 } else if (wpas_p2p_create_iface(wpa_s)) {
4360 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4361 return 0;
4362
4363 os_memcpy(intended_iface_addr,
4364 wpa_s->pending_interface_addr, ETH_ALEN);
4365 } else {
4366 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4367 ETH_ALEN);
4368 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004369 return 1;
4370 }
4371
4372 return 0;
4373}
4374
4375
4376static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004377 u8 *ssid, size_t *ssid_len, int *group_iface,
4378 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004379{
4380 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004381 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004382 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004383
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004384 /*
4385 * group_iface will be set to 1 only if a dedicated interface for P2P
4386 * role is required. First, we try to reuse an active GO. However,
4387 * if it is not present, we will try to reactivate an existing
4388 * persistent group and set group_iface to 1, so the caller will know
4389 * that the pending interface should be used.
4390 */
4391 *group_iface = 0;
4392
4393 if (freq)
4394 *freq = 0;
4395
4396 go = wpas_p2p_get_go_group(wpa_s);
4397 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004398 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004399 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004400 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004401 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4402 else
4403 return 0;
4404 } else {
4405 s = go->current_ssid;
4406 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4407 if (freq)
4408 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004409 }
4410
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004411 os_memcpy(ssid, s->ssid, s->ssid_len);
4412 *ssid_len = s->ssid_len;
4413
4414 return 1;
4415}
4416
4417
4418static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4419 const u8 *ssid, size_t ssid_len)
4420{
4421 struct wpa_supplicant *wpa_s = ctx;
4422 struct wpa_ssid *s;
4423 int save_config = 0;
4424 size_t i;
4425
4426 /* Start with our first choice of Persistent Groups */
4427 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4428 if (go && ssid && ssid_len &&
4429 s->ssid_len == ssid_len &&
4430 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4431 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4432 break;
4433
4434 /* Remove stale persistent group */
4435 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004436 wpa_dbg(wpa_s, MSG_DEBUG,
4437 "P2P: Remove stale persistent group id=%d",
4438 s->id);
4439 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004440 wpa_config_remove_network(wpa_s->conf, s->id);
4441 save_config = 1;
4442 continue;
4443 }
4444
4445 for (i = 0; i < s->num_p2p_clients; i++) {
4446 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4447 peer, ETH_ALEN) != 0)
4448 continue;
4449
4450 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4451 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4452 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4453 break;
4454 }
4455 s->num_p2p_clients--;
4456 save_config = 1;
4457 }
4458
4459 if (save_config)
4460 p2p_config_write(wpa_s);
4461
4462 /* Return TRUE if valid SSID remains */
4463 return s != NULL;
4464}
4465
4466
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004467static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4468 const u8 *feat_cap, size_t feat_cap_len)
4469{
4470 static const char pref[] = " feature_cap=";
4471 int ret;
4472
4473 buf[0] = '\0';
4474
4475 /*
4476 * We expect a feature capability to contain at least one byte to be
4477 * reported. The string buffer provided by the caller function is
4478 * expected to be big enough to contain all bytes of the attribute for
4479 * known specifications. This function truncates the reported bytes if
4480 * the feature capability data exceeds the string buffer size.
4481 */
4482 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4483 return;
4484
4485 os_memcpy(buf, pref, sizeof(pref));
4486 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4487 buf_len - sizeof(pref) + 1,
4488 feat_cap, feat_cap_len);
4489
4490 if (ret != (2 * (int) feat_cap_len))
4491 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4492}
4493
4494
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004495static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4496 const u8 *adv_mac, const u8 *ses_mac,
4497 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4498 u8 conncap, int passwd_id,
4499 const u8 *persist_ssid,
4500 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004501 int prov_start, const char *session_info,
4502 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004503 unsigned int freq,
4504 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004505{
4506 struct wpa_supplicant *wpa_s = ctx;
4507 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004508 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004509 int save_config = 0;
4510 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004511 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004512
4513 if (!dev)
4514 return;
4515
4516 os_memset(mac, 0, ETH_ALEN);
4517 if (!adv_mac)
4518 adv_mac = mac;
4519 if (!ses_mac)
4520 ses_mac = mac;
4521 if (!grp_mac)
4522 grp_mac = mac;
4523
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004524 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4525 feat_cap, feat_cap_len);
4526
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004527 if (prov_start) {
4528 if (session_info == NULL) {
4529 wpa_msg_global(wpa_s, MSG_INFO,
4530 P2P_EVENT_P2PS_PROVISION_START MACSTR
4531 " adv_id=%x conncap=%x"
4532 " adv_mac=" MACSTR
4533 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004534 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004535 MAC2STR(dev), adv_id, conncap,
4536 MAC2STR(adv_mac),
4537 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004538 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004539 } else {
4540 wpa_msg_global(wpa_s, MSG_INFO,
4541 P2P_EVENT_P2PS_PROVISION_START MACSTR
4542 " adv_id=%x conncap=%x"
4543 " adv_mac=" MACSTR
4544 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004545 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004546 MAC2STR(dev), adv_id, conncap,
4547 MAC2STR(adv_mac),
4548 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004549 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004550 }
4551 return;
4552 }
4553
4554 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4555 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4556
4557 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4558 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4559 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4560
4561 if (persistent_go && !persistent_go->num_p2p_clients) {
4562 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004563 wpa_dbg(wpa_s, MSG_DEBUG,
4564 "P2P: Remove empty persistent group id=%d",
4565 persistent_go->id);
4566 wpas_notify_persistent_group_removed(wpa_s,
4567 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004568 wpa_config_remove_network(wpa_s->conf,
4569 persistent_go->id);
4570 }
4571
4572 wpa_msg_global(wpa_s, MSG_INFO,
4573 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4574 " status=%d"
4575 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004576 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004577 MAC2STR(dev), status,
4578 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004579 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004580 return;
4581 }
4582
4583 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004584 if (persist_ssid && persist_ssid_size)
4585 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4586 persist_ssid_size);
4587
4588 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4589 is_zero_ether_addr(grp_mac)) {
4590 wpa_dbg(wpa_s, MSG_ERROR,
4591 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4592 return;
4593 }
4594
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004595 for (;;) {
4596 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4597 if (!stale)
4598 break;
4599
4600 if (s && s->ssid_len == stale->ssid_len &&
4601 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4602 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4603 break;
4604
4605 /* Remove stale persistent group */
4606 if (stale->mode != WPAS_MODE_P2P_GO ||
4607 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004608 wpa_dbg(wpa_s, MSG_DEBUG,
4609 "P2P: Remove stale persistent group id=%d",
4610 stale->id);
4611 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004612 wpa_config_remove_network(wpa_s->conf, stale->id);
4613 } else {
4614 size_t i;
4615
4616 for (i = 0; i < stale->num_p2p_clients; i++) {
4617 if (os_memcmp(stale->p2p_client_list +
4618 i * ETH_ALEN,
4619 dev, ETH_ALEN) == 0) {
4620 os_memmove(stale->p2p_client_list +
4621 i * ETH_ALEN,
4622 stale->p2p_client_list +
4623 (i + 1) * ETH_ALEN,
4624 (stale->num_p2p_clients -
4625 i - 1) * ETH_ALEN);
4626 break;
4627 }
4628 }
4629 stale->num_p2p_clients--;
4630 }
4631 save_config = 1;
4632 }
4633
4634 if (save_config)
4635 p2p_config_write(wpa_s);
4636
4637 if (s) {
4638 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4639 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4640
4641 if (persistent_go && s != persistent_go &&
4642 !persistent_go->num_p2p_clients) {
4643 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004644 wpa_dbg(wpa_s, MSG_DEBUG,
4645 "P2P: Remove empty persistent group id=%d",
4646 persistent_go->id);
4647 wpas_notify_persistent_group_removed(wpa_s,
4648 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004649 wpa_config_remove_network(wpa_s->conf,
4650 persistent_go->id);
4651 /* Save config */
4652 }
4653
4654 wpa_msg_global(wpa_s, MSG_INFO,
4655 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4656 " status=%d"
4657 " adv_id=%x adv_mac=" MACSTR
4658 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004659 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004660 MAC2STR(dev), status,
4661 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004662 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004663 return;
4664 }
4665
Hai Shalom5f92bc92019-04-18 11:54:11 -07004666 wpa_s->global->pending_p2ps_group = 0;
4667 wpa_s->global->pending_p2ps_group_freq = 0;
4668
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004669 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004670 /*
4671 * We need to copy the interface name. Simply saving a
4672 * pointer isn't enough, since if we use pending_interface_name
4673 * it will be overwritten when the group is added.
4674 */
4675 char go_ifname[100];
4676
4677 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004678 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004679 if (!response_done) {
4680 wpa_s->global->pending_p2ps_group = 1;
4681 wpa_s->global->pending_p2ps_group_freq = freq;
4682 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004684 if (!wpas_p2p_create_iface(wpa_s))
4685 os_memcpy(go_ifname, wpa_s->ifname,
4686 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004687 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004688 os_memcpy(go_ifname,
4689 wpa_s->pending_interface_name,
4690 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004691
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004692 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004693 wpas_p2ps_prov_complete(
4694 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4695 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004696 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004697 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4698 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004699 return;
4700 }
4701
4702 /* If PD Resp complete, start up the GO */
4703 if (response_done && persistent_go) {
4704 wpas_p2p_group_add_persistent(
4705 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004706 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004707 persistent_go->mode ==
4708 WPAS_MODE_P2P_GO ?
4709 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Sunil Ravi036cec52023-03-29 11:35:17 -07004710 0, 0, false, 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004711 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004712 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004713 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004714 }
4715
4716 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004717 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4718 ETH_ALEN);
4719 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004720 }
4721 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004722 os_memcpy(go_ifname, go_wpa_s->ifname,
4723 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004724
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004725 if (is_zero_ether_addr(grp_mac)) {
4726 wpa_dbg(go_wpa_s, MSG_DEBUG,
4727 "P2P: Setting PIN-1 for ANY");
4728 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4729 "12345670", NULL, 0,
4730 0);
4731 } else {
4732 wpa_dbg(go_wpa_s, MSG_DEBUG,
4733 "P2P: Setting PIN-1 for " MACSTR,
4734 MAC2STR(grp_mac));
4735 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4736 "12345670", NULL, 0,
4737 0);
4738 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004739
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004740 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4741 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004742 }
4743
4744 wpa_msg_global(wpa_s, MSG_INFO,
4745 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4746 " status=%d conncap=%x"
4747 " adv_id=%x adv_mac=" MACSTR
4748 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004749 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004750 MAC2STR(dev), status, conncap,
4751 adv_id, MAC2STR(adv_mac),
4752 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004753 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004754 return;
4755 }
4756
4757 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4758 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4759
4760 if (persistent_go && !persistent_go->num_p2p_clients) {
4761 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004762 wpa_dbg(wpa_s, MSG_DEBUG,
4763 "P2P: Remove empty persistent group id=%d",
4764 persistent_go->id);
4765 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004766 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4767 }
4768
4769 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004770 char ssid_hex[32 * 2 + 1];
4771
4772 if (group_ssid)
4773 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4774 group_ssid, group_ssid_len);
4775 else
4776 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004777 wpa_msg_global(wpa_s, MSG_INFO,
4778 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4779 " status=%d conncap=%x"
4780 " adv_id=%x adv_mac=" MACSTR
4781 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004782 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004783 MAC2STR(dev), status, conncap,
4784 adv_id, MAC2STR(adv_mac),
4785 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004786 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4787 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004788 } else {
4789 wpa_msg_global(wpa_s, MSG_INFO,
4790 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4791 " status=%d conncap=%x"
4792 " adv_id=%x adv_mac=" MACSTR
4793 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004794 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004795 MAC2STR(dev), status, conncap,
4796 adv_id, MAC2STR(adv_mac),
4797 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004798 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004799 }
4800}
4801
4802
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004803static int _wpas_p2p_in_progress(void *ctx)
4804{
4805 struct wpa_supplicant *wpa_s = ctx;
4806 return wpas_p2p_in_progress(wpa_s);
4807}
4808
4809
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004810static int wpas_prov_disc_resp_cb(void *ctx)
4811{
4812 struct wpa_supplicant *wpa_s = ctx;
4813 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004814 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004815
4816 if (!wpa_s->global->pending_p2ps_group)
4817 return 0;
4818
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004819 freq = wpa_s->global->pending_p2ps_group_freq;
4820 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004821 wpa_s->global->pending_p2ps_group = 0;
4822
4823 if (wpas_p2p_get_go_group(wpa_s))
4824 return 0;
4825 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4826
4827 if (persistent_go) {
4828 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004829 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4830 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004831 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004832 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07004833 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004834 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004835 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4836 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004837 }
4838
4839 return 1;
4840}
4841
4842
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004843static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4844 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004845 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004846{
4847 struct wpa_supplicant *wpa_s = ctx;
4848
4849 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4850 WPA_IF_P2P_CLIENT, len, freq_list);
4851}
4852
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004853int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4854{
JaeMan Park9de97322022-07-11 15:36:43 +09004855 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004856 u8 addr[ETH_ALEN] = {0};
4857
4858 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4859 return 0;
4860
Hai Shalom60840252021-02-19 19:02:11 -08004861 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4862 if (is_zero_ether_addr(
4863 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4864 !is_zero_ether_addr(wpa_s->own_addr)) {
4865 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4866 wpa_s->own_addr, ETH_ALEN);
4867 }
4868 return 0;
4869 }
4870
4871 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004872 if (random_mac_addr(addr) < 0) {
4873 wpa_msg(wpa_s, MSG_INFO,
4874 "Failed to generate random MAC address");
4875 return -EINVAL;
4876 }
4877
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004878 /* Store generated MAC address. */
4879 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4880 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004881 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004882 /* If there are existing saved groups, restore last MAC address.
4883 * if there is no last used MAC address, the last one is
4884 * factory MAC. */
4885 if (is_zero_ether_addr(
4886 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004887 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004888 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4889 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004890 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4891 }
4892
JaeMan Park9de97322022-07-11 15:36:43 +09004893 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4894
4895 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004896 wpa_msg(wpa_s, MSG_INFO,
4897 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004898 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004899 }
4900
JaeMan Park9de97322022-07-11 15:36:43 +09004901 ret = wpa_supplicant_update_mac_addr(wpa_s);
4902
4903 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004904 wpa_msg(wpa_s, MSG_INFO,
4905 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004906 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004907 }
4908
4909 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4910 MAC2STR(addr));
4911
4912 return 0;
4913}
4914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915/**
4916 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4917 * @global: Pointer to global data from wpa_supplicant_init()
4918 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4919 * Returns: 0 on success, -1 on failure
4920 */
4921int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4922{
4923 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 int i;
4925
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004926 if (wpa_s->conf->p2p_disabled)
4927 return 0;
4928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4930 return 0;
4931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 if (global->p2p)
4933 return 0;
4934
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004935 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4936 wpa_msg(wpa_s, MSG_ERROR,
4937 "Failed to initialize P2P random MAC address.");
4938 return -1;
4939 }
4940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004943 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 p2p.p2p_scan = wpas_p2p_scan;
4945 p2p.send_action = wpas_send_action;
4946 p2p.send_action_done = wpas_send_action_done;
4947 p2p.go_neg_completed = wpas_go_neg_completed;
4948 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4949 p2p.dev_found = wpas_dev_found;
4950 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004951 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 p2p.start_listen = wpas_start_listen;
4953 p2p.stop_listen = wpas_stop_listen;
4954 p2p.send_probe_resp = wpas_send_probe_resp;
4955 p2p.sd_request = wpas_sd_request;
4956 p2p.sd_response = wpas_sd_response;
4957 p2p.prov_disc_req = wpas_prov_disc_req;
4958 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004959 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 p2p.invitation_process = wpas_invitation_process;
4961 p2p.invitation_received = wpas_invitation_received;
4962 p2p.invitation_result = wpas_invitation_result;
4963 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004964 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004965 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004966 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004967 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004968 p2p.get_persistent_group = wpas_get_persistent_group;
4969 p2p.get_go_info = wpas_get_go_info;
4970 p2p.remove_stale_groups = wpas_remove_stale_groups;
4971 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4972 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4973 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004974 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004975 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Shuibing Daie2fad412023-05-05 14:08:11 -07004976 p2p.p2p_dfs_chan_enable = wpa_s->conf->p2p_dfs_chan_enable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977
4978 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004979 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980 p2p.dev_name = wpa_s->conf->device_name;
4981 p2p.manufacturer = wpa_s->conf->manufacturer;
4982 p2p.model_name = wpa_s->conf->model_name;
4983 p2p.model_number = wpa_s->conf->model_number;
4984 p2p.serial_number = wpa_s->conf->serial_number;
4985 if (wpa_s->wps) {
4986 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4987 p2p.config_methods = wpa_s->wps->config_methods;
4988 }
4989
Hai Shalom60840252021-02-19 19:02:11 -08004990 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4991 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004992 wpa_printf(MSG_ERROR,
4993 "P2P: Failed to configure supported channel list");
4994 return -1;
4995 }
4996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 if (wpa_s->conf->p2p_listen_reg_class &&
4998 wpa_s->conf->p2p_listen_channel) {
4999 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5000 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005001 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 /*
5004 * Pick one of the social channels randomly as the listen
5005 * channel.
5006 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005007 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005008 &p2p.channel,
5009 &global->p2p_go_avoid_freq,
5010 &global->p2p_disallow_freq) !=
5011 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005012 wpa_printf(MSG_INFO,
5013 "P2P: No social channels supported by the driver - do not enable P2P");
5014 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005015 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005016 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005018 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5019 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020
5021 if (wpa_s->conf->p2p_oper_reg_class &&
5022 wpa_s->conf->p2p_oper_channel) {
5023 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5024 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5025 p2p.cfg_op_channel = 1;
5026 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5027 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5028
5029 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005031 * Use random operation channel from 2.4 GHz band social
5032 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5033 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005035 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005036 &p2p.op_channel, NULL,
5037 NULL) != 0) {
5038 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005039 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08005040 p2p.op_reg_class = 0;
5041 p2p.op_channel = 0;
5042 /* This will be overridden during group setup in
5043 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005044 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045 p2p.cfg_op_channel = 0;
5046 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5047 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5048 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005049
5050 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5051 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5052 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5053 }
5054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5056 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5057 p2p.country[2] = 0x04;
5058 } else
5059 os_memcpy(p2p.country, "XX\x04", 3);
5060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5062 WPS_DEV_TYPE_LEN);
5063
5064 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5065 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5066 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5067
5068 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5069 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5070
5071 p2p.max_peers = 100;
5072
5073 if (wpa_s->conf->p2p_ssid_postfix) {
5074 p2p.ssid_postfix_len =
5075 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5076 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5077 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5078 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5079 p2p.ssid_postfix_len);
5080 }
5081
5082 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5083
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005084 p2p.max_listen = wpa_s->max_remain_on_chan;
5085
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005086 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5087 wpa_s->conf->p2p_passphrase_len <= 63)
5088 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5089 else
5090 p2p.passphrase_len = 8;
5091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 global->p2p = p2p_init(&p2p);
5093 if (global->p2p == NULL)
5094 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005095 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096
5097 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5098 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5099 continue;
5100 p2p_add_wps_vendor_extension(
5101 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5102 }
5103
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005104 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 return 0;
5107}
5108
5109
5110/**
5111 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5112 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5113 *
5114 * This function deinitialize per-interface P2P data.
5115 */
5116void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5117{
5118 if (wpa_s->driver && wpa_s->drv_priv)
5119 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005120
5121 if (wpa_s->go_params) {
5122 /* Clear any stored provisioning info */
5123 p2p_clear_provisioning_info(
5124 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005125 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005126 }
5127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005128 os_free(wpa_s->go_params);
5129 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005130 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5132 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005133 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5135 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5136 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005137 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005138 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005139 wpas_p2p_listen_work_done(wpa_s);
5140 if (wpa_s->p2p_send_action_work) {
5141 os_free(wpa_s->p2p_send_action_work->ctx);
5142 radio_work_done(wpa_s->p2p_send_action_work);
5143 wpa_s->p2p_send_action_work = NULL;
5144 }
5145 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005146
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005147 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5148 wpa_s->p2p_oob_dev_pw = NULL;
5149
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005150 os_free(wpa_s->p2p_group_common_freqs);
5151 wpa_s->p2p_group_common_freqs = NULL;
5152 wpa_s->p2p_group_common_freqs_num = 0;
5153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154 /* TODO: remove group interface from the driver if this wpa_s instance
5155 * is on top of a P2P group interface */
5156}
5157
5158
5159/**
5160 * wpas_p2p_deinit_global - Deinitialize global P2P module
5161 * @global: Pointer to global data from wpa_supplicant_init()
5162 *
5163 * This function deinitializes the global (per device) P2P module.
5164 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005165static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166{
5167 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005168
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005169 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005170
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005171 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172
5173 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5175 wpa_s = wpa_s->next;
5176 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177 tmp = global->ifaces;
5178 while (tmp &&
5179 (tmp == wpa_s ||
5180 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5181 tmp = tmp->next;
5182 }
5183 if (tmp == NULL)
5184 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005185 /* Disconnect from the P2P group and deinit the interface */
5186 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187 }
5188
5189 /*
5190 * Deinit GO data on any possibly remaining interface (if main
5191 * interface is used as GO).
5192 */
5193 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5194 if (wpa_s->ap_iface)
5195 wpas_p2p_group_deinit(wpa_s);
5196 }
5197
5198 p2p_deinit(global->p2p);
5199 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005200 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005201}
5202
5203
5204static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5205{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005206 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005207 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 if (wpa_s->drv_flags &
5209 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5210 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5211 return 1; /* P2P group requires a new interface in every case
5212 */
5213 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5214 return 0; /* driver does not support concurrent operations */
5215 if (wpa_s->global->ifaces->next)
5216 return 1; /* more that one interface already in use */
5217 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5218 return 1; /* this interface is already in use */
5219 return 0;
5220}
5221
5222
5223static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5224 const u8 *peer_addr,
5225 enum p2p_wps_method wps_method,
5226 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005227 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005228 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005229{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005230 if (persistent_group && wpa_s->conf->persistent_reconnect)
5231 persistent_group = 2;
5232
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005233 /*
5234 * Increase GO config timeout if HT40 is used since it takes some time
5235 * to scan channels for coex purposes before the BSS can be started.
5236 */
5237 p2p_set_config_timeout(wpa_s->global->p2p,
5238 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5241 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005242 persistent_group, ssid ? ssid->ssid : NULL,
5243 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005244 wpa_s->p2p_pd_before_go_neg, pref_freq,
5245 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5246 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005247}
5248
5249
5250static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5251 const u8 *peer_addr,
5252 enum p2p_wps_method wps_method,
5253 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005254 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005255 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005257 if (persistent_group && wpa_s->conf->persistent_reconnect)
5258 persistent_group = 2;
5259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5261 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005262 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005263 ssid ? ssid->ssid_len : 0, pref_freq,
5264 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5265 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266}
5267
5268
5269static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5270{
5271 wpa_s->p2p_join_scan_count++;
5272 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5273 wpa_s->p2p_join_scan_count);
5274 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5275 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5276 " for join operationg - stop join attempt",
5277 MAC2STR(wpa_s->pending_join_iface_addr));
5278 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005279 if (wpa_s->p2p_auto_pd) {
5280 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005281 wpa_msg_global(wpa_s, MSG_INFO,
5282 P2P_EVENT_PROV_DISC_FAILURE
5283 " p2p_dev_addr=" MACSTR " status=N/A",
5284 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285 return;
5286 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005287 if (wpa_s->p2p_fallback_to_go_neg) {
5288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5289 "failed - fall back to GO Negotiation");
5290 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5291 P2P_EVENT_FALLBACK_TO_GO_NEG
5292 "reason=join-failed");
5293 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5294 return;
5295 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005296 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005297 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005298 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005299 }
5300}
5301
5302
Dmitry Shmidt04949592012-07-19 12:16:46 -07005303static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5304{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005305 int res;
5306 unsigned int num, i;
5307 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005308
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005309 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5310 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005311 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005312 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005313
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005314 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5315 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005316 if (!freqs)
5317 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005318
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005319 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5320 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005321
5322 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005323 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005324 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5325 freq);
5326 res = 0;
5327 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005328 }
5329 }
5330
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005331 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005332 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005333
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005334exit_free:
5335 os_free(freqs);
5336 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005337}
5338
5339
5340static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5341 const u8 *peer_dev_addr)
5342{
5343 struct wpa_bss *bss;
5344 int updated;
5345
5346 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5347 if (bss == NULL)
5348 return -1;
5349 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5350 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5351 "last scan");
5352 return 0;
5353 }
5354
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005355 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5356 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005357 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5358 "%ld.%06ld (%supdated in last scan)",
5359 bss->last_update.sec, bss->last_update.usec,
5360 updated ? "": "not ");
5361
5362 return updated;
5363}
5364
5365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005366static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5367 struct wpa_scan_results *scan_res)
5368{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005369 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370 int freq;
5371 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5374
5375 if (wpa_s->global->p2p_disabled)
5376 return;
5377
Dmitry Shmidt04949592012-07-19 12:16:46 -07005378 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5379 scan_res ? (int) scan_res->num : -1,
5380 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381
5382 if (scan_res)
5383 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5384
Dmitry Shmidt04949592012-07-19 12:16:46 -07005385 if (wpa_s->p2p_auto_pd) {
5386 int join = wpas_p2p_peer_go(wpa_s,
5387 wpa_s->pending_join_dev_addr);
5388 if (join == 0 &&
5389 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5390 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005391 bss = wpa_bss_get_bssid_latest(
5392 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005393 if (bss) {
5394 freq = bss->freq;
5395 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5396 "the peer " MACSTR " at %d MHz",
5397 wpa_s->auto_pd_scan_retry,
5398 MAC2STR(wpa_s->
5399 pending_join_dev_addr),
5400 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005401 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005402 return;
5403 }
5404 }
5405
5406 if (join < 0)
5407 join = 0;
5408
5409 wpa_s->p2p_auto_pd = 0;
5410 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5411 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5412 MAC2STR(wpa_s->pending_join_dev_addr), join);
5413 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005414 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005415 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005416 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005417 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005418 wpa_msg_global(wpa_s, MSG_INFO,
5419 P2P_EVENT_PROV_DISC_FAILURE
5420 " p2p_dev_addr=" MACSTR " status=N/A",
5421 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005422 }
5423 return;
5424 }
5425
5426 if (wpa_s->p2p_auto_join) {
5427 int join = wpas_p2p_peer_go(wpa_s,
5428 wpa_s->pending_join_dev_addr);
5429 if (join < 0) {
5430 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5431 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005432 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005433 P2P_EVENT_FALLBACK_TO_GO_NEG
5434 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005435 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5436 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5437 wpa_s->p2p_persistent_group, 0, 0, 0,
5438 wpa_s->p2p_go_intent,
5439 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005440 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005441 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005442 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005443 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005444 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005445 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005446 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005447 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005448 NULL, 0,
5449 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005450 return;
5451 }
5452
5453 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5454 "try to join the group", join ? "" :
5455 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005456 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005457 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005458 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005459 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005460 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005461 }
5462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5464 wpa_s->pending_join_iface_addr);
5465 if (freq < 0 &&
5466 p2p_get_interface_addr(wpa_s->global->p2p,
5467 wpa_s->pending_join_dev_addr,
5468 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005469 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5470 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5472 "address for join from " MACSTR " to " MACSTR
5473 " based on newly discovered P2P peer entry",
5474 MAC2STR(wpa_s->pending_join_iface_addr),
5475 MAC2STR(iface_addr));
5476 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5477 ETH_ALEN);
5478
5479 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5480 wpa_s->pending_join_iface_addr);
5481 }
5482 if (freq >= 0) {
5483 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5484 "from P2P peer table: %d MHz", freq);
5485 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005486 if (wpa_s->p2p_join_ssid_len) {
5487 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5488 MACSTR " and SSID %s",
5489 MAC2STR(wpa_s->pending_join_iface_addr),
5490 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5491 wpa_s->p2p_join_ssid_len));
5492 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5493 wpa_s->p2p_join_ssid,
5494 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005495 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005496 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5497 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5498 bss = wpa_bss_get_bssid_latest(wpa_s,
5499 wpa_s->pending_join_iface_addr);
5500 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005502 u8 dev_addr[ETH_ALEN];
5503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504 freq = bss->freq;
5505 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005506 "from BSS table: %d MHz (SSID %s)", freq,
5507 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005508 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005509 dev_addr) == 0 &&
5510 os_memcmp(wpa_s->pending_join_dev_addr,
5511 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5512 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5513 ETH_ALEN) != 0) {
5514 wpa_printf(MSG_DEBUG,
5515 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5516 MAC2STR(dev_addr),
5517 MAC2STR(wpa_s->pending_join_dev_addr));
5518 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5519 ETH_ALEN);
5520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521 }
5522 if (freq > 0) {
5523 u16 method;
5524
Dmitry Shmidt04949592012-07-19 12:16:46 -07005525 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005526 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005527 P2P_EVENT_GROUP_FORMATION_FAILURE
5528 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005529 wpas_notify_p2p_group_formation_failure(
5530 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005531 return;
5532 }
5533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5535 "prior to joining an existing group (GO " MACSTR
5536 " freq=%u MHz)",
5537 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5538 wpa_s->pending_pd_before_join = 1;
5539
5540 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541 case WPS_PIN_DISPLAY:
5542 method = WPS_CONFIG_KEYPAD;
5543 break;
5544 case WPS_PIN_KEYPAD:
5545 method = WPS_CONFIG_DISPLAY;
5546 break;
5547 case WPS_PBC:
5548 method = WPS_CONFIG_PUSHBUTTON;
5549 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005550 case WPS_P2PS:
5551 method = WPS_CONFIG_P2PS;
5552 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005553 default:
5554 method = 0;
5555 break;
5556 }
5557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005558 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5559 wpa_s->pending_join_dev_addr) ==
5560 method)) {
5561 /*
5562 * We have already performed provision discovery for
5563 * joining the group. Proceed directly to join
5564 * operation without duplicated provision discovery. */
5565 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5566 "with " MACSTR " already done - proceed to "
5567 "join",
5568 MAC2STR(wpa_s->pending_join_dev_addr));
5569 wpa_s->pending_pd_before_join = 0;
5570 goto start;
5571 }
5572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005573 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005574 wpa_s->pending_join_dev_addr,
5575 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005576 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5578 "Discovery Request before joining an "
5579 "existing group");
5580 wpa_s->pending_pd_before_join = 0;
5581 goto start;
5582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583 return;
5584 }
5585
5586 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5587 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5588 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5589 wpas_p2p_check_join_scan_limit(wpa_s);
5590 return;
5591
5592start:
5593 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005594 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5595 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596}
5597
5598
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005599static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5600 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005602 int ret;
5603 struct wpa_driver_scan_params params;
5604 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005605 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005606 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005607 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005608
5609 os_memset(&params, 0, sizeof(params));
5610
5611 /* P2P Wildcard SSID */
5612 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005613 if (ssid && ssid_len) {
5614 params.ssids[0].ssid = ssid;
5615 params.ssids[0].ssid_len = ssid_len;
5616 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5617 wpa_s->p2p_join_ssid_len = ssid_len;
5618 } else {
5619 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5620 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5621 wpa_s->p2p_join_ssid_len = 0;
5622 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623
5624 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5626 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5627 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628 if (wps_ie == NULL) {
5629 wpas_p2p_scan_res_join(wpa_s, NULL);
5630 return;
5631 }
5632
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005633 if (!freq) {
5634 int oper_freq;
5635 /*
5636 * If freq is not provided, check the operating freq of the GO
5637 * and use a single channel scan on if possible.
5638 */
5639 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5640 wpa_s->pending_join_iface_addr);
5641 if (oper_freq > 0)
5642 freq = oper_freq;
5643 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005644 if (freq > 0) {
5645 freqs[0] = freq;
5646 params.freqs = freqs;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005647 } else {
5648 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005651 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5652 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5653 if (ies == NULL) {
5654 wpabuf_free(wps_ie);
5655 wpas_p2p_scan_res_join(wpa_s, NULL);
5656 return;
5657 }
5658 wpabuf_put_buf(ies, wps_ie);
5659 wpabuf_free(wps_ie);
5660
5661 bands = wpas_get_bands(wpa_s, freqs);
5662 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5663
5664 params.p2p_probe = 1;
5665 params.extra_ies = wpabuf_head(ies);
5666 params.extra_ies_len = wpabuf_len(ies);
5667
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005668 if (wpa_s->clear_driver_scan_cache) {
5669 wpa_printf(MSG_DEBUG,
5670 "Request driver to clear scan cache due to local BSS flush");
5671 params.only_new_results = 1;
5672 }
5673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674 /*
5675 * Run a scan to update BSS table and start Provision Discovery once
5676 * the new scan results become available.
5677 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005678 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005679 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005680 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005681 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005682 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005683 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005684 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005685 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005686 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005687
5688 wpabuf_free(ies);
5689
5690 if (ret) {
5691 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5692 "try again later");
5693 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5694 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5695 wpas_p2p_check_join_scan_limit(wpa_s);
5696 }
5697}
5698
5699
Dmitry Shmidt04949592012-07-19 12:16:46 -07005700static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5701{
5702 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005703 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005704}
5705
5706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005707static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005708 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005709 int auto_join, int op_freq,
5710 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711{
5712 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005713 MACSTR " dev " MACSTR " op_freq=%d)%s",
5714 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005715 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005716 if (ssid && ssid_len) {
5717 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5718 wpa_ssid_txt(ssid, ssid_len));
5719 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720
Dmitry Shmidt04949592012-07-19 12:16:46 -07005721 wpa_s->p2p_auto_pd = 0;
5722 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005723 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5724 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5725 wpa_s->pending_join_wps_method = wps_method;
5726
5727 /* Make sure we are not running find during connection establishment */
5728 wpas_p2p_stop_find(wpa_s);
5729
5730 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005731 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005732 return 0;
5733}
5734
5735
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005736static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5737 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005738{
5739 struct wpa_supplicant *group;
5740 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005741 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742
5743 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5744 if (group == NULL)
5745 return -1;
5746 if (group != wpa_s) {
5747 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5748 sizeof(group->p2p_pin));
5749 group->p2p_wps_method = wpa_s->p2p_wps_method;
5750 }
5751
Hai Shalom74f70d42019-02-11 14:42:39 -08005752 /*
5753 * Need to mark the current interface for p2p_group_formation
5754 * when a separate group interface is not used. This is needed
5755 * to allow p2p_cancel stop a pending p2p_connect-join.
5756 * wpas_p2p_init_group_interface() addresses this for the case
5757 * where a separate group interface is used.
5758 */
5759 if (group == wpa_s->parent)
5760 wpa_s->global->p2p_group_formation = group;
5761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005762 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005763 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764
5765 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005766 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005767 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5768 ETH_ALEN);
5769 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005770 if (freq && ssid && ssid_len) {
5771 res.freq = freq;
5772 res.ssid_len = ssid_len;
5773 os_memcpy(res.ssid, ssid, ssid_len);
5774 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005775 if (ssid && ssid_len) {
5776 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5777 ssid, ssid_len);
5778 } else {
5779 bss = wpa_bss_get_bssid_latest(
5780 wpa_s, wpa_s->pending_join_iface_addr);
5781 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005782 if (bss) {
5783 res.freq = bss->freq;
5784 res.ssid_len = bss->ssid_len;
5785 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5786 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5787 bss->freq,
5788 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005789 } else if (ssid && ssid_len) {
5790 res.ssid_len = ssid_len;
5791 os_memcpy(res.ssid, ssid, ssid_len);
5792 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5793 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005794 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005795 }
5796
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005797 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5798 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5799 "starting client");
5800 wpa_drv_cancel_remain_on_channel(wpa_s);
5801 wpa_s->off_channel_freq = 0;
5802 wpa_s->roc_waiting_drv_freq = 0;
5803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804 wpas_start_wps_enrollee(group, &res);
5805
5806 /*
5807 * Allow a longer timeout for join-a-running-group than normal 15
5808 * second group formation timeout since the GO may not have authorized
5809 * our connection yet.
5810 */
5811 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5812 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5813 wpa_s, NULL);
5814
5815 return 0;
5816}
5817
5818
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005819static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005820 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005821 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005822 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005823{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005824 struct wpa_used_freq_data *freqs;
5825 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005826 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305827 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005828
5829 max_pref_freq = *num_pref_freq;
5830 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005831
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005832 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5833 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005834 if (!freqs)
5835 return -1;
5836
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005837 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5838 wpa_s->num_multichan_concurrent);
5839
5840 /*
5841 * It is possible that the total number of used frequencies is bigger
5842 * than the number of frequencies used for P2P, so get the system wide
5843 * number of unused frequencies.
5844 */
5845 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5846
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005847 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005848 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5849 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005850
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005851 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005852 int ret;
5853 if (go)
5854 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5855 else
5856 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5857 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005858 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005859 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5860 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005861 /*
5862 * If freq is a DFS channel and DFS is offloaded
5863 * to the driver, allow P2P GO to use it.
5864 */
5865 wpa_printf(MSG_DEBUG,
5866 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5867 freq);
5868 } else {
5869 wpa_printf(MSG_DEBUG,
5870 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5871 freq);
5872 res = -3;
5873 goto exit_free;
5874 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005875 }
5876
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005877 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005878 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005879 freq_in_use = 1;
5880 }
5881
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005882 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005883 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5884 freq);
5885 res = -2;
5886 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005887 }
5888 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5889 "requested channel (%u MHz)", freq);
5890 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005891 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005892 }
5893
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005894 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005895
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305896 if (*pref_freq == 0) {
5897 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005898 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305899 while (i < wpa_s->conf->num_p2p_pref_chan) {
5900 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5901 wpa_s->conf->p2p_pref_chan[i].op_class,
5902 wpa_s->conf->p2p_pref_chan[i].chan);
5903
5904 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5905 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5906 best_freq = p2p_pref_freq;
5907 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5908 "from P2P preferred channel list", best_freq);
5909 break;
5910 } else {
5911 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5912 "frequency (%u MHz) ", p2p_pref_freq);
5913 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005914 i++;
5915 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305916 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305917 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
Sunil Ravi036cec52023-03-29 11:35:17 -07005918 best_freq, go);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305919
Sunil Ravi036cec52023-03-29 11:35:17 -07005920 *num_pref_freq = max_pref_freq;
5921 res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
5922 num_pref_freq);
5923 if (res > 0)
5924 best_freq = res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005925 }
5926 }
5927
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005928 /* We have a candidate frequency to use */
5929 if (best_freq > 0) {
5930 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005931 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005932 best_freq);
5933 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005934 } else {
5935 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 -07005936 best_freq);
5937 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005938 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005939 } else if (num_unused > 0) {
5940 wpa_printf(MSG_DEBUG,
5941 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5942 *force_freq = 0;
5943 } else {
5944 wpa_printf(MSG_DEBUG,
5945 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5946 res = -2;
5947 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005948 }
5949
5950exit_ok:
5951 res = 0;
5952exit_free:
5953 os_free(freqs);
5954 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005955}
5956
5957
Hai Shalomc1a21442022-02-04 13:43:00 -08005958static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5959 const u8 *peer_addr)
5960{
5961 if (wpa_s->conf->p2p_6ghz_disable ||
5962 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5963 HOSTAPD_MODE_IEEE80211A, true))
5964 return false;
5965
5966 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5967 return false;
5968 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5969 return false;
5970
5971 return true;
5972}
5973
5974
5975static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5976 const u8 *peer_addr, bool allow_6ghz, int freq)
5977{
5978 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5979 wpa_printf(MSG_DEBUG,
5980 "P2P: Allow connection on 6 GHz channels");
5981 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5982 } else {
5983 if (is_6ghz_freq(freq))
5984 return -2;
5985 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5986 }
5987
5988 return 0;
5989}
5990
5991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992/**
5993 * wpas_p2p_connect - Request P2P Group Formation to be started
5994 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5995 * @peer_addr: Address of the peer P2P Device
5996 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5997 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005998 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005999 * @join: Whether to join an existing group (as a client) instead of starting
6000 * Group Owner negotiation; @peer_addr is BSSID in that case
6001 * @auth: Whether to only authorize the connection instead of doing that and
6002 * initiating Group Owner negotiation
6003 * @go_intent: GO Intent or -1 to use default
6004 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006005 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07006006 * @persistent_id: Persistent group credentials to use for forcing GO
6007 * parameters or -1 to generate new values (SSID/passphrase)
6008 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6009 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006010 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006011 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006012 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07006013 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006014 * @group_ssid: Specific Group SSID for join or %NULL if not set
6015 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08006016 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006017 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6018 * failure, -2 on failure due to channel not currently available,
6019 * -3 if forced channel is not supported
6020 */
6021int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6022 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006023 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006024 int go_intent, int freq, unsigned int vht_center_freq2,
6025 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006026 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006027 const u8 *group_ssid, size_t group_ssid_len,
6028 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006029{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006030 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006031 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006033 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006034 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00006035 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6036 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037
6038 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6039 return -1;
6040
Dmitry Shmidt04949592012-07-19 12:16:46 -07006041 if (persistent_id >= 0) {
6042 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6043 if (ssid == NULL || ssid->disabled != 2 ||
6044 ssid->mode != WPAS_MODE_P2P_GO)
6045 return -1;
6046 }
6047
Hai Shalomc1a21442022-02-04 13:43:00 -08006048 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006049 return -2;
6050
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006051 os_free(wpa_s->global->add_psk);
6052 wpa_s->global->add_psk = NULL;
6053
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006054 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006055 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006056 wpa_s->global->pending_p2ps_group_freq = 0;
6057 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006059 if (go_intent < 0)
6060 go_intent = wpa_s->conf->p2p_go_intent;
6061
6062 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006063 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006064
6065 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006066 wpa_s->p2p_persistent_group = !!persistent_group;
6067 wpa_s->p2p_persistent_id = persistent_id;
6068 wpa_s->p2p_go_intent = go_intent;
6069 wpa_s->p2p_connect_freq = freq;
6070 wpa_s->p2p_fallback_to_go_neg = 0;
6071 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006072 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006073 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006074 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6075 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006076 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006077 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006078
6079 if (pin)
6080 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6081 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006082 if (wps_generate_pin((unsigned int *) &ret) < 0)
6083 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006084 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6085 "%08d", ret);
6086 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6087 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6089 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006090 } else if (wps_method == WPS_P2PS) {
6091 /* Force the P2Ps default PIN to be used */
6092 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093 } else
6094 wpa_s->p2p_pin[0] = '\0';
6095
Dmitry Shmidt04949592012-07-19 12:16:46 -07006096 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6098 if (auth) {
6099 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6100 "connect a running group from " MACSTR,
6101 MAC2STR(peer_addr));
6102 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6103 return ret;
6104 }
6105 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6106 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6107 iface_addr) < 0) {
6108 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6109 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6110 dev_addr);
6111 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006112 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006113 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006114 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6115 "%ld.%06ld",
6116 wpa_s->p2p_auto_started.sec,
6117 wpa_s->p2p_auto_started.usec);
6118 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006119 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006120 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006121 auto_join, freq,
6122 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006123 return -1;
6124 return ret;
6125 }
6126
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006127 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006128 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006129 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006130 if (res)
6131 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006132 wpas_p2p_set_own_freq_preference(wpa_s,
6133 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006134
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006135 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006137 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006139 if (wpa_s->create_p2p_iface) {
6140 /* Prepare to add a new interface for the group */
6141 iftype = WPA_IF_P2P_GROUP;
6142 if (go_intent == 15)
6143 iftype = WPA_IF_P2P_GO;
6144 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6145 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6146 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006147 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006149
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006150 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006151 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006152 if (wpa_s->p2p_mgmt)
6153 if_addr = wpa_s->parent->own_addr;
6154 else
6155 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006156 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6157 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158
6159 if (auth) {
6160 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006161 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006162 force_freq, persistent_group, ssid,
6163 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006164 return -1;
6165 return ret;
6166 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006167
6168 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6169 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006170 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006171 if (wpa_s->create_p2p_iface)
6172 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173 return -1;
6174 }
6175 return ret;
6176}
6177
6178
6179/**
6180 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6181 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6182 * @freq: Frequency of the channel in MHz
6183 * @duration: Duration of the stay on the channel in milliseconds
6184 *
6185 * This callback is called when the driver indicates that it has started the
6186 * requested remain-on-channel duration.
6187 */
6188void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6189 unsigned int freq, unsigned int duration)
6190{
6191 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6192 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006193 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)",
6194 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6195 wpa_s->roc_waiting_drv_freq, freq, duration);
6196 if (wpa_s->off_channel_freq &&
6197 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6199 wpa_s->pending_listen_duration);
6200 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006201 } else {
6202 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6203 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6204 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205 }
6206}
6207
6208
Jithu Jance57cea1a2014-08-20 10:22:04 -07006209int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006210{
6211 /* Limit maximum Listen state time based on driver limitation. */
6212 if (timeout > wpa_s->max_remain_on_chan)
6213 timeout = wpa_s->max_remain_on_chan;
6214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215 return p2p_listen(wpa_s->global->p2p, timeout);
6216}
6217
6218
6219/**
6220 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6221 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6222 * @freq: Frequency of the channel in MHz
6223 *
6224 * This callback is called when the driver indicates that a remain-on-channel
6225 * operation has been completed, i.e., the duration on the requested channel
6226 * has timed out.
6227 */
6228void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6229 unsigned int freq)
6230{
6231 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6232 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006233 wpa_s->global->p2p_long_listen,
6234 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006235 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6237 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006238 if (wpa_s->global->p2p_long_listen > 0)
6239 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006240 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6241 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006242 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006244 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006246 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006247 } else {
6248 /*
6249 * When listen duration is over, stop listen & update p2p_state
6250 * to IDLE.
6251 */
6252 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006253 }
6254}
6255
6256
6257/**
6258 * wpas_p2p_group_remove - Remove a P2P group
6259 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6260 * @ifname: Network interface name of the group interface or "*" to remove all
6261 * groups
6262 * Returns: 0 on success, -1 on failure
6263 *
6264 * This function is used to remove a P2P group. This can be used to disconnect
6265 * from a group in which the local end is a P2P Client or to end a P2P Group in
6266 * case the local end is the Group Owner. If a virtual network interface was
6267 * created for this group, that interface will be removed. Otherwise, only the
6268 * configured P2P group network will be removed from the interface.
6269 */
6270int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6271{
6272 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006273 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006274
6275 if (os_strcmp(ifname, "*") == 0) {
6276 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006277 bool calling_wpa_s_group_removed = false;
6278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279 wpa_s = global->ifaces;
6280 while (wpa_s) {
6281 prev = wpa_s;
6282 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006283 if (prev->p2p_group_interface !=
6284 NOT_P2P_GROUP_INTERFACE ||
6285 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006286 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006287 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006288 if (prev == calling_wpa_s)
6289 calling_wpa_s_group_removed = true;
6290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006292
6293 if (!calling_wpa_s_group_removed &&
6294 (calling_wpa_s->p2p_group_interface !=
6295 NOT_P2P_GROUP_INTERFACE ||
6296 (calling_wpa_s->current_ssid &&
6297 calling_wpa_s->current_ssid->p2p_group))) {
6298 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6299 wpas_p2p_disconnect_safely(calling_wpa_s,
6300 calling_wpa_s);
6301 }
6302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303 return 0;
6304 }
6305
6306 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6307 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6308 break;
6309 }
6310
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006311 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006312}
6313
6314
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006315static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6316{
6317 unsigned int r;
6318
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006319 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6320 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006321 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006322 int res;
6323
6324 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6325 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006326 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6327 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6328
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006329 if (!res && size > 0) {
6330 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006331 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006332 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006333 pref_freq_list[i].freq) ||
6334 wpas_p2p_disallowed_freq(
6335 wpa_s->global,
6336 pref_freq_list[i].freq) ||
6337 !p2p_pref_freq_allowed(&pref_freq_list[i],
6338 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006339 wpa_printf(MSG_DEBUG,
6340 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006341 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006342 i++;
6343 }
6344 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006345 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006346 wpa_printf(MSG_DEBUG,
6347 "P2P: Using preferred_freq_list[%d]=%d",
6348 i, freq);
6349 } else {
6350 wpa_printf(MSG_DEBUG,
6351 "P2P: All driver preferred frequencies are disallowed for P2P use");
6352 }
6353 } else {
6354 wpa_printf(MSG_DEBUG,
6355 "P2P: No preferred frequency list available");
6356 }
6357 }
6358
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006359 if (freq == 2) {
6360 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6361 "band");
6362 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006363 p2p_supported_freq_go(wpa_s->global->p2p,
6364 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006365 freq = wpa_s->best_24_freq;
6366 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6367 "channel: %d MHz", freq);
6368 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006369 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6370 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006371 int possible_2g_freqs[] = {
6372 /* operating class 81 */
6373 2412, 2437, 2462,
6374 };
6375 int possible_2g_freqs_num =
6376 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6377 int i;
6378 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6379 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6380 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6381 break;
6382 }
6383 }
6384
6385 if (i >= possible_2g_freqs_num) {
6386 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6387 "2.4 GHz channel for P2P group");
6388 return -1;
6389 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006390 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6391 "channel: %d MHz", freq);
6392 }
6393 }
6394
6395 if (freq == 5) {
6396 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6397 "band");
6398 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006399 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006400 wpa_s->best_5_freq)) {
6401 freq = wpa_s->best_5_freq;
6402 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6403 "channel: %d MHz", freq);
6404 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006405 const int freqs[] = {
6406 /* operating class 115 */
6407 5180, 5200, 5220, 5240,
6408 /* operating class 124 */
6409 5745, 5765, 5785, 5805,
6410 };
6411 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6412
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006413 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6414 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006415
6416 /*
6417 * most of 5G channels are DFS, only operating class 115 and 124
6418 * are available possibly, randomly pick a start to check them.
6419 */
6420 int possible_5g_freqs[] = {
6421 /* operating class 115 */
6422 5180, 5200, 5220, 5240,
6423 /* operating class 124 */
6424 5745, 5765, 5785, 5805,
6425 };
6426 int possible_5g_freqs_num =
6427 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6428
Jimmy Chen64b90632020-09-29 17:27:34 +08006429 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6430 if (p2p_supported_freq_go(
6431 wpa_s->global->p2p,
6432 possible_5g_freqs[r % possible_5g_freqs_num])) {
6433 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6434 break;
6435 }
6436 }
6437
6438 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006439 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6440 "5 GHz channel for P2P group");
6441 return -1;
6442 }
6443 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6444 "channel: %d MHz", freq);
6445 }
6446 }
6447
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006448 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006449 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006450 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6451 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006452 /*
6453 * If freq is a DFS channel and DFS is offloaded to the
6454 * driver, allow P2P GO to use it.
6455 */
6456 wpa_printf(MSG_DEBUG, "P2P: "
6457 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6458 __func__, freq);
6459 return freq;
6460 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006461 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6462 "(%u MHz) is not supported for P2P uses",
6463 freq);
6464 return -1;
6465 }
6466
6467 return freq;
6468}
6469
6470
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006471static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6472 const struct p2p_channels *channels,
6473 int freq)
6474{
6475 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6476 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6477 freq_included(wpa_s, channels, freq))
6478 return 1;
6479 return 0;
6480}
6481
6482
6483static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6484 struct p2p_go_neg_results *params,
6485 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006486{
6487 unsigned int i, r;
6488
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006489 /* try all channels in operating class 115 */
6490 for (i = 0; i < 4; i++) {
6491 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006492 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006493 goto out;
6494 }
6495
6496 /* try all channels in operating class 124 */
6497 for (i = 0; i < 4; i++) {
6498 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006499 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006500 goto out;
6501 }
6502
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006503 /* try social channel class 180 channel 2 */
6504 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006505 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006506 goto out;
6507
6508 /* try all channels in reg. class 180 */
6509 for (i = 0; i < 4; i++) {
6510 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006511 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006512 goto out;
6513 }
6514
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006515 /* try some random selection of the social channels */
6516 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6517 return;
6518
6519 for (i = 0; i < 3; i++) {
6520 params->freq = 2412 + ((r + i) % 3) * 25;
6521 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6522 goto out;
6523 }
6524
6525 /* try all other channels in operating class 81 */
6526 for (i = 0; i < 11; i++) {
6527 params->freq = 2412 + i * 5;
6528
6529 /* skip social channels; covered in the previous loop */
6530 if (params->freq == 2412 ||
6531 params->freq == 2437 ||
6532 params->freq == 2462)
6533 continue;
6534
6535 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6536 goto out;
6537 }
6538
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006539 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006540 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006541 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006542out:
6543 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6544 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006545}
6546
6547
Roshan Pius3a1667e2018-07-03 15:17:14 -07006548static int wpas_same_band(int freq1, int freq2)
6549{
6550 enum hostapd_hw_mode mode1, mode2;
6551 u8 chan1, chan2;
6552
6553 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6554 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6555 if (mode1 == NUM_HOSTAPD_MODES)
6556 return 0;
6557 return mode1 == mode2;
6558}
6559
6560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006561static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6562 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006563 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006564 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006565 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006566 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006567{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006568 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006569 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006570 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006571 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006572 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573
6574 os_memset(params, 0, sizeof(*params));
6575 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006576 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006577 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006578 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006579 params->max_oper_chwidth = max_oper_chwidth;
6580 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006581 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006582
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006583 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6584 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006585 if (!freqs)
6586 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006588 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006589 wpa_s->num_multichan_concurrent,
6590 false);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006591
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006592 if (wpa_s->current_ssid &&
6593 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6594 wpa_s->wpa_state == WPA_COMPLETED) {
6595 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6596 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006597
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006598 /*
6599 * If the frequency selection is done for an active P2P GO that
6600 * is not sharing a frequency, allow to select a new frequency
6601 * even if there are no unused frequencies as we are about to
6602 * move the P2P GO so its frequency can be re-used.
6603 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006604 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006605 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6606 freqs[i].flags == 0) {
6607 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006608 break;
6609 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006610 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006611 }
6612
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006613 /* Try to use EDMG channel */
6614 if (params->edmg) {
6615 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6616 goto success;
6617 params->edmg = 0;
6618 }
6619
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006620 /* try using the forced freq */
6621 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006622 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6623 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006624 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006625 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006626 freq);
6627 goto fail;
6628 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006629 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6630 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006631 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6632 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006633 /*
6634 * If freq is a DFS channel and DFS is offloaded
6635 * to the driver, allow P2P GO to use it.
6636 */
6637 wpa_printf(MSG_DEBUG,
6638 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6639 __func__, freq);
6640 } else {
6641 wpa_printf(MSG_DEBUG,
6642 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6643 freq);
6644 goto fail;
6645 }
6646 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006647
6648 for (i = 0; i < num; i++) {
6649 if (freqs[i].freq == freq) {
6650 wpa_printf(MSG_DEBUG,
6651 "P2P: forced freq (%d MHz) is also shared",
6652 freq);
6653 params->freq = freq;
6654 goto success;
6655 }
6656 }
6657
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006658 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006659 wpa_printf(MSG_DEBUG,
6660 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6661 freq);
6662 goto fail;
6663 }
6664
6665 wpa_printf(MSG_DEBUG,
6666 "P2P: force GO freq (%d MHz) on a free channel",
6667 freq);
6668 params->freq = freq;
6669 goto success;
6670 }
6671
6672 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006673 if (num &&
6674 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006675 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6676 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6677 wpa_printf(MSG_DEBUG,
6678 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006679 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006680 params->freq = cand;
6681 goto success;
6682 }
6683
6684 /* try using one of the shared freqs */
6685 for (i = 0; i < num; i++) {
6686 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6687 freqs[i].freq)) {
6688 wpa_printf(MSG_DEBUG,
6689 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006690 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006691 params->freq = freqs[i].freq;
6692 goto success;
6693 }
6694 }
6695 }
6696
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006697 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006698 wpa_printf(MSG_DEBUG,
6699 "P2P: Cannot force GO on any of the channels we are already using");
6700 goto fail;
6701 }
6702
6703 /* try using the setting from the configuration file */
6704 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6705 wpa_s->conf->p2p_oper_channel >= 1 &&
6706 wpa_s->conf->p2p_oper_channel <= 11 &&
6707 wpas_p2p_supported_freq_go(
6708 wpa_s, channels,
6709 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6710 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6711 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6712 "frequency %d MHz", params->freq);
6713 goto success;
6714 }
6715
6716 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6717 wpa_s->conf->p2p_oper_reg_class == 116 ||
6718 wpa_s->conf->p2p_oper_reg_class == 117 ||
6719 wpa_s->conf->p2p_oper_reg_class == 124 ||
6720 wpa_s->conf->p2p_oper_reg_class == 125 ||
6721 wpa_s->conf->p2p_oper_reg_class == 126 ||
6722 wpa_s->conf->p2p_oper_reg_class == 127) &&
6723 wpas_p2p_supported_freq_go(wpa_s, channels,
6724 5000 +
6725 5 * wpa_s->conf->p2p_oper_channel)) {
6726 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6727 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6728 "frequency %d MHz", params->freq);
6729 goto success;
6730 }
6731
6732 /* Try using best channels */
6733 if (wpa_s->conf->p2p_oper_channel == 0 &&
6734 wpa_s->best_overall_freq > 0 &&
6735 wpas_p2p_supported_freq_go(wpa_s, channels,
6736 wpa_s->best_overall_freq)) {
6737 params->freq = wpa_s->best_overall_freq;
6738 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6739 "channel %d MHz", params->freq);
6740 goto success;
6741 }
6742
6743 if (wpa_s->conf->p2p_oper_channel == 0 &&
6744 wpa_s->best_24_freq > 0 &&
6745 wpas_p2p_supported_freq_go(wpa_s, channels,
6746 wpa_s->best_24_freq)) {
6747 params->freq = wpa_s->best_24_freq;
6748 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6749 "channel %d MHz", params->freq);
6750 goto success;
6751 }
6752
6753 if (wpa_s->conf->p2p_oper_channel == 0 &&
6754 wpa_s->best_5_freq > 0 &&
6755 wpas_p2p_supported_freq_go(wpa_s, channels,
6756 wpa_s->best_5_freq)) {
6757 params->freq = wpa_s->best_5_freq;
6758 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6759 "channel %d MHz", params->freq);
6760 goto success;
6761 }
6762
6763 /* try using preferred channels */
6764 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6765 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6766 params->freq = cand;
6767 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6768 "channels", params->freq);
6769 goto success;
6770 }
6771
Roshan Pius3a1667e2018-07-03 15:17:14 -07006772 /* Try using a channel that allows VHT to be used with 80 MHz */
6773 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6774 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6775 enum hostapd_hw_mode mode;
6776 struct hostapd_hw_modes *hwmode;
6777 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006778 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006779
6780 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006781 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006782 mode = ieee80211_freq_to_chan(cand, &chan);
6783 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006784 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006785 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006786 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6787 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006788 continue;
6789 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6790 params->freq = cand;
6791 wpa_printf(MSG_DEBUG,
6792 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6793 params->freq);
6794 goto success;
6795 }
6796 }
6797 }
6798
6799 /* Try using a channel that allows HT to be used with 40 MHz on the same
6800 * band so that CSA can be used */
6801 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6802 wpa_s->p2p_group_common_freqs) {
6803 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6804 enum hostapd_hw_mode mode;
6805 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006806 u8 chan, op_class;
6807 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006808
Hai Shalomc1a21442022-02-04 13:43:00 -08006809 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006810 cand = wpa_s->p2p_group_common_freqs[i];
6811 mode = ieee80211_freq_to_chan(cand, &chan);
6812 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006813 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006814 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6815 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006816 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006817 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006818 if (is_6ghz &&
6819 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6820 BW40) == ALLOWED)
6821 supported = true;
6822
6823 if (!is_6ghz &&
6824 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006825 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6826 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006827 wpas_p2p_verify_channel(
6828 wpa_s, hwmode, op_class, chan,
6829 BW40MINUS) == ALLOWED)
6830 supported = true;
6831
6832 if (!supported && !is_6ghz &&
6833 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006834 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6835 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006836 wpas_p2p_verify_channel(
6837 wpa_s, hwmode, op_class, chan,
6838 BW40PLUS) == ALLOWED)
6839 supported = true;
6840
6841 if (!supported)
6842 continue;
6843
Roshan Pius3a1667e2018-07-03 15:17:14 -07006844 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6845 params->freq = cand;
6846 wpa_printf(MSG_DEBUG,
6847 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6848 params->freq);
6849 goto success;
6850 }
6851 }
6852 }
6853
6854 /* Try using one of the group common freqs on the same band so that CSA
6855 * can be used */
6856 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6857 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6858 cand = wpa_s->p2p_group_common_freqs[i];
6859 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6860 cand))
6861 continue;
6862 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6863 params->freq = cand;
6864 wpa_printf(MSG_DEBUG,
6865 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6866 params->freq);
6867 goto success;
6868 }
6869 }
6870 }
6871
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006872 /* Try using one of the group common freqs */
6873 if (wpa_s->p2p_group_common_freqs) {
6874 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6875 cand = wpa_s->p2p_group_common_freqs[i];
6876 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6877 params->freq = cand;
6878 wpa_printf(MSG_DEBUG,
6879 "P2P: Use freq %d MHz common with the peer",
6880 params->freq);
6881 goto success;
6882 }
6883 }
6884 }
6885
6886 /* no preference, select some channel */
6887 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6888
6889 if (params->freq == 0) {
6890 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6891 goto fail;
6892 }
6893
6894success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006895 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006896 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006897fail:
6898 os_free(freqs);
6899 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900}
6901
6902
6903static struct wpa_supplicant *
6904wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6905 int go)
6906{
6907 struct wpa_supplicant *group_wpa_s;
6908
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006909 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006910 if (wpa_s->p2p_mgmt) {
6911 /*
6912 * We may be called on the p2p_dev interface which
6913 * cannot be used for group operations, so always use
6914 * the primary interface.
6915 */
6916 wpa_s->parent->p2pdev = wpa_s;
6917 wpa_s = wpa_s->parent;
6918 }
6919 wpa_dbg(wpa_s, MSG_DEBUG,
6920 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006921 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006922 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006923 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006925 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926
6927 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006928 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006929 wpa_msg_global(wpa_s, MSG_ERROR,
6930 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006932 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6934 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006935 wpa_msg_global(wpa_s, MSG_ERROR,
6936 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937 wpas_p2p_remove_pending_group_interface(wpa_s);
6938 return NULL;
6939 }
6940
Roshan Pius3a1667e2018-07-03 15:17:14 -07006941 if (go && wpa_s->p2p_go_do_acs) {
6942 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6943 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6944 wpa_s->p2p_go_do_acs = 0;
6945 }
6946
Hai Shalomc1a21442022-02-04 13:43:00 -08006947 if (go && wpa_s->p2p_go_allow_dfs) {
6948 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6949 wpa_s->p2p_go_allow_dfs = 0;
6950 }
6951
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006952 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6953 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006954 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955 return group_wpa_s;
6956}
6957
6958
6959/**
6960 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6961 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6962 * @persistent_group: Whether to create a persistent group
6963 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006964 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006965 * @ht40: Start GO with 40 MHz channel width
6966 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006967 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006968 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006969 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970 * Returns: 0 on success, -1 on failure
6971 *
6972 * This function creates a new P2P group with the local end as the Group Owner,
6973 * i.e., without using Group Owner Negotiation.
6974 */
6975int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006976 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006977 int max_oper_chwidth, int he, int edmg,
6978 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006979{
6980 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006981 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006982
6983 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6984 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006985 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6986 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006987
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006988 os_free(wpa_s->global->add_psk);
6989 wpa_s->global->add_psk = NULL;
6990
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006991 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006992 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006993 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006994
Roshan Pius3a1667e2018-07-03 15:17:14 -07006995 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00006996 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
6997 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006998 return -1;
6999 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000
Sunil Ravi77a0f092022-10-03 00:53:41 +00007001 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007002 ht40, vht, max_oper_chwidth, he, edmg,
7003 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007004 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007006 p2p_go_params(wpa_s->global->p2p, &params);
7007 params.persistent_group = persistent_group;
7008
7009 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7010 if (wpa_s == NULL)
7011 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007012 if (freq > 0)
7013 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007014 wpas_start_wps_go(wpa_s, &params, 0);
7015
7016 return 0;
7017}
7018
7019
7020static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007021 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00007022 int freq, int force_scan, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007023 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007024{
7025 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07007026 int other_iface_found = 0;
7027 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007028
7029 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
7030 if (wpa_s == NULL)
7031 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007032 if (force_scan)
7033 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007034 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007035
7036 wpa_supplicant_ap_deinit(wpa_s);
7037
7038 ssid = wpa_config_add_network(wpa_s->conf);
7039 if (ssid == NULL)
7040 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007041 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042 wpa_config_set_network_defaults(ssid);
7043 ssid->temporary = 1;
7044 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007045 ssid->pbss = params->pbss;
7046 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7047 WPA_CIPHER_CCMP;
7048 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007049 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Sunil Ravi036cec52023-03-29 11:35:17 -07007050 if (is_6ghz_freq(freq) &&
7051 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
7052 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
7053 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
7054 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
7055 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7056 wpa_dbg(wpa_s, MSG_DEBUG,
7057 "P2P: Enable SAE auth_alg and key_mgmt");
7058 }
7059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007060 ssid->ssid = os_malloc(params->ssid_len);
7061 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007062 wpa_config_remove_network(wpa_s->conf, ssid->id);
7063 return -1;
7064 }
7065 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7066 ssid->ssid_len = params->ssid_len;
7067 ssid->p2p_group = 1;
7068 ssid->export_keys = 1;
7069 if (params->psk_set) {
7070 os_memcpy(ssid->psk, params->psk, 32);
7071 ssid->psk_set = 1;
7072 }
7073 if (params->passphrase)
7074 ssid->passphrase = os_strdup(params->passphrase);
7075
Sunil Ravi036cec52023-03-29 11:35:17 -07007076 if (go_bssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007077 ssid->bssid_set = 1;
Sunil Ravi036cec52023-03-29 11:35:17 -07007078 os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
Matthew Wangdcf19452022-11-07 20:42:52 -08007079 }
7080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007081 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007082 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007083 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007084 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007085 wpa_s->p2p_go_group_formation_completed = 0;
7086 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007087
Matthew Wang9ae940b2022-09-14 21:25:34 -07007088 /*
7089 * Get latest scan results from driver in case cached scan results from
7090 * interfaces on the same wiphy allow us to skip the next scan by fast
7091 * associating. Also update the scan time to the most recent scan result
7092 * fetch time on the same radio so it reflects the actual time the last
7093 * scan result event occurred.
7094 */
7095 wpa_supplicant_update_scan_results(wpa_s);
7096 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7097 radio_list) {
7098 if (ifs == wpa_s)
7099 continue;
7100 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7101 &ifs->last_scan)) {
7102 other_iface_found = 1;
7103 wpa_s->last_scan.sec = ifs->last_scan.sec;
7104 wpa_s->last_scan.usec = ifs->last_scan.usec;
7105 }
7106 }
7107
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007108 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007109 NULL);
7110 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7111 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007112 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007113 wpa_supplicant_select_network(wpa_s, ssid);
7114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115 return 0;
7116}
7117
7118
7119int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7120 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007121 int force_freq, int neg_freq,
7122 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007123 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007124 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007125 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007126 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00007127 bool allow_6ghz, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007128 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007129{
7130 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007131 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007132
7133 if (ssid->disabled != 2 || ssid->ssid == NULL)
7134 return -1;
7135
7136 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7137 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7138 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7139 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007140 if (go == 0 &&
7141 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007142 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007143 /*
7144 * This can happen if Invitation Response frame was lost
7145 * and the peer (GO of a persistent group) tries to
7146 * invite us again. Reschedule the timeout to avoid
7147 * terminating the wait for the connection too early
7148 * since we now know that the peer is still trying to
7149 * invite us instead of having already started the GO.
7150 */
7151 wpa_printf(MSG_DEBUG,
7152 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7153 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7154 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007155 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007157 return 0;
7158 }
7159
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007160 os_free(wpa_s->global->add_psk);
7161 wpa_s->global->add_psk = NULL;
7162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007163 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007164 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165
Dmitry Shmidt04949592012-07-19 12:16:46 -07007166 wpa_s->p2p_fallback_to_go_neg = 0;
7167
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007168 if (ssid->mode == WPAS_MODE_P2P_GO) {
7169 if (force_freq > 0) {
7170 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7171 if (freq < 0)
7172 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007173 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007174 } else {
7175 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7176 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007177 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007178 freq = 0;
7179 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007180 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007181 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007182 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007183 struct os_reltime now;
7184 struct wpa_bss *bss =
7185 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007186
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007187 os_get_reltime(&now);
7188 if (bss &&
7189 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007190 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007191 freq = bss->freq;
7192 else
7193 freq = 0;
7194 }
7195
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007196 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Sunil Ravi036cec52023-03-29 11:35:17 -07007197 force_scan, retry_limit, go_bssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007198 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007199 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007200 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007201
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007202 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007203 ht40, vht, max_oper_chwidth, he, edmg,
7204 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007205 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007206
7207 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007208 params.psk_set = ssid->psk_set;
7209 if (params.psk_set)
7210 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007211 if (ssid->passphrase) {
7212 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7213 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7214 "persistent group");
7215 return -1;
7216 }
7217 os_strlcpy(params.passphrase, ssid->passphrase,
7218 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7221 params.ssid_len = ssid->ssid_len;
7222 params.persistent_group = 1;
7223
7224 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7225 if (wpa_s == NULL)
7226 return -1;
7227
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007228 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7229
Dmitry Shmidt56052862013-10-04 10:23:25 -07007230 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007231 wpas_start_wps_go(wpa_s, &params, 0);
7232
7233 return 0;
7234}
7235
7236
7237static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7238 struct wpabuf *proberesp_ies)
7239{
7240 struct wpa_supplicant *wpa_s = ctx;
7241 if (wpa_s->ap_iface) {
7242 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007243 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7244 wpabuf_free(beacon_ies);
7245 wpabuf_free(proberesp_ies);
7246 return;
7247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007248 if (beacon_ies) {
7249 wpabuf_free(hapd->p2p_beacon_ie);
7250 hapd->p2p_beacon_ie = beacon_ies;
7251 }
7252 wpabuf_free(hapd->p2p_probe_resp_ie);
7253 hapd->p2p_probe_resp_ie = proberesp_ies;
7254 } else {
7255 wpabuf_free(beacon_ies);
7256 wpabuf_free(proberesp_ies);
7257 }
7258 wpa_supplicant_ap_update_beacon(wpa_s);
7259}
7260
7261
7262static void wpas_p2p_idle_update(void *ctx, int idle)
7263{
7264 struct wpa_supplicant *wpa_s = ctx;
7265 if (!wpa_s->ap_iface)
7266 return;
7267 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007268 if (idle) {
7269 if (wpa_s->global->p2p_fail_on_wps_complete &&
7270 wpa_s->p2p_in_provisioning) {
7271 wpas_p2p_grpform_fail_after_wps(wpa_s);
7272 return;
7273 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007274 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007275 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007276 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7277}
7278
7279
7280struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007281 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007282{
7283 struct p2p_group *group;
7284 struct p2p_group_config *cfg;
7285
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007286 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7287 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007288 return NULL;
7289
7290 cfg = os_zalloc(sizeof(*cfg));
7291 if (cfg == NULL)
7292 return NULL;
7293
Dmitry Shmidt04949592012-07-19 12:16:46 -07007294 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007295 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007296 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007297 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007298 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7299 if (wpa_s->max_stations &&
7300 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7301 cfg->max_clients = wpa_s->max_stations;
7302 else
7303 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007304 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7305 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007306 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007307 cfg->cb_ctx = wpa_s;
7308 cfg->ie_update = wpas_p2p_ie_update;
7309 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007310 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7311 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312
7313 group = p2p_group_init(wpa_s->global->p2p, cfg);
7314 if (group == NULL)
7315 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007316 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007317 p2p_group_notif_formation_done(group);
7318 wpa_s->p2p_group = group;
7319 return group;
7320}
7321
7322
7323void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7324 int registrar)
7325{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007326 struct wpa_ssid *ssid = wpa_s->current_ssid;
7327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007328 if (!wpa_s->p2p_in_provisioning) {
7329 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7330 "provisioning not in progress");
7331 return;
7332 }
7333
Dmitry Shmidt04949592012-07-19 12:16:46 -07007334 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7335 u8 go_dev_addr[ETH_ALEN];
7336 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7337 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7338 ssid->ssid_len);
7339 /* Clear any stored provisioning info */
7340 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7341 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007342
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007343 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007344 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007345 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007346 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7347 /*
7348 * Use a separate timeout for initial data connection to
7349 * complete to allow the group to be removed automatically if
7350 * something goes wrong in this step before the P2P group idle
7351 * timeout mechanism is taken into use.
7352 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007353 wpa_dbg(wpa_s, MSG_DEBUG,
7354 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7355 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007356 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7357 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007358 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007359 /* Complete group formation on successful data connection. */
7360 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007361 } else if (ssid) {
7362 /*
7363 * Use a separate timeout for initial data connection to
7364 * complete to allow the group to be removed automatically if
7365 * the client does not complete data connection successfully.
7366 */
7367 wpa_dbg(wpa_s, MSG_DEBUG,
7368 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7369 P2P_MAX_INITIAL_CONN_WAIT_GO);
7370 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7371 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007372 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007373 /*
7374 * Complete group formation on first successful data connection
7375 */
7376 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007378 if (wpa_s->global->p2p)
7379 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007380 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381}
7382
7383
Jouni Malinen75ecf522011-06-27 15:19:46 -07007384void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7385 struct wps_event_fail *fail)
7386{
7387 if (!wpa_s->p2p_in_provisioning) {
7388 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7389 "provisioning not in progress");
7390 return;
7391 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007392
7393 if (wpa_s->go_params) {
7394 p2p_clear_provisioning_info(
7395 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007396 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007397 }
7398
Jouni Malinen75ecf522011-06-27 15:19:46 -07007399 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007400
7401 if (wpa_s == wpa_s->global->p2p_group_formation) {
7402 /*
7403 * Allow some time for the failed WPS negotiation exchange to
7404 * complete, but remove the group since group formation cannot
7405 * succeed after provisioning failure.
7406 */
7407 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7408 wpa_s->global->p2p_fail_on_wps_complete = 1;
7409 eloop_deplete_timeout(0, 50000,
7410 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007411 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007412 }
7413}
7414
7415
7416int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7417{
7418 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7419 !wpa_s->p2p_in_provisioning)
7420 return 0;
7421
7422 wpas_p2p_grpform_fail_after_wps(wpa_s);
7423
7424 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007425}
7426
7427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007429 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007430 enum wpas_p2p_prov_disc_use use,
7431 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007432{
7433 u16 config_methods;
7434
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007435 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007436 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007437 wpa_s->p2p_fallback_to_go_neg = 0;
7438 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007439 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7440 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007441 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7442 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7443
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007444 wpa_printf(MSG_DEBUG,
7445 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7446 __func__, p2ps_prov->conncap,
7447 p2ps_prov->adv_id, p2ps_prov->conncap,
7448 p2ps_prov->status, p2ps_prov->info);
7449
7450 config_methods = 0;
7451 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007452 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007453 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007454 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007455 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7456 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007457 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007458 else {
7459 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007460 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007461 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007463
Dmitry Shmidt04949592012-07-19 12:16:46 -07007464 if (use == WPAS_P2P_PD_AUTO) {
7465 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7466 wpa_s->pending_pd_config_methods = config_methods;
7467 wpa_s->p2p_auto_pd = 1;
7468 wpa_s->p2p_auto_join = 0;
7469 wpa_s->pending_pd_before_join = 0;
7470 wpa_s->auto_pd_scan_retry = 0;
7471 wpas_p2p_stop_find(wpa_s);
7472 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007473 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007474 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7475 wpa_s->p2p_auto_started.sec,
7476 wpa_s->p2p_auto_started.usec);
7477 wpas_p2p_join_scan(wpa_s, NULL);
7478 return 0;
7479 }
7480
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007481 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7482 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007483 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007484 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007485
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007486 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007487 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007488 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007489}
7490
7491
7492int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7493 char *end)
7494{
7495 return p2p_scan_result_text(ies, ies_len, buf, end);
7496}
7497
7498
7499static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7500{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007501 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007502 return;
7503
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007504 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007505 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007506 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7507 wpa_s, NULL);
7508 offchannel_send_action_done(wpa_s);
7509 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007511 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7512 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007513 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007514}
7515
7516
7517int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7518 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007519 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007520 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007521 u8 seek_cnt, const char **seek_string, int freq,
7522 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007523{
7524 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007525 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526
Dmitry Shmidt04949592012-07-19 12:16:46 -07007527 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007528 wpa_s->p2p_in_provisioning) {
7529 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7530 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7531 " (P2P disabled)" : "",
7532 wpa_s->p2p_in_provisioning ?
7533 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007535 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007537 wpa_supplicant_cancel_sched_scan(wpa_s);
7538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007539 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007540 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007541 search_delay, seek_cnt, seek_string, freq,
7542 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543}
7544
7545
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007546static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7547 struct wpa_scan_results *scan_res)
7548{
7549 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7550
7551 if (wpa_s->p2p_scan_work) {
7552 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7553 wpa_s->p2p_scan_work = NULL;
7554 radio_work_done(work);
7555 }
7556
7557 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7558 return;
7559
7560 /*
7561 * Indicate that results have been processed so that the P2P module can
7562 * continue pending tasks.
7563 */
Hai Shalom60840252021-02-19 19:02:11 -08007564 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007565}
7566
7567
7568static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007569{
7570 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007571 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007572 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7573 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007574
7575 if (wpa_s->global->p2p)
7576 p2p_stop_find(wpa_s->global->p2p);
7577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007578 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7579 wpa_printf(MSG_DEBUG,
7580 "P2P: Do not consider the scan results after stop_find");
7581 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7582 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007583}
7584
7585
7586void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7587{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007588 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007589 if (!wpa_s->global->pending_group_iface_for_p2ps)
7590 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007591}
7592
7593
7594static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7595{
7596 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007597 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007598}
7599
7600
7601int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7602{
7603 int res;
7604
7605 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7606 return -1;
7607
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007608 if (wpa_s->p2p_lo_started) {
7609 wpa_printf(MSG_DEBUG,
7610 "P2P: Cannot start P2P listen, it is offloaded");
7611 return -1;
7612 }
7613
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007614 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007615 wpas_p2p_clear_pending_action_tx(wpa_s);
7616
7617 if (timeout == 0) {
7618 /*
7619 * This is a request for unlimited Listen state. However, at
7620 * least for now, this is mapped to a Listen state for one
7621 * hour.
7622 */
7623 timeout = 3600;
7624 }
7625 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007626 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007628 /*
7629 * Stop previous find/listen operation to avoid trying to request a new
7630 * remain-on-channel operation while the driver is still running the
7631 * previous one.
7632 */
7633 if (wpa_s->global->p2p)
7634 p2p_stop_find(wpa_s->global->p2p);
7635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007636 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7637 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007638 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007639 eloop_register_timeout(timeout, 0,
7640 wpas_p2p_long_listen_timeout,
7641 wpa_s, NULL);
7642 }
7643
7644 return res;
7645}
7646
7647
7648int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7649 u8 *buf, size_t len, int p2p_group)
7650{
7651 struct wpabuf *p2p_ie;
7652 int ret;
7653
7654 if (wpa_s->global->p2p_disabled)
7655 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007656 /*
7657 * Advertize mandatory cross connection capability even on
7658 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7659 */
7660 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007661 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007662 if (wpa_s->global->p2p == NULL)
7663 return -1;
7664 if (bss == NULL)
7665 return -1;
7666
7667 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7668 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7669 p2p_group, p2p_ie);
7670 wpabuf_free(p2p_ie);
7671
7672 return ret;
7673}
7674
7675
7676int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007677 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007678 const u8 *ie, size_t ie_len,
7679 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007680{
7681 if (wpa_s->global->p2p_disabled)
7682 return 0;
7683 if (wpa_s->global->p2p == NULL)
7684 return 0;
7685
Dmitry Shmidt04949592012-07-19 12:16:46 -07007686 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007687 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007688 case P2P_PREQ_NOT_P2P:
7689 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7690 ssi_signal);
7691 /* fall through */
7692 case P2P_PREQ_MALFORMED:
7693 case P2P_PREQ_NOT_LISTEN:
7694 case P2P_PREQ_NOT_PROCESSED:
7695 default: /* make gcc happy */
7696 return 0;
7697 case P2P_PREQ_PROCESSED:
7698 return 1;
7699 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007700}
7701
7702
7703void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7704 const u8 *sa, const u8 *bssid,
7705 u8 category, const u8 *data, size_t len, int freq)
7706{
7707 if (wpa_s->global->p2p_disabled)
7708 return;
7709 if (wpa_s->global->p2p == NULL)
7710 return;
7711
7712 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7713 freq);
7714}
7715
7716
7717void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7718{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007719 unsigned int bands;
7720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007721 if (wpa_s->global->p2p_disabled)
7722 return;
7723 if (wpa_s->global->p2p == NULL)
7724 return;
7725
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007726 bands = wpas_get_bands(wpa_s, NULL);
7727 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007728}
7729
7730
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007731static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007732{
7733 p2p_group_deinit(wpa_s->p2p_group);
7734 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007735
7736 wpa_s->ap_configured_cb = NULL;
7737 wpa_s->ap_configured_cb_ctx = NULL;
7738 wpa_s->ap_configured_cb_data = NULL;
7739 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007740}
7741
7742
7743int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7744{
Hai Shalomfdcde762020-04-02 11:19:20 -07007745 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7748 return -1;
7749
7750 return p2p_reject(wpa_s->global->p2p, addr);
7751}
7752
7753
7754/* Invite to reinvoke a persistent group */
7755int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007756 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007757 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007758 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007759{
7760 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007761 u8 *bssid = NULL;
7762 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007763 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007764 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007765 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7766 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007767
Hai Shalomc1a21442022-02-04 13:43:00 -08007768 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7769 return -1;
7770
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007771 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007772 if (peer_addr)
7773 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7774 else
7775 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007776
Jouni Malinen31be0a42012-08-31 21:20:51 +03007777 wpa_s->p2p_persistent_go_freq = freq;
7778 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007779 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007780 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007781 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7782 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007783 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007784 if (ssid->mode == WPAS_MODE_P2P_GO) {
7785 role = P2P_INVITE_ROLE_GO;
7786 if (peer_addr == NULL) {
7787 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7788 "address in invitation command");
7789 return -1;
7790 }
7791 if (wpas_p2p_create_iface(wpa_s)) {
7792 if (wpas_p2p_add_group_interface(wpa_s,
7793 WPA_IF_P2P_GO) < 0) {
7794 wpa_printf(MSG_ERROR, "P2P: Failed to "
7795 "allocate a new interface for the "
7796 "group");
7797 return -1;
7798 }
7799 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007800 } else if (wpa_s->p2p_mgmt)
7801 bssid = wpa_s->parent->own_addr;
7802 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 bssid = wpa_s->own_addr;
7804 } else {
7805 role = P2P_INVITE_ROLE_CLIENT;
7806 peer_addr = ssid->bssid;
7807 }
7808 wpa_s->pending_invite_ssid_id = ssid->id;
7809
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007810 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007811 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007812 role == P2P_INVITE_ROLE_GO,
7813 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007814 if (res)
7815 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007817 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7818 return -1;
7819
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007820 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7821
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007822 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7823 no_pref_freq_given && pref_freq > 0 &&
7824 wpa_s->num_multichan_concurrent > 1 &&
7825 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7826 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7827 pref_freq);
7828 pref_freq = 0;
7829 }
7830
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007831 /*
7832 * Stop any find/listen operations before invitation and possibly
7833 * connection establishment.
7834 */
7835 wpas_p2p_stop_find_oper(wpa_s);
7836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007837 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007838 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007839 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007840}
7841
7842
7843/* Invite to join an active group */
7844int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007845 const u8 *peer_addr, const u8 *go_dev_addr,
7846 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007847{
7848 struct wpa_global *global = wpa_s->global;
7849 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007850 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007851 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007852 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007853 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007854 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007855 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7856 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007857
Jouni Malinen31be0a42012-08-31 21:20:51 +03007858 wpa_s->p2p_persistent_go_freq = 0;
7859 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007860 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007861 wpa_s->p2p_go_vht_center_freq2 = 0;
7862 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007863 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007865 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7866 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7867 break;
7868 }
7869 if (wpa_s == NULL) {
7870 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7871 return -1;
7872 }
7873
7874 ssid = wpa_s->current_ssid;
7875 if (ssid == NULL) {
7876 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7877 "invitation");
7878 return -1;
7879 }
7880
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007881 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007882 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007883 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007884 ssid->ssid, ssid->ssid_len);
7885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007886 if (ssid->mode == WPAS_MODE_P2P_GO) {
7887 role = P2P_INVITE_ROLE_ACTIVE_GO;
7888 bssid = wpa_s->own_addr;
7889 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007890 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007891 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007892 } else {
7893 role = P2P_INVITE_ROLE_CLIENT;
7894 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7895 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7896 "invite to current group");
7897 return -1;
7898 }
7899 bssid = wpa_s->bssid;
7900 if (go_dev_addr == NULL &&
7901 !is_zero_ether_addr(wpa_s->go_dev_addr))
7902 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007903 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7904 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007906 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007908 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7909 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007910 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7911 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007912
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007913 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007914 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007915 role == P2P_INVITE_ROLE_ACTIVE_GO,
7916 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007917 if (res)
7918 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007919 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007921 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007922 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007923 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007924}
7925
7926
7927void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7928{
7929 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007930 u8 go_dev_addr[ETH_ALEN];
7931 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007932 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007933 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007934 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007935
Dmitry Shmidt04949592012-07-19 12:16:46 -07007936 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7937 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007938 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007939 }
7940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007941 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007942 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007943
7944 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007945 if (!wpa_s->p2p_go_group_formation_completed &&
7946 wpa_s->global->p2p_group_formation == wpa_s) {
7947 wpa_dbg(wpa_s, MSG_DEBUG,
7948 "P2P: Marking group formation completed on client on data connection");
7949 wpa_s->p2p_go_group_formation_completed = 1;
7950 wpa_s->global->p2p_group_formation = NULL;
7951 wpa_s->p2p_in_provisioning = 0;
7952 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00007953 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007954 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007956 os_memset(go_dev_addr, 0, ETH_ALEN);
7957 if (ssid->bssid_set)
7958 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7959 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7960 ssid->ssid_len);
7961 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7962
7963 if (wpa_s->global->p2p_group_formation == wpa_s)
7964 wpa_s->global->p2p_group_formation = NULL;
7965
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007966 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7967 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007968
7969 ip_addr[0] = '\0';
7970 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007971 int res;
7972
7973 res = os_snprintf(ip_addr, sizeof(ip_addr),
7974 " ip_addr=%u.%u.%u.%u "
7975 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7976 ip[0], ip[1], ip[2], ip[3],
7977 ip[4], ip[5], ip[6], ip[7],
7978 ip[8], ip[9], ip[10], ip[11]);
7979 if (os_snprintf_error(sizeof(ip_addr), res))
7980 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007981 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007982 }
7983
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007984 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7985 ssid->passphrase == NULL && ssid->psk_set ?
7986 ssid->psk : NULL,
7987 ssid->passphrase, go_dev_addr, persistent,
7988 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007989
7990 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007991 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7992 ssid, go_dev_addr);
7993
Hai Shalom5f92bc92019-04-18 11:54:11 -07007994 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007995}
7996
7997
7998int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7999 u32 interval1, u32 duration2, u32 interval2)
8000{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008001 int ret;
8002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8004 return -1;
8005
8006 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
8007 wpa_s->current_ssid == NULL ||
8008 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
8009 return -1;
8010
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008011 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
8012 wpa_s->own_addr, wpa_s->assoc_freq,
8013 duration1, interval1, duration2, interval2);
8014 if (ret == 0)
8015 wpa_s->waiting_presence_resp = 1;
8016
8017 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008018}
8019
8020
8021int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
8022 unsigned int interval)
8023{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008024 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8025 return -1;
8026
8027 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
8028}
8029
8030
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008031static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
8032{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008033 if (wpa_s->current_ssid == NULL) {
8034 /*
8035 * current_ssid can be cleared when P2P client interface gets
8036 * disconnected, so assume this interface was used as P2P
8037 * client.
8038 */
8039 return 1;
8040 }
8041 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008042 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
8043}
8044
8045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008046static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
8047{
8048 struct wpa_supplicant *wpa_s = eloop_ctx;
8049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008050 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008051 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
8052 "disabled");
8053 return;
8054 }
8055
Dmitry Shmidt04949592012-07-19 12:16:46 -07008056 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
8057 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008058 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008059}
8060
8061
8062static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
8063{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008064 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008065
Dmitry Shmidt04949592012-07-19 12:16:46 -07008066 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8067 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008069 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8070 return;
8071
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008072 timeout = wpa_s->conf->p2p_group_idle;
8073 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8074 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8075 timeout = P2P_MAX_CLIENT_IDLE;
8076
8077 if (timeout == 0)
8078 return;
8079
Dmitry Shmidt04949592012-07-19 12:16:46 -07008080 if (timeout < 0) {
8081 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8082 timeout = 0; /* special client mode no-timeout */
8083 else
8084 return;
8085 }
8086
8087 if (wpa_s->p2p_in_provisioning) {
8088 /*
8089 * Use the normal group formation timeout during the
8090 * provisioning phase to avoid terminating this process too
8091 * early due to group idle timeout.
8092 */
8093 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8094 "during provisioning");
8095 return;
8096 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308097
Dmitry Shmidt04949592012-07-19 12:16:46 -07008098 if (wpa_s->show_group_started) {
8099 /*
8100 * Use the normal group formation timeout between the end of
8101 * the provisioning phase and completion of 4-way handshake to
8102 * avoid terminating this process too early due to group idle
8103 * timeout.
8104 */
8105 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8106 "while waiting for initial 4-way handshake to "
8107 "complete");
8108 return;
8109 }
8110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008111 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008112 timeout);
8113 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8114 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008115}
8116
8117
Jouni Malinen2b89da82012-08-31 22:04:41 +03008118/* Returns 1 if the interface was removed */
8119int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8120 u16 reason_code, const u8 *ie, size_t ie_len,
8121 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008122{
8123 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008124 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008125
Dmitry Shmidt04949592012-07-19 12:16:46 -07008126 if (!locally_generated)
8127 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8128 ie_len);
8129
8130 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8131 wpa_s->current_ssid &&
8132 wpa_s->current_ssid->p2p_group &&
8133 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8134 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8135 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008136 if (wpas_p2p_group_delete(wpa_s,
8137 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8138 > 0)
8139 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008140 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008141
8142 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008143}
8144
8145
8146void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008147 u16 reason_code, const u8 *ie, size_t ie_len,
8148 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008149{
8150 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8151 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008152
Dmitry Shmidt04949592012-07-19 12:16:46 -07008153 if (!locally_generated)
8154 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8155 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008156}
8157
8158
8159void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8160{
8161 struct p2p_data *p2p = wpa_s->global->p2p;
8162
8163 if (p2p == NULL)
8164 return;
8165
8166 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8167 return;
8168
8169 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8170 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8171
8172 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8173 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8174
8175 if (wpa_s->wps &&
8176 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8177 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8178
8179 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8180 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8181
8182 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8183 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8184 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8185 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8186 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8187 }
8188
8189 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8190 p2p_set_sec_dev_types(p2p,
8191 (void *) wpa_s->conf->sec_device_type,
8192 wpa_s->conf->num_sec_device_types);
8193
8194 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8195 int i;
8196 p2p_remove_wps_vendor_extensions(p2p);
8197 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8198 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8199 continue;
8200 p2p_add_wps_vendor_extension(
8201 p2p, wpa_s->conf->wps_vendor_ext[i]);
8202 }
8203 }
8204
8205 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8206 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8207 char country[3];
8208 country[0] = wpa_s->conf->country[0];
8209 country[1] = wpa_s->conf->country[1];
8210 country[2] = 0x04;
8211 p2p_set_country(p2p, country);
8212 }
8213
8214 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8215 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8216 wpa_s->conf->p2p_ssid_postfix ?
8217 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8218 0);
8219 }
8220
8221 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8222 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008223
8224 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8225 u8 reg_class, channel;
8226 int ret;
8227 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008228 u8 channel_forced;
8229
Jouni Malinen75ecf522011-06-27 15:19:46 -07008230 if (wpa_s->conf->p2p_listen_reg_class &&
8231 wpa_s->conf->p2p_listen_channel) {
8232 reg_class = wpa_s->conf->p2p_listen_reg_class;
8233 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008234 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008235 } else {
8236 reg_class = 81;
8237 /*
8238 * Pick one of the social channels randomly as the
8239 * listen channel.
8240 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008241 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8242 channel = 1;
8243 else
8244 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008245 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008246 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008247 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8248 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008249 if (ret)
8250 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8251 "failed: %d", ret);
8252 }
8253 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8254 u8 op_reg_class, op_channel, cfg_op_channel;
8255 int ret = 0;
8256 unsigned int r;
8257 if (wpa_s->conf->p2p_oper_reg_class &&
8258 wpa_s->conf->p2p_oper_channel) {
8259 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8260 op_channel = wpa_s->conf->p2p_oper_channel;
8261 cfg_op_channel = 1;
8262 } else {
8263 op_reg_class = 81;
8264 /*
8265 * Use random operation channel from (1, 6, 11)
8266 *if no other preference is indicated.
8267 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008268 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8269 op_channel = 1;
8270 else
8271 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008272 cfg_op_channel = 0;
8273 }
8274 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8275 cfg_op_channel);
8276 if (ret)
8277 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8278 "failed: %d", ret);
8279 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008280
8281 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8282 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8283 wpa_s->conf->p2p_pref_chan) < 0) {
8284 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8285 "update failed");
8286 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008287
8288 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8289 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8290 "update failed");
8291 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008292 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008293
8294 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8295 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008296}
8297
8298
8299int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8300 int duration)
8301{
8302 if (!wpa_s->ap_iface)
8303 return -1;
8304 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8305 duration);
8306}
8307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008308
8309int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8310{
8311 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8312 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008313
8314 wpa_s->global->cross_connection = enabled;
8315 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8316
8317 if (!enabled) {
8318 struct wpa_supplicant *iface;
8319
8320 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8321 {
8322 if (iface->cross_connect_enabled == 0)
8323 continue;
8324
8325 iface->cross_connect_enabled = 0;
8326 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008327 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008328 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8329 iface->ifname,
8330 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008331 }
8332 }
8333
8334 return 0;
8335}
8336
8337
8338static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8339{
8340 struct wpa_supplicant *iface;
8341
8342 if (!uplink->global->cross_connection)
8343 return;
8344
8345 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8346 if (!iface->cross_connect_enabled)
8347 continue;
8348 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8349 0)
8350 continue;
8351 if (iface->ap_iface == NULL)
8352 continue;
8353 if (iface->cross_connect_in_use)
8354 continue;
8355
8356 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008357 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008358 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8359 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008360 }
8361}
8362
8363
8364static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8365{
8366 struct wpa_supplicant *iface;
8367
8368 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8369 if (!iface->cross_connect_enabled)
8370 continue;
8371 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8372 0)
8373 continue;
8374 if (!iface->cross_connect_in_use)
8375 continue;
8376
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008377 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008378 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8379 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008380 iface->cross_connect_in_use = 0;
8381 }
8382}
8383
8384
8385void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8386{
8387 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8388 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8389 wpa_s->cross_connect_disallowed)
8390 wpas_p2p_disable_cross_connect(wpa_s);
8391 else
8392 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008393 if (!wpa_s->ap_iface &&
8394 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8395 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008396}
8397
8398
8399void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8400{
8401 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008402 if (!wpa_s->ap_iface &&
8403 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8404 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008405 wpas_p2p_set_group_idle_timeout(wpa_s);
8406}
8407
8408
8409static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8410{
8411 struct wpa_supplicant *iface;
8412
8413 if (!wpa_s->global->cross_connection)
8414 return;
8415
8416 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8417 if (iface == wpa_s)
8418 continue;
8419 if (iface->drv_flags &
8420 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8421 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008422 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8423 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008424 continue;
8425
8426 wpa_s->cross_connect_enabled = 1;
8427 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8428 sizeof(wpa_s->cross_connect_uplink));
8429 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8430 "%s to %s whenever uplink is available",
8431 wpa_s->ifname, wpa_s->cross_connect_uplink);
8432
8433 if (iface->ap_iface || iface->current_ssid == NULL ||
8434 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8435 iface->cross_connect_disallowed ||
8436 iface->wpa_state != WPA_COMPLETED)
8437 break;
8438
8439 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008440 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008441 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8442 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008443 break;
8444 }
8445}
8446
8447
Sunil Ravi036cec52023-03-29 11:35:17 -07008448static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008449{
8450 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8451 !wpa_s->p2p_in_provisioning)
8452 return 0; /* not P2P client operation */
8453
8454 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8455 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008456 if (wpa_s != wpa_s->p2pdev)
8457 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008458 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008459 return 1;
8460}
8461
8462
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008463void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8464{
8465 struct wpa_supplicant *wpa_s = eloop_ctx;
8466 wpas_p2p_notif_pbc_overlap(wpa_s);
8467}
8468
8469
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008470void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8471 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008472{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008473 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008474 struct wpa_used_freq_data *freqs = NULL;
8475 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008476
8477 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8478 return;
8479
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008480 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8481 if (!freqs)
8482 return;
8483
Sunil Ravi77d572f2023-01-17 23:58:31 +00008484 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008486 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008487 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008488 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8489 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008490 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8491 "channel list");
8492 return;
8493 }
8494
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008495 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008496
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008497 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008498
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008499 /*
8500 * The used frequencies map changed, so it is possible that a GO is
8501 * using a channel that is no longer valid for P2P use. It is also
8502 * possible that due to policy consideration, it would be preferable to
8503 * move it to a frequency already used by other station interfaces.
8504 */
8505 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8506
8507 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008508}
8509
8510
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008511static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8512 struct wpa_scan_results *scan_res)
8513{
8514 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8515}
8516
8517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008518int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8519{
8520 struct wpa_global *global = wpa_s->global;
8521 int found = 0;
8522 const u8 *peer;
8523
8524 if (global->p2p == NULL)
8525 return -1;
8526
8527 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8528
8529 if (wpa_s->pending_interface_name[0] &&
8530 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8531 found = 1;
8532
8533 peer = p2p_get_go_neg_peer(global->p2p);
8534 if (peer) {
8535 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8536 MACSTR, MAC2STR(peer));
8537 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008538 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008539 }
8540
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008541 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8542 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8543 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8544 found = 1;
8545 }
8546
8547 if (wpa_s->pending_pd_before_join) {
8548 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8549 wpa_s->pending_pd_before_join = 0;
8550 found = 1;
8551 }
8552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008553 wpas_p2p_stop_find(wpa_s);
8554
8555 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8556 if (wpa_s == global->p2p_group_formation &&
8557 (wpa_s->p2p_in_provisioning ||
8558 wpa_s->parent->pending_interface_type ==
8559 WPA_IF_P2P_CLIENT)) {
8560 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8561 "formation found - cancelling",
8562 wpa_s->ifname);
8563 found = 1;
8564 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008565 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008566 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008567 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008568 break;
8569 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008570 wpas_p2p_group_delete(wpa_s,
8571 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008572 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008573 } else if (wpa_s->p2p_in_invitation) {
8574 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8575 wpa_s->ifname);
8576 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008577 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008578 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008579 }
8580 }
8581
8582 if (!found) {
8583 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8584 return -1;
8585 }
8586
8587 return 0;
8588}
8589
8590
8591void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8592{
8593 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8594 return;
8595
8596 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8597 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008598 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008599}
8600
8601
8602void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8603 int freq_24, int freq_5, int freq_overall)
8604{
8605 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008606 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008607 return;
8608 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8609}
8610
8611
8612int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8613{
8614 u8 peer[ETH_ALEN];
8615 struct p2p_data *p2p = wpa_s->global->p2p;
8616
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008617 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008618 return -1;
8619
8620 if (hwaddr_aton(addr, peer))
8621 return -1;
8622
8623 return p2p_unauthorize(p2p, peer);
8624}
8625
8626
8627/**
8628 * wpas_p2p_disconnect - Disconnect from a P2P Group
8629 * @wpa_s: Pointer to wpa_supplicant data
8630 * Returns: 0 on success, -1 on failure
8631 *
8632 * This can be used to disconnect from a group in which the local end is a P2P
8633 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8634 * virtual network interface was created for this group, that interface will be
8635 * removed. Otherwise, only the configured P2P group network will be removed
8636 * from the interface.
8637 */
8638int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8639{
8640
8641 if (wpa_s == NULL)
8642 return -1;
8643
Jouni Malinen2b89da82012-08-31 22:04:41 +03008644 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8645 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008646}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008647
8648
8649int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8650{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008651 int ret;
8652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8654 return 0;
8655
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008656 ret = p2p_in_progress(wpa_s->global->p2p);
8657 if (ret == 0) {
8658 /*
8659 * Check whether there is an ongoing WPS provisioning step (or
8660 * other parts of group formation) on another interface since
8661 * p2p_in_progress() does not report this to avoid issues for
8662 * scans during such provisioning step.
8663 */
8664 if (wpa_s->global->p2p_group_formation &&
8665 wpa_s->global->p2p_group_formation != wpa_s) {
8666 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8667 "in group formation",
8668 wpa_s->global->p2p_group_formation->ifname);
8669 ret = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008670 } else if (wpa_s->global->p2p_group_formation == wpa_s) {
8671 wpa_dbg(wpa_s, MSG_DEBUG,
8672 "P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
8673 ret = 2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008674 }
8675 }
8676
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008677 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008678 struct os_reltime now;
8679 os_get_reltime(&now);
8680 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8681 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008682 /* Wait for the first client has expired */
8683 wpa_s->global->p2p_go_wait_client.sec = 0;
8684 } else {
8685 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8686 ret = 1;
8687 }
8688 }
8689
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008690 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008691}
8692
8693
8694void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8695 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008696{
8697 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8698 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008699 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008700 /**
8701 * Remove the network by scheduling the group formation
8702 * timeout to happen immediately. The teardown code
8703 * needs to be scheduled to run asynch later so that we
8704 * don't delete data from under ourselves unexpectedly.
8705 * Calling wpas_p2p_group_formation_timeout directly
8706 * causes a series of crashes in WPS failure scenarios.
8707 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008708 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8709 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008710 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008711 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008712 }
8713}
8714
8715
8716struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008717 const u8 *addr, const u8 *ssid,
8718 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008719{
8720 struct wpa_ssid *s;
8721 size_t i;
8722
8723 for (s = wpa_s->conf->ssid; s; s = s->next) {
8724 if (s->disabled != 2)
8725 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008726 if (ssid &&
8727 (ssid_len != s->ssid_len ||
8728 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8729 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008730 if (addr == NULL) {
8731 if (s->mode == WPAS_MODE_P2P_GO)
8732 return s;
8733 continue;
8734 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008735 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8736 return s; /* peer is GO in the persistent group */
8737 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8738 continue;
8739 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008740 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008741 addr, ETH_ALEN) == 0)
8742 return s; /* peer is P2P client in persistent
8743 * group */
8744 }
8745 }
8746
8747 return NULL;
8748}
8749
8750
8751void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8752 const u8 *addr)
8753{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008754 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008755 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008756 /*
8757 * This can happen if WPS provisioning step is not terminated
8758 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8759 * peer was able to connect, there is no need to time out group
8760 * formation after this, though. In addition, this is used with
8761 * the initial connection wait on the GO as a separate formation
8762 * timeout and as such, expected to be hit after the initial WPS
8763 * provisioning step.
8764 */
8765 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008766
8767 if (!wpa_s->p2p_go_group_formation_completed &&
8768 !wpa_s->group_formation_reported) {
8769 /*
8770 * GO has not yet notified group formation success since
8771 * the WPS step was not completed cleanly. Do that
8772 * notification now since the P2P Client was able to
8773 * connect and as such, must have received the
8774 * credential from the WPS step.
8775 */
8776 if (wpa_s->global->p2p)
8777 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008778 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008779 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008780 }
8781 if (!wpa_s->p2p_go_group_formation_completed) {
8782 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8783 wpa_s->p2p_go_group_formation_completed = 1;
8784 wpa_s->global->p2p_group_formation = NULL;
8785 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008786 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008787 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008788 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008789 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008790 if (addr == NULL)
8791 return;
8792 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8793}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008794
Dmitry Shmidt04949592012-07-19 12:16:46 -07008795
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008796static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8797 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008798{
8799 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008800 int ret = 0;
8801
Dmitry Shmidt04949592012-07-19 12:16:46 -07008802 if (wpa_s->global->p2p_group_formation)
8803 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008804 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008805 offchannel_send_action_done(wpa_s);
8806 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008807 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008808 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8809 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8810 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8811 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008812 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008813 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008814 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008815 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008816 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008817 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008818 wpa_s->p2p_go_he,
8819 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008820 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008821 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008822}
8823
8824
8825int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8826{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008827 int res;
8828
Dmitry Shmidt04949592012-07-19 12:16:46 -07008829 if (!wpa_s->p2p_fallback_to_go_neg ||
8830 wpa_s->p2p_in_provisioning <= 5)
8831 return 0;
8832
8833 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8834 return 0; /* peer operating as a GO */
8835
8836 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8837 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008838 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008839 "reason=GO-not-found");
8840 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008841
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008842 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008843}
8844
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008845
8846unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8847{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008848 struct wpa_supplicant *ifs;
8849
8850 if (wpa_s->wpa_state > WPA_SCANNING) {
8851 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8852 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008853 wpa_s->conf->p2p_search_delay);
8854 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008855 }
8856
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008857 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8858 radio_list) {
8859 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008860 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8861 "delay due to concurrent operation on "
8862 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008863 wpa_s->conf->p2p_search_delay,
8864 ifs->ifname);
8865 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008866 }
8867 }
8868
8869 return 0;
8870}
8871
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008872
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008873static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8874 struct wpa_ssid *s, const u8 *addr,
8875 int iface_addr)
8876{
8877 struct psk_list_entry *psk, *tmp;
8878 int changed = 0;
8879
8880 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8881 list) {
8882 if ((iface_addr && !psk->p2p &&
8883 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8884 (!iface_addr && psk->p2p &&
8885 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8886 wpa_dbg(wpa_s, MSG_DEBUG,
8887 "P2P: Remove persistent group PSK list entry for "
8888 MACSTR " p2p=%u",
8889 MAC2STR(psk->addr), psk->p2p);
8890 dl_list_del(&psk->list);
8891 os_free(psk);
8892 changed++;
8893 }
8894 }
8895
8896 return changed;
8897}
8898
8899
8900void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8901 const u8 *p2p_dev_addr,
8902 const u8 *psk, size_t psk_len)
8903{
8904 struct wpa_ssid *ssid = wpa_s->current_ssid;
8905 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008906 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008907
8908 if (psk_len != sizeof(p->psk))
8909 return;
8910
8911 if (p2p_dev_addr) {
8912 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8913 " p2p_dev_addr=" MACSTR,
8914 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8915 if (is_zero_ether_addr(p2p_dev_addr))
8916 p2p_dev_addr = NULL;
8917 } else {
8918 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8919 MAC2STR(mac_addr));
8920 }
8921
8922 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8924 /* To be added to persistent group once created */
8925 if (wpa_s->global->add_psk == NULL) {
8926 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8927 if (wpa_s->global->add_psk == NULL)
8928 return;
8929 }
8930 p = wpa_s->global->add_psk;
8931 if (p2p_dev_addr) {
8932 p->p2p = 1;
8933 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8934 } else {
8935 p->p2p = 0;
8936 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8937 }
8938 os_memcpy(p->psk, psk, psk_len);
8939 return;
8940 }
8941
8942 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8943 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8944 return;
8945 }
8946
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008947 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008948 ssid->ssid_len);
8949 if (!persistent) {
8950 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8951 return;
8952 }
8953
8954 p = os_zalloc(sizeof(*p));
8955 if (p == NULL)
8956 return;
8957 if (p2p_dev_addr) {
8958 p->p2p = 1;
8959 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8960 } else {
8961 p->p2p = 0;
8962 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8963 }
8964 os_memcpy(p->psk, psk, psk_len);
8965
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008966 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8967 (last = dl_list_last(&persistent->psk_list,
8968 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008969 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8970 MACSTR " (p2p=%u) to make room for a new one",
8971 MAC2STR(last->addr), last->p2p);
8972 dl_list_del(&last->list);
8973 os_free(last);
8974 }
8975
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008976 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008977 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8978 p2p_dev_addr == NULL);
8979 if (p2p_dev_addr) {
8980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8981 MACSTR, MAC2STR(p2p_dev_addr));
8982 } else {
8983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8984 MAC2STR(mac_addr));
8985 }
8986 dl_list_add(&persistent->psk_list, &p->list);
8987
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008988 if (wpa_s->p2pdev->conf->update_config &&
8989 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008990 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008991}
8992
8993
8994static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8995 struct wpa_ssid *s, const u8 *addr,
8996 int iface_addr)
8997{
8998 int res;
8999
9000 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009001 if (res > 0 && wpa_s->conf->update_config &&
9002 wpa_config_write(wpa_s->confname, wpa_s->conf))
9003 wpa_dbg(wpa_s, MSG_DEBUG,
9004 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009005}
9006
9007
9008static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
9009 const u8 *peer, int iface_addr)
9010{
9011 struct hostapd_data *hapd;
9012 struct hostapd_wpa_psk *psk, *prev, *rem;
9013 struct sta_info *sta;
9014
9015 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
9016 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
9017 return;
9018
9019 /* Remove per-station PSK entry */
9020 hapd = wpa_s->ap_iface->bss[0];
9021 prev = NULL;
9022 psk = hapd->conf->ssid.wpa_psk;
9023 while (psk) {
9024 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
9025 (!iface_addr &&
9026 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
9027 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
9028 MACSTR " iface_addr=%d",
9029 MAC2STR(peer), iface_addr);
9030 if (prev)
9031 prev->next = psk->next;
9032 else
9033 hapd->conf->ssid.wpa_psk = psk->next;
9034 rem = psk;
9035 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07009036 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009037 } else {
9038 prev = psk;
9039 psk = psk->next;
9040 }
9041 }
9042
9043 /* Disconnect from group */
9044 if (iface_addr)
9045 sta = ap_get_sta(hapd, peer);
9046 else
9047 sta = ap_get_sta_p2p(hapd, peer);
9048 if (sta) {
9049 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
9050 " (iface_addr=%d) from group",
9051 MAC2STR(peer), iface_addr);
9052 hostapd_drv_sta_deauth(hapd, sta->addr,
9053 WLAN_REASON_DEAUTH_LEAVING);
9054 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
9055 }
9056}
9057
9058
9059void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
9060 int iface_addr)
9061{
9062 struct wpa_ssid *s;
9063 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009064 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009065
9066 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
9067
9068 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009069 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009070 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9071 continue;
9072 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009073 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9074 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009075 }
9076
9077 /* Remove from any operating group */
9078 for (w = wpa_s->global->ifaces; w; w = w->next)
9079 wpas_p2p_remove_client_go(w, peer, iface_addr);
9080}
9081
9082
9083static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9084{
9085 struct wpa_supplicant *wpa_s = eloop_ctx;
9086 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9087}
9088
9089
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009090static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9091{
9092 struct wpa_supplicant *wpa_s = eloop_ctx;
9093
9094 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9095 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9096}
9097
9098
9099int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9100 struct wpa_ssid *ssid)
9101{
9102 struct wpa_supplicant *iface;
9103
9104 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9105 if (!iface->current_ssid ||
9106 iface->current_ssid->frequency == freq ||
9107 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9108 !iface->current_ssid->p2p_group))
9109 continue;
9110
9111 /* Remove the connection with least priority */
9112 if (!wpas_is_p2p_prioritized(iface)) {
9113 /* STA connection has priority over existing
9114 * P2P connection, so remove the interface. */
9115 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9116 eloop_register_timeout(0, 0,
9117 wpas_p2p_group_freq_conflict,
9118 iface, NULL);
9119 /* If connection in progress is P2P connection, do not
9120 * proceed for the connection. */
9121 if (wpa_s == iface)
9122 return -1;
9123 else
9124 return 0;
9125 } else {
9126 /* P2P connection has priority, disable the STA network
9127 */
9128 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9129 ssid);
9130 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9131 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9132 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9133 ETH_ALEN);
9134 /* If P2P connection is in progress, continue
9135 * connecting...*/
9136 if (wpa_s == iface)
9137 return 0;
9138 else
9139 return -1;
9140 }
9141 }
9142
9143 return 0;
9144}
9145
9146
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009147int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9148{
9149 struct wpa_ssid *ssid = wpa_s->current_ssid;
9150
9151 if (ssid == NULL || !ssid->p2p_group)
9152 return 0;
9153
9154 if (wpa_s->p2p_last_4way_hs_fail &&
9155 wpa_s->p2p_last_4way_hs_fail == ssid) {
9156 u8 go_dev_addr[ETH_ALEN];
9157 struct wpa_ssid *persistent;
9158
9159 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9160 ssid->ssid,
9161 ssid->ssid_len) <= 0) {
9162 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9163 goto disconnect;
9164 }
9165
9166 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9167 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009168 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009169 ssid->ssid,
9170 ssid->ssid_len);
9171 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9173 goto disconnect;
9174 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009175 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009176 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9177 persistent->id);
9178 disconnect:
9179 wpa_s->p2p_last_4way_hs_fail = NULL;
9180 /*
9181 * Remove the group from a timeout to avoid issues with caller
9182 * continuing to use the interface if this is on a P2P group
9183 * interface.
9184 */
9185 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9186 wpa_s, NULL);
9187 return 1;
9188 }
9189
9190 wpa_s->p2p_last_4way_hs_fail = ssid;
9191 return 0;
9192}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009193
9194
9195#ifdef CONFIG_WPS_NFC
9196
9197static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9198 struct wpabuf *p2p)
9199{
9200 struct wpabuf *ret;
9201 size_t wsc_len;
9202
9203 if (p2p == NULL) {
9204 wpabuf_free(wsc);
9205 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9206 return NULL;
9207 }
9208
9209 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9210 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9211 if (ret == NULL) {
9212 wpabuf_free(wsc);
9213 wpabuf_free(p2p);
9214 return NULL;
9215 }
9216
9217 wpabuf_put_be16(ret, wsc_len);
9218 if (wsc)
9219 wpabuf_put_buf(ret, wsc);
9220 wpabuf_put_be16(ret, wpabuf_len(p2p));
9221 wpabuf_put_buf(ret, p2p);
9222
9223 wpabuf_free(wsc);
9224 wpabuf_free(p2p);
9225 wpa_hexdump_buf(MSG_DEBUG,
9226 "P2P: Generated NFC connection handover message", ret);
9227
9228 if (ndef && ret) {
9229 struct wpabuf *tmp;
9230 tmp = ndef_build_p2p(ret);
9231 wpabuf_free(ret);
9232 if (tmp == NULL) {
9233 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9234 return NULL;
9235 }
9236 ret = tmp;
9237 }
9238
9239 return ret;
9240}
9241
9242
9243static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9244 struct wpa_ssid **ssid, u8 *go_dev_addr)
9245{
9246 struct wpa_supplicant *iface;
9247
9248 if (go_dev_addr)
9249 os_memset(go_dev_addr, 0, ETH_ALEN);
9250 if (ssid)
9251 *ssid = NULL;
9252 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9253 if (iface->wpa_state < WPA_ASSOCIATING ||
9254 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9255 !iface->current_ssid->p2p_group ||
9256 iface->current_ssid->mode != WPAS_MODE_INFRA)
9257 continue;
9258 if (ssid)
9259 *ssid = iface->current_ssid;
9260 if (go_dev_addr)
9261 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9262 return iface->assoc_freq;
9263 }
9264 return 0;
9265}
9266
9267
9268struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9269 int ndef)
9270{
9271 struct wpabuf *wsc, *p2p;
9272 struct wpa_ssid *ssid;
9273 u8 go_dev_addr[ETH_ALEN];
9274 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9275
9276 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9277 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9278 return NULL;
9279 }
9280
9281 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9282 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9283 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9284 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9285 return NULL;
9286 }
9287
9288 if (cli_freq == 0) {
9289 wsc = wps_build_nfc_handover_req_p2p(
9290 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9291 } else
9292 wsc = NULL;
9293 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9294 go_dev_addr, ssid ? ssid->ssid : NULL,
9295 ssid ? ssid->ssid_len : 0);
9296
9297 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9298}
9299
9300
9301struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9302 int ndef, int tag)
9303{
9304 struct wpabuf *wsc, *p2p;
9305 struct wpa_ssid *ssid;
9306 u8 go_dev_addr[ETH_ALEN];
9307 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9308
9309 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9310 return NULL;
9311
9312 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9313 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9314 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9315 return NULL;
9316
9317 if (cli_freq == 0) {
9318 wsc = wps_build_nfc_handover_sel_p2p(
9319 wpa_s->parent->wps,
9320 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9321 DEV_PW_NFC_CONNECTION_HANDOVER,
9322 wpa_s->conf->wps_nfc_dh_pubkey,
9323 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9324 } else
9325 wsc = NULL;
9326 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9327 go_dev_addr, ssid ? ssid->ssid : NULL,
9328 ssid ? ssid->ssid_len : 0);
9329
9330 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9331}
9332
9333
9334static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9335 struct p2p_nfc_params *params)
9336{
9337 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9338 "connection handover (freq=%d)",
9339 params->go_freq);
9340
9341 if (params->go_freq && params->go_ssid_len) {
9342 wpa_s->p2p_wps_method = WPS_NFC;
9343 wpa_s->pending_join_wps_method = WPS_NFC;
9344 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9345 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9346 ETH_ALEN);
9347 return wpas_p2p_join_start(wpa_s, params->go_freq,
9348 params->go_ssid,
9349 params->go_ssid_len);
9350 }
9351
9352 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9353 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009354 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009355 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009356 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009357 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009358 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009359}
9360
9361
9362static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9363 struct p2p_nfc_params *params, int tag)
9364{
9365 int res, persistent;
9366 struct wpa_ssid *ssid;
9367
9368 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9369 "connection handover");
9370 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9371 ssid = wpa_s->current_ssid;
9372 if (ssid == NULL)
9373 continue;
9374 if (ssid->mode != WPAS_MODE_P2P_GO)
9375 continue;
9376 if (wpa_s->ap_iface == NULL)
9377 continue;
9378 break;
9379 }
9380 if (wpa_s == NULL) {
9381 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9382 return -1;
9383 }
9384
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009385 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009386 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009387 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009388 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9389 return -1;
9390 }
9391 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009392 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9393 wpa_s->p2pdev->p2p_oob_dev_pw,
9394 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9395 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009396 if (res)
9397 return res;
9398
9399 if (!tag) {
9400 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9401 return 0;
9402 }
9403
9404 if (!params->peer ||
9405 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9406 return 0;
9407
9408 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9409 " to join", MAC2STR(params->peer->p2p_device_addr));
9410
9411 wpa_s->global->p2p_invite_group = wpa_s;
9412 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009413 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009414 params->peer->p2p_device_addr,
9415 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009416 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009417
9418 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9419 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9420 ssid->ssid, ssid->ssid_len, ssid->frequency,
9421 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009422 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009423}
9424
9425
9426static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9427 struct p2p_nfc_params *params,
9428 int forced_freq)
9429{
9430 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9431 "connection handover");
9432 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9433 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009434 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009435 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009436 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009437 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009438}
9439
9440
9441static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9442 struct p2p_nfc_params *params,
9443 int forced_freq)
9444{
9445 int res;
9446
9447 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9448 "connection handover");
9449 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9450 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009451 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009452 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009453 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009454 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009455 if (res)
9456 return res;
9457
9458 res = wpas_p2p_listen(wpa_s, 60);
9459 if (res) {
9460 p2p_unauthorize(wpa_s->global->p2p,
9461 params->peer->p2p_device_addr);
9462 }
9463
9464 return res;
9465}
9466
9467
9468static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9469 const struct wpabuf *data,
9470 int sel, int tag, int forced_freq)
9471{
9472 const u8 *pos, *end;
9473 u16 len, id;
9474 struct p2p_nfc_params params;
9475 int res;
9476
9477 os_memset(&params, 0, sizeof(params));
9478 params.sel = sel;
9479
9480 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9481
9482 pos = wpabuf_head(data);
9483 end = pos + wpabuf_len(data);
9484
9485 if (end - pos < 2) {
9486 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9487 "attributes");
9488 return -1;
9489 }
9490 len = WPA_GET_BE16(pos);
9491 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009492 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009493 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9494 "attributes");
9495 return -1;
9496 }
9497 params.wsc_attr = pos;
9498 params.wsc_len = len;
9499 pos += len;
9500
9501 if (end - pos < 2) {
9502 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9503 "attributes");
9504 return -1;
9505 }
9506 len = WPA_GET_BE16(pos);
9507 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009508 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009509 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9510 "attributes");
9511 return -1;
9512 }
9513 params.p2p_attr = pos;
9514 params.p2p_len = len;
9515 pos += len;
9516
9517 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9518 params.wsc_attr, params.wsc_len);
9519 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9520 params.p2p_attr, params.p2p_len);
9521 if (pos < end) {
9522 wpa_hexdump(MSG_DEBUG,
9523 "P2P: Ignored extra data after P2P attributes",
9524 pos, end - pos);
9525 }
9526
9527 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9528 if (res)
9529 return res;
9530
9531 if (params.next_step == NO_ACTION)
9532 return 0;
9533
9534 if (params.next_step == BOTH_GO) {
9535 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9536 MAC2STR(params.peer->p2p_device_addr));
9537 return 0;
9538 }
9539
9540 if (params.next_step == PEER_CLIENT) {
9541 if (!is_zero_ether_addr(params.go_dev_addr)) {
9542 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9543 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9544 " ssid=\"%s\"",
9545 MAC2STR(params.peer->p2p_device_addr),
9546 params.go_freq,
9547 MAC2STR(params.go_dev_addr),
9548 wpa_ssid_txt(params.go_ssid,
9549 params.go_ssid_len));
9550 } else {
9551 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9552 "peer=" MACSTR " freq=%d",
9553 MAC2STR(params.peer->p2p_device_addr),
9554 params.go_freq);
9555 }
9556 return 0;
9557 }
9558
9559 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9560 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9561 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9562 return 0;
9563 }
9564
9565 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9566 wpa_s->p2p_oob_dev_pw = NULL;
9567
9568 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9569 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9570 "received");
9571 return -1;
9572 }
9573
9574 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9575 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9576 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9577 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9578 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9579 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9580 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9581
9582 if (tag) {
9583 if (id < 0x10) {
9584 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9585 "peer OOB Device Password Id %u", id);
9586 return -1;
9587 }
9588 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9589 "Device Password Id %u", id);
9590 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9591 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9592 params.oob_dev_pw_len -
9593 WPS_OOB_PUBKEY_HASH_LEN - 2);
9594 wpa_s->p2p_oob_dev_pw_id = id;
9595 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9596 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9597 params.oob_dev_pw_len -
9598 WPS_OOB_PUBKEY_HASH_LEN - 2);
9599 if (wpa_s->p2p_oob_dev_pw == NULL)
9600 return -1;
9601
9602 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9603 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9604 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9605 return -1;
9606 } else {
9607 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9608 "without Device Password");
9609 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9610 }
9611
9612 switch (params.next_step) {
9613 case NO_ACTION:
9614 case BOTH_GO:
9615 case PEER_CLIENT:
9616 /* already covered above */
9617 return 0;
9618 case JOIN_GROUP:
9619 return wpas_p2p_nfc_join_group(wpa_s, &params);
9620 case AUTH_JOIN:
9621 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9622 case INIT_GO_NEG:
9623 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9624 case RESP_GO_NEG:
9625 /* TODO: use own OOB Dev Pw */
9626 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9627 }
9628
9629 return -1;
9630}
9631
9632
9633int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9634 const struct wpabuf *data, int forced_freq)
9635{
9636 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9637 return -1;
9638
9639 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9640}
9641
9642
9643int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9644 const struct wpabuf *req,
9645 const struct wpabuf *sel, int forced_freq)
9646{
9647 struct wpabuf *tmp;
9648 int ret;
9649
9650 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9651 return -1;
9652
9653 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9654
9655 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9656 wpabuf_head(req), wpabuf_len(req));
9657 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9658 wpabuf_head(sel), wpabuf_len(sel));
9659 if (forced_freq)
9660 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9661 tmp = ndef_parse_p2p(init ? sel : req);
9662 if (tmp == NULL) {
9663 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9664 return -1;
9665 }
9666
9667 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9668 forced_freq);
9669 wpabuf_free(tmp);
9670
9671 return ret;
9672}
9673
9674
9675int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9676{
9677 const u8 *if_addr;
9678 int go_intent = wpa_s->conf->p2p_go_intent;
9679 struct wpa_supplicant *iface;
9680
9681 if (wpa_s->global->p2p == NULL)
9682 return -1;
9683
9684 if (!enabled) {
9685 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9686 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9687 {
9688 if (!iface->ap_iface)
9689 continue;
9690 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9691 }
9692 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9693 0, NULL);
9694 if (wpa_s->p2p_nfc_tag_enabled)
9695 wpas_p2p_remove_pending_group_interface(wpa_s);
9696 wpa_s->p2p_nfc_tag_enabled = 0;
9697 return 0;
9698 }
9699
9700 if (wpa_s->global->p2p_disabled)
9701 return -1;
9702
9703 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9704 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9705 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9706 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9707 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9708 "to allow static handover cases");
9709 return -1;
9710 }
9711
9712 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9713
9714 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9715 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9716 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9717 if (wpa_s->p2p_oob_dev_pw == NULL)
9718 return -1;
9719 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9720
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009721 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9722 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9723 /*
9724 * P2P Group Interface present and the command came on group
9725 * interface, so enable the token for the current interface.
9726 */
9727 wpa_s->create_p2p_iface = 0;
9728 } else {
9729 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9730 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009731
9732 if (wpa_s->create_p2p_iface) {
9733 enum wpa_driver_if_type iftype;
9734 /* Prepare to add a new interface for the group */
9735 iftype = WPA_IF_P2P_GROUP;
9736 if (go_intent == 15)
9737 iftype = WPA_IF_P2P_GO;
9738 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9739 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9740 "interface for the group");
9741 return -1;
9742 }
9743
9744 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009745 } else if (wpa_s->p2p_mgmt)
9746 if_addr = wpa_s->parent->own_addr;
9747 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009748 if_addr = wpa_s->own_addr;
9749
9750 wpa_s->p2p_nfc_tag_enabled = enabled;
9751
9752 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9753 struct hostapd_data *hapd;
9754 if (iface->ap_iface == NULL)
9755 continue;
9756 hapd = iface->ap_iface->bss[0];
9757 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9758 hapd->conf->wps_nfc_dh_pubkey =
9759 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9760 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9761 hapd->conf->wps_nfc_dh_privkey =
9762 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9763 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9764 hapd->conf->wps_nfc_dev_pw =
9765 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9766 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9767
9768 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9769 wpa_dbg(iface, MSG_DEBUG,
9770 "P2P: Failed to enable NFC Tag for GO");
9771 }
9772 }
9773 p2p_set_authorized_oob_dev_pw_id(
9774 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9775 if_addr);
9776
9777 return 0;
9778}
9779
9780#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009781
9782
9783static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9784 struct wpa_used_freq_data *freqs,
9785 unsigned int num)
9786{
9787 u8 curr_chan, cand, chan;
9788 unsigned int i;
9789
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009790 /*
9791 * If possible, optimize the Listen channel to be a channel that is
9792 * already used by one of the other interfaces.
9793 */
9794 if (!wpa_s->conf->p2p_optimize_listen_chan)
9795 return;
9796
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009797 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9798 for (i = 0, cand = 0; i < num; i++) {
9799 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9800 if (curr_chan == chan) {
9801 cand = 0;
9802 break;
9803 }
9804
9805 if (chan == 1 || chan == 6 || chan == 11)
9806 cand = chan;
9807 }
9808
9809 if (cand) {
9810 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009811 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009812 cand);
9813 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9814 }
9815}
9816
9817
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009818static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009819{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009820 struct hostapd_config *conf;
9821 struct p2p_go_neg_results params;
9822 struct csa_settings csa_settings;
9823 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9824 int old_freq = current_ssid->frequency;
9825 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009826
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009827 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9828 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9829 return -1;
9830 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009831
9832 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009833 * TODO: This function may not always work correctly. For example,
9834 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009835 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009836 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9837 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009838 wpa_dbg(wpa_s, MSG_DEBUG,
9839 "P2P CSA: Failed to select new frequency for GO");
9840 return -1;
9841 }
9842
9843 if (current_ssid->frequency == params.freq) {
9844 wpa_dbg(wpa_s, MSG_DEBUG,
9845 "P2P CSA: Selected same frequency - not moving GO");
9846 return 0;
9847 }
9848
9849 conf = hostapd_config_defaults();
9850 if (!conf) {
9851 wpa_dbg(wpa_s, MSG_DEBUG,
9852 "P2P CSA: Failed to allocate default config");
9853 return -1;
9854 }
9855
9856 current_ssid->frequency = params.freq;
9857 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9858 wpa_dbg(wpa_s, MSG_DEBUG,
9859 "P2P CSA: Failed to create new GO config");
9860 ret = -1;
9861 goto out;
9862 }
9863
Sunil4a3f9f52022-07-19 22:04:39 +00009864 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9865 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009866 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009867 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9868 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009869 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9870 wpa_s->ap_iface->current_mode->mode,
9871 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9872 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009873 ret = -1;
9874 goto out;
9875 }
9876
9877 os_memset(&csa_settings, 0, sizeof(csa_settings));
9878 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9879 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9880 csa_settings.freq_params.freq = params.freq;
9881 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9882 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9883 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9884
9885 if (conf->ieee80211ac) {
9886 int freq1 = 0, freq2 = 0;
9887 u8 chan, opclass;
9888
9889 if (ieee80211_freq_to_channel_ext(params.freq,
9890 conf->secondary_channel,
9891 conf->vht_oper_chwidth,
9892 &opclass, &chan) ==
9893 NUM_HOSTAPD_MODES) {
9894 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9895 ret = -1;
9896 goto out;
9897 }
9898
9899 if (conf->vht_oper_centr_freq_seg0_idx)
9900 freq1 = ieee80211_chan_to_freq(
9901 NULL, opclass,
9902 conf->vht_oper_centr_freq_seg0_idx);
9903
9904 if (conf->vht_oper_centr_freq_seg1_idx)
9905 freq2 = ieee80211_chan_to_freq(
9906 NULL, opclass,
9907 conf->vht_oper_centr_freq_seg1_idx);
9908
9909 if (freq1 < 0 || freq2 < 0) {
9910 wpa_dbg(wpa_s, MSG_DEBUG,
9911 "P2P CSA: Selected invalid VHT center freqs");
9912 ret = -1;
9913 goto out;
9914 }
9915
9916 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9917 csa_settings.freq_params.center_freq1 = freq1;
9918 csa_settings.freq_params.center_freq2 = freq2;
9919
9920 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009921 case CONF_OPER_CHWIDTH_80MHZ:
9922 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009923 csa_settings.freq_params.bandwidth = 80;
9924 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009925 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009926 csa_settings.freq_params.bandwidth = 160;
9927 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009928 default:
9929 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009930 }
9931 }
9932
9933 ret = ap_switch_channel(wpa_s, &csa_settings);
9934out:
9935 current_ssid->frequency = old_freq;
9936 hostapd_config_free(conf);
9937 return ret;
9938}
9939
9940
9941static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9942{
9943 struct p2p_go_neg_results params;
9944 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009945 void (*ap_configured_cb)(void *ctx, void *data);
9946 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009947
9948 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9949
9950 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9951 current_ssid->frequency);
9952
9953 /* Stop the AP functionality */
9954 /* TODO: Should do this in a way that does not indicated to possible
9955 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009956 /* If this action occurs before a group is started, the callback should
9957 * be preserved, or GROUP-STARTED event would be lost. If this action
9958 * occurs after a group is started, these pointers are all NULL and
9959 * harmless. */
9960 ap_configured_cb = wpa_s->ap_configured_cb;
9961 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9962 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009963 wpa_supplicant_ap_deinit(wpa_s);
9964
9965 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009966 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9967 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009968 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9969 wpas_p2p_group_delete(wpa_s,
9970 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9971 return;
9972 }
9973 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9974 params.freq);
9975
9976 if (params.freq &&
9977 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9978 wpa_printf(MSG_DEBUG,
9979 "P2P: Selected freq (%u MHz) is not valid for P2P",
9980 params.freq);
9981 wpas_p2p_group_delete(wpa_s,
9982 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9983 return;
9984 }
9985
Hai Shalom899fcc72020-10-19 14:38:18 -07009986 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009987 wpa_s->ap_configured_cb = ap_configured_cb;
9988 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9989 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009990
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009991 /* Update the frequency */
9992 current_ssid->frequency = params.freq;
9993 wpa_s->connect_without_scan = current_ssid;
9994 wpa_s->reassociate = 1;
9995 wpa_s->disconnected = 0;
9996 wpa_supplicant_req_scan(wpa_s, 0, 0);
9997}
9998
9999
10000static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
10001{
10002 struct wpa_supplicant *wpa_s = eloop_ctx;
10003
10004 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010005 return;
10006
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010007 wpas_p2p_go_update_common_freqs(wpa_s);
10008
10009 /* Do not move GO in the middle of a CSA */
10010 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10011 wpa_printf(MSG_DEBUG,
10012 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010013 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010014 }
10015
10016 /*
10017 * First, try a channel switch flow. If it is not supported or fails,
10018 * take down the GO and bring it up again.
10019 */
10020 if (wpas_p2p_move_go_csa(wpa_s) < 0)
10021 wpas_p2p_move_go_no_csa(wpa_s);
10022}
10023
10024
10025static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
10026{
10027 struct wpa_supplicant *wpa_s = eloop_ctx;
10028 struct wpa_used_freq_data *freqs = NULL;
10029 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010030
10031 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
10032 if (!freqs)
10033 return;
10034
Sunil Ravi77d572f2023-01-17 23:58:31 +000010035 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010036
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010037 /* Previous attempt to move a GO was not possible -- try again. */
10038 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
10039 WPAS_P2P_CHANNEL_UPDATE_ANY);
10040
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010041 os_free(freqs);
10042}
10043
10044
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010045/*
10046 * Consider moving a GO from its currently used frequency:
10047 * 1. It is possible that due to regulatory consideration the frequency
10048 * can no longer be used and there is a need to evacuate the GO.
10049 * 2. It is possible that due to MCC considerations, it would be preferable
10050 * to move the GO to a channel that is currently used by some other
10051 * station interface.
10052 *
10053 * In case a frequency that became invalid is once again valid, cancel a
10054 * previously initiated GO frequency change.
10055 */
10056static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
10057 struct wpa_used_freq_data *freqs,
10058 unsigned int num)
10059{
10060 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
10061 unsigned int timeout;
10062 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010063 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010064
10065 wpas_p2p_go_update_common_freqs(wpa_s);
10066
10067 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010068 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010069 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010070 for (i = 0, invalid_freq = 0; i < num; i++) {
10071 if (freqs[i].freq == freq) {
10072 flags = freqs[i].flags;
10073
10074 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010075 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10076 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010077 wpa_dbg(wpa_s, MSG_DEBUG,
10078 "P2P: Freq=%d MHz no longer valid for GO",
10079 freq);
10080 invalid_freq = 1;
10081 }
10082 } else if (freqs[i].flags == 0) {
10083 /* Freq is not used by any other station interface */
10084 continue;
10085 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010086 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010087 /* Freq is not valid for P2P use cases */
10088 continue;
10089 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10090 P2P_GO_FREQ_MOVE_SCM) {
10091 policy_move = 1;
10092 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10093 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10094 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10095 policy_move = 1;
10096 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10097 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10098 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10099 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10100 policy_move = 1;
10101 } else if ((wpa_s->drv_flags &
10102 WPA_DRIVER_FLAGS_AP_CSA) &&
10103 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10104 u8 chan;
10105
10106 /*
10107 * We do not support CSA between bands, so move
10108 * GO only within the same band.
10109 */
10110 if (wpa_s->ap_iface->current_mode->mode ==
10111 ieee80211_freq_to_chan(freqs[i].freq,
10112 &chan))
10113 policy_move = 1;
10114 }
10115 }
10116 }
10117
10118 wpa_dbg(wpa_s, MSG_DEBUG,
10119 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10120 invalid_freq, policy_move, flags);
10121
10122 /*
10123 * The channel is valid, or we are going to have a policy move, so
10124 * cancel timeout.
10125 */
10126 if (!invalid_freq || policy_move) {
10127 wpa_dbg(wpa_s, MSG_DEBUG,
10128 "P2P: Cancel a GO move from freq=%d MHz", freq);
10129 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10130
10131 if (wpas_p2p_in_progress(wpa_s)) {
10132 wpa_dbg(wpa_s, MSG_DEBUG,
10133 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10134 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10135 wpa_s, NULL);
10136 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10137 wpas_p2p_reconsider_moving_go,
10138 wpa_s, NULL);
10139 return;
10140 }
10141 }
10142
10143 if (!invalid_freq && (!policy_move || flags != 0)) {
10144 wpa_dbg(wpa_s, MSG_DEBUG,
10145 "P2P: Not initiating a GO frequency change");
10146 return;
10147 }
10148
10149 /*
10150 * Do not consider moving GO if it is in the middle of a CSA. When the
10151 * CSA is finished this flow should be retriggered.
10152 */
10153 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10154 wpa_dbg(wpa_s, MSG_DEBUG,
10155 "P2P: Not initiating a GO frequency change - CSA is in progress");
10156 return;
10157 }
10158
10159 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10160 timeout = P2P_GO_FREQ_CHANGE_TIME;
10161 else
10162 timeout = 0;
10163
10164 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10165 freq, timeout);
10166 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10167 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10168}
10169
10170
10171static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10172 struct wpa_used_freq_data *freqs,
10173 unsigned int num,
10174 enum wpas_p2p_channel_update_trig trig)
10175{
10176 struct wpa_supplicant *ifs;
10177
10178 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10179 NULL);
10180
10181 /*
10182 * Travers all the radio interfaces, and for each GO interface, check
10183 * if there is a need to move the GO from the frequency it is using,
10184 * or in case the frequency is valid again, cancel the evacuation flow.
10185 */
10186 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10187 radio_list) {
10188 if (ifs->current_ssid == NULL ||
10189 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10190 continue;
10191
10192 /*
10193 * The GO was just started or completed channel switch, no need
10194 * to move it.
10195 */
10196 if (wpa_s == ifs &&
10197 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10198 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10199 wpa_dbg(wpa_s, MSG_DEBUG,
10200 "P2P: GO move - schedule re-consideration");
10201 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10202 wpas_p2p_reconsider_moving_go,
10203 wpa_s, NULL);
10204 continue;
10205 }
10206
10207 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10208 }
10209}
10210
10211
10212void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10213{
10214 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10215 return;
10216
10217 wpas_p2p_update_channel_list(wpa_s,
10218 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10219}
10220
10221
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010222void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10223{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010224 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10225 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10226 "the management interface is being removed");
10227 wpas_p2p_deinit_global(wpa_s->global);
10228 }
10229}
10230
10231
10232void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10233{
10234 if (wpa_s->ap_iface->bss)
10235 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10236 wpas_p2p_group_deinit(wpa_s);
10237}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010238
10239
10240int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10241 unsigned int period, unsigned int interval,
10242 unsigned int count)
10243{
10244 struct p2p_data *p2p = wpa_s->global->p2p;
10245 u8 *device_types;
10246 size_t dev_types_len;
10247 struct wpabuf *buf;
10248 int ret;
10249
10250 if (wpa_s->p2p_lo_started) {
10251 wpa_dbg(wpa_s, MSG_DEBUG,
10252 "P2P Listen offload is already started");
10253 return 0;
10254 }
10255
10256 if (wpa_s->global->p2p == NULL ||
10257 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10258 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10259 return -1;
10260 }
10261
10262 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10263 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10264 freq);
10265 return -1;
10266 }
10267
10268 /* Get device type */
10269 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10270 WPS_DEV_TYPE_LEN;
10271 device_types = os_malloc(dev_types_len);
10272 if (!device_types)
10273 return -1;
10274 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10275 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10276 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10277
10278 /* Get Probe Response IE(s) */
10279 buf = p2p_build_probe_resp_template(p2p, freq);
10280 if (!buf) {
10281 os_free(device_types);
10282 return -1;
10283 }
10284
10285 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10286 device_types, dev_types_len,
10287 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10288 if (ret < 0)
10289 wpa_dbg(wpa_s, MSG_DEBUG,
10290 "P2P: Failed to start P2P listen offload");
10291
10292 os_free(device_types);
10293 wpabuf_free(buf);
10294
10295 if (ret == 0) {
10296 wpa_s->p2p_lo_started = 1;
10297
10298 /* Stop current P2P listen if any */
10299 wpas_stop_listen(wpa_s);
10300 }
10301
10302 return ret;
10303}
10304
10305
10306int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10307{
10308 int ret;
10309
10310 if (!wpa_s->p2p_lo_started)
10311 return 0;
10312
10313 ret = wpa_drv_p2p_lo_stop(wpa_s);
10314 if (ret < 0)
10315 wpa_dbg(wpa_s, MSG_DEBUG,
10316 "P2P: Failed to stop P2P listen offload");
10317
10318 wpa_s->p2p_lo_started = 0;
10319 return ret;
10320}