blob: c1508631f8bf0761acb7ee912a02b66b59f0e141 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +0000132 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Sunil Ravi036cec52023-03-29 11:35:17 -0700168static int wpas_p2p_disallowed_freq(struct wpa_global *global,
169 unsigned int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170
171
Hai Shalomc1a21442022-02-04 13:43:00 -0800172static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
173{
174 int he_capab = 0;
175
176 if (mode)
177 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
178 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
179 return he_capab;
180}
181
182
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700183/*
184 * Get the number of concurrent channels that the HW can operate, but that are
185 * currently not in use by any of the wpa_supplicant interfaces.
186 */
187static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
188{
189 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800190 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700191
192 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
193 if (!freqs)
194 return -1;
195
196 num = get_shared_radio_freqs(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000197 wpa_s->num_multichan_concurrent, false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198 os_free(freqs);
199
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800200 unused = wpa_s->num_multichan_concurrent - num;
201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
202 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700203}
204
205
206/*
207 * Get the frequencies that are currently in use by one or more of the virtual
208 * interfaces, and that are also valid for P2P operation.
209 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210static unsigned int
211wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
212 struct wpa_used_freq_data *p2p_freqs,
213 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700215 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700216 unsigned int num, i, j;
217
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700218 freqs = os_calloc(wpa_s->num_multichan_concurrent,
219 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700220 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700221 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700222
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700223 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000224 wpa_s->num_multichan_concurrent,
225 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700226
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700227 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228
229 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700230 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700231 p2p_freqs[j++] = freqs[i];
232 }
233
234 os_free(freqs);
235
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700236 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800237
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700238 return j;
239}
240
241
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700242static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
243 int freq)
244{
245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
246 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700247
248 /* Use the wpa_s used to control the P2P Device operation */
249 wpa_s = wpa_s->global->p2p_init_wpa_s;
250
251 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800252 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
253 wpas_p2p_num_unused_channels(wpa_s) > 0) {
254 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
255 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700256 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800257 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700258 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
259}
260
261
Hai Shalom60840252021-02-19 19:02:11 -0800262static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
263{
264 unsigned int delay = wpas_p2p_search_delay(wpa_s);
265
266 /* In case of concurrent P2P and external scans, delay P2P search. */
267 if (external_scan_running(wpa_s->radio)) {
268 delay = wpa_s->conf->p2p_search_delay;
269 wpa_printf(MSG_DEBUG,
270 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
271 delay);
272 }
273
274 p2p_scan_res_handled(wpa_s->global->p2p, delay);
275}
276
277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
279 struct wpa_scan_results *scan_res)
280{
281 size_t i;
282
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 if (wpa_s->p2p_scan_work) {
284 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
285 wpa_s->p2p_scan_work = NULL;
286 radio_work_done(work);
287 }
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
290 return;
291
292 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
293 (int) scan_res->num);
294
295 for (i = 0; i < scan_res->num; i++) {
296 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800297 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700298 const u8 *ies;
299 size_t ies_len;
300
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800301 time_tmp_age.sec = bss->age / 1000;
302 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800303 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700304
305 ies = (const u8 *) (bss + 1);
306 ies_len = bss->ie_len;
307 if (bss->beacon_ie_len > 0 &&
308 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
309 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
310 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
311 MACSTR, MAC2STR(bss->bssid));
312 ies = ies + ies_len;
313 ies_len = bss->beacon_ie_len;
314 }
315
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800318 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700319 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 break;
321 }
322
Hai Shalom60840252021-02-19 19:02:11 -0800323 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324}
325
326
Hai Shalom60840252021-02-19 19:02:11 -0800327static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700328{
Hai Shalom60840252021-02-19 19:02:11 -0800329 if (wpa_s->p2p_scan_work) {
330 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700331
Hai Shalom60840252021-02-19 19:02:11 -0800332 wpa_s->p2p_scan_work = NULL;
333 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700334 }
335
Hai Shalom60840252021-02-19 19:02:11 -0800336 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
337 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700338
Hai Shalom60840252021-02-19 19:02:11 -0800339 wpa_dbg(wpa_s, MSG_DEBUG,
340 "P2P: Failed to get scan results - try to continue");
341 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700342}
343
344
Sunil Ravi77d572f2023-01-17 23:58:31 +0000345void wpas_p2p_scan_freqs(struct wpa_supplicant *wpa_s,
346 struct wpa_driver_scan_params *params,
347 bool include_6ghz)
348{
349 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
350 params, false, false, false);
351 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
352 params, false, false, false);
353 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD,
354 params, false, false, false);
355 if (!wpa_s->conf->p2p_6ghz_disable &&
356 is_p2p_allow_6ghz(wpa_s->global->p2p) && include_6ghz)
357 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
358 params, true, true, false);
359}
360
361
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800362static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
363{
364 struct wpa_supplicant *wpa_s = work->wpa_s;
365 struct wpa_driver_scan_params *params = work->ctx;
366 int ret;
367
368 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800369 if (!work->started) {
370 wpa_scan_free_params(params);
371 return;
372 }
373
374 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 return;
376 }
377
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800378 if (wpa_s->clear_driver_scan_cache) {
379 wpa_printf(MSG_DEBUG,
380 "Request driver to clear scan cache due to local BSS flush");
381 params->only_new_results = 1;
382 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700383
Sunil Ravi77d572f2023-01-17 23:58:31 +0000384 if (!params->freqs)
385 wpas_p2p_scan_freqs(wpa_s, params, params->p2p_include_6ghz);
386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800387 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800388 if (ret == 0)
389 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 wpa_scan_free_params(params);
391 work->ctx = NULL;
392 if (ret) {
393 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800394 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800395 return;
396 }
397
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800398 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800399 os_get_reltime(&wpa_s->scan_trigger_time);
400 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800401 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800402 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800403 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800404 wpa_s->p2p_scan_work = work;
405}
406
407
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
409 int freq)
410{
411 if (wpa_s->global->p2p_24ghz_social_channels &&
412 (freq == 2412 || freq == 2437 || freq == 2462)) {
413 /*
414 * Search all social channels regardless of whether these have
415 * been disabled for P2P operating channel use to avoid missing
416 * peers.
417 */
418 return 1;
419 }
420 return p2p_supported_freq(wpa_s->global->p2p, freq);
421}
422
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
425 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800426 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
427 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428{
429 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800430 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700432 unsigned int num_channels = 0;
433 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700435 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800436 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437
438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
439 return -1;
440
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800441 if (wpa_s->p2p_scan_work) {
442 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
443 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700444 }
445
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800446 params = os_zalloc(sizeof(*params));
447 if (params == NULL)
448 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449
450 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800451 params->num_ssids = 1;
452 n = os_malloc(P2P_WILDCARD_SSID_LEN);
453 if (n == NULL)
454 goto fail;
455 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
456 params->ssids[0].ssid = n;
457 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458
459 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700460 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
461 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 num_req_dev_types, req_dev_types);
463 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800464 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000465
466 /*
467 * In case 6 GHz channels are requested as part of the P2P scan, only
468 * the PSCs would be included as P2P GOs are not expected to be
469 * collocated, i.e., they would not be announced in the RNR element of
470 * other APs.
471 */
Hai Shalomc1a21442022-02-04 13:43:00 -0800472 if (!wpa_s->conf->p2p_6ghz_disable)
473 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 switch (type) {
475 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
477 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800478 if (params->freqs == NULL)
479 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700480 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481 if (wpas_p2p_search_social_channel(
482 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700483 params->freqs[num_channels++] =
484 social_channels_freq[i];
485 }
486 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 break;
488 case P2P_SCAN_FULL:
489 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800490 case P2P_SCAN_SPECIFIC:
491 params->freqs = os_calloc(2, sizeof(int));
492 if (params->freqs == NULL)
493 goto fail;
494 params->freqs[0] = freq;
495 params->freqs[1] = 0;
496 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700498 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
499 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800500 if (params->freqs == NULL)
501 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700502 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 if (wpas_p2p_search_social_channel(
504 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700505 params->freqs[num_channels++] =
506 social_channels_freq[i];
507 }
508 if (p2p_supported_freq(wpa_s->global->p2p, freq))
509 params->freqs[num_channels++] = freq;
510 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 break;
512 }
513
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800514 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
515 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
516 if (ies == NULL) {
517 wpabuf_free(wps_ie);
518 goto fail;
519 }
520 wpabuf_put_buf(ies, wps_ie);
521 wpabuf_free(wps_ie);
522
523 bands = wpas_get_bands(wpa_s, params->freqs);
524 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
525
526 params->p2p_probe = 1;
527 n = os_malloc(wpabuf_len(ies));
528 if (n == NULL) {
529 wpabuf_free(ies);
530 goto fail;
531 }
532 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
533 params->extra_ies = n;
534 params->extra_ies_len = wpabuf_len(ies);
535 wpabuf_free(ies);
536
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800537 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800538 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
539 params) < 0)
540 goto fail;
541 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543fail:
544 wpa_scan_free_params(params);
545 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546}
547
548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
550{
551 switch (p2p_group_interface) {
552 case P2P_GROUP_INTERFACE_PENDING:
553 return WPA_IF_P2P_GROUP;
554 case P2P_GROUP_INTERFACE_GO:
555 return WPA_IF_P2P_GO;
556 case P2P_GROUP_INTERFACE_CLIENT:
557 return WPA_IF_P2P_CLIENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000558 default:
559 return WPA_IF_P2P_GROUP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561}
562
563
564static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
565 const u8 *ssid,
566 size_t ssid_len, int *go)
567{
568 struct wpa_ssid *s;
569
570 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
571 for (s = wpa_s->conf->ssid; s; s = s->next) {
572 if (s->disabled != 0 || !s->p2p_group ||
573 s->ssid_len != ssid_len ||
574 os_memcmp(ssid, s->ssid, ssid_len) != 0)
575 continue;
576 if (s->mode == WPAS_MODE_P2P_GO &&
577 s != wpa_s->current_ssid)
578 continue;
579 if (go)
580 *go = s->mode == WPAS_MODE_P2P_GO;
581 return wpa_s;
582 }
583 }
584
585 return NULL;
586}
587
588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
590{
591 struct wpa_supplicant *wpa_s = eloop_ctx;
592 wpa_printf(MSG_DEBUG,
593 "P2P: Complete previously requested removal of %s",
594 wpa_s->ifname);
595 wpas_p2p_disconnect(wpa_s);
596}
597
598
599static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
600 struct wpa_supplicant *calling_wpa_s)
601{
602 if (calling_wpa_s == wpa_s && wpa_s &&
603 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
604 /*
605 * The calling wpa_s instance is going to be removed. Do that
606 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700607 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608 * control interface responses on the per-interface socket.
609 */
610 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
611 wpa_s, NULL) < 0)
612 return -1;
613 return 0;
614 }
615
616 return wpas_p2p_disconnect(wpa_s);
617}
618
619
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800620/* Determine total number of clients in active groups where we are the GO */
621static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
622{
623 unsigned int count = 0;
624 struct wpa_ssid *s;
625
626 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
627 for (s = wpa_s->conf->ssid; s; s = s->next) {
628 wpa_printf(MSG_DEBUG,
629 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
630 wpa_s, s, s->disabled, s->p2p_group,
631 s->mode);
632 if (!s->disabled && s->p2p_group &&
633 s->mode == WPAS_MODE_P2P_GO) {
634 count += p2p_get_group_num_members(
635 wpa_s->p2p_group);
636 }
637 }
638 }
639
640 return count;
641}
642
643
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800644static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
645{
646 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
647 !wpa_s->current_ssid->disabled &&
648 wpa_s->current_ssid->p2p_group &&
649 wpa_s->current_ssid->p2p_persistent_group;
650}
651
652
653static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
654{
655 return p2p_is_active_persistent_group(wpa_s) &&
656 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
657}
658
659
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660/* Find an interface for a P2P group where we are the GO */
661static struct wpa_supplicant *
662wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
663{
664 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665
666 if (!wpa_s)
667 return NULL;
668
669 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800670 if (!p2p_is_active_persistent_go(wpa_s))
671 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800672
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800673 /* Prefer a group with connected clients */
674 if (p2p_get_group_num_members(wpa_s->p2p_group))
675 return wpa_s;
676 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677 }
678
679 /* No group with connected clients, so pick the one without (if any) */
680 return save;
681}
682
683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800684static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800685{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 return p2p_is_active_persistent_group(wpa_s) &&
687 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
688}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800689
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800690
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800691/* Find an interface for a P2P group where we are the P2P Client */
692static struct wpa_supplicant *
693wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
694{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800695 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800696 if (p2p_is_active_persistent_cli(wpa_s))
697 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800698 }
699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800700 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800701}
702
703
704/* Find a persistent group where we are the GO */
705static struct wpa_ssid *
706wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
707{
708 struct wpa_ssid *s;
709
710 for (s = wpa_s->conf->ssid; s; s = s->next) {
711 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
712 return s;
713 }
714
715 return NULL;
716}
717
718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
720 unsigned int *force_freq,
721 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800722{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800723 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 struct wpa_ssid *s;
725 u8 conncap = P2PS_SETUP_NONE;
726 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800727 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800728 struct wpa_ssid *persistent_go;
729 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000730 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
731 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732
733 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
734
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800735 if (force_freq)
736 *force_freq = 0;
737 if (pref_freq)
738 *pref_freq = 0;
739
740 size = P2P_MAX_PREF_CHANNELS;
741 if (force_freq && pref_freq &&
742 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
743 (int *) pref_freq, 0, pref_freq_list, &size))
744 wpas_p2p_set_own_freq_preference(wpa_s,
745 *force_freq ? *force_freq :
746 *pref_freq);
747
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800748 /*
749 * For non-concurrent capable devices:
750 * If persistent_go, then no new.
751 * If GO, then no client.
752 * If client, then no GO.
753 */
754 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800755 if (go_wpa_s)
756 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800757 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800758 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
759 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800760
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800761 wpa_printf(MSG_DEBUG,
762 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
763 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800764
765 /* If not concurrent, restrict our choices */
766 if (p2p_no_group_iface) {
767 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
768
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800769 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800770 return P2PS_SETUP_NONE;
771
772 if (go_wpa_s) {
773 if (role == P2PS_SETUP_CLIENT ||
774 incoming == P2PS_SETUP_GROUP_OWNER ||
775 p2p_client_limit_reached(go_wpa_s->p2p_group))
776 return P2PS_SETUP_NONE;
777
778 return P2PS_SETUP_GROUP_OWNER;
779 }
780
781 if (persistent_go) {
782 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
783 if (!incoming)
784 return P2PS_SETUP_GROUP_OWNER |
785 P2PS_SETUP_CLIENT;
786 if (incoming == P2PS_SETUP_NEW) {
787 u8 r;
788
789 if (os_get_random(&r, sizeof(r)) < 0 ||
790 (r & 1))
791 return P2PS_SETUP_CLIENT;
792 return P2PS_SETUP_GROUP_OWNER;
793 }
794 }
795 }
796 }
797
798 /* If a required role has been specified, handle it here */
799 if (role && role != P2PS_SETUP_NEW) {
800 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
802 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
803 /*
804 * Peer has an active GO, so if the role allows it and
805 * we do not have any active roles, become client.
806 */
807 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
808 !cli_wpa_s)
809 return P2PS_SETUP_CLIENT;
810
811 /* fall through */
812
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 case P2PS_SETUP_NONE:
814 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 conncap = role;
816 goto grp_owner;
817
818 case P2PS_SETUP_GROUP_OWNER:
819 /*
820 * Must be a complimentary role - cannot be a client to
821 * more than one peer.
822 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800823 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800824 return P2PS_SETUP_NONE;
825
826 return P2PS_SETUP_CLIENT;
827
828 case P2PS_SETUP_CLIENT:
829 /* Must be a complimentary role */
830 if (incoming != role) {
831 conncap = P2PS_SETUP_GROUP_OWNER;
832 goto grp_owner;
833 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700834 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800835
836 default:
837 return P2PS_SETUP_NONE;
838 }
839 }
840
841 /*
842 * For now, we only will support ownership of one group, and being a
843 * client of one group. Therefore, if we have either an existing GO
844 * group, or an existing client group, we will not do a new GO
845 * negotiation, but rather try to re-use the existing groups.
846 */
847 switch (incoming) {
848 case P2PS_SETUP_NONE:
849 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800850 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800851 conncap = P2PS_SETUP_GROUP_OWNER;
852 else if (!owned_members)
853 conncap = P2PS_SETUP_NEW;
854 else if (incoming == P2PS_SETUP_NONE)
855 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
856 else
857 conncap = P2PS_SETUP_CLIENT;
858 break;
859
860 case P2PS_SETUP_CLIENT:
861 conncap = P2PS_SETUP_GROUP_OWNER;
862 break;
863
864 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800865 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800866 conncap = P2PS_SETUP_CLIENT;
867 break;
868
869 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
870 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800871 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800872 conncap = P2PS_SETUP_GROUP_OWNER;
873 else {
874 u8 r;
875
876 if (os_get_random(&r, sizeof(r)) < 0 ||
877 (r & 1))
878 conncap = P2PS_SETUP_CLIENT;
879 else
880 conncap = P2PS_SETUP_GROUP_OWNER;
881 }
882 break;
883
884 default:
885 return P2PS_SETUP_NONE;
886 }
887
888grp_owner:
889 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
890 (!incoming && (conncap & P2PS_SETUP_NEW))) {
891 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
892 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800893
894 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800895 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800896 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800897 wpa_s->p2p_mgmt ?
898 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800899 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800900 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800901 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800902 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800903 wpa_printf(MSG_ERROR,
904 "P2P: Failed to allocate a new interface for the group");
905 return P2PS_SETUP_NONE;
906 }
907 wpa_s->global->pending_group_iface_for_p2ps = 1;
908 p2p_set_intended_addr(wpa_s->global->p2p,
909 wpa_s->pending_interface_addr);
910 }
911 }
912
913 return conncap;
914}
915
916
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700917static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
918 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919{
920 struct wpa_ssid *ssid;
921 char *gtype;
922 const char *reason;
923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 ssid = wpa_s->current_ssid;
925 if (ssid == NULL) {
926 /*
927 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700928 * pending P2P group interface waiting for provisioning or a
929 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 */
931 ssid = wpa_s->conf->ssid;
932 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700933 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 break;
935 ssid = ssid->next;
936 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300937 if (ssid == NULL &&
938 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
939 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
941 "not found");
942 return -1;
943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 }
945 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
946 gtype = "GO";
947 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
948 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
949 wpa_s->reassociate = 0;
950 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 gtype = "client";
952 } else
953 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700954
955 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
956 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
957
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700959 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
961 wpa_s, NULL)) {
962 wpa_printf(MSG_DEBUG,
963 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
964 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
965 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
966 wpa_s, NULL);
967 }
968 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 if (wpa_s->cross_connect_in_use) {
971 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800972 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700973 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
974 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700976 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 case P2P_GROUP_REMOVAL_REQUESTED:
978 reason = " reason=REQUESTED";
979 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
981 reason = " reason=FORMATION_FAILED";
982 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
984 reason = " reason=IDLE";
985 break;
986 case P2P_GROUP_REMOVAL_UNAVAILABLE:
987 reason = " reason=UNAVAILABLE";
988 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
990 reason = " reason=GO_ENDING_SESSION";
991 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700992 case P2P_GROUP_REMOVAL_PSK_FAILURE:
993 reason = " reason=PSK_FAILURE";
994 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800995 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
996 reason = " reason=FREQ_CONFLICT";
997 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 default:
999 reason = "";
1000 break;
1001 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001002 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001003 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001004 P2P_EVENT_GROUP_REMOVED "%s %s%s",
1005 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001008 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
1009 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
1011 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001012 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001013 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001014 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
1015 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001016 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001018 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019
Dmitry Shmidt15907092014-03-25 10:42:57 -07001020 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001021 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001022 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1023 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001024
Dmitry Shmidt96571392013-10-14 12:54:46 -07001025 /*
1026 * Make sure wait for the first client does not remain active after the
1027 * group has been removed.
1028 */
1029 wpa_s->global->p2p_go_wait_client.sec = 0;
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1032 struct wpa_global *global;
1033 char *ifname;
1034 enum wpa_driver_if_type type;
1035 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1036 wpa_s->ifname);
1037 global = wpa_s->global;
1038 ifname = os_strdup(wpa_s->ifname);
1039 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001041 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s = global->ifaces;
1043 if (wpa_s && ifname)
1044 wpa_drv_if_remove(wpa_s, type, ifname);
1045 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001046 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 }
1048
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001049 /*
1050 * The primary interface was used for P2P group operations, so
1051 * need to reset its p2pdev.
1052 */
1053 wpa_s->p2pdev = wpa_s->parent;
1054
Dmitry Shmidt96571392013-10-14 12:54:46 -07001055 if (!wpa_s->p2p_go_group_formation_completed) {
1056 wpa_s->global->p2p_group_formation = NULL;
1057 wpa_s->p2p_in_provisioning = 0;
1058 }
1059
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001060 wpa_s->show_group_started = 0;
1061 os_free(wpa_s->go_params);
1062 wpa_s->go_params = NULL;
1063
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001064 os_free(wpa_s->p2p_group_common_freqs);
1065 wpa_s->p2p_group_common_freqs = NULL;
1066 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001067 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001068 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001069
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001070 wpa_s->waiting_presence_resp = 0;
1071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1073 if (ssid && (ssid->p2p_group ||
1074 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1075 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1076 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001077 if (ssid == wpa_s->current_ssid) {
1078 wpa_sm_set_config(wpa_s->wpa, NULL);
1079 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001081 }
1082 /*
1083 * Networks objects created during any P2P activities are not
1084 * exposed out as they might/will confuse certain non-P2P aware
1085 * applications since these network objects won't behave like
1086 * regular ones.
1087 *
1088 * Likewise, we don't send out network removed signals for such
1089 * network objects.
1090 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001091 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 wpa_config_remove_network(wpa_s->conf, id);
1093 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001094 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 } else {
1096 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1097 "found");
1098 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001099 if (wpa_s->ap_iface)
1100 wpa_supplicant_ap_deinit(wpa_s);
1101 else
1102 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001103
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001104 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1105
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001106 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107}
1108
1109
1110static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1111 u8 *go_dev_addr,
1112 const u8 *ssid, size_t ssid_len)
1113{
1114 struct wpa_bss *bss;
1115 const u8 *bssid;
1116 struct wpabuf *p2p;
1117 u8 group_capab;
1118 const u8 *addr;
1119
1120 if (wpa_s->go_params)
1121 bssid = wpa_s->go_params->peer_interface_addr;
1122 else
1123 bssid = wpa_s->bssid;
1124
1125 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001126 if (bss == NULL && wpa_s->go_params &&
1127 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1128 bss = wpa_bss_get_p2p_dev_addr(
1129 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 if (bss == NULL) {
1131 u8 iface_addr[ETH_ALEN];
1132 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1133 iface_addr) == 0)
1134 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1135 }
1136 if (bss == NULL) {
1137 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1138 "group is persistent - BSS " MACSTR " not found",
1139 MAC2STR(bssid));
1140 return 0;
1141 }
1142
1143 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001144 if (p2p == NULL)
1145 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1146 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 if (p2p == NULL) {
1148 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1149 "group is persistent - BSS " MACSTR
1150 " did not include P2P IE", MAC2STR(bssid));
1151 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001152 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001154 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 bss->beacon_ie_len);
1156 return 0;
1157 }
1158
1159 group_capab = p2p_get_group_capab(p2p);
1160 addr = p2p_get_go_dev_addr(p2p);
1161 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1162 "group_capab=0x%x", group_capab);
1163 if (addr) {
1164 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1165 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1166 MAC2STR(addr));
1167 } else
1168 os_memset(go_dev_addr, 0, ETH_ALEN);
1169 wpabuf_free(p2p);
1170
1171 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1172 "go_dev_addr=" MACSTR,
1173 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1174
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001175 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176}
1177
1178
Jouni Malinen75ecf522011-06-27 15:19:46 -07001179static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1180 struct wpa_ssid *ssid,
1181 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182{
1183 struct wpa_ssid *s;
1184 int changed = 0;
1185
1186 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1187 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1188 for (s = wpa_s->conf->ssid; s; s = s->next) {
1189 if (s->disabled == 2 &&
1190 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1191 s->ssid_len == ssid->ssid_len &&
1192 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1193 break;
1194 }
1195
1196 if (s) {
1197 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1198 "entry");
1199 if (ssid->passphrase && !s->passphrase)
1200 changed = 1;
1201 else if (ssid->passphrase && s->passphrase &&
1202 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1203 changed = 1;
1204 } else {
1205 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1206 "entry");
1207 changed = 1;
1208 s = wpa_config_add_network(wpa_s->conf);
1209 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001210 return -1;
1211
1212 /*
1213 * Instead of network_added we emit persistent_group_added
1214 * notification. Also to keep the defense checks in
1215 * persistent_group obj registration method, we set the
1216 * relevant flags in s to designate it as a persistent group.
1217 */
1218 s->p2p_group = 1;
1219 s->p2p_persistent_group = 1;
1220 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 wpa_config_set_network_defaults(s);
1222 }
1223
1224 s->p2p_group = 1;
1225 s->p2p_persistent_group = 1;
1226 s->disabled = 2;
1227 s->bssid_set = 1;
1228 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1229 s->mode = ssid->mode;
1230 s->auth_alg = WPA_AUTH_ALG_OPEN;
1231 s->key_mgmt = WPA_KEY_MGMT_PSK;
1232 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001233 s->pbss = ssid->pbss;
1234 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 s->export_keys = 1;
1236 if (ssid->passphrase) {
1237 os_free(s->passphrase);
1238 s->passphrase = os_strdup(ssid->passphrase);
1239 }
1240 if (ssid->psk_set) {
1241 s->psk_set = 1;
1242 os_memcpy(s->psk, ssid->psk, 32);
1243 }
1244 if (s->passphrase && !s->psk_set)
1245 wpa_config_update_psk(s);
1246 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1247 os_free(s->ssid);
1248 s->ssid = os_malloc(ssid->ssid_len);
1249 }
1250 if (s->ssid) {
1251 s->ssid_len = ssid->ssid_len;
1252 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1253 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001254 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1255 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1256 wpa_s->global->add_psk = NULL;
1257 changed = 1;
1258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 if (changed && wpa_s->conf->update_config &&
1261 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1262 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1263 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001264
1265 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266}
1267
1268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001269static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1270 const u8 *addr)
1271{
1272 struct wpa_ssid *ssid, *s;
1273 u8 *n;
1274 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001275 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001276 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001277
1278 ssid = wpa_s->current_ssid;
1279 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1280 !ssid->p2p_persistent_group)
1281 return;
1282
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001283 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001284 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1285 continue;
1286
1287 if (s->ssid_len == ssid->ssid_len &&
1288 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1289 break;
1290 }
1291
1292 if (s == NULL)
1293 return;
1294
1295 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001296 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001297 ETH_ALEN) != 0)
1298 continue;
1299
1300 if (i == s->num_p2p_clients - 1)
1301 return; /* already the most recent entry */
1302
1303 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001304 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1305 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1306 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001307 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001308 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1309 ETH_ALEN);
1310 os_memset(s->p2p_client_list +
1311 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1312 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001313 found = 1;
1314 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001315 }
1316
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001317 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1318 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001319 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001320 if (n == NULL)
1321 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001322 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1323 ETH_ALEN);
1324 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1325 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001326 s->p2p_client_list = n;
1327 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001328 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001329 /* Not enough room for an additional entry - drop the oldest
1330 * entry */
1331 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001332 s->p2p_client_list + 2 * ETH_ALEN,
1333 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001334 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001335 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001336 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001337 os_memset(s->p2p_client_list +
1338 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1339 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001340 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001341
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001342 if (p2p_wpa_s->conf->update_config &&
1343 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001344 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001345}
1346
1347
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001348static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1349 int go, struct wpa_ssid *ssid, int freq,
1350 const u8 *psk, const char *passphrase,
1351 const u8 *go_dev_addr, int persistent,
1352 const char *extra)
1353{
1354 const char *ssid_txt;
1355 char psk_txt[65];
1356
1357 if (psk)
1358 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1359 else
1360 psk_txt[0] = '\0';
1361
1362 if (ssid)
1363 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1364 else
1365 ssid_txt = "";
1366
1367 if (passphrase && passphrase[0] == '\0')
1368 passphrase = NULL;
1369
1370 /*
1371 * Include PSK/passphrase only in the control interface message and
1372 * leave it out from the debug log entry.
1373 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001374 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001375 P2P_EVENT_GROUP_STARTED
1376 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1377 MACSTR "%s%s",
1378 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1379 psk ? " psk=" : "", psk_txt,
1380 passphrase ? " passphrase=\"" : "",
1381 passphrase ? passphrase : "",
1382 passphrase ? "\"" : "",
1383 MAC2STR(go_dev_addr),
1384 persistent ? " [PERSISTENT]" : "", extra);
1385 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1386 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1387 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1388 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1389 extra);
1390}
1391
1392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001394 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395{
1396 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 int client;
1398 int persistent;
1399 u8 go_dev_addr[ETH_ALEN];
1400
1401 /*
1402 * This callback is likely called for the main interface. Update wpa_s
1403 * to use the group interface if a new interface was created for the
1404 * group.
1405 */
1406 if (wpa_s->global->p2p_group_formation)
1407 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001408 if (wpa_s->p2p_go_group_formation_completed) {
1409 wpa_s->global->p2p_group_formation = NULL;
1410 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001411 } else if (wpa_s->p2p_in_provisioning && !success) {
1412 wpa_msg(wpa_s, MSG_DEBUG,
1413 "P2P: Stop provisioning state due to failure");
1414 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001415 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001416 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001417 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001418 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419
1420 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001421 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001422 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001423 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001424 if (already_deleted)
1425 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001426 wpas_p2p_group_delete(wpa_s,
1427 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 return;
1429 }
1430
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001431 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001432 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433
1434 ssid = wpa_s->current_ssid;
1435 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1436 ssid->mode = WPAS_MODE_P2P_GO;
1437 p2p_group_notif_formation_done(wpa_s->p2p_group);
1438 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1439 }
1440
1441 persistent = 0;
1442 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 client = ssid->mode == WPAS_MODE_INFRA;
1444 if (ssid->mode == WPAS_MODE_P2P_GO) {
1445 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001446 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1447 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001448 } else {
1449 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 persistent = wpas_p2p_persistent_group(wpa_s,
1451 go_dev_addr,
1452 ssid->ssid,
1453 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 client = wpa_s->p2p_group_interface ==
1457 P2P_GROUP_INTERFACE_CLIENT;
1458 os_memset(go_dev_addr, 0, ETH_ALEN);
1459 }
1460
1461 wpa_s->show_group_started = 0;
1462 if (client) {
1463 /*
1464 * Indicate event only after successfully completed 4-way
1465 * handshake, i.e., when the interface is ready for data
1466 * packets.
1467 */
1468 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001469 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001470 wpas_p2p_group_started(wpa_s, 1, ssid,
1471 ssid ? ssid->frequency : 0,
1472 ssid && ssid->passphrase == NULL &&
1473 ssid->psk_set ? ssid->psk : NULL,
1474 ssid ? ssid->passphrase : NULL,
1475 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 wpas_p2p_cross_connect_setup(wpa_s);
1477 wpas_p2p_set_group_idle_timeout(wpa_s);
1478 }
1479
1480 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001481 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1482 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001483 else {
1484 os_free(wpa_s->global->add_psk);
1485 wpa_s->global->add_psk = NULL;
1486 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001487
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001488 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001489 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001490 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492}
1493
1494
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495struct send_action_work {
1496 unsigned int freq;
1497 u8 dst[ETH_ALEN];
1498 u8 src[ETH_ALEN];
1499 u8 bssid[ETH_ALEN];
1500 size_t len;
1501 unsigned int wait_time;
1502 u8 buf[0];
1503};
1504
1505
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001506static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1507{
1508 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1509
1510 wpa_printf(MSG_DEBUG,
1511 "P2P: Free Action frame radio work @%p (freq=%u dst="
1512 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1513 wpa_s->p2p_send_action_work, awork->freq,
1514 MAC2STR(awork->dst), MAC2STR(awork->src),
1515 MAC2STR(awork->bssid), awork->wait_time);
1516 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1517 awork->buf, awork->len);
1518 os_free(awork);
1519 wpa_s->p2p_send_action_work->ctx = NULL;
1520 radio_work_done(wpa_s->p2p_send_action_work);
1521 wpa_s->p2p_send_action_work = NULL;
1522}
1523
1524
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001525static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1526 void *timeout_ctx)
1527{
1528 struct wpa_supplicant *wpa_s = eloop_ctx;
1529
1530 if (!wpa_s->p2p_send_action_work)
1531 return;
1532
1533 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001534 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001535}
1536
1537
Dmitry Shmidt03658832014-08-13 11:03:49 -07001538static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001540 if (wpa_s->p2p_send_action_work) {
1541 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001543 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001544 wpa_printf(MSG_DEBUG,
1545 "P2P: Clear Action TX work @%p (wait_time=%u)",
1546 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001547 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001548 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001549 } else {
1550 /*
1551 * In theory, this should not be needed, but number of
1552 * places in the P2P code is still using non-zero wait
1553 * time for the last Action frame in the sequence and
1554 * some of these do not call send_action_done().
1555 */
1556 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1557 wpa_s, NULL);
1558 eloop_register_timeout(
1559 0, awork->wait_time * 1000,
1560 wpas_p2p_send_action_work_timeout,
1561 wpa_s, NULL);
1562 }
1563 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001564}
1565
1566
1567static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1568 unsigned int freq,
1569 const u8 *dst, const u8 *src,
1570 const u8 *bssid,
1571 const u8 *data, size_t data_len,
1572 enum offchannel_send_action_result
1573 result)
1574{
1575 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1576
1577 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1580 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001582 switch (result) {
1583 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1584 res = P2P_SEND_ACTION_SUCCESS;
1585 break;
1586 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1587 res = P2P_SEND_ACTION_NO_ACK;
1588 break;
1589 case OFFCHANNEL_SEND_ACTION_FAILED:
1590 res = P2P_SEND_ACTION_FAILED;
1591 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 }
1593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001594 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001596 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1597 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001598 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001599 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1600 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001602 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1603 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001604 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001605 P2P_EVENT_FALLBACK_TO_GO_NEG
1606 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001607 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1608 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 }
1610}
1611
1612
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1614{
1615 struct wpa_supplicant *wpa_s = work->wpa_s;
1616 struct send_action_work *awork = work->ctx;
1617
1618 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001619 if (work->started) {
1620 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1621 wpa_s, NULL);
1622 wpa_s->p2p_send_action_work = NULL;
1623 offchannel_send_action_done(wpa_s);
1624 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001625 os_free(awork);
1626 return;
1627 }
1628
1629 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1630 awork->bssid, awork->buf, awork->len,
1631 awork->wait_time,
1632 wpas_p2p_send_action_tx_status, 1) < 0) {
1633 os_free(awork);
1634 radio_work_done(work);
1635 return;
1636 }
1637 wpa_s->p2p_send_action_work = work;
1638}
1639
1640
1641static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1642 unsigned int freq, const u8 *dst,
1643 const u8 *src, const u8 *bssid, const u8 *buf,
1644 size_t len, unsigned int wait_time)
1645{
1646 struct send_action_work *awork;
1647
Hai Shalom81f62d82019-07-22 12:10:00 -07001648 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001649 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1650 return -1;
1651 }
1652
1653 awork = os_zalloc(sizeof(*awork) + len);
1654 if (awork == NULL)
1655 return -1;
1656
1657 awork->freq = freq;
1658 os_memcpy(awork->dst, dst, ETH_ALEN);
1659 os_memcpy(awork->src, src, ETH_ALEN);
1660 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1661 awork->len = len;
1662 awork->wait_time = wait_time;
1663 os_memcpy(awork->buf, buf, len);
1664
Hai Shalom81f62d82019-07-22 12:10:00 -07001665 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001666 wpas_send_action_cb, awork) < 0) {
1667 os_free(awork);
1668 return -1;
1669 }
1670
1671 return 0;
1672}
1673
1674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1676 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001677 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678{
1679 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001680 int listen_freq = -1, send_freq = -1;
1681
Hai Shalom021b0b52019-04-10 11:17:58 -07001682 if (scheduled)
1683 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001684 if (wpa_s->p2p_listen_work)
1685 listen_freq = wpa_s->p2p_listen_work->freq;
1686 if (wpa_s->p2p_send_action_work)
1687 send_freq = wpa_s->p2p_send_action_work->freq;
1688 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001689 int res;
1690
1691 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1692 listen_freq, send_freq, freq);
1693 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1694 len, wait_time);
1695 if (res == 0 && scheduled)
1696 *scheduled = 1;
1697 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001698 }
1699
1700 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1702 wait_time,
1703 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704}
1705
1706
1707static void wpas_send_action_done(void *ctx)
1708{
1709 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001710
1711 if (wpa_s->p2p_send_action_work) {
1712 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1713 wpa_s, NULL);
1714 os_free(wpa_s->p2p_send_action_work->ctx);
1715 radio_work_done(wpa_s->p2p_send_action_work);
1716 wpa_s->p2p_send_action_work = NULL;
1717 }
1718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720}
1721
1722
1723static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1724 struct p2p_go_neg_results *params)
1725{
1726 if (wpa_s->go_params == NULL) {
1727 wpa_s->go_params = os_malloc(sizeof(*params));
1728 if (wpa_s->go_params == NULL)
1729 return -1;
1730 }
1731 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1732 return 0;
1733}
1734
1735
1736static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1737 struct p2p_go_neg_results *res)
1738{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001739 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001740 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1741 " dev_addr " MACSTR " wps_method %d",
1742 MAC2STR(res->peer_interface_addr),
1743 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1745 res->ssid, res->ssid_len);
1746 wpa_supplicant_ap_deinit(wpa_s);
1747 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001748 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001749 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001750#ifdef CONFIG_WPS_NFC
1751 } else if (res->wps_method == WPS_NFC) {
1752 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1753 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001754 wpa_s->p2pdev->p2p_oob_dev_pw,
1755 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1756 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001757 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001758 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001759 NULL,
1760 NULL, 0, 0);
1761#endif /* CONFIG_WPS_NFC */
1762 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001764 if (wpa_s->p2p_wps_method == WPS_P2PS)
1765 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1767 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1768 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1769 wpa_s->p2p_pin, 1, dev_pw_id);
1770 }
1771}
1772
1773
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001774static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1775 struct wpa_ssid *ssid)
1776{
1777 struct wpa_ssid *persistent;
1778 struct psk_list_entry *psk;
1779 struct hostapd_data *hapd;
1780
1781 if (!wpa_s->ap_iface)
1782 return;
1783
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001784 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001785 ssid->ssid_len);
1786 if (persistent == NULL)
1787 return;
1788
1789 hapd = wpa_s->ap_iface->bss[0];
1790
1791 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1792 list) {
1793 struct hostapd_wpa_psk *hpsk;
1794
1795 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1796 MACSTR " psk=%d",
1797 MAC2STR(psk->addr), psk->p2p);
1798 hpsk = os_zalloc(sizeof(*hpsk));
1799 if (hpsk == NULL)
1800 break;
1801 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1802 if (psk->p2p)
1803 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1804 else
1805 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1806 hpsk->next = hapd->conf->ssid.wpa_psk;
1807 hapd->conf->ssid.wpa_psk = hpsk;
1808 }
1809}
1810
1811
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001812static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1813{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001814 char buf[20 + P2P_MAX_CHANNELS * 6];
1815 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001816 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001817 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001818
Roshan Pius3a1667e2018-07-03 15:17:14 -07001819 pos = buf;
1820 end = pos + sizeof(buf);
1821 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1822 res = os_snprintf(pos, end - pos, " %d",
1823 wpa_s->p2p_group_common_freqs[i]);
1824 if (os_snprintf_error(end - pos, res))
1825 break;
1826 pos += res;
1827 }
1828 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001829
Roshan Pius3a1667e2018-07-03 15:17:14 -07001830 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831}
1832
1833
1834static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1835 struct p2p_go_neg_results *params)
1836{
1837 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1838
1839 wpa_s->p2p_group_common_freqs_num = 0;
1840 os_free(wpa_s->p2p_group_common_freqs);
1841 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1842 if (!wpa_s->p2p_group_common_freqs)
1843 return;
1844
1845 for (i = 0; i < len; i++) {
1846 if (!wpa_s->go_params->freq_list[i])
1847 break;
1848 wpa_s->p2p_group_common_freqs[i] =
1849 wpa_s->go_params->freq_list[i];
1850 }
1851 wpa_s->p2p_group_common_freqs_num = i;
1852}
1853
1854
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001855static void p2p_config_write(struct wpa_supplicant *wpa_s)
1856{
1857#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001858 if (wpa_s->p2pdev->conf->update_config &&
1859 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001860 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1861#endif /* CONFIG_NO_CONFIG_WRITE */
1862}
1863
1864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865static void p2p_go_configured(void *ctx, void *data)
1866{
1867 struct wpa_supplicant *wpa_s = ctx;
1868 struct p2p_go_neg_results *params = data;
1869 struct wpa_ssid *ssid;
1870
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001871 wpa_s->ap_configured_cb = NULL;
1872 wpa_s->ap_configured_cb_ctx = NULL;
1873 wpa_s->ap_configured_cb_data = NULL;
1874 if (!wpa_s->go_params) {
1875 wpa_printf(MSG_ERROR,
1876 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1877 return;
1878 }
1879
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 p2p_go_save_group_common_freqs(wpa_s, params);
1881 p2p_go_dump_common_freqs(wpa_s);
1882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 ssid = wpa_s->current_ssid;
1884 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1885 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1886 if (wpa_s->global->p2p_group_formation == wpa_s)
1887 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001888 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1889 params->passphrase[0] == '\0' ?
1890 params->psk : NULL,
1891 params->passphrase,
1892 wpa_s->global->p2p_dev_addr,
1893 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001894 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001896 if (wpa_s->p2pdev->p2ps_method_config_any) {
1897 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001898 wpa_dbg(wpa_s, MSG_DEBUG,
1899 "P2PS: Setting default PIN for ANY");
1900 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1901 "12345670", NULL, 0,
1902 0);
1903 } else {
1904 wpa_dbg(wpa_s, MSG_DEBUG,
1905 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001906 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001907 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001908 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001909 "12345670", NULL, 0, 0);
1910 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001911 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001912 }
1913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001914 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001915 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001916 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001917 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001918 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001919 wpas_p2p_add_psk_list(wpa_s, ssid);
1920 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001921
1922 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001923 params->persistent_group, 0,
1924 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 wpas_p2p_cross_connect_setup(wpa_s);
1926 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001927
1928 if (wpa_s->p2p_first_connection_timeout) {
1929 wpa_dbg(wpa_s, MSG_DEBUG,
1930 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1931 wpa_s->p2p_first_connection_timeout);
1932 wpa_s->p2p_go_group_formation_completed = 0;
1933 wpa_s->global->p2p_group_formation = wpa_s;
1934 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001935 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001936 eloop_register_timeout(
1937 wpa_s->p2p_first_connection_timeout, 0,
1938 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001939 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001940 }
1941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 return;
1943 }
1944
1945 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1946 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1947 params->peer_interface_addr)) {
1948 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1949 "filtering");
1950 return;
1951 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001952 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001954 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001955#ifdef CONFIG_WPS_NFC
1956 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001957 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001958 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001959 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001960 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1961 return;
1962 }
1963 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001964 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1965 wpa_s->p2pdev->p2p_oob_dev_pw,
1966 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1967 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001968#endif /* CONFIG_WPS_NFC */
1969 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001971 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 os_free(wpa_s->go_params);
1973 wpa_s->go_params = NULL;
1974}
1975
1976
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001977/**
1978 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1979 * @freq: Frequency (MHz) to convert
1980 * @op_class: Buffer for returning operating class
1981 * @op_edmg_channel: Buffer for returning channel number
1982 * Returns: 0 on success, -1 on failure
1983 *
1984 * This can be used to find the highest channel bonding which includes the
1985 * specified frequency.
1986 */
1987static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1988 unsigned int freq,
1989 u8 *op_class, u8 *op_edmg_channel)
1990{
1991 struct hostapd_hw_modes *hwmode;
1992 struct ieee80211_edmg_config edmg;
1993 unsigned int i;
1994 enum chan_width chanwidth[] = {
1995 CHAN_WIDTH_8640,
1996 CHAN_WIDTH_6480,
1997 CHAN_WIDTH_4320,
1998 };
1999
2000 if (!wpa_s->hw.modes)
2001 return -1;
2002
2003 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08002004 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002005 if (!hwmode) {
2006 wpa_printf(MSG_ERROR,
2007 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2008 return -1;
2009 }
2010
2011 /* Find the highest EDMG channel bandwidth to start the P2P GO */
2012 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2013 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2014 op_class,
2015 op_edmg_channel) < 0)
2016 continue;
2017
2018 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2019 if (edmg.channels &&
2020 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2021 wpa_printf(MSG_DEBUG,
2022 "Freq %u to EDMG channel %u at opclass %u",
2023 freq, *op_edmg_channel, *op_class);
2024 return 0;
2025 }
2026 }
2027
2028 return -1;
2029}
2030
2031
2032int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2033 struct p2p_go_neg_results *params)
2034{
2035 u8 op_channel, op_class;
2036 int freq;
2037
2038 /* Try social channel as primary channel frequency */
2039 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2040
2041 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2042 &op_channel) == 0) {
2043 wpa_printf(MSG_DEBUG,
2044 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2045 freq, op_channel, op_class);
2046 params->freq = freq;
2047 return 0;
2048 }
2049
2050 return -1;
2051}
2052
2053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2055 struct p2p_go_neg_results *params,
2056 int group_formation)
2057{
2058 struct wpa_ssid *ssid;
2059
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002060 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2061 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2062 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2063 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002065 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066
2067 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002068 if (ssid == NULL) {
2069 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002074 wpa_s->p2p_go_group_formation_completed = 0;
2075 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002076 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 wpa_config_set_network_defaults(ssid);
2079 ssid->temporary = 1;
2080 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002081 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2083 WPAS_MODE_P2P_GO;
2084 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002085 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002086 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002087 ssid->max_oper_chwidth = params->max_oper_chwidth;
2088 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002089 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002090 if (params->edmg) {
2091 u8 op_channel, op_class;
2092
2093 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2094 &op_class, &op_channel)) {
2095 ssid->edmg_channel = op_channel;
2096 ssid->enable_edmg = params->edmg;
2097 } else {
2098 wpa_dbg(wpa_s, MSG_DEBUG,
2099 "P2P: Could not match EDMG channel, freq %d, for GO",
2100 params->freq);
2101 }
2102 }
2103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 ssid->ssid = os_zalloc(params->ssid_len + 1);
2105 if (ssid->ssid) {
2106 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2107 ssid->ssid_len = params->ssid_len;
2108 }
2109 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2110 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002111 if (is_6ghz_freq(ssid->frequency) &&
2112 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2113 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2114 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2115 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002116 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
Hai Shalomc1a21442022-02-04 13:43:00 -08002117 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2118 } else {
2119 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121 ssid->proto = WPA_PROTO_RSN;
2122 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002123 ssid->group_cipher = WPA_CIPHER_CCMP;
2124 if (params->freq > 56160) {
2125 /*
2126 * Enable GCMP instead of CCMP as pairwise_cipher and
2127 * group_cipher in 60 GHz.
2128 */
2129 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2130 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002131 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2132 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002133 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002134 if (os_strlen(params->passphrase) > 0) {
2135 ssid->passphrase = os_strdup(params->passphrase);
2136 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002137 wpa_msg_global(wpa_s, MSG_ERROR,
2138 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002139 wpa_config_remove_network(wpa_s->conf, ssid->id);
2140 return;
2141 }
2142 } else
2143 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002144 ssid->psk_set = params->psk_set;
2145 if (ssid->psk_set)
2146 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002147 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002148 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002149 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150
2151 wpa_s->ap_configured_cb = p2p_go_configured;
2152 wpa_s->ap_configured_cb_ctx = wpa_s;
2153 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002154 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002155 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 wpa_s->reassociate = 1;
2157 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002158 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2159 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 wpa_supplicant_req_scan(wpa_s, 0, 0);
2161}
2162
2163
2164static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2165 const struct wpa_supplicant *src)
2166{
2167 struct wpa_config *d;
2168 const struct wpa_config *s;
2169
2170 d = dst->conf;
2171 s = src->conf;
2172
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002173#define C(n) \
2174do { \
2175 if (s->n && !d->n) \
2176 d->n = os_strdup(s->n); \
2177} while (0)
2178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 C(device_name);
2180 C(manufacturer);
2181 C(model_name);
2182 C(model_number);
2183 C(serial_number);
2184 C(config_methods);
2185#undef C
2186
2187 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2188 os_memcpy(d->sec_device_type, s->sec_device_type,
2189 sizeof(d->sec_device_type));
2190 d->num_sec_device_types = s->num_sec_device_types;
2191
2192 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002193 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002195 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002196 d->max_num_sta = s->max_num_sta;
2197 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002198 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002199 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002200 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002201 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002202 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002203 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002204 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002205 d->pmf = s->pmf;
2206 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002207 d->sae_pwe = s->sae_pwe;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002208
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002209 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2210 !d->wps_nfc_pw_from_config) {
2211 wpabuf_free(d->wps_nfc_dh_privkey);
2212 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002213 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2214 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2215 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002216 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002217 d->go_interworking = s->go_interworking;
2218 d->go_access_network_type = s->go_access_network_type;
2219 d->go_internet = s->go_internet;
2220 d->go_venue_group = s->go_venue_group;
2221 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002222 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223}
2224
2225
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002226static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2227 char *ifname, size_t len)
2228{
2229 char *ifname_ptr = wpa_s->ifname;
2230
2231 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2232 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2233 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2234 }
2235
2236 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2237 if (os_strlen(ifname) >= IFNAMSIZ &&
2238 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002239 int res;
2240
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002241 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002242 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2243 if (os_snprintf_error(len, res) && len)
2244 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002245 }
2246}
2247
2248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2250 enum wpa_driver_if_type type)
2251{
2252 char ifname[120], force_ifname[120];
2253
2254 if (wpa_s->pending_interface_name[0]) {
2255 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2256 "- skip creation of a new one");
2257 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2258 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2259 "unknown?! ifname='%s'",
2260 wpa_s->pending_interface_name);
2261 return -1;
2262 }
2263 return 0;
2264 }
2265
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002266 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267 force_ifname[0] = '\0';
2268
2269 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2270 ifname);
2271 wpa_s->p2p_group_idx++;
2272
2273 wpa_s->pending_interface_type = type;
2274 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2275 wpa_s->pending_interface_addr, NULL) < 0) {
2276 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2277 "interface");
2278 return -1;
2279 }
2280
Jimmy Chen36c21992018-11-29 16:46:43 +08002281 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002282 random_mac_addr(wpa_s->pending_interface_addr);
2283 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2284 " for the group",
2285 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002286 }
2287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 if (force_ifname[0]) {
2289 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2290 force_ifname);
2291 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2292 sizeof(wpa_s->pending_interface_name));
2293 } else
2294 os_strlcpy(wpa_s->pending_interface_name, ifname,
2295 sizeof(wpa_s->pending_interface_name));
2296 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2297 MACSTR, wpa_s->pending_interface_name,
2298 MAC2STR(wpa_s->pending_interface_addr));
2299
2300 return 0;
2301}
2302
2303
2304static void wpas_p2p_remove_pending_group_interface(
2305 struct wpa_supplicant *wpa_s)
2306{
2307 if (!wpa_s->pending_interface_name[0] ||
2308 is_zero_ether_addr(wpa_s->pending_interface_addr))
2309 return; /* No pending virtual interface */
2310
2311 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2312 wpa_s->pending_interface_name);
2313 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2314 wpa_s->pending_interface_name);
2315 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2316 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002317 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318}
2319
2320
2321static struct wpa_supplicant *
2322wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2323{
2324 struct wpa_interface iface;
2325 struct wpa_supplicant *group_wpa_s;
2326
2327 if (!wpa_s->pending_interface_name[0]) {
2328 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2329 if (!wpas_p2p_create_iface(wpa_s))
2330 return NULL;
2331 /*
2332 * Something has forced us to remove the pending interface; try
2333 * to create a new one and hope for the best that we will get
2334 * the same local address.
2335 */
2336 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2337 WPA_IF_P2P_CLIENT) < 0)
2338 return NULL;
2339 }
2340
2341 os_memset(&iface, 0, sizeof(iface));
2342 iface.ifname = wpa_s->pending_interface_name;
2343 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002344 if (wpa_s->conf->ctrl_interface == NULL &&
2345 wpa_s->parent != wpa_s &&
2346 wpa_s->p2p_mgmt &&
2347 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2348 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2349 else
2350 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002352 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 if (group_wpa_s == NULL) {
2354 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2355 "wpa_supplicant interface");
2356 return NULL;
2357 }
2358 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2360 P2P_GROUP_INTERFACE_CLIENT;
2361 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002362 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363
2364 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2365
Jimmy Chen36c21992018-11-29 16:46:43 +08002366 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002367 if (wpa_drv_set_mac_addr(group_wpa_s,
2368 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002369 wpa_msg(group_wpa_s, MSG_INFO,
2370 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002371 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2372 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002373 return NULL;
2374 }
2375
2376 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2377 wpa_msg(group_wpa_s, MSG_INFO,
2378 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002379 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2380 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002381 return NULL;
2382 }
2383
Hai Shalom74f70d42019-02-11 14:42:39 -08002384 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2385 " for the group",
2386 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002387 }
2388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 return group_wpa_s;
2390}
2391
2392
2393static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2394 void *timeout_ctx)
2395{
2396 struct wpa_supplicant *wpa_s = eloop_ctx;
2397 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002398 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002399}
2400
2401
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002402static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2403 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002404{
2405 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002406 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407 if (wpa_s->global->p2p)
2408 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002409 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410}
2411
2412
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002413static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2414{
2415 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2416 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002417 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002418 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002419 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002420 wpa_s->global->p2p_fail_on_wps_complete = 0;
2421}
2422
2423
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002424void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2425{
2426 if (wpa_s->global->p2p_group_formation != wpa_s)
2427 return;
2428 /* Speed up group formation timeout since this cannot succeed */
2429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002430 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002431 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002432 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002433}
2434
2435
Matthew Wang06b42472022-11-10 06:56:31 +00002436bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2437{
2438 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002439 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
Matthew Wang06b42472022-11-10 06:56:31 +00002440 return false;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002441
Matthew Wang06b42472022-11-10 06:56:31 +00002442 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2443 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2444 wpa_s->p2pdev, NULL);
2445 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2446 wpa_s->p2pdev, NULL);
2447 return true;
2448}
2449
2450
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002451static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452{
2453 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002454 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
2456 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2457 wpa_drv_cancel_remain_on_channel(wpa_s);
2458 wpa_s->off_channel_freq = 0;
2459 wpa_s->roc_waiting_drv_freq = 0;
2460 }
2461
2462 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002463 wpa_msg_global(wpa_s, MSG_INFO,
2464 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2465 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002466 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 wpas_p2p_remove_pending_group_interface(wpa_s);
2468 return;
2469 }
2470
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002471 if (!res->role_go) {
2472 /* Inform driver of the operating channel of GO. */
2473 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2474 }
2475
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002476 if (wpa_s->p2p_go_ht40)
2477 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002478 if (wpa_s->p2p_go_vht)
2479 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002480 if (wpa_s->p2p_go_he)
2481 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002482 if (wpa_s->p2p_go_edmg)
2483 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002484 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2485 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002486
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002487 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2488 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2489 " wps_method=%s",
2490 res->role_go ? "GO" : "client", res->freq, res->ht40,
2491 MAC2STR(res->peer_device_addr),
2492 MAC2STR(res->peer_interface_addr),
2493 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002494 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495
Dmitry Shmidt04949592012-07-19 12:16:46 -07002496 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2497 struct wpa_ssid *ssid;
2498 ssid = wpa_config_get_network(wpa_s->conf,
2499 wpa_s->p2p_persistent_id);
2500 if (ssid && ssid->disabled == 2 &&
2501 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2502 size_t len = os_strlen(ssid->passphrase);
2503 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2504 "on requested persistent group");
2505 os_memcpy(res->passphrase, ssid->passphrase, len);
2506 res->passphrase[len] = '\0';
2507 }
2508 }
2509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002511 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2513 if (group_wpa_s == NULL) {
2514 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002515 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2516 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002517 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 return;
2519 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2521 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002523 group_wpa_s = wpa_s->parent;
2524 wpa_s->global->p2p_group_formation = group_wpa_s;
2525 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002526 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002529 group_wpa_s->p2p_in_provisioning = 1;
2530 group_wpa_s->p2pdev = wpa_s;
2531 if (group_wpa_s != wpa_s) {
2532 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2533 sizeof(group_wpa_s->p2p_pin));
2534 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2535 }
2536 if (res->role_go) {
2537 wpas_start_wps_go(group_wpa_s, res, 1);
2538 } else {
2539 os_get_reltime(&group_wpa_s->scan_min_time);
2540 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 }
2542
Hai Shalomfdcde762020-04-02 11:19:20 -07002543 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2545
2546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2547 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2548 (res->peer_config_timeout % 100) * 10000,
2549 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2550}
2551
2552
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002553static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2554 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555{
2556 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002557 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002558 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2559 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002561 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562}
2563
2564
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002565static void wpas_dev_found(void *ctx, const u8 *addr,
2566 const struct p2p_peer_info *info,
2567 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002569 u8 *wfd_dev_info = NULL;
2570 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002571 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002572 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002573#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 struct wpa_supplicant *wpa_s = ctx;
2575 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002576 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002577 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002578
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002579#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002580 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2581 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002582 if (wfd_dev_info_hex) {
2583 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2584 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2585 // Only used for notification, so not handling error.
2586 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2587 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002588
2589 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2590 WFD_SUBELEM_R2_DEVICE_INFO);
2591 if (wfd_r2_dev_info_hex) {
2592 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2593 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2594 // Only used for notification, so not handling error.
2595 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2596 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002597#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002598
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002599 if (info->p2ps_instance) {
2600 char str[256];
2601 const u8 *buf = wpabuf_head(info->p2ps_instance);
2602 size_t len = wpabuf_len(info->p2ps_instance);
2603
2604 while (len) {
2605 u32 id;
2606 u16 methods;
2607 u8 str_len;
2608
2609 if (len < 4 + 2 + 1)
2610 break;
2611 id = WPA_GET_LE32(buf);
2612 buf += sizeof(u32);
2613 methods = WPA_GET_BE16(buf);
2614 buf += sizeof(u16);
2615 str_len = *buf++;
2616 if (str_len > len - 4 - 2 - 1)
2617 break;
2618 os_memcpy(str, buf, str_len);
2619 str[str_len] = '\0';
2620 buf += str_len;
2621 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2622
2623 wpa_msg_global(wpa_s, MSG_INFO,
2624 P2P_EVENT_DEVICE_FOUND MACSTR
2625 " p2p_dev_addr=" MACSTR
2626 " pri_dev_type=%s name='%s'"
2627 " config_methods=0x%x"
2628 " dev_capab=0x%x"
2629 " group_capab=0x%x"
2630 " adv_id=%x asp_svc=%s%s",
2631 MAC2STR(addr),
2632 MAC2STR(info->p2p_device_addr),
2633 wps_dev_type_bin2str(
2634 info->pri_dev_type,
2635 devtype, sizeof(devtype)),
2636 info->device_name, methods,
2637 info->dev_capab, info->group_capab,
2638 id, str,
2639 info->vendor_elems ?
2640 " vendor_elems=1" : "");
2641 }
2642 goto done;
2643 }
2644
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002645 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2646 " p2p_dev_addr=" MACSTR
2647 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002648 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002649 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2650 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2651 sizeof(devtype)),
2652 info->device_name, info->config_methods,
2653 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002654 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002655 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002656 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2657 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002658 info->vendor_elems ? " vendor_elems=1" : "",
2659 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002661done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002662 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002663 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002664#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002665
Roshan Piusfd2fd662017-01-23 13:41:57 -08002666 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002667 wfd_dev_info_len, wfd_r2_dev_info,
2668 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002669 os_free(wfd_dev_info);
jiangpingpinga96a5e82023-11-16 15:02:17 +08002670 os_free(wfd_r2_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671}
2672
2673
2674static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2675{
2676 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002677
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002678 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2679 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2682}
2683
2684
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002685static void wpas_find_stopped(void *ctx)
2686{
2687 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002688
2689 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2690 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2691
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002692 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002693 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002694}
2695
2696
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002697struct wpas_p2p_listen_work {
2698 unsigned int freq;
2699 unsigned int duration;
2700 struct wpabuf *probe_resp_ie;
2701};
2702
2703
2704static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2705{
2706 if (lwork == NULL)
2707 return;
2708 wpabuf_free(lwork->probe_resp_ie);
2709 os_free(lwork);
2710}
2711
2712
2713static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2714{
2715 struct wpas_p2p_listen_work *lwork;
2716
2717 if (!wpa_s->p2p_listen_work)
2718 return;
2719
2720 lwork = wpa_s->p2p_listen_work->ctx;
2721 wpas_p2p_listen_work_free(lwork);
2722 radio_work_done(wpa_s->p2p_listen_work);
2723 wpa_s->p2p_listen_work = NULL;
2724}
2725
2726
2727static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2728{
2729 struct wpa_supplicant *wpa_s = work->wpa_s;
2730 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002731 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002732
2733 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002734 if (work->started) {
2735 wpa_s->p2p_listen_work = NULL;
2736 wpas_stop_listen(wpa_s);
2737 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002738 wpas_p2p_listen_work_free(lwork);
2739 return;
2740 }
2741
2742 wpa_s->p2p_listen_work = work;
2743
2744 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2745
2746 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2747 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2748 "report received Probe Request frames");
2749 wpas_p2p_listen_work_done(wpa_s);
2750 return;
2751 }
2752
2753 wpa_s->pending_listen_freq = lwork->freq;
2754 wpa_s->pending_listen_duration = lwork->duration;
2755
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002756 duration = lwork->duration;
2757#ifdef CONFIG_TESTING_OPTIONS
2758 if (wpa_s->extra_roc_dur) {
2759 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2760 duration, duration + wpa_s->extra_roc_dur);
2761 duration += wpa_s->extra_roc_dur;
2762 }
2763#endif /* CONFIG_TESTING_OPTIONS */
2764
2765 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002766 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2767 "to remain on channel (%u MHz) for Listen "
2768 "state", lwork->freq);
2769 wpas_p2p_listen_work_done(wpa_s);
2770 wpa_s->pending_listen_freq = 0;
2771 return;
2772 }
2773 wpa_s->off_channel_freq = 0;
2774 wpa_s->roc_waiting_drv_freq = lwork->freq;
2775}
2776
2777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778static int wpas_start_listen(void *ctx, unsigned int freq,
2779 unsigned int duration,
2780 const struct wpabuf *probe_resp_ie)
2781{
2782 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002783 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002785 if (wpa_s->p2p_listen_work) {
2786 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787 return -1;
2788 }
2789
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002790 lwork = os_zalloc(sizeof(*lwork));
2791 if (lwork == NULL)
2792 return -1;
2793 lwork->freq = freq;
2794 lwork->duration = duration;
2795 if (probe_resp_ie) {
2796 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2797 if (lwork->probe_resp_ie == NULL) {
2798 wpas_p2p_listen_work_free(lwork);
2799 return -1;
2800 }
2801 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002802
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002803 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2804 lwork) < 0) {
2805 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 return -1;
2807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808
2809 return 0;
2810}
2811
2812
2813static void wpas_stop_listen(void *ctx)
2814{
2815 struct wpa_supplicant *wpa_s = ctx;
2816 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2817 wpa_drv_cancel_remain_on_channel(wpa_s);
2818 wpa_s->off_channel_freq = 0;
2819 wpa_s->roc_waiting_drv_freq = 0;
2820 }
2821 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002822
2823 /*
2824 * Don't cancel Probe Request RX reporting for a connected P2P Client
2825 * handling Probe Request frames.
2826 */
2827 if (!wpa_s->p2p_cli_probe)
2828 wpa_drv_probe_req_report(wpa_s, 0);
2829
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002830 wpas_p2p_listen_work_done(wpa_s);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002831
2832 if (radio_work_pending(wpa_s, "p2p-listen")) {
2833 wpa_printf(MSG_DEBUG,
2834 "P2P: p2p-listen is still pending - remove it");
2835 radio_remove_works(wpa_s, "p2p-listen", 0);
2836 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837}
2838
2839
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002840static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2841 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842{
2843 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002844 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002845 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846}
2847
2848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2850 const u8 *peer, const char *params,
2851 unsigned int generated_pin)
2852{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002853 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2854 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855}
2856
2857
2858static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2859 const u8 *peer, const char *params)
2860{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002861 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2862 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863}
2864
2865
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002866static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2867 const u8 *dev_addr, const u8 *pri_dev_type,
2868 const char *dev_name, u16 supp_config_methods,
2869 u8 dev_capab, u8 group_capab, const u8 *group_id,
2870 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871{
2872 struct wpa_supplicant *wpa_s = ctx;
2873 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002874 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 u8 empty_dev_type[8];
2876 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002878 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002879
2880 if (group_id) {
2881 for (group = wpa_s->global->ifaces; group; group = group->next)
2882 {
2883 struct wpa_ssid *s = group->current_ssid;
2884 if (s != NULL &&
2885 s->mode == WPAS_MODE_P2P_GO &&
2886 group_id_len - ETH_ALEN == s->ssid_len &&
2887 os_memcmp(group_id + ETH_ALEN, s->ssid,
2888 s->ssid_len) == 0)
2889 break;
2890 }
2891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892
2893 if (pri_dev_type == NULL) {
2894 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2895 pri_dev_type = empty_dev_type;
2896 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002897 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2898 " pri_dev_type=%s name='%s' config_methods=0x%x "
2899 "dev_capab=0x%x group_capab=0x%x%s%s",
2900 MAC2STR(dev_addr),
2901 wps_dev_type_bin2str(pri_dev_type, devtype,
2902 sizeof(devtype)),
2903 dev_name, supp_config_methods, dev_capab, group_capab,
2904 group ? " group=" : "",
2905 group ? group->ifname : "");
2906 if (os_snprintf_error(sizeof(params), res))
2907 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002911 if (wps_generate_pin(&generated_pin) < 0) {
2912 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2913 wpas_notify_p2p_provision_discovery(
2914 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002915 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002916 return;
2917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 wpas_prov_disc_local_display(wpa_s, peer, params,
2919 generated_pin);
2920 } else if (config_methods & WPS_CONFIG_KEYPAD)
2921 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2922 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002923 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2924 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002925
2926 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2927 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002928 config_methods, generated_pin,
2929 group ? group->ifname : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930}
2931
2932
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002933static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934{
2935 struct wpa_supplicant *wpa_s = ctx;
2936 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002937 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002939 if (wpa_s->pending_pd_before_join &&
2940 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2941 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2942 wpa_s->pending_pd_before_join = 0;
2943 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2944 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002945 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002946 return;
2947 }
2948
Dmitry Shmidt04949592012-07-19 12:16:46 -07002949 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002950 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2951 int res;
2952
2953 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2954 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2955 if (os_snprintf_error(sizeof(params), res))
2956 params[sizeof(params) - 1] = '\0';
2957 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002958 params[0] = '\0';
2959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002961 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002963 if (wps_generate_pin(&generated_pin) < 0) {
2964 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2965 wpas_notify_p2p_provision_discovery(
2966 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002967 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002968 return;
2969 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002970 wpas_prov_disc_local_display(wpa_s, peer, params,
2971 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002973 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2974 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975
Jouni Malinen75ecf522011-06-27 15:19:46 -07002976 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2977 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002978 config_methods, generated_pin,
2979 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980}
2981
2982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002983static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002984 enum p2p_prov_disc_status status,
2985 u32 adv_id, const u8 *adv_mac,
2986 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002987{
2988 struct wpa_supplicant *wpa_s = ctx;
2989
Dmitry Shmidt04949592012-07-19 12:16:46 -07002990 if (wpa_s->p2p_fallback_to_go_neg) {
2991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2992 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002993 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002994 P2P_EVENT_FALLBACK_TO_GO_NEG
2995 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002996 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2997 return;
2998 }
2999
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003000 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003001 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003002 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3003 "join-existing-group operation (no ACK for PD "
3004 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003005 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003006 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003007 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003008
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003009 if (adv_id && adv_mac && deferred_session_resp) {
3010 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3011 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3012 " deferred_session_resp='%s'",
3013 MAC2STR(peer), status, adv_id,
3014 deferred_session_resp);
3015 } else if (adv_id && adv_mac) {
3016 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3017 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3018 MAC2STR(peer), status, adv_id);
3019 } else {
3020 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3021 " p2p_dev_addr=" MACSTR " status=%d",
3022 MAC2STR(peer), status);
3023 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003024
Jouni Malinen75ecf522011-06-27 15:19:46 -07003025 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003026 status, 0, 0, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003027}
3028
3029
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003030static int freq_included(struct wpa_supplicant *wpa_s,
3031 const struct p2p_channels *channels,
3032 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003033{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003034 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3035 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3036 return 1;
3037 return 0;
3038}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003039
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003040
3041static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3042{
3043 unsigned int num = P2P_MAX_CHANNELS;
3044 int *common_freqs;
3045 int ret;
3046
3047 p2p_go_dump_common_freqs(wpa_s);
3048 common_freqs = os_calloc(num, sizeof(int));
3049 if (!common_freqs)
3050 return;
3051
3052 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3053 if (ret < 0) {
3054 wpa_dbg(wpa_s, MSG_DEBUG,
3055 "P2P: Failed to get group common freqs");
3056 os_free(common_freqs);
3057 return;
3058 }
3059
3060 os_free(wpa_s->p2p_group_common_freqs);
3061 wpa_s->p2p_group_common_freqs = common_freqs;
3062 wpa_s->p2p_group_common_freqs_num = num;
3063 p2p_go_dump_common_freqs(wpa_s);
3064}
3065
3066
3067/*
3068 * Check if the given frequency is one of the possible operating frequencies
3069 * set after the completion of the GO Negotiation.
3070 */
3071static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3072{
3073 unsigned int i;
3074
3075 p2p_go_dump_common_freqs(wpa_s);
3076
3077 /* assume no restrictions */
3078 if (!wpa_s->p2p_group_common_freqs_num)
3079 return 1;
3080
3081 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3082 if (wpa_s->p2p_group_common_freqs[i] == freq)
3083 return 1;
3084 }
3085 return 0;
3086}
3087
3088
3089static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3090 struct sta_info *sta, void *ctx)
3091{
3092 int *ecsa_support = ctx;
3093
3094 *ecsa_support &= sta->ecsa_supported;
3095
3096 return 0;
3097}
3098
3099
3100/* Check if all the peers support eCSA */
3101static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3102{
3103 int ecsa_support = 1;
3104
3105 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3106 &ecsa_support);
3107
3108 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003109}
3110
3111
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003112/**
3113 * Pick the best frequency to use from all the currently used frequencies.
3114 */
3115static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3116 struct wpa_used_freq_data *freqs,
3117 unsigned int num)
3118{
3119 unsigned int i, c;
3120
3121 /* find a candidate freq that is supported by P2P */
3122 for (c = 0; c < num; c++)
3123 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3124 break;
3125
3126 if (c == num)
3127 return 0;
3128
3129 /* once we have a candidate, try to find a 'better' one */
3130 for (i = c + 1; i < num; i++) {
3131 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3132 continue;
3133
3134 /*
3135 * 1. Infrastructure station interfaces have higher preference.
3136 * 2. P2P Clients have higher preference.
3137 * 3. All others.
3138 */
3139 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3140 c = i;
3141 break;
3142 }
3143
3144 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3145 c = i;
3146 }
3147 return freqs[c].freq;
3148}
3149
3150
Sunil Ravi036cec52023-03-29 11:35:17 -07003151/**
3152 * Pick the best frequency the driver suggests.
3153 *
3154 * num_pref_freq is used as both input and output
3155 * - input: the max size of pref_freq_list,
3156 * - output: the valid size of pref_freq_list filled with data.
3157 */
3158static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3159 struct weighted_pcl *pref_freq_list,
3160 unsigned int *num_pref_freq)
3161{
3162 int best_freq = 0;
3163 unsigned int max_pref_freq, i;
3164 int res;
3165 enum wpa_driver_if_type iface_type;
3166
3167 max_pref_freq = *num_pref_freq;
3168 *num_pref_freq = 0;
3169
3170 if (go)
3171 iface_type = WPA_IF_P2P_GO;
3172 else
3173 iface_type = WPA_IF_P2P_CLIENT;
3174
3175 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3176 pref_freq_list);
3177 if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3178 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3179 max_pref_freq);
3180 if (res || !max_pref_freq) {
3181 wpa_printf(MSG_DEBUG,
3182 "P2P: No preferred frequency list available");
3183 return 0;
3184 }
3185
3186 *num_pref_freq = max_pref_freq;
3187 i = 0;
3188 while (i < *num_pref_freq &&
3189 (!p2p_supported_freq(wpa_s->global->p2p,
3190 pref_freq_list[i].freq) ||
3191 wpas_p2p_disallowed_freq(wpa_s->global,
3192 pref_freq_list[i].freq) ||
3193 !p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3194 wpa_printf(MSG_DEBUG,
3195 "P2P: preferred_freq_list[%d]=%d is disallowed",
3196 i, pref_freq_list[i].freq);
3197 i++;
3198 }
3199 if (i != *num_pref_freq) {
3200 best_freq = pref_freq_list[i].freq;
3201 wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3202 i, best_freq);
3203 } else {
3204 wpa_printf(MSG_DEBUG,
3205 "P2P: All driver preferred frequencies are disallowed for P2P use");
3206 *num_pref_freq = 0;
3207 }
3208
3209 return best_freq;
3210}
3211
3212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3214 const u8 *go_dev_addr, const u8 *ssid,
3215 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003216 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003217 const struct p2p_channels *channels,
3218 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219{
3220 struct wpa_supplicant *wpa_s = ctx;
3221 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003222 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003224 int best_freq;
Sunil Ravi036cec52023-03-29 11:35:17 -07003225 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3226 unsigned int num_pref_freq;
3227 int res;
3228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229
3230 if (!persistent_group) {
3231 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003232 " to join an active group (SSID: %s)",
3233 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3235 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3236 == 0 ||
3237 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3238 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3239 "authorized invitation");
3240 goto accept_inv;
3241 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003242
3243#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003244 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3245 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003246 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003247 wpa_s->p2p_wps_method = WPS_NFC;
3248 wpa_s->pending_join_wps_method = WPS_NFC;
3249 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003250 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003251 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003252 bssid, ETH_ALEN);
3253 goto accept_inv;
3254 }
3255#endif /* CONFIG_WPS_NFC */
3256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 /*
3258 * Do not accept the invitation automatically; notify user and
3259 * request approval.
3260 */
3261 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3262 }
3263
3264 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3265 if (grp) {
3266 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3267 "running persistent group");
3268 if (*go)
3269 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3270 goto accept_inv;
3271 }
3272
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003273 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3274 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3275 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3276 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003277 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003278 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3280
3281 for (s = wpa_s->conf->ssid; s; s = s->next) {
3282 if (s->disabled == 2 &&
3283 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3284 s->ssid_len == ssid_len &&
3285 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3286 break;
3287 }
3288
3289 if (!s) {
3290 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3291 " requested reinvocation of an unknown group",
3292 MAC2STR(sa));
3293 return P2P_SC_FAIL_UNKNOWN_GROUP;
3294 }
3295
3296 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3297 *go = 1;
3298 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3299 wpa_printf(MSG_DEBUG, "P2P: The only available "
3300 "interface is already in use - reject "
3301 "invitation");
3302 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3303 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003304 if (wpa_s->p2p_mgmt)
3305 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3306 ETH_ALEN);
3307 else
3308 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 } else if (s->mode == WPAS_MODE_P2P_GO) {
3310 *go = 1;
3311 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3312 {
3313 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3314 "interface address for the group");
3315 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3316 }
3317 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3318 ETH_ALEN);
3319 }
3320
3321accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003322 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3323
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003324 best_freq = 0;
3325 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3326 sizeof(struct wpa_used_freq_data));
3327 if (freqs) {
3328 int num_channels = wpa_s->num_multichan_concurrent;
3329 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3330 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3331 os_free(freqs);
3332 }
3333
Sunil Ravi036cec52023-03-29 11:35:17 -07003334 num_pref_freq = P2P_MAX_PREF_CHANNELS;
3335 res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3336 &num_pref_freq);
3337 if (res > 0)
3338 best_freq = res;
3339
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003340 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003341 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003342 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003343 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003344
3345 if (wpa_s->num_multichan_concurrent < 2 ||
3346 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3347 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 -07003348 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003349 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350 }
3351
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003352 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3353 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003354 if (*go == 0) {
3355 /* We are the client */
3356 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3357 "running a GO but we are capable of MCC, "
3358 "figure out the best channel to use");
3359 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003360 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003361 /* We are the GO, and *force_freq is not in the
3362 * intersection */
3363 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3364 "in intersection but we are capable of MCC, "
3365 "figure out the best channel to use",
3366 *force_freq);
3367 *force_freq = 0;
3368 }
3369 }
3370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003371 return P2P_SC_SUCCESS;
3372}
3373
3374
3375static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3376 const u8 *ssid, size_t ssid_len,
3377 const u8 *go_dev_addr, u8 status,
3378 int op_freq)
3379{
3380 struct wpa_supplicant *wpa_s = ctx;
3381 struct wpa_ssid *s;
3382
3383 for (s = wpa_s->conf->ssid; s; s = s->next) {
3384 if (s->disabled == 2 &&
3385 s->ssid_len == ssid_len &&
3386 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3387 break;
3388 }
3389
3390 if (status == P2P_SC_SUCCESS) {
3391 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003392 " was accepted; op_freq=%d MHz, SSID=%s",
3393 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003395 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003396 if (go) {
3397 wpa_msg_global(wpa_s, MSG_INFO,
3398 P2P_EVENT_INVITATION_ACCEPTED
3399 "sa=" MACSTR
3400 " persistent=%d freq=%d",
3401 MAC2STR(sa), s->id, op_freq);
3402 } else {
3403 wpa_msg_global(wpa_s, MSG_INFO,
3404 P2P_EVENT_INVITATION_ACCEPTED
3405 "sa=" MACSTR
3406 " persistent=%d",
3407 MAC2STR(sa), s->id);
3408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003410 wpa_s, s, go, 0, op_freq, 0,
3411 wpa_s->conf->p2p_go_ht40,
3412 wpa_s->conf->p2p_go_vht,
3413 0,
3414 wpa_s->conf->p2p_go_he,
3415 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003416 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003417 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003418 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003420 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003421 wpa_msg_global(wpa_s, MSG_INFO,
3422 P2P_EVENT_INVITATION_ACCEPTED
3423 "sa=" MACSTR " go_dev_addr=" MACSTR
3424 " bssid=" MACSTR " unknown-network",
3425 MAC2STR(sa), MAC2STR(go_dev_addr),
3426 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003428 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003429 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430 }
3431 return;
3432 }
3433
3434 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3435 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3436 " was rejected (status %u)", MAC2STR(sa), status);
3437 return;
3438 }
3439
3440 if (!s) {
3441 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003442 wpa_msg_global(wpa_s, MSG_INFO,
3443 P2P_EVENT_INVITATION_RECEIVED
3444 "sa=" MACSTR " go_dev_addr=" MACSTR
3445 " bssid=" MACSTR " unknown-network",
3446 MAC2STR(sa), MAC2STR(go_dev_addr),
3447 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003449 wpa_msg_global(wpa_s, MSG_INFO,
3450 P2P_EVENT_INVITATION_RECEIVED
3451 "sa=" MACSTR " go_dev_addr=" MACSTR
3452 " unknown-network",
3453 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003455 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3456 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457 return;
3458 }
3459
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003460 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003461 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3462 "sa=" MACSTR " persistent=%d freq=%d",
3463 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003464 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003465 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3466 "sa=" MACSTR " persistent=%d",
3467 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003468 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003469 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3470 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471}
3472
3473
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003474static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3475 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003476 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003477{
3478 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003479 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003480
3481 if (ssid == NULL)
3482 return;
3483
3484 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003485 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003486 ETH_ALEN) == 0)
3487 break;
3488 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003489 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003490 if (ssid->mode != WPAS_MODE_P2P_GO &&
3491 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3492 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3493 "due to invitation result", ssid->id);
3494 wpas_notify_network_removed(wpa_s, ssid);
3495 wpa_config_remove_network(wpa_s->conf, ssid->id);
3496 return;
3497 }
3498 return; /* Peer not found in client list */
3499 }
3500
3501 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003502 "group %d client list%s",
3503 MAC2STR(peer), ssid->id,
3504 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003505 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3506 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3507 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003508 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003509 if (p2p_wpa_s->conf->update_config &&
3510 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003511 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003512}
3513
3514
3515static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3516 const u8 *peer)
3517{
3518 struct wpa_ssid *ssid;
3519
3520 wpa_s = wpa_s->global->p2p_invite_group;
3521 if (wpa_s == NULL)
3522 return; /* No known invitation group */
3523 ssid = wpa_s->current_ssid;
3524 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3525 !ssid->p2p_persistent_group)
3526 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003527 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003528 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003529 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003530}
3531
3532
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003533static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003534 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003535 const u8 *peer, int neg_freq,
3536 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537{
3538 struct wpa_supplicant *wpa_s = ctx;
3539 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003540 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541
3542 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003543 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3544 "status=%d " MACSTR,
3545 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003547 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3548 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003549 }
3550 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3551
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003552 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3553 status, MAC2STR(peer));
3554 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003555 struct wpa_supplicant *group_if =
3556 wpa_s->global->p2p_invite_group;
3557
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003558 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3559 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003560
3561 /*
3562 * Invitation to an active group. If this is successful and we
3563 * are the GO, set the client wait to postpone some concurrent
3564 * operations and to allow provisioning and connection to happen
3565 * more quickly.
3566 */
3567 if (status == P2P_SC_SUCCESS &&
3568 group_if && group_if->current_ssid &&
3569 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3570 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3571#ifdef CONFIG_TESTING_OPTIONS
3572 if (group_if->p2p_go_csa_on_inv) {
3573 wpa_printf(MSG_DEBUG,
3574 "Testing: force P2P GO CSA after invitation");
3575 eloop_cancel_timeout(
3576 wpas_p2p_reconsider_moving_go,
3577 wpa_s, NULL);
3578 eloop_register_timeout(
3579 0, 50000,
3580 wpas_p2p_reconsider_moving_go,
3581 wpa_s, NULL);
3582 }
3583#endif /* CONFIG_TESTING_OPTIONS */
3584 }
3585 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003586 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003588 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3589 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3590 "invitation exchange to indicate readiness for "
3591 "re-invocation");
3592 }
3593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003595 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3596 ssid = wpa_config_get_network(
3597 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003598 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003599 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600 wpas_p2p_remove_pending_group_interface(wpa_s);
3601 return;
3602 }
3603
3604 ssid = wpa_config_get_network(wpa_s->conf,
3605 wpa_s->pending_invite_ssid_id);
3606 if (ssid == NULL) {
3607 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3608 "data matching with invitation");
3609 return;
3610 }
3611
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003612 /*
3613 * The peer could have missed our ctrl::ack frame for Invitation
3614 * Response and continue retransmitting the frame. To reduce the
3615 * likelihood of the peer not getting successful TX status for the
3616 * Invitation Response frame, wait a short time here before starting
3617 * the persistent group so that we will remain on the current channel to
3618 * acknowledge any possible retransmission from the peer.
3619 */
3620 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3621 "starting persistent group");
3622 os_sleep(0, 50000);
3623
Dmitry Shmidt15907092014-03-25 10:42:57 -07003624 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003625 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003626 freq = neg_freq;
3627 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003628 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003629 freq = peer_oper_freq;
3630 else
3631 freq = 0;
3632
3633 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3634 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003635 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003636 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003637 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003638 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003639 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003640 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003641 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003642 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003643 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003644 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003645 ssid->mode == WPAS_MODE_P2P_GO ?
3646 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003647 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00003648 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003649 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650}
3651
3652
Dmitry Shmidt04949592012-07-19 12:16:46 -07003653static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3654 unsigned int freq)
3655{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003656 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3657 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003658 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003659}
3660
3661
3662static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3663{
3664 reg->channel[reg->channels] = chan;
3665 reg->channels++;
3666}
3667
3668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003670 struct p2p_channels *chan,
3671 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672{
3673 int i, cla = 0;
3674
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003675 wpa_s->global->p2p_24ghz_social_channels = 1;
3676
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003677 os_memset(cli_chan, 0, sizeof(*cli_chan));
3678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3680 "band");
3681
3682 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3683 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003684 chan->reg_class[cla].channels = 0;
3685 for (i = 0; i < 11; i++) {
3686 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3687 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3688 }
3689 if (chan->reg_class[cla].channels)
3690 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691
3692 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3693 "band");
3694
3695 /* Operating class 115 - 5 GHz, channels 36-48 */
3696 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003697 chan->reg_class[cla].channels = 0;
3698 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3699 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3700 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3701 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3702 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3703 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3704 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3705 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3706 if (chan->reg_class[cla].channels)
3707 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708
3709 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3710 "band");
3711
3712 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3713 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003714 chan->reg_class[cla].channels = 0;
3715 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3716 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3717 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3718 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3719 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3720 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3721 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3722 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3723 if (chan->reg_class[cla].channels)
3724 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725
3726 chan->reg_classes = cla;
3727 return 0;
3728}
3729
3730
Hai Shalomc1a21442022-02-04 13:43:00 -08003731static enum chan_allowed has_channel(struct wpa_global *global,
3732 struct hostapd_hw_modes *mode, u8 op_class,
3733 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734{
3735 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003736 unsigned int freq;
3737
Hai Shalomc1a21442022-02-04 13:43:00 -08003738 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003739 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003740 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003741
3742 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003743 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 if (flags)
3745 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003746 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003747 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003748 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3749 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003750 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3751 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003752 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 }
3754 }
3755
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003756 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757}
3758
3759
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003760static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3761 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003762 u8 channel, const u8 *center_channels,
3763 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003764{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003765 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003766
3767 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3768 return 0;
3769
Hai Shalomc1a21442022-02-04 13:43:00 -08003770 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003771 /*
3772 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3773 * so the center channel is 6 channels away from the start/end.
3774 */
3775 if (channel >= center_channels[i] - 6 &&
3776 channel <= center_channels[i] + 6)
3777 return center_channels[i];
3778
3779 return 0;
3780}
3781
3782
Hai Shalomc1a21442022-02-04 13:43:00 -08003783static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3784 155, 171 };
3785static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3786 119, 135, 151, 167, 183, 199,
3787 215 };
3788
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003789static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3790 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003791 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003792{
3793 u8 center_chan;
3794 int i, flags;
3795 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003796 const u8 *chans;
3797 size_t num_chans;
3798 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003799
Hai Shalomc1a21442022-02-04 13:43:00 -08003800 if (is_6ghz) {
3801 chans = center_channels_6ghz_80mhz;
3802 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3803 } else {
3804 chans = center_channels_5ghz_80mhz;
3805 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3806 }
3807 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3808 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003809 if (!center_chan)
3810 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003811 if (!wpa_s->p2p_go_allow_dfs &&
3812 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003813 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3814
3815 /* check all the channels are available */
3816 for (i = 0; i < 4; i++) {
3817 int adj_chan = center_chan - 6 + i * 4;
3818
Hai Shalomc1a21442022-02-04 13:43:00 -08003819 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3820 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003821 if (res == NOT_ALLOWED)
3822 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003823 if (res == RADAR)
3824 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003825 if (res == NO_IR)
3826 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003827 if (!is_6ghz) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003828 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003829 return NOT_ALLOWED;
3830 } else if (is_6ghz &&
3831 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3832 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003833 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003834 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003835 }
3836
3837 return ret;
3838}
3839
3840
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003841static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3842 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003843 u8 channel, const u8 *center_channels,
3844 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003845{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003846 unsigned int i;
3847
3848 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3849 return 0;
3850
Hai Shalomc1a21442022-02-04 13:43:00 -08003851 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003852 /*
3853 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3854 * so the center channel is 14 channels away from the start/end.
3855 */
3856 if (channel >= center_channels[i] - 14 &&
3857 channel <= center_channels[i] + 14)
3858 return center_channels[i];
3859
3860 return 0;
3861}
3862
3863
Hai Shalomc1a21442022-02-04 13:43:00 -08003864static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3865static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3866 207 };
3867
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003868static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3869 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003870 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003871{
3872 u8 center_chan;
3873 int i, flags;
3874 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003875 const u8 *chans;
3876 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003877
Hai Shalomc1a21442022-02-04 13:43:00 -08003878 if (is_6ghz_op_class(op_class)) {
3879 chans = center_channels_6ghz_160mhz;
3880 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3881 } else {
3882 chans = center_channels_5ghz_160mhz;
3883 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3884 }
3885 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3886 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003887 if (!center_chan)
3888 return NOT_ALLOWED;
3889 /* VHT 160 MHz uses DFS channels in most countries. */
3890
3891 /* Check all the channels are available */
3892 for (i = 0; i < 8; i++) {
3893 int adj_chan = center_chan - 14 + i * 4;
3894
Hai Shalomc1a21442022-02-04 13:43:00 -08003895 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3896 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003897 if (res == NOT_ALLOWED)
3898 return NOT_ALLOWED;
3899
Hai Shalomc1a21442022-02-04 13:43:00 -08003900 if (res == RADAR)
3901 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003902 if (res == NO_IR)
3903 ret = NO_IR;
3904
Hai Shalomc1a21442022-02-04 13:43:00 -08003905 if (!is_6ghz_op_class(op_class)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003906 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
3907 !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003908 return NOT_ALLOWED;
3909 } else if (is_6ghz_op_class(op_class) &&
3910 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3911 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003912 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003913 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003914 }
3915
3916 return ret;
3917}
3918
3919
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003920static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3921 struct hostapd_hw_modes *mode,
3922 u8 channel)
3923{
3924 struct ieee80211_edmg_config edmg;
3925
3926 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3927 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3928 return ALLOWED;
3929
3930 return NOT_ALLOWED;
3931}
3932
3933
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003934static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3935 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003936 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003937{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003938 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003939 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003940
Sunil Ravi89eba102022-09-13 21:04:37 -07003941 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3942 p2p_channel_to_freq(op_class, channel)))
3943 return NOT_ALLOWED;
3944
Hai Shalomc1a21442022-02-04 13:43:00 -08003945 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003946 if (bw == BW40MINUS) {
3947 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3948 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003949 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3950 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003951 } else if (bw == BW40PLUS) {
3952 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3953 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003954 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3955 NULL);
3956 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3957 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3958 return NOT_ALLOWED;
3959 if (get_6ghz_sec_channel(channel) < 0)
3960 res2 = has_channel(wpa_s->global, mode, op_class,
3961 channel - 4, NULL);
3962 else
3963 res2 = has_channel(wpa_s->global, mode, op_class,
3964 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003965 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003966 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3967 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003968 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003969 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3970 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003971 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3972 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003973 }
3974
3975 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3976 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003977 if (res == NO_IR || res2 == NO_IR)
3978 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003979 if (res == RADAR || res2 == RADAR)
3980 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003981 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003982}
3983
3984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003986 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003987 struct p2p_channels *cli_chan,
3988 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003990 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003991 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003993 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3995 "of all supported channels; assume dualband "
3996 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003997 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 }
3999
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004000 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004002 for (op = 0; global_op_class[op].op_class; op++) {
4003 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08004004 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004005 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Shuibing Daie2fad412023-05-05 14:08:11 -07004006 bool check_dfs_supported = (is_p2p_dfs_chan_enabled(wpa_s->global->p2p)
Shuibing Dai64a8a892023-03-08 10:26:22 -08004007 && is_dfs_global_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008
Shuibing Dai64a8a892023-03-08 10:26:22 -08004009 if ((!check_dfs_supported && o->p2p == NO_P2P_SUPP) ||
Hai Shalom60840252021-02-19 19:02:11 -08004010 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004011 continue;
4012
Hai Shalomfdcde762020-04-02 11:19:20 -07004013 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4014 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 if (mode == NULL)
4016 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004017 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4018 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004019 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004020 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07004021
4022 /* Check for non-continuous jump in channel index
4023 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08004024 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07004025 ch < 149 && ch + o->inc > 149)
4026 ch = 149;
4027
Hai Shalomc1a21442022-02-04 13:43:00 -08004028 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4029 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004030 if (res == ALLOWED) {
4031 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004032 if (cla == P2P_MAX_REG_CLASSES)
4033 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004034 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4035 o->op_class);
4036 reg = &chan->reg_class[cla];
4037 cla++;
4038 reg->reg_class = o->op_class;
4039 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004040 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4041 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004042 reg->channel[reg->channels] = ch;
4043 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004044 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004045 wpa_s->conf->p2p_add_cli_chan) {
4046 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004047 if (cli_cla == P2P_MAX_REG_CLASSES)
4048 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004049 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4050 o->op_class);
4051 cli_reg = &cli_chan->reg_class[cli_cla];
4052 cli_cla++;
4053 cli_reg->reg_class = o->op_class;
4054 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004055 if (cli_reg->channels ==
4056 P2P_MAX_REG_CLASS_CHANNELS)
4057 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004058 cli_reg->channel[cli_reg->channels] = ch;
4059 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 }
4062 if (reg) {
4063 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4064 reg->channel, reg->channels);
4065 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004066 if (cli_reg) {
4067 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4068 cli_reg->channel, cli_reg->channels);
4069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 }
4071
4072 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004073 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075 return 0;
4076}
4077
4078
Hai Shalomc1a21442022-02-04 13:43:00 -08004079int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4080 struct hostapd_hw_modes *mode,
4081 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004082{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004083 int op;
4084 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004085
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004086 for (op = 0; global_op_class[op].op_class; op++) {
4087 const struct oper_class_map *o = &global_op_class[op];
Sunil Ravi77d572f2023-01-17 23:58:31 +00004088 u16 ch = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004089
Hai Shalomc1a21442022-02-04 13:43:00 -08004090 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4091 * driver offloaded DFS. */
4092 if ((o->p2p == NO_P2P_SUPP &&
4093 (!is_dfs_global_op_class(o->op_class) ||
4094 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004095 (is_6ghz_op_class(o->op_class) &&
4096 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004097 continue;
4098
Sunil Ravi77d572f2023-01-17 23:58:31 +00004099 /* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4100 * set up the BSS with a primary 20 MHz channel that coincides
4101 * with a preferred scanning channel (PSC)."
4102 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4103 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4104 * match the PSC.
4105 */
Hai Shalomc1a21442022-02-04 13:43:00 -08004106 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4107 get_6ghz_sec_channel(channel) < 0)
Sunil Ravi77d572f2023-01-17 23:58:31 +00004108 ch = 4;
Hai Shalomc1a21442022-02-04 13:43:00 -08004109
Sunil Ravi77d572f2023-01-17 23:58:31 +00004110 for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004111 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004112 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4113 o->bw != BW40) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00004114 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004115 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004116 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4117 ch, o->bw);
4118 if (ret == ALLOWED) {
4119 if (is_6ghz_op_class(o->op_class) &&
4120 o->bw == BW40)
4121 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004122 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004123 }
4124 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4125 /* Allow RADAR channels used for driver
4126 * offloaded DFS */
4127 return (o->bw == BW40MINUS) ? -1 : 1;
4128 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004129 }
4130 }
4131 return 0;
4132}
4133
4134
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004135int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004136 struct hostapd_hw_modes *mode, u8 channel,
4137 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004138{
Hai Shalomc1a21442022-02-04 13:43:00 -08004139 const u8 *chans;
4140 size_t num_chans;
4141 enum chan_allowed ret;
4142
4143 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4144 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004145 return 0;
4146
Hai Shalomc1a21442022-02-04 13:43:00 -08004147 if (is_6ghz_op_class(op_class)) {
4148 chans = center_channels_6ghz_80mhz;
4149 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4150 } else {
4151 chans = center_channels_5ghz_80mhz;
4152 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4153 }
4154 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4155 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004156}
4157
4158
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004159int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004160 struct hostapd_hw_modes *mode, u8 channel,
4161 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004162{
Hai Shalomc1a21442022-02-04 13:43:00 -08004163 const u8 *chans;
4164 size_t num_chans;
4165 enum chan_allowed ret;
4166
4167 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4168 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004169 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004170 if (is_6ghz_op_class(op_class)) {
4171 chans = center_channels_6ghz_160mhz;
4172 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4173 } else {
4174 chans = center_channels_5ghz_160mhz;
4175 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4176 }
4177 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4178 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004179}
4180
4181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4183 size_t buf_len)
4184{
4185 struct wpa_supplicant *wpa_s = ctx;
4186
4187 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4188 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4189 break;
4190 }
4191 if (wpa_s == NULL)
4192 return -1;
4193
4194 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4195}
4196
4197
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004198struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4199 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004200{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004201 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4202 struct wpa_ssid *s = wpa_s->current_ssid;
4203 if (s == NULL)
4204 continue;
4205 if (s->mode != WPAS_MODE_P2P_GO &&
4206 s->mode != WPAS_MODE_AP &&
4207 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4208 continue;
4209 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004210 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004211 continue;
4212 return wpa_s;
4213 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004214
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004215 return NULL;
4216
4217}
4218
4219
4220struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4221 const u8 *peer_dev_addr)
4222{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004223 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4224 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004225 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004226 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004227 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4228 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004229 }
4230
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004231 return NULL;
4232}
4233
4234
4235static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4236{
4237 struct wpa_supplicant *wpa_s = ctx;
4238
4239 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004240}
4241
4242
Dmitry Shmidt18463232014-01-24 12:29:41 -08004243static int wpas_is_concurrent_session_active(void *ctx)
4244{
4245 struct wpa_supplicant *wpa_s = ctx;
4246 struct wpa_supplicant *ifs;
4247
4248 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4249 if (ifs == wpa_s)
4250 continue;
4251 if (ifs->wpa_state > WPA_ASSOCIATED)
4252 return 1;
4253 }
4254 return 0;
4255}
4256
4257
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004258static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4259{
4260 struct wpa_supplicant *wpa_s = ctx;
4261 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4262}
4263
4264
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004265int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4266 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004267{
4268 struct wpa_interface iface;
4269 struct wpa_supplicant *p2pdev_wpa_s;
4270 char ifname[100];
4271 char force_name[100];
4272 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004273 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004274
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004275 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4276 wpa_s->ifname);
4277 if (os_snprintf_error(sizeof(ifname), ret))
4278 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004279 /* Cut length at the maximum size. Note that we don't need to ensure
4280 * collision free names here as the created interface is not a netdev.
4281 */
4282 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004283 force_name[0] = '\0';
4284 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004285
4286 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4287 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4288 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4289
4290 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004291 force_name, wpa_s->pending_interface_addr, NULL);
4292 if (ret < 0) {
4293 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4294 return ret;
4295 }
4296 os_strlcpy(wpa_s->pending_interface_name, ifname,
4297 sizeof(wpa_s->pending_interface_name));
4298
4299 os_memset(&iface, 0, sizeof(iface));
4300 iface.p2p_mgmt = 1;
4301 iface.ifname = wpa_s->pending_interface_name;
4302 iface.driver = wpa_s->driver->name;
4303 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004304
4305 /*
4306 * If a P2P Device configuration file was given, use it as the interface
4307 * configuration file (instead of using parent's configuration file.
4308 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004309 if (conf_p2p_dev) {
4310 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004311 iface.ctrl_interface = NULL;
4312 } else {
4313 iface.confname = wpa_s->confname;
4314 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4315 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004316
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004317 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004318 if (!p2pdev_wpa_s) {
4319 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4320 return -1;
4321 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004322
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004323 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004324 wpa_s->pending_interface_name[0] = '\0';
4325 return 0;
4326}
4327
4328
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004329static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4330 const u8 *noa, size_t noa_len)
4331{
4332 struct wpa_supplicant *wpa_s, *intf = ctx;
4333 char hex[100];
4334
4335 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4336 if (wpa_s->waiting_presence_resp)
4337 break;
4338 }
4339 if (!wpa_s) {
4340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4341 return;
4342 }
4343 wpa_s->waiting_presence_resp = 0;
4344
4345 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4346 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4347 " status=%u noa=%s", MAC2STR(src), status, hex);
4348}
4349
4350
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004351static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4352 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004353 u8 *ret_ssid, size_t *ret_ssid_len,
4354 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004355{
4356 struct wpa_supplicant *wpa_s = ctx;
4357 struct wpa_ssid *s;
4358
4359 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4360 if (s) {
4361 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4362 *ret_ssid_len = s->ssid_len;
4363 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004364
4365 if (s->mode != WPAS_MODE_P2P_GO) {
4366 os_memset(intended_iface_addr, 0, ETH_ALEN);
4367 } else if (wpas_p2p_create_iface(wpa_s)) {
4368 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4369 return 0;
4370
4371 os_memcpy(intended_iface_addr,
4372 wpa_s->pending_interface_addr, ETH_ALEN);
4373 } else {
4374 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4375 ETH_ALEN);
4376 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004377 return 1;
4378 }
4379
4380 return 0;
4381}
4382
4383
4384static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004385 u8 *ssid, size_t *ssid_len, int *group_iface,
4386 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004387{
4388 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004389 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004390 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004391
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004392 /*
4393 * group_iface will be set to 1 only if a dedicated interface for P2P
4394 * role is required. First, we try to reuse an active GO. However,
4395 * if it is not present, we will try to reactivate an existing
4396 * persistent group and set group_iface to 1, so the caller will know
4397 * that the pending interface should be used.
4398 */
4399 *group_iface = 0;
4400
4401 if (freq)
4402 *freq = 0;
4403
4404 go = wpas_p2p_get_go_group(wpa_s);
4405 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004406 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004407 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004408 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004409 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4410 else
4411 return 0;
4412 } else {
4413 s = go->current_ssid;
4414 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4415 if (freq)
4416 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004417 }
4418
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004419 os_memcpy(ssid, s->ssid, s->ssid_len);
4420 *ssid_len = s->ssid_len;
4421
4422 return 1;
4423}
4424
4425
4426static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4427 const u8 *ssid, size_t ssid_len)
4428{
4429 struct wpa_supplicant *wpa_s = ctx;
4430 struct wpa_ssid *s;
4431 int save_config = 0;
4432 size_t i;
4433
4434 /* Start with our first choice of Persistent Groups */
4435 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4436 if (go && ssid && ssid_len &&
4437 s->ssid_len == ssid_len &&
4438 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4439 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4440 break;
4441
4442 /* Remove stale persistent group */
4443 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004444 wpa_dbg(wpa_s, MSG_DEBUG,
4445 "P2P: Remove stale persistent group id=%d",
4446 s->id);
4447 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004448 wpa_config_remove_network(wpa_s->conf, s->id);
4449 save_config = 1;
4450 continue;
4451 }
4452
4453 for (i = 0; i < s->num_p2p_clients; i++) {
4454 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4455 peer, ETH_ALEN) != 0)
4456 continue;
4457
4458 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4459 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4460 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4461 break;
4462 }
4463 s->num_p2p_clients--;
4464 save_config = 1;
4465 }
4466
4467 if (save_config)
4468 p2p_config_write(wpa_s);
4469
4470 /* Return TRUE if valid SSID remains */
4471 return s != NULL;
4472}
4473
4474
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004475static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4476 const u8 *feat_cap, size_t feat_cap_len)
4477{
4478 static const char pref[] = " feature_cap=";
4479 int ret;
4480
4481 buf[0] = '\0';
4482
4483 /*
4484 * We expect a feature capability to contain at least one byte to be
4485 * reported. The string buffer provided by the caller function is
4486 * expected to be big enough to contain all bytes of the attribute for
4487 * known specifications. This function truncates the reported bytes if
4488 * the feature capability data exceeds the string buffer size.
4489 */
4490 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4491 return;
4492
4493 os_memcpy(buf, pref, sizeof(pref));
4494 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4495 buf_len - sizeof(pref) + 1,
4496 feat_cap, feat_cap_len);
4497
4498 if (ret != (2 * (int) feat_cap_len))
4499 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4500}
4501
4502
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004503static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4504 const u8 *adv_mac, const u8 *ses_mac,
4505 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4506 u8 conncap, int passwd_id,
4507 const u8 *persist_ssid,
4508 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004509 int prov_start, const char *session_info,
4510 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004511 unsigned int freq,
4512 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004513{
4514 struct wpa_supplicant *wpa_s = ctx;
4515 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004516 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004517 int save_config = 0;
4518 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004519 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004520
4521 if (!dev)
4522 return;
4523
4524 os_memset(mac, 0, ETH_ALEN);
4525 if (!adv_mac)
4526 adv_mac = mac;
4527 if (!ses_mac)
4528 ses_mac = mac;
4529 if (!grp_mac)
4530 grp_mac = mac;
4531
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004532 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4533 feat_cap, feat_cap_len);
4534
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004535 if (prov_start) {
4536 if (session_info == NULL) {
4537 wpa_msg_global(wpa_s, MSG_INFO,
4538 P2P_EVENT_P2PS_PROVISION_START MACSTR
4539 " adv_id=%x conncap=%x"
4540 " adv_mac=" MACSTR
4541 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004542 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004543 MAC2STR(dev), adv_id, conncap,
4544 MAC2STR(adv_mac),
4545 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004546 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004547 } else {
4548 wpa_msg_global(wpa_s, MSG_INFO,
4549 P2P_EVENT_P2PS_PROVISION_START MACSTR
4550 " adv_id=%x conncap=%x"
4551 " adv_mac=" MACSTR
4552 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004553 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004554 MAC2STR(dev), adv_id, conncap,
4555 MAC2STR(adv_mac),
4556 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004557 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004558 }
4559 return;
4560 }
4561
4562 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4563 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4564
4565 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4566 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4567 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4568
4569 if (persistent_go && !persistent_go->num_p2p_clients) {
4570 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004571 wpa_dbg(wpa_s, MSG_DEBUG,
4572 "P2P: Remove empty persistent group id=%d",
4573 persistent_go->id);
4574 wpas_notify_persistent_group_removed(wpa_s,
4575 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004576 wpa_config_remove_network(wpa_s->conf,
4577 persistent_go->id);
4578 }
4579
4580 wpa_msg_global(wpa_s, MSG_INFO,
4581 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4582 " status=%d"
4583 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004584 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004585 MAC2STR(dev), status,
4586 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004587 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004588 return;
4589 }
4590
4591 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004592 if (persist_ssid && persist_ssid_size)
4593 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4594 persist_ssid_size);
4595
4596 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4597 is_zero_ether_addr(grp_mac)) {
4598 wpa_dbg(wpa_s, MSG_ERROR,
4599 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4600 return;
4601 }
4602
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004603 for (;;) {
4604 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4605 if (!stale)
4606 break;
4607
4608 if (s && s->ssid_len == stale->ssid_len &&
4609 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4610 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4611 break;
4612
4613 /* Remove stale persistent group */
4614 if (stale->mode != WPAS_MODE_P2P_GO ||
4615 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004616 wpa_dbg(wpa_s, MSG_DEBUG,
4617 "P2P: Remove stale persistent group id=%d",
4618 stale->id);
4619 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004620 wpa_config_remove_network(wpa_s->conf, stale->id);
4621 } else {
4622 size_t i;
4623
4624 for (i = 0; i < stale->num_p2p_clients; i++) {
4625 if (os_memcmp(stale->p2p_client_list +
4626 i * ETH_ALEN,
4627 dev, ETH_ALEN) == 0) {
4628 os_memmove(stale->p2p_client_list +
4629 i * ETH_ALEN,
4630 stale->p2p_client_list +
4631 (i + 1) * ETH_ALEN,
4632 (stale->num_p2p_clients -
4633 i - 1) * ETH_ALEN);
4634 break;
4635 }
4636 }
4637 stale->num_p2p_clients--;
4638 }
4639 save_config = 1;
4640 }
4641
4642 if (save_config)
4643 p2p_config_write(wpa_s);
4644
4645 if (s) {
4646 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4647 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4648
4649 if (persistent_go && s != persistent_go &&
4650 !persistent_go->num_p2p_clients) {
4651 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004652 wpa_dbg(wpa_s, MSG_DEBUG,
4653 "P2P: Remove empty persistent group id=%d",
4654 persistent_go->id);
4655 wpas_notify_persistent_group_removed(wpa_s,
4656 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004657 wpa_config_remove_network(wpa_s->conf,
4658 persistent_go->id);
4659 /* Save config */
4660 }
4661
4662 wpa_msg_global(wpa_s, MSG_INFO,
4663 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4664 " status=%d"
4665 " adv_id=%x adv_mac=" MACSTR
4666 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004667 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004668 MAC2STR(dev), status,
4669 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004670 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004671 return;
4672 }
4673
Hai Shalom5f92bc92019-04-18 11:54:11 -07004674 wpa_s->global->pending_p2ps_group = 0;
4675 wpa_s->global->pending_p2ps_group_freq = 0;
4676
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004677 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004678 /*
4679 * We need to copy the interface name. Simply saving a
4680 * pointer isn't enough, since if we use pending_interface_name
4681 * it will be overwritten when the group is added.
4682 */
4683 char go_ifname[100];
4684
4685 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004686 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004687 if (!response_done) {
4688 wpa_s->global->pending_p2ps_group = 1;
4689 wpa_s->global->pending_p2ps_group_freq = freq;
4690 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004691
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004692 if (!wpas_p2p_create_iface(wpa_s))
4693 os_memcpy(go_ifname, wpa_s->ifname,
4694 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004695 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004696 os_memcpy(go_ifname,
4697 wpa_s->pending_interface_name,
4698 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004700 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004701 wpas_p2ps_prov_complete(
4702 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4703 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004704 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004705 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4706 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004707 return;
4708 }
4709
4710 /* If PD Resp complete, start up the GO */
4711 if (response_done && persistent_go) {
4712 wpas_p2p_group_add_persistent(
4713 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004714 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004715 persistent_go->mode ==
4716 WPAS_MODE_P2P_GO ?
4717 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Sunil Ravi036cec52023-03-29 11:35:17 -07004718 0, 0, false, 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004719 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004720 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004721 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004722 }
4723
4724 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004725 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4726 ETH_ALEN);
4727 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004728 }
4729 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004730 os_memcpy(go_ifname, go_wpa_s->ifname,
4731 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004733 if (is_zero_ether_addr(grp_mac)) {
4734 wpa_dbg(go_wpa_s, MSG_DEBUG,
4735 "P2P: Setting PIN-1 for ANY");
4736 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4737 "12345670", NULL, 0,
4738 0);
4739 } else {
4740 wpa_dbg(go_wpa_s, MSG_DEBUG,
4741 "P2P: Setting PIN-1 for " MACSTR,
4742 MAC2STR(grp_mac));
4743 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4744 "12345670", NULL, 0,
4745 0);
4746 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004747
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004748 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4749 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004750 }
4751
4752 wpa_msg_global(wpa_s, MSG_INFO,
4753 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4754 " status=%d conncap=%x"
4755 " adv_id=%x adv_mac=" MACSTR
4756 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004757 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004758 MAC2STR(dev), status, conncap,
4759 adv_id, MAC2STR(adv_mac),
4760 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004761 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004762 return;
4763 }
4764
4765 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4766 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4767
4768 if (persistent_go && !persistent_go->num_p2p_clients) {
4769 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004770 wpa_dbg(wpa_s, MSG_DEBUG,
4771 "P2P: Remove empty persistent group id=%d",
4772 persistent_go->id);
4773 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004774 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4775 }
4776
4777 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004778 char ssid_hex[32 * 2 + 1];
4779
4780 if (group_ssid)
4781 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4782 group_ssid, group_ssid_len);
4783 else
4784 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004785 wpa_msg_global(wpa_s, MSG_INFO,
4786 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4787 " status=%d conncap=%x"
4788 " adv_id=%x adv_mac=" MACSTR
4789 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004790 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004791 MAC2STR(dev), status, conncap,
4792 adv_id, MAC2STR(adv_mac),
4793 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004794 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4795 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004796 } else {
4797 wpa_msg_global(wpa_s, MSG_INFO,
4798 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4799 " status=%d conncap=%x"
4800 " adv_id=%x adv_mac=" MACSTR
4801 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004802 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004803 MAC2STR(dev), status, conncap,
4804 adv_id, MAC2STR(adv_mac),
4805 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004806 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004807 }
4808}
4809
4810
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004811static int _wpas_p2p_in_progress(void *ctx)
4812{
4813 struct wpa_supplicant *wpa_s = ctx;
4814 return wpas_p2p_in_progress(wpa_s);
4815}
4816
4817
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004818static int wpas_prov_disc_resp_cb(void *ctx)
4819{
4820 struct wpa_supplicant *wpa_s = ctx;
4821 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004822 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004823
4824 if (!wpa_s->global->pending_p2ps_group)
4825 return 0;
4826
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004827 freq = wpa_s->global->pending_p2ps_group_freq;
4828 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004829 wpa_s->global->pending_p2ps_group = 0;
4830
4831 if (wpas_p2p_get_go_group(wpa_s))
4832 return 0;
4833 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4834
4835 if (persistent_go) {
4836 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004837 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4838 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004839 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004840 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07004841 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004842 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004843 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4844 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004845 }
4846
4847 return 1;
4848}
4849
4850
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004851static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4852 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004853 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004854{
4855 struct wpa_supplicant *wpa_s = ctx;
4856
4857 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4858 WPA_IF_P2P_CLIENT, len, freq_list);
4859}
4860
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004861int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4862{
JaeMan Park9de97322022-07-11 15:36:43 +09004863 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004864 u8 addr[ETH_ALEN] = {0};
4865
4866 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4867 return 0;
4868
Hai Shalom60840252021-02-19 19:02:11 -08004869 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4870 if (is_zero_ether_addr(
4871 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4872 !is_zero_ether_addr(wpa_s->own_addr)) {
4873 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4874 wpa_s->own_addr, ETH_ALEN);
4875 }
4876 return 0;
4877 }
4878
4879 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004880 if (random_mac_addr(addr) < 0) {
4881 wpa_msg(wpa_s, MSG_INFO,
4882 "Failed to generate random MAC address");
4883 return -EINVAL;
4884 }
4885
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004886 /* Store generated MAC address. */
4887 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4888 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004889 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004890 /* If there are existing saved groups, restore last MAC address.
4891 * if there is no last used MAC address, the last one is
4892 * factory MAC. */
4893 if (is_zero_ether_addr(
4894 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004895 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004896 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4897 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004898 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4899 }
4900
JaeMan Park9de97322022-07-11 15:36:43 +09004901 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4902
4903 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004904 wpa_msg(wpa_s, MSG_INFO,
4905 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004906 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004907 }
4908
JaeMan Park9de97322022-07-11 15:36:43 +09004909 ret = wpa_supplicant_update_mac_addr(wpa_s);
4910
4911 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004912 wpa_msg(wpa_s, MSG_INFO,
4913 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004914 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004915 }
4916
4917 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4918 MAC2STR(addr));
4919
4920 return 0;
4921}
4922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923/**
4924 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4925 * @global: Pointer to global data from wpa_supplicant_init()
4926 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4927 * Returns: 0 on success, -1 on failure
4928 */
4929int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4930{
4931 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 int i;
4933
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004934 if (wpa_s->conf->p2p_disabled)
4935 return 0;
4936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4938 return 0;
4939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 if (global->p2p)
4941 return 0;
4942
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004943 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4944 wpa_msg(wpa_s, MSG_ERROR,
4945 "Failed to initialize P2P random MAC address.");
4946 return -1;
4947 }
4948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004951 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 p2p.p2p_scan = wpas_p2p_scan;
4953 p2p.send_action = wpas_send_action;
4954 p2p.send_action_done = wpas_send_action_done;
4955 p2p.go_neg_completed = wpas_go_neg_completed;
4956 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4957 p2p.dev_found = wpas_dev_found;
4958 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004959 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 p2p.start_listen = wpas_start_listen;
4961 p2p.stop_listen = wpas_stop_listen;
4962 p2p.send_probe_resp = wpas_send_probe_resp;
4963 p2p.sd_request = wpas_sd_request;
4964 p2p.sd_response = wpas_sd_response;
4965 p2p.prov_disc_req = wpas_prov_disc_req;
4966 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004967 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 p2p.invitation_process = wpas_invitation_process;
4969 p2p.invitation_received = wpas_invitation_received;
4970 p2p.invitation_result = wpas_invitation_result;
4971 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004972 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004973 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004974 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004975 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004976 p2p.get_persistent_group = wpas_get_persistent_group;
4977 p2p.get_go_info = wpas_get_go_info;
4978 p2p.remove_stale_groups = wpas_remove_stale_groups;
4979 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4980 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4981 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004982 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004983 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Shuibing Daie2fad412023-05-05 14:08:11 -07004984 p2p.p2p_dfs_chan_enable = wpa_s->conf->p2p_dfs_chan_enable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985
4986 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004987 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 p2p.dev_name = wpa_s->conf->device_name;
4989 p2p.manufacturer = wpa_s->conf->manufacturer;
4990 p2p.model_name = wpa_s->conf->model_name;
4991 p2p.model_number = wpa_s->conf->model_number;
4992 p2p.serial_number = wpa_s->conf->serial_number;
4993 if (wpa_s->wps) {
4994 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4995 p2p.config_methods = wpa_s->wps->config_methods;
4996 }
4997
Hai Shalom60840252021-02-19 19:02:11 -08004998 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4999 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005000 wpa_printf(MSG_ERROR,
5001 "P2P: Failed to configure supported channel list");
5002 return -1;
5003 }
5004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 if (wpa_s->conf->p2p_listen_reg_class &&
5006 wpa_s->conf->p2p_listen_channel) {
5007 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5008 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005009 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 /*
5012 * Pick one of the social channels randomly as the listen
5013 * channel.
5014 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005015 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005016 &p2p.channel,
5017 &global->p2p_go_avoid_freq,
5018 &global->p2p_disallow_freq) !=
5019 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005020 wpa_printf(MSG_INFO,
5021 "P2P: No social channels supported by the driver - do not enable P2P");
5022 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005023 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005024 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005026 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5027 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028
5029 if (wpa_s->conf->p2p_oper_reg_class &&
5030 wpa_s->conf->p2p_oper_channel) {
5031 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5032 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5033 p2p.cfg_op_channel = 1;
5034 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5035 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5036
5037 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005039 * Use random operation channel from 2.4 GHz band social
5040 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5041 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005043 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005044 &p2p.op_channel, NULL,
5045 NULL) != 0) {
5046 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005047 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08005048 p2p.op_reg_class = 0;
5049 p2p.op_channel = 0;
5050 /* This will be overridden during group setup in
5051 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053 p2p.cfg_op_channel = 0;
5054 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5055 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5056 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005057
5058 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5059 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5060 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5061 }
5062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5064 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5065 p2p.country[2] = 0x04;
5066 } else
5067 os_memcpy(p2p.country, "XX\x04", 3);
5068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5070 WPS_DEV_TYPE_LEN);
5071
5072 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5073 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5074 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5075
5076 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5077 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5078
5079 p2p.max_peers = 100;
5080
5081 if (wpa_s->conf->p2p_ssid_postfix) {
5082 p2p.ssid_postfix_len =
5083 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5084 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5085 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5086 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5087 p2p.ssid_postfix_len);
5088 }
5089
5090 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5091
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005092 p2p.max_listen = wpa_s->max_remain_on_chan;
5093
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005094 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5095 wpa_s->conf->p2p_passphrase_len <= 63)
5096 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5097 else
5098 p2p.passphrase_len = 8;
5099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 global->p2p = p2p_init(&p2p);
5101 if (global->p2p == NULL)
5102 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005103 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104
5105 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5106 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5107 continue;
5108 p2p_add_wps_vendor_extension(
5109 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5110 }
5111
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005112 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114 return 0;
5115}
5116
5117
5118/**
5119 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5120 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5121 *
5122 * This function deinitialize per-interface P2P data.
5123 */
5124void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5125{
5126 if (wpa_s->driver && wpa_s->drv_priv)
5127 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005128
5129 if (wpa_s->go_params) {
5130 /* Clear any stored provisioning info */
5131 p2p_clear_provisioning_info(
5132 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005133 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005134 }
5135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136 os_free(wpa_s->go_params);
5137 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005138 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5140 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005141 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5143 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5144 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005145 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005146 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005147 wpas_p2p_listen_work_done(wpa_s);
5148 if (wpa_s->p2p_send_action_work) {
5149 os_free(wpa_s->p2p_send_action_work->ctx);
5150 radio_work_done(wpa_s->p2p_send_action_work);
5151 wpa_s->p2p_send_action_work = NULL;
5152 }
5153 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005155 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5156 wpa_s->p2p_oob_dev_pw = NULL;
5157
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005158 os_free(wpa_s->p2p_group_common_freqs);
5159 wpa_s->p2p_group_common_freqs = NULL;
5160 wpa_s->p2p_group_common_freqs_num = 0;
5161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162 /* TODO: remove group interface from the driver if this wpa_s instance
5163 * is on top of a P2P group interface */
5164}
5165
5166
5167/**
5168 * wpas_p2p_deinit_global - Deinitialize global P2P module
5169 * @global: Pointer to global data from wpa_supplicant_init()
5170 *
5171 * This function deinitializes the global (per device) P2P module.
5172 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005173static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174{
5175 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005177 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005178
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005179 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180
5181 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5183 wpa_s = wpa_s->next;
5184 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185 tmp = global->ifaces;
5186 while (tmp &&
5187 (tmp == wpa_s ||
5188 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5189 tmp = tmp->next;
5190 }
5191 if (tmp == NULL)
5192 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005193 /* Disconnect from the P2P group and deinit the interface */
5194 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195 }
5196
5197 /*
5198 * Deinit GO data on any possibly remaining interface (if main
5199 * interface is used as GO).
5200 */
5201 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5202 if (wpa_s->ap_iface)
5203 wpas_p2p_group_deinit(wpa_s);
5204 }
5205
5206 p2p_deinit(global->p2p);
5207 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209}
5210
5211
5212static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5213{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005214 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005215 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216 if (wpa_s->drv_flags &
5217 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5218 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5219 return 1; /* P2P group requires a new interface in every case
5220 */
5221 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5222 return 0; /* driver does not support concurrent operations */
5223 if (wpa_s->global->ifaces->next)
5224 return 1; /* more that one interface already in use */
5225 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5226 return 1; /* this interface is already in use */
5227 return 0;
5228}
5229
5230
5231static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5232 const u8 *peer_addr,
5233 enum p2p_wps_method wps_method,
5234 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005235 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005236 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005238 if (persistent_group && wpa_s->conf->persistent_reconnect)
5239 persistent_group = 2;
5240
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005241 /*
5242 * Increase GO config timeout if HT40 is used since it takes some time
5243 * to scan channels for coex purposes before the BSS can be started.
5244 */
5245 p2p_set_config_timeout(wpa_s->global->p2p,
5246 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5249 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005250 persistent_group, ssid ? ssid->ssid : NULL,
5251 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005252 wpa_s->p2p_pd_before_go_neg, pref_freq,
5253 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5254 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255}
5256
5257
5258static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5259 const u8 *peer_addr,
5260 enum p2p_wps_method wps_method,
5261 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005262 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005263 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005265 if (persistent_group && wpa_s->conf->persistent_reconnect)
5266 persistent_group = 2;
5267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5269 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005270 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005271 ssid ? ssid->ssid_len : 0, pref_freq,
5272 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5273 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274}
5275
5276
5277static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5278{
5279 wpa_s->p2p_join_scan_count++;
5280 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5281 wpa_s->p2p_join_scan_count);
5282 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5283 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5284 " for join operationg - stop join attempt",
5285 MAC2STR(wpa_s->pending_join_iface_addr));
5286 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005287 if (wpa_s->p2p_auto_pd) {
5288 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005289 wpa_msg_global(wpa_s, MSG_INFO,
5290 P2P_EVENT_PROV_DISC_FAILURE
5291 " p2p_dev_addr=" MACSTR " status=N/A",
5292 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005293 return;
5294 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005295 if (wpa_s->p2p_fallback_to_go_neg) {
5296 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5297 "failed - fall back to GO Negotiation");
5298 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5299 P2P_EVENT_FALLBACK_TO_GO_NEG
5300 "reason=join-failed");
5301 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5302 return;
5303 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005304 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005305 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005306 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307 }
5308}
5309
5310
Dmitry Shmidt04949592012-07-19 12:16:46 -07005311static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5312{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005313 int res;
5314 unsigned int num, i;
5315 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005316
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005317 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5318 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005320 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005321
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005322 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5323 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005324 if (!freqs)
5325 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005326
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005327 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5328 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005329
5330 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005331 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005332 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5333 freq);
5334 res = 0;
5335 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005336 }
5337 }
5338
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005339 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005340 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005341
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005342exit_free:
5343 os_free(freqs);
5344 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005345}
5346
5347
5348static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5349 const u8 *peer_dev_addr)
5350{
5351 struct wpa_bss *bss;
5352 int updated;
5353
5354 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5355 if (bss == NULL)
5356 return -1;
5357 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5358 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5359 "last scan");
5360 return 0;
5361 }
5362
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005363 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5364 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005365 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5366 "%ld.%06ld (%supdated in last scan)",
5367 bss->last_update.sec, bss->last_update.usec,
5368 updated ? "": "not ");
5369
5370 return updated;
5371}
5372
5373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5375 struct wpa_scan_results *scan_res)
5376{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005377 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005378 int freq;
5379 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5382
5383 if (wpa_s->global->p2p_disabled)
5384 return;
5385
Dmitry Shmidt04949592012-07-19 12:16:46 -07005386 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5387 scan_res ? (int) scan_res->num : -1,
5388 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389
5390 if (scan_res)
5391 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5392
Dmitry Shmidt04949592012-07-19 12:16:46 -07005393 if (wpa_s->p2p_auto_pd) {
5394 int join = wpas_p2p_peer_go(wpa_s,
5395 wpa_s->pending_join_dev_addr);
5396 if (join == 0 &&
5397 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5398 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005399 bss = wpa_bss_get_bssid_latest(
5400 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005401 if (bss) {
5402 freq = bss->freq;
5403 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5404 "the peer " MACSTR " at %d MHz",
5405 wpa_s->auto_pd_scan_retry,
5406 MAC2STR(wpa_s->
5407 pending_join_dev_addr),
5408 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005409 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005410 return;
5411 }
5412 }
5413
5414 if (join < 0)
5415 join = 0;
5416
5417 wpa_s->p2p_auto_pd = 0;
5418 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5419 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5420 MAC2STR(wpa_s->pending_join_dev_addr), join);
5421 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005422 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005423 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005424 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005425 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005426 wpa_msg_global(wpa_s, MSG_INFO,
5427 P2P_EVENT_PROV_DISC_FAILURE
5428 " p2p_dev_addr=" MACSTR " status=N/A",
5429 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005430 }
5431 return;
5432 }
5433
5434 if (wpa_s->p2p_auto_join) {
5435 int join = wpas_p2p_peer_go(wpa_s,
5436 wpa_s->pending_join_dev_addr);
5437 if (join < 0) {
5438 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5439 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005440 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005441 P2P_EVENT_FALLBACK_TO_GO_NEG
5442 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5444 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5445 wpa_s->p2p_persistent_group, 0, 0, 0,
5446 wpa_s->p2p_go_intent,
5447 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005448 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005449 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005450 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005451 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005452 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005453 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005454 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005455 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005456 NULL, 0,
5457 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005458 return;
5459 }
5460
5461 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5462 "try to join the group", join ? "" :
5463 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005464 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005465 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005466 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005467 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005468 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005469 }
5470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5472 wpa_s->pending_join_iface_addr);
5473 if (freq < 0 &&
5474 p2p_get_interface_addr(wpa_s->global->p2p,
5475 wpa_s->pending_join_dev_addr,
5476 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005477 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5478 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5480 "address for join from " MACSTR " to " MACSTR
5481 " based on newly discovered P2P peer entry",
5482 MAC2STR(wpa_s->pending_join_iface_addr),
5483 MAC2STR(iface_addr));
5484 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5485 ETH_ALEN);
5486
5487 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5488 wpa_s->pending_join_iface_addr);
5489 }
5490 if (freq >= 0) {
5491 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5492 "from P2P peer table: %d MHz", freq);
5493 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005494 if (wpa_s->p2p_join_ssid_len) {
5495 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5496 MACSTR " and SSID %s",
5497 MAC2STR(wpa_s->pending_join_iface_addr),
5498 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5499 wpa_s->p2p_join_ssid_len));
5500 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5501 wpa_s->p2p_join_ssid,
5502 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005503 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005504 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5505 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5506 bss = wpa_bss_get_bssid_latest(wpa_s,
5507 wpa_s->pending_join_iface_addr);
5508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005510 u8 dev_addr[ETH_ALEN];
5511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512 freq = bss->freq;
5513 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005514 "from BSS table: %d MHz (SSID %s)", freq,
5515 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005516 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005517 dev_addr) == 0 &&
5518 os_memcmp(wpa_s->pending_join_dev_addr,
5519 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5520 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5521 ETH_ALEN) != 0) {
5522 wpa_printf(MSG_DEBUG,
5523 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5524 MAC2STR(dev_addr),
5525 MAC2STR(wpa_s->pending_join_dev_addr));
5526 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5527 ETH_ALEN);
5528 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 }
5530 if (freq > 0) {
5531 u16 method;
5532
Dmitry Shmidt04949592012-07-19 12:16:46 -07005533 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005534 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005535 P2P_EVENT_GROUP_FORMATION_FAILURE
5536 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005537 wpas_notify_p2p_group_formation_failure(
5538 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005539 return;
5540 }
5541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5543 "prior to joining an existing group (GO " MACSTR
5544 " freq=%u MHz)",
5545 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5546 wpa_s->pending_pd_before_join = 1;
5547
5548 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005549 case WPS_PIN_DISPLAY:
5550 method = WPS_CONFIG_KEYPAD;
5551 break;
5552 case WPS_PIN_KEYPAD:
5553 method = WPS_CONFIG_DISPLAY;
5554 break;
5555 case WPS_PBC:
5556 method = WPS_CONFIG_PUSHBUTTON;
5557 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005558 case WPS_P2PS:
5559 method = WPS_CONFIG_P2PS;
5560 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005561 default:
5562 method = 0;
5563 break;
5564 }
5565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005566 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5567 wpa_s->pending_join_dev_addr) ==
5568 method)) {
5569 /*
5570 * We have already performed provision discovery for
5571 * joining the group. Proceed directly to join
5572 * operation without duplicated provision discovery. */
5573 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5574 "with " MACSTR " already done - proceed to "
5575 "join",
5576 MAC2STR(wpa_s->pending_join_dev_addr));
5577 wpa_s->pending_pd_before_join = 0;
5578 goto start;
5579 }
5580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005581 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005582 wpa_s->pending_join_dev_addr,
5583 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005584 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005585 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5586 "Discovery Request before joining an "
5587 "existing group");
5588 wpa_s->pending_pd_before_join = 0;
5589 goto start;
5590 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 return;
5592 }
5593
5594 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5595 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5596 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5597 wpas_p2p_check_join_scan_limit(wpa_s);
5598 return;
5599
5600start:
5601 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005602 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5603 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604}
5605
5606
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005607static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5608 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610 int ret;
5611 struct wpa_driver_scan_params params;
5612 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005613 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005614 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005615 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005616
5617 os_memset(&params, 0, sizeof(params));
5618
5619 /* P2P Wildcard SSID */
5620 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005621 if (ssid && ssid_len) {
5622 params.ssids[0].ssid = ssid;
5623 params.ssids[0].ssid_len = ssid_len;
5624 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5625 wpa_s->p2p_join_ssid_len = ssid_len;
5626 } else {
5627 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5628 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5629 wpa_s->p2p_join_ssid_len = 0;
5630 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005631
5632 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005633 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5634 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5635 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636 if (wps_ie == NULL) {
5637 wpas_p2p_scan_res_join(wpa_s, NULL);
5638 return;
5639 }
5640
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005641 if (!freq) {
5642 int oper_freq;
5643 /*
5644 * If freq is not provided, check the operating freq of the GO
5645 * and use a single channel scan on if possible.
5646 */
5647 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5648 wpa_s->pending_join_iface_addr);
5649 if (oper_freq > 0)
5650 freq = oper_freq;
5651 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005652 if (freq > 0) {
5653 freqs[0] = freq;
5654 params.freqs = freqs;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005655 } else {
5656 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005658
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005659 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5660 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5661 if (ies == NULL) {
5662 wpabuf_free(wps_ie);
5663 wpas_p2p_scan_res_join(wpa_s, NULL);
5664 return;
5665 }
5666 wpabuf_put_buf(ies, wps_ie);
5667 wpabuf_free(wps_ie);
5668
5669 bands = wpas_get_bands(wpa_s, freqs);
5670 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5671
5672 params.p2p_probe = 1;
5673 params.extra_ies = wpabuf_head(ies);
5674 params.extra_ies_len = wpabuf_len(ies);
5675
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005676 if (wpa_s->clear_driver_scan_cache) {
5677 wpa_printf(MSG_DEBUG,
5678 "Request driver to clear scan cache due to local BSS flush");
5679 params.only_new_results = 1;
5680 }
5681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005682 /*
5683 * Run a scan to update BSS table and start Provision Discovery once
5684 * the new scan results become available.
5685 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005686 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005687 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005688 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005689 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005690 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005691 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005692 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005693 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695
5696 wpabuf_free(ies);
5697
5698 if (ret) {
5699 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5700 "try again later");
5701 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5702 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5703 wpas_p2p_check_join_scan_limit(wpa_s);
5704 }
5705}
5706
5707
Dmitry Shmidt04949592012-07-19 12:16:46 -07005708static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5709{
5710 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005711 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005712}
5713
5714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005716 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005717 int auto_join, int op_freq,
5718 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005719{
5720 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005721 MACSTR " dev " MACSTR " op_freq=%d)%s",
5722 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005723 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005724 if (ssid && ssid_len) {
5725 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5726 wpa_ssid_txt(ssid, ssid_len));
5727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005728
Dmitry Shmidt04949592012-07-19 12:16:46 -07005729 wpa_s->p2p_auto_pd = 0;
5730 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5732 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5733 wpa_s->pending_join_wps_method = wps_method;
5734
5735 /* Make sure we are not running find during connection establishment */
5736 wpas_p2p_stop_find(wpa_s);
5737
5738 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005739 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740 return 0;
5741}
5742
5743
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005744static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5745 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746{
5747 struct wpa_supplicant *group;
5748 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005749 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005750
5751 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5752 if (group == NULL)
5753 return -1;
5754 if (group != wpa_s) {
5755 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5756 sizeof(group->p2p_pin));
5757 group->p2p_wps_method = wpa_s->p2p_wps_method;
5758 }
5759
Hai Shalom74f70d42019-02-11 14:42:39 -08005760 /*
5761 * Need to mark the current interface for p2p_group_formation
5762 * when a separate group interface is not used. This is needed
5763 * to allow p2p_cancel stop a pending p2p_connect-join.
5764 * wpas_p2p_init_group_interface() addresses this for the case
5765 * where a separate group interface is used.
5766 */
5767 if (group == wpa_s->parent)
5768 wpa_s->global->p2p_group_formation = group;
5769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005771 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772
5773 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005774 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5776 ETH_ALEN);
5777 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005778 if (freq && ssid && ssid_len) {
5779 res.freq = freq;
5780 res.ssid_len = ssid_len;
5781 os_memcpy(res.ssid, ssid, ssid_len);
5782 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005783 if (ssid && ssid_len) {
5784 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5785 ssid, ssid_len);
5786 } else {
5787 bss = wpa_bss_get_bssid_latest(
5788 wpa_s, wpa_s->pending_join_iface_addr);
5789 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005790 if (bss) {
5791 res.freq = bss->freq;
5792 res.ssid_len = bss->ssid_len;
5793 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5794 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5795 bss->freq,
5796 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005797 } else if (ssid && ssid_len) {
5798 res.ssid_len = ssid_len;
5799 os_memcpy(res.ssid, ssid, ssid_len);
5800 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5801 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005802 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005803 }
5804
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005805 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5806 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5807 "starting client");
5808 wpa_drv_cancel_remain_on_channel(wpa_s);
5809 wpa_s->off_channel_freq = 0;
5810 wpa_s->roc_waiting_drv_freq = 0;
5811 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812 wpas_start_wps_enrollee(group, &res);
5813
5814 /*
5815 * Allow a longer timeout for join-a-running-group than normal 15
5816 * second group formation timeout since the GO may not have authorized
5817 * our connection yet.
5818 */
5819 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5820 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5821 wpa_s, NULL);
5822
5823 return 0;
5824}
5825
5826
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005827static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005828 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005829 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005830 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005831{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005832 struct wpa_used_freq_data *freqs;
5833 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005834 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305835 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005836
5837 max_pref_freq = *num_pref_freq;
5838 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005839
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005840 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5841 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005842 if (!freqs)
5843 return -1;
5844
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005845 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5846 wpa_s->num_multichan_concurrent);
5847
5848 /*
5849 * It is possible that the total number of used frequencies is bigger
5850 * than the number of frequencies used for P2P, so get the system wide
5851 * number of unused frequencies.
5852 */
5853 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5854
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005855 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005856 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5857 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005858
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005859 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005860 int ret;
5861 if (go)
5862 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5863 else
5864 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5865 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005866 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005867 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5868 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005869 /*
5870 * If freq is a DFS channel and DFS is offloaded
5871 * to the driver, allow P2P GO to use it.
5872 */
5873 wpa_printf(MSG_DEBUG,
5874 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5875 freq);
5876 } else {
5877 wpa_printf(MSG_DEBUG,
5878 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5879 freq);
5880 res = -3;
5881 goto exit_free;
5882 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005883 }
5884
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005885 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005886 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005887 freq_in_use = 1;
5888 }
5889
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005890 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005891 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5892 freq);
5893 res = -2;
5894 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005895 }
5896 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5897 "requested channel (%u MHz)", freq);
5898 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005899 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005900 }
5901
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005902 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005903
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305904 if (*pref_freq == 0) {
5905 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005906 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305907 while (i < wpa_s->conf->num_p2p_pref_chan) {
5908 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5909 wpa_s->conf->p2p_pref_chan[i].op_class,
5910 wpa_s->conf->p2p_pref_chan[i].chan);
5911
5912 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5913 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5914 best_freq = p2p_pref_freq;
5915 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5916 "from P2P preferred channel list", best_freq);
5917 break;
5918 } else {
5919 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5920 "frequency (%u MHz) ", p2p_pref_freq);
5921 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005922 i++;
5923 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305924 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305925 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
Sunil Ravi036cec52023-03-29 11:35:17 -07005926 best_freq, go);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305927
Sunil Ravi036cec52023-03-29 11:35:17 -07005928 *num_pref_freq = max_pref_freq;
5929 res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
5930 num_pref_freq);
5931 if (res > 0)
5932 best_freq = res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005933 }
5934 }
5935
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005936 /* We have a candidate frequency to use */
5937 if (best_freq > 0) {
5938 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005939 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005940 best_freq);
5941 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005942 } else {
5943 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 -07005944 best_freq);
5945 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005946 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005947 } else if (num_unused > 0) {
5948 wpa_printf(MSG_DEBUG,
5949 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5950 *force_freq = 0;
5951 } else {
5952 wpa_printf(MSG_DEBUG,
5953 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5954 res = -2;
5955 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005956 }
5957
5958exit_ok:
5959 res = 0;
5960exit_free:
5961 os_free(freqs);
5962 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005963}
5964
5965
Hai Shalomc1a21442022-02-04 13:43:00 -08005966static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5967 const u8 *peer_addr)
5968{
5969 if (wpa_s->conf->p2p_6ghz_disable ||
5970 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5971 HOSTAPD_MODE_IEEE80211A, true))
5972 return false;
5973
5974 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5975 return false;
5976 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5977 return false;
5978
5979 return true;
5980}
5981
5982
5983static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5984 const u8 *peer_addr, bool allow_6ghz, int freq)
5985{
5986 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5987 wpa_printf(MSG_DEBUG,
5988 "P2P: Allow connection on 6 GHz channels");
5989 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5990 } else {
5991 if (is_6ghz_freq(freq))
5992 return -2;
5993 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5994 }
5995
5996 return 0;
5997}
5998
5999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000/**
6001 * wpas_p2p_connect - Request P2P Group Formation to be started
6002 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6003 * @peer_addr: Address of the peer P2P Device
6004 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
6005 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07006006 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007 * @join: Whether to join an existing group (as a client) instead of starting
6008 * Group Owner negotiation; @peer_addr is BSSID in that case
6009 * @auth: Whether to only authorize the connection instead of doing that and
6010 * initiating Group Owner negotiation
6011 * @go_intent: GO Intent or -1 to use default
6012 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006013 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07006014 * @persistent_id: Persistent group credentials to use for forcing GO
6015 * parameters or -1 to generate new values (SSID/passphrase)
6016 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6017 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006018 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006019 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006020 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07006021 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006022 * @group_ssid: Specific Group SSID for join or %NULL if not set
6023 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08006024 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006025 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6026 * failure, -2 on failure due to channel not currently available,
6027 * -3 if forced channel is not supported
6028 */
6029int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6030 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006031 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006032 int go_intent, int freq, unsigned int vht_center_freq2,
6033 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006034 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006035 const u8 *group_ssid, size_t group_ssid_len,
6036 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006038 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006039 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006040 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006041 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006042 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00006043 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6044 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006045
6046 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6047 return -1;
6048
Dmitry Shmidt04949592012-07-19 12:16:46 -07006049 if (persistent_id >= 0) {
6050 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6051 if (ssid == NULL || ssid->disabled != 2 ||
6052 ssid->mode != WPAS_MODE_P2P_GO)
6053 return -1;
6054 }
6055
Hai Shalomc1a21442022-02-04 13:43:00 -08006056 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006057 return -2;
6058
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006059 os_free(wpa_s->global->add_psk);
6060 wpa_s->global->add_psk = NULL;
6061
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006062 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006063 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006064 wpa_s->global->pending_p2ps_group_freq = 0;
6065 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067 if (go_intent < 0)
6068 go_intent = wpa_s->conf->p2p_go_intent;
6069
6070 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006071 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006072
6073 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006074 wpa_s->p2p_persistent_group = !!persistent_group;
6075 wpa_s->p2p_persistent_id = persistent_id;
6076 wpa_s->p2p_go_intent = go_intent;
6077 wpa_s->p2p_connect_freq = freq;
6078 wpa_s->p2p_fallback_to_go_neg = 0;
6079 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006080 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006081 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006082 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6083 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006084 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006085 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086
6087 if (pin)
6088 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6089 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006090 if (wps_generate_pin((unsigned int *) &ret) < 0)
6091 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006092 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6093 "%08d", ret);
6094 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6095 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006096 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6097 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006098 } else if (wps_method == WPS_P2PS) {
6099 /* Force the P2Ps default PIN to be used */
6100 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006101 } else
6102 wpa_s->p2p_pin[0] = '\0';
6103
Dmitry Shmidt04949592012-07-19 12:16:46 -07006104 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6106 if (auth) {
6107 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6108 "connect a running group from " MACSTR,
6109 MAC2STR(peer_addr));
6110 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6111 return ret;
6112 }
6113 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6114 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6115 iface_addr) < 0) {
6116 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6117 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6118 dev_addr);
6119 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006120 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006121 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006122 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6123 "%ld.%06ld",
6124 wpa_s->p2p_auto_started.sec,
6125 wpa_s->p2p_auto_started.usec);
6126 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006127 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006128 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006129 auto_join, freq,
6130 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006131 return -1;
6132 return ret;
6133 }
6134
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006135 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006136 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006137 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006138 if (res)
6139 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006140 wpas_p2p_set_own_freq_preference(wpa_s,
6141 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006143 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006145 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006147 if (wpa_s->create_p2p_iface) {
6148 /* Prepare to add a new interface for the group */
6149 iftype = WPA_IF_P2P_GROUP;
6150 if (go_intent == 15)
6151 iftype = WPA_IF_P2P_GO;
6152 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6153 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6154 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006155 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006157
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006158 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006159 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006160 if (wpa_s->p2p_mgmt)
6161 if_addr = wpa_s->parent->own_addr;
6162 else
6163 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006164 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6165 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006166
6167 if (auth) {
6168 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006169 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006170 force_freq, persistent_group, ssid,
6171 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172 return -1;
6173 return ret;
6174 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006175
6176 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6177 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006178 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006179 if (wpa_s->create_p2p_iface)
6180 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181 return -1;
6182 }
6183 return ret;
6184}
6185
6186
6187/**
6188 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6189 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6190 * @freq: Frequency of the channel in MHz
6191 * @duration: Duration of the stay on the channel in milliseconds
6192 *
6193 * This callback is called when the driver indicates that it has started the
6194 * requested remain-on-channel duration.
6195 */
6196void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6197 unsigned int freq, unsigned int duration)
6198{
6199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6200 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006201 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)",
6202 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6203 wpa_s->roc_waiting_drv_freq, freq, duration);
6204 if (wpa_s->off_channel_freq &&
6205 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6207 wpa_s->pending_listen_duration);
6208 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006209 } else {
6210 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6211 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6212 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006213 }
6214}
6215
6216
Jithu Jance57cea1a2014-08-20 10:22:04 -07006217int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006218{
6219 /* Limit maximum Listen state time based on driver limitation. */
6220 if (timeout > wpa_s->max_remain_on_chan)
6221 timeout = wpa_s->max_remain_on_chan;
6222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006223 return p2p_listen(wpa_s->global->p2p, timeout);
6224}
6225
6226
6227/**
6228 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6229 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6230 * @freq: Frequency of the channel in MHz
6231 *
6232 * This callback is called when the driver indicates that a remain-on-channel
6233 * operation has been completed, i.e., the duration on the requested channel
6234 * has timed out.
6235 */
6236void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6237 unsigned int freq)
6238{
6239 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6240 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006241 wpa_s->global->p2p_long_listen,
6242 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006243 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006244 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6245 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006246 if (wpa_s->global->p2p_long_listen > 0)
6247 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6249 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006250 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006252 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006253 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006254 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006255 } else {
6256 /*
6257 * When listen duration is over, stop listen & update p2p_state
6258 * to IDLE.
6259 */
6260 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006261 }
6262}
6263
6264
6265/**
6266 * wpas_p2p_group_remove - Remove a P2P group
6267 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6268 * @ifname: Network interface name of the group interface or "*" to remove all
6269 * groups
6270 * Returns: 0 on success, -1 on failure
6271 *
6272 * This function is used to remove a P2P group. This can be used to disconnect
6273 * from a group in which the local end is a P2P Client or to end a P2P Group in
6274 * case the local end is the Group Owner. If a virtual network interface was
6275 * created for this group, that interface will be removed. Otherwise, only the
6276 * configured P2P group network will be removed from the interface.
6277 */
6278int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6279{
6280 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006281 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282
6283 if (os_strcmp(ifname, "*") == 0) {
6284 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006285 bool calling_wpa_s_group_removed = false;
6286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006287 wpa_s = global->ifaces;
6288 while (wpa_s) {
6289 prev = wpa_s;
6290 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006291 if (prev->p2p_group_interface !=
6292 NOT_P2P_GROUP_INTERFACE ||
6293 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006294 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006295 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006296 if (prev == calling_wpa_s)
6297 calling_wpa_s_group_removed = true;
6298 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006299 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006300
6301 if (!calling_wpa_s_group_removed &&
6302 (calling_wpa_s->p2p_group_interface !=
6303 NOT_P2P_GROUP_INTERFACE ||
6304 (calling_wpa_s->current_ssid &&
6305 calling_wpa_s->current_ssid->p2p_group))) {
6306 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6307 wpas_p2p_disconnect_safely(calling_wpa_s,
6308 calling_wpa_s);
6309 }
6310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006311 return 0;
6312 }
6313
6314 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6315 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6316 break;
6317 }
6318
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006319 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006320}
6321
6322
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006323static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6324{
6325 unsigned int r;
6326
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006327 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6328 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006329 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006330 int res;
6331
6332 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6333 &size, pref_freq_list);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006334 if (!res && size > 0 && !is_p2p_allow_6ghz(wpa_s->global->p2p))
Hai Shalomc1a21442022-02-04 13:43:00 -08006335 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6336
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006337 if (!res && size > 0) {
6338 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006339 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006340 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006341 pref_freq_list[i].freq) ||
6342 wpas_p2p_disallowed_freq(
6343 wpa_s->global,
6344 pref_freq_list[i].freq) ||
6345 !p2p_pref_freq_allowed(&pref_freq_list[i],
6346 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006347 wpa_printf(MSG_DEBUG,
6348 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006349 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006350 i++;
6351 }
6352 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006353 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006354 wpa_printf(MSG_DEBUG,
6355 "P2P: Using preferred_freq_list[%d]=%d",
6356 i, freq);
6357 } else {
6358 wpa_printf(MSG_DEBUG,
6359 "P2P: All driver preferred frequencies are disallowed for P2P use");
6360 }
6361 } else {
6362 wpa_printf(MSG_DEBUG,
6363 "P2P: No preferred frequency list available");
6364 }
6365 }
6366
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006367 if (freq == 2) {
6368 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6369 "band");
6370 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006371 p2p_supported_freq_go(wpa_s->global->p2p,
6372 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006373 freq = wpa_s->best_24_freq;
6374 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6375 "channel: %d MHz", freq);
6376 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006377 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6378 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006379 int possible_2g_freqs[] = {
6380 /* operating class 81 */
6381 2412, 2437, 2462,
6382 };
6383 int possible_2g_freqs_num =
6384 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6385 int i;
6386 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6387 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6388 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6389 break;
6390 }
6391 }
6392
6393 if (i >= possible_2g_freqs_num) {
6394 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6395 "2.4 GHz channel for P2P group");
6396 return -1;
6397 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006398 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6399 "channel: %d MHz", freq);
6400 }
6401 }
6402
6403 if (freq == 5) {
6404 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6405 "band");
6406 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006407 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006408 wpa_s->best_5_freq)) {
6409 freq = wpa_s->best_5_freq;
6410 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6411 "channel: %d MHz", freq);
6412 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006413 const int freqs[] = {
6414 /* operating class 115 */
6415 5180, 5200, 5220, 5240,
6416 /* operating class 124 */
6417 5745, 5765, 5785, 5805,
6418 };
6419 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6420
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006421 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6422 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006423
6424 /*
6425 * most of 5G channels are DFS, only operating class 115 and 124
6426 * are available possibly, randomly pick a start to check them.
6427 */
6428 int possible_5g_freqs[] = {
6429 /* operating class 115 */
6430 5180, 5200, 5220, 5240,
6431 /* operating class 124 */
6432 5745, 5765, 5785, 5805,
6433 };
6434 int possible_5g_freqs_num =
6435 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6436
Jimmy Chen64b90632020-09-29 17:27:34 +08006437 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6438 if (p2p_supported_freq_go(
6439 wpa_s->global->p2p,
6440 possible_5g_freqs[r % possible_5g_freqs_num])) {
6441 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6442 break;
6443 }
6444 }
6445
6446 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006447 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6448 "5 GHz channel for P2P group");
6449 return -1;
6450 }
6451 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6452 "channel: %d MHz", freq);
6453 }
6454 }
6455
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006456 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006457 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006458 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6459 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006460 /*
6461 * If freq is a DFS channel and DFS is offloaded to the
6462 * driver, allow P2P GO to use it.
6463 */
6464 wpa_printf(MSG_DEBUG, "P2P: "
6465 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6466 __func__, freq);
6467 return freq;
6468 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006469 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6470 "(%u MHz) is not supported for P2P uses",
6471 freq);
6472 return -1;
6473 }
6474
6475 return freq;
6476}
6477
6478
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006479static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6480 const struct p2p_channels *channels,
6481 int freq)
6482{
6483 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6484 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6485 freq_included(wpa_s, channels, freq))
6486 return 1;
6487 return 0;
6488}
6489
6490
6491static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6492 struct p2p_go_neg_results *params,
6493 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006494{
6495 unsigned int i, r;
6496
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006497 /* try all channels in operating class 115 */
6498 for (i = 0; i < 4; i++) {
6499 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006500 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006501 goto out;
6502 }
6503
6504 /* try all channels in operating class 124 */
6505 for (i = 0; i < 4; i++) {
6506 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006507 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006508 goto out;
6509 }
6510
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006511 /* try social channel class 180 channel 2 */
6512 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006513 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006514 goto out;
6515
6516 /* try all channels in reg. class 180 */
6517 for (i = 0; i < 4; i++) {
6518 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006519 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006520 goto out;
6521 }
6522
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006523 /* try some random selection of the social channels */
6524 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6525 return;
6526
6527 for (i = 0; i < 3; i++) {
6528 params->freq = 2412 + ((r + i) % 3) * 25;
6529 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6530 goto out;
6531 }
6532
6533 /* try all other channels in operating class 81 */
6534 for (i = 0; i < 11; i++) {
6535 params->freq = 2412 + i * 5;
6536
6537 /* skip social channels; covered in the previous loop */
6538 if (params->freq == 2412 ||
6539 params->freq == 2437 ||
6540 params->freq == 2462)
6541 continue;
6542
6543 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6544 goto out;
6545 }
6546
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006547 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006548 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006549 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006550out:
6551 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6552 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006553}
6554
6555
Roshan Pius3a1667e2018-07-03 15:17:14 -07006556static int wpas_same_band(int freq1, int freq2)
6557{
6558 enum hostapd_hw_mode mode1, mode2;
6559 u8 chan1, chan2;
6560
6561 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6562 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6563 if (mode1 == NUM_HOSTAPD_MODES)
6564 return 0;
6565 return mode1 == mode2;
6566}
6567
6568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006569static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6570 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006571 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006572 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006573 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006574 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006575{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006576 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006577 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006578 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006579 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006580 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006581
6582 os_memset(params, 0, sizeof(*params));
6583 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006584 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006585 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006586 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006587 params->max_oper_chwidth = max_oper_chwidth;
6588 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006589 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006591 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6592 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006593 if (!freqs)
6594 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006596 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006597 wpa_s->num_multichan_concurrent,
6598 false);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006599
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006600 if (wpa_s->current_ssid &&
6601 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6602 wpa_s->wpa_state == WPA_COMPLETED) {
6603 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6604 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006605
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006606 /*
6607 * If the frequency selection is done for an active P2P GO that
6608 * is not sharing a frequency, allow to select a new frequency
6609 * even if there are no unused frequencies as we are about to
6610 * move the P2P GO so its frequency can be re-used.
6611 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006612 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006613 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6614 freqs[i].flags == 0) {
6615 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006616 break;
6617 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006618 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006619 }
6620
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006621 /* Try to use EDMG channel */
6622 if (params->edmg) {
6623 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6624 goto success;
6625 params->edmg = 0;
6626 }
6627
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006628 /* try using the forced freq */
6629 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006630 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6631 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006632 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006633 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006634 freq);
6635 goto fail;
6636 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006637 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6638 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006639 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6640 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006641 /*
6642 * If freq is a DFS channel and DFS is offloaded
6643 * to the driver, allow P2P GO to use it.
6644 */
6645 wpa_printf(MSG_DEBUG,
6646 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6647 __func__, freq);
6648 } else {
6649 wpa_printf(MSG_DEBUG,
6650 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6651 freq);
6652 goto fail;
6653 }
6654 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006655
6656 for (i = 0; i < num; i++) {
6657 if (freqs[i].freq == freq) {
6658 wpa_printf(MSG_DEBUG,
6659 "P2P: forced freq (%d MHz) is also shared",
6660 freq);
6661 params->freq = freq;
6662 goto success;
6663 }
6664 }
6665
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006666 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006667 wpa_printf(MSG_DEBUG,
6668 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6669 freq);
6670 goto fail;
6671 }
6672
6673 wpa_printf(MSG_DEBUG,
6674 "P2P: force GO freq (%d MHz) on a free channel",
6675 freq);
6676 params->freq = freq;
6677 goto success;
6678 }
6679
6680 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006681 if (num &&
6682 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006683 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6684 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6685 wpa_printf(MSG_DEBUG,
6686 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006687 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006688 params->freq = cand;
6689 goto success;
6690 }
6691
6692 /* try using one of the shared freqs */
6693 for (i = 0; i < num; i++) {
6694 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6695 freqs[i].freq)) {
6696 wpa_printf(MSG_DEBUG,
6697 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006698 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006699 params->freq = freqs[i].freq;
6700 goto success;
6701 }
6702 }
6703 }
6704
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006705 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006706 wpa_printf(MSG_DEBUG,
6707 "P2P: Cannot force GO on any of the channels we are already using");
6708 goto fail;
6709 }
6710
6711 /* try using the setting from the configuration file */
6712 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6713 wpa_s->conf->p2p_oper_channel >= 1 &&
6714 wpa_s->conf->p2p_oper_channel <= 11 &&
6715 wpas_p2p_supported_freq_go(
6716 wpa_s, channels,
6717 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6718 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6719 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6720 "frequency %d MHz", params->freq);
6721 goto success;
6722 }
6723
6724 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6725 wpa_s->conf->p2p_oper_reg_class == 116 ||
6726 wpa_s->conf->p2p_oper_reg_class == 117 ||
6727 wpa_s->conf->p2p_oper_reg_class == 124 ||
6728 wpa_s->conf->p2p_oper_reg_class == 125 ||
6729 wpa_s->conf->p2p_oper_reg_class == 126 ||
6730 wpa_s->conf->p2p_oper_reg_class == 127) &&
6731 wpas_p2p_supported_freq_go(wpa_s, channels,
6732 5000 +
6733 5 * wpa_s->conf->p2p_oper_channel)) {
6734 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6735 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6736 "frequency %d MHz", params->freq);
6737 goto success;
6738 }
6739
6740 /* Try using best channels */
6741 if (wpa_s->conf->p2p_oper_channel == 0 &&
6742 wpa_s->best_overall_freq > 0 &&
6743 wpas_p2p_supported_freq_go(wpa_s, channels,
6744 wpa_s->best_overall_freq)) {
6745 params->freq = wpa_s->best_overall_freq;
6746 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6747 "channel %d MHz", params->freq);
6748 goto success;
6749 }
6750
6751 if (wpa_s->conf->p2p_oper_channel == 0 &&
6752 wpa_s->best_24_freq > 0 &&
6753 wpas_p2p_supported_freq_go(wpa_s, channels,
6754 wpa_s->best_24_freq)) {
6755 params->freq = wpa_s->best_24_freq;
6756 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6757 "channel %d MHz", params->freq);
6758 goto success;
6759 }
6760
6761 if (wpa_s->conf->p2p_oper_channel == 0 &&
6762 wpa_s->best_5_freq > 0 &&
6763 wpas_p2p_supported_freq_go(wpa_s, channels,
6764 wpa_s->best_5_freq)) {
6765 params->freq = wpa_s->best_5_freq;
6766 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6767 "channel %d MHz", params->freq);
6768 goto success;
6769 }
6770
6771 /* try using preferred channels */
6772 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6773 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6774 params->freq = cand;
6775 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6776 "channels", params->freq);
6777 goto success;
6778 }
6779
Roshan Pius3a1667e2018-07-03 15:17:14 -07006780 /* Try using a channel that allows VHT to be used with 80 MHz */
6781 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6782 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6783 enum hostapd_hw_mode mode;
6784 struct hostapd_hw_modes *hwmode;
6785 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006786 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006787
6788 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006789 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006790 mode = ieee80211_freq_to_chan(cand, &chan);
6791 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006792 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006793 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006794 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6795 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006796 continue;
6797 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6798 params->freq = cand;
6799 wpa_printf(MSG_DEBUG,
6800 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6801 params->freq);
6802 goto success;
6803 }
6804 }
6805 }
6806
6807 /* Try using a channel that allows HT to be used with 40 MHz on the same
6808 * band so that CSA can be used */
6809 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6810 wpa_s->p2p_group_common_freqs) {
6811 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6812 enum hostapd_hw_mode mode;
6813 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006814 u8 chan, op_class;
6815 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006816
Hai Shalomc1a21442022-02-04 13:43:00 -08006817 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006818 cand = wpa_s->p2p_group_common_freqs[i];
6819 mode = ieee80211_freq_to_chan(cand, &chan);
6820 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006821 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006822 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6823 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006824 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006825 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006826 if (is_6ghz &&
6827 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6828 BW40) == ALLOWED)
6829 supported = true;
6830
6831 if (!is_6ghz &&
6832 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006833 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6834 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006835 wpas_p2p_verify_channel(
6836 wpa_s, hwmode, op_class, chan,
6837 BW40MINUS) == ALLOWED)
6838 supported = true;
6839
6840 if (!supported && !is_6ghz &&
6841 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006842 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6843 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006844 wpas_p2p_verify_channel(
6845 wpa_s, hwmode, op_class, chan,
6846 BW40PLUS) == ALLOWED)
6847 supported = true;
6848
6849 if (!supported)
6850 continue;
6851
Roshan Pius3a1667e2018-07-03 15:17:14 -07006852 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6853 params->freq = cand;
6854 wpa_printf(MSG_DEBUG,
6855 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6856 params->freq);
6857 goto success;
6858 }
6859 }
6860 }
6861
6862 /* Try using one of the group common freqs on the same band so that CSA
6863 * can be used */
6864 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6865 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6866 cand = wpa_s->p2p_group_common_freqs[i];
6867 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6868 cand))
6869 continue;
6870 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6871 params->freq = cand;
6872 wpa_printf(MSG_DEBUG,
6873 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6874 params->freq);
6875 goto success;
6876 }
6877 }
6878 }
6879
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006880 /* Try using one of the group common freqs */
6881 if (wpa_s->p2p_group_common_freqs) {
6882 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6883 cand = wpa_s->p2p_group_common_freqs[i];
6884 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6885 params->freq = cand;
6886 wpa_printf(MSG_DEBUG,
6887 "P2P: Use freq %d MHz common with the peer",
6888 params->freq);
6889 goto success;
6890 }
6891 }
6892 }
6893
6894 /* no preference, select some channel */
6895 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6896
6897 if (params->freq == 0) {
6898 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6899 goto fail;
6900 }
6901
6902success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006903 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006904 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006905fail:
6906 os_free(freqs);
6907 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006908}
6909
6910
6911static struct wpa_supplicant *
6912wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6913 int go)
6914{
6915 struct wpa_supplicant *group_wpa_s;
6916
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006917 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006918 if (wpa_s->p2p_mgmt) {
6919 /*
6920 * We may be called on the p2p_dev interface which
6921 * cannot be used for group operations, so always use
6922 * the primary interface.
6923 */
6924 wpa_s->parent->p2pdev = wpa_s;
6925 wpa_s = wpa_s->parent;
6926 }
6927 wpa_dbg(wpa_s, MSG_DEBUG,
6928 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006929 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006930 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006931 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006932 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934
6935 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006936 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006937 wpa_msg_global(wpa_s, MSG_ERROR,
6938 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006939 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006940 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6942 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006943 wpa_msg_global(wpa_s, MSG_ERROR,
6944 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006945 wpas_p2p_remove_pending_group_interface(wpa_s);
6946 return NULL;
6947 }
6948
Roshan Pius3a1667e2018-07-03 15:17:14 -07006949 if (go && wpa_s->p2p_go_do_acs) {
6950 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6951 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6952 wpa_s->p2p_go_do_acs = 0;
6953 }
6954
Hai Shalomc1a21442022-02-04 13:43:00 -08006955 if (go && wpa_s->p2p_go_allow_dfs) {
6956 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6957 wpa_s->p2p_go_allow_dfs = 0;
6958 }
6959
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6961 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006962 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006963 return group_wpa_s;
6964}
6965
6966
6967/**
6968 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6969 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6970 * @persistent_group: Whether to create a persistent group
6971 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006972 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006973 * @ht40: Start GO with 40 MHz channel width
6974 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006975 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006976 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006977 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006978 * Returns: 0 on success, -1 on failure
6979 *
6980 * This function creates a new P2P group with the local end as the Group Owner,
6981 * i.e., without using Group Owner Negotiation.
6982 */
6983int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006984 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006985 int max_oper_chwidth, int he, int edmg,
6986 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006987{
6988 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006989 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006990
6991 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6992 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006993 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6994 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006996 os_free(wpa_s->global->add_psk);
6997 wpa_s->global->add_psk = NULL;
6998
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006999 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007000 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007001 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07007002
Roshan Pius3a1667e2018-07-03 15:17:14 -07007003 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00007004 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
7005 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007006 return -1;
7007 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007008
Sunil Ravi77a0f092022-10-03 00:53:41 +00007009 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007010 ht40, vht, max_oper_chwidth, he, edmg,
7011 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007012 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007014 p2p_go_params(wpa_s->global->p2p, &params);
7015 params.persistent_group = persistent_group;
7016
7017 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7018 if (wpa_s == NULL)
7019 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007020 if (freq > 0)
7021 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007022 wpas_start_wps_go(wpa_s, &params, 0);
7023
7024 return 0;
7025}
7026
7027
7028static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007029 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00007030 int freq, int force_scan, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007031 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007032{
7033 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07007034 int other_iface_found = 0;
7035 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007036
7037 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
7038 if (wpa_s == NULL)
7039 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007040 if (force_scan)
7041 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007042 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043
7044 wpa_supplicant_ap_deinit(wpa_s);
7045
7046 ssid = wpa_config_add_network(wpa_s->conf);
7047 if (ssid == NULL)
7048 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007049 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050 wpa_config_set_network_defaults(ssid);
7051 ssid->temporary = 1;
7052 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007053 ssid->pbss = params->pbss;
7054 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7055 WPA_CIPHER_CCMP;
7056 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Sunil Ravi036cec52023-03-29 11:35:17 -07007058 if (is_6ghz_freq(freq) &&
7059 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
7060 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
7061 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
7062 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
7063 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7064 wpa_dbg(wpa_s, MSG_DEBUG,
7065 "P2P: Enable SAE auth_alg and key_mgmt");
7066 }
7067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007068 ssid->ssid = os_malloc(params->ssid_len);
7069 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 wpa_config_remove_network(wpa_s->conf, ssid->id);
7071 return -1;
7072 }
7073 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7074 ssid->ssid_len = params->ssid_len;
7075 ssid->p2p_group = 1;
7076 ssid->export_keys = 1;
7077 if (params->psk_set) {
7078 os_memcpy(ssid->psk, params->psk, 32);
7079 ssid->psk_set = 1;
7080 }
7081 if (params->passphrase)
7082 ssid->passphrase = os_strdup(params->passphrase);
7083
Sunil Ravi036cec52023-03-29 11:35:17 -07007084 if (go_bssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007085 ssid->bssid_set = 1;
Sunil Ravi036cec52023-03-29 11:35:17 -07007086 os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
Matthew Wangdcf19452022-11-07 20:42:52 -08007087 }
7088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007089 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007090 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007091 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007092 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007093 wpa_s->p2p_go_group_formation_completed = 0;
7094 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007095
Matthew Wang9ae940b2022-09-14 21:25:34 -07007096 /*
7097 * Get latest scan results from driver in case cached scan results from
7098 * interfaces on the same wiphy allow us to skip the next scan by fast
7099 * associating. Also update the scan time to the most recent scan result
7100 * fetch time on the same radio so it reflects the actual time the last
7101 * scan result event occurred.
7102 */
7103 wpa_supplicant_update_scan_results(wpa_s);
7104 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7105 radio_list) {
7106 if (ifs == wpa_s)
7107 continue;
7108 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7109 &ifs->last_scan)) {
7110 other_iface_found = 1;
7111 wpa_s->last_scan.sec = ifs->last_scan.sec;
7112 wpa_s->last_scan.usec = ifs->last_scan.usec;
7113 }
7114 }
7115
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007116 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007117 NULL);
7118 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7119 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007120 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007121 wpa_supplicant_select_network(wpa_s, ssid);
7122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007123 return 0;
7124}
7125
7126
7127int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7128 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007129 int force_freq, int neg_freq,
7130 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007131 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007132 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007133 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007134 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00007135 bool allow_6ghz, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007136 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137{
7138 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007139 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140
7141 if (ssid->disabled != 2 || ssid->ssid == NULL)
7142 return -1;
7143
7144 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7145 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7146 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7147 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007148 if (go == 0 &&
7149 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007150 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007151 /*
7152 * This can happen if Invitation Response frame was lost
7153 * and the peer (GO of a persistent group) tries to
7154 * invite us again. Reschedule the timeout to avoid
7155 * terminating the wait for the connection too early
7156 * since we now know that the peer is still trying to
7157 * invite us instead of having already started the GO.
7158 */
7159 wpa_printf(MSG_DEBUG,
7160 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7161 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7162 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007163 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165 return 0;
7166 }
7167
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007168 os_free(wpa_s->global->add_psk);
7169 wpa_s->global->add_psk = NULL;
7170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007171 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007172 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173
Dmitry Shmidt04949592012-07-19 12:16:46 -07007174 wpa_s->p2p_fallback_to_go_neg = 0;
7175
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007176 if (ssid->mode == WPAS_MODE_P2P_GO) {
7177 if (force_freq > 0) {
7178 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7179 if (freq < 0)
7180 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007181 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007182 } else {
7183 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7184 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007185 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007186 freq = 0;
7187 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007188 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007189 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007190 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007191 struct os_reltime now;
7192 struct wpa_bss *bss =
7193 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007194
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007195 os_get_reltime(&now);
7196 if (bss &&
7197 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007198 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007199 freq = bss->freq;
7200 else
7201 freq = 0;
7202 }
7203
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007204 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Sunil Ravi036cec52023-03-29 11:35:17 -07007205 force_scan, retry_limit, go_bssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007206 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007207 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007208 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007209
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007210 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007211 ht40, vht, max_oper_chwidth, he, edmg,
7212 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007213 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007214
7215 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007216 params.psk_set = ssid->psk_set;
7217 if (params.psk_set)
7218 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007219 if (ssid->passphrase) {
7220 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7221 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7222 "persistent group");
7223 return -1;
7224 }
7225 os_strlcpy(params.passphrase, ssid->passphrase,
7226 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007228 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7229 params.ssid_len = ssid->ssid_len;
7230 params.persistent_group = 1;
7231
7232 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7233 if (wpa_s == NULL)
7234 return -1;
7235
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007236 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7237
Dmitry Shmidt56052862013-10-04 10:23:25 -07007238 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239 wpas_start_wps_go(wpa_s, &params, 0);
7240
7241 return 0;
7242}
7243
7244
7245static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7246 struct wpabuf *proberesp_ies)
7247{
7248 struct wpa_supplicant *wpa_s = ctx;
7249 if (wpa_s->ap_iface) {
7250 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007251 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7252 wpabuf_free(beacon_ies);
7253 wpabuf_free(proberesp_ies);
7254 return;
7255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007256 if (beacon_ies) {
7257 wpabuf_free(hapd->p2p_beacon_ie);
7258 hapd->p2p_beacon_ie = beacon_ies;
7259 }
7260 wpabuf_free(hapd->p2p_probe_resp_ie);
7261 hapd->p2p_probe_resp_ie = proberesp_ies;
7262 } else {
7263 wpabuf_free(beacon_ies);
7264 wpabuf_free(proberesp_ies);
7265 }
7266 wpa_supplicant_ap_update_beacon(wpa_s);
7267}
7268
7269
7270static void wpas_p2p_idle_update(void *ctx, int idle)
7271{
7272 struct wpa_supplicant *wpa_s = ctx;
7273 if (!wpa_s->ap_iface)
7274 return;
7275 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007276 if (idle) {
7277 if (wpa_s->global->p2p_fail_on_wps_complete &&
7278 wpa_s->p2p_in_provisioning) {
7279 wpas_p2p_grpform_fail_after_wps(wpa_s);
7280 return;
7281 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007282 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007283 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007284 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7285}
7286
7287
7288struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007289 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007290{
7291 struct p2p_group *group;
7292 struct p2p_group_config *cfg;
7293
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007294 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7295 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007296 return NULL;
7297
7298 cfg = os_zalloc(sizeof(*cfg));
7299 if (cfg == NULL)
7300 return NULL;
7301
Dmitry Shmidt04949592012-07-19 12:16:46 -07007302 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007303 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007304 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007305 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007306 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7307 if (wpa_s->max_stations &&
7308 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7309 cfg->max_clients = wpa_s->max_stations;
7310 else
7311 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007312 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7313 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007314 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007315 cfg->cb_ctx = wpa_s;
7316 cfg->ie_update = wpas_p2p_ie_update;
7317 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007318 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7319 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007320
7321 group = p2p_group_init(wpa_s->global->p2p, cfg);
7322 if (group == NULL)
7323 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007324 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007325 p2p_group_notif_formation_done(group);
7326 wpa_s->p2p_group = group;
7327 return group;
7328}
7329
7330
7331void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7332 int registrar)
7333{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007334 struct wpa_ssid *ssid = wpa_s->current_ssid;
7335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007336 if (!wpa_s->p2p_in_provisioning) {
7337 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7338 "provisioning not in progress");
7339 return;
7340 }
7341
Dmitry Shmidt04949592012-07-19 12:16:46 -07007342 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7343 u8 go_dev_addr[ETH_ALEN];
7344 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7345 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7346 ssid->ssid_len);
7347 /* Clear any stored provisioning info */
7348 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7349 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007350
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007351 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007353 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007354 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7355 /*
7356 * Use a separate timeout for initial data connection to
7357 * complete to allow the group to be removed automatically if
7358 * something goes wrong in this step before the P2P group idle
7359 * timeout mechanism is taken into use.
7360 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007361 wpa_dbg(wpa_s, MSG_DEBUG,
7362 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7363 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007364 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7365 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007366 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007367 /* Complete group formation on successful data connection. */
7368 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007369 } else if (ssid) {
7370 /*
7371 * Use a separate timeout for initial data connection to
7372 * complete to allow the group to be removed automatically if
7373 * the client does not complete data connection successfully.
7374 */
7375 wpa_dbg(wpa_s, MSG_DEBUG,
7376 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7377 P2P_MAX_INITIAL_CONN_WAIT_GO);
7378 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7379 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007380 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007381 /*
7382 * Complete group formation on first successful data connection
7383 */
7384 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007385 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007386 if (wpa_s->global->p2p)
7387 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007388 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007389}
7390
7391
Jouni Malinen75ecf522011-06-27 15:19:46 -07007392void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7393 struct wps_event_fail *fail)
7394{
7395 if (!wpa_s->p2p_in_provisioning) {
7396 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7397 "provisioning not in progress");
7398 return;
7399 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007400
7401 if (wpa_s->go_params) {
7402 p2p_clear_provisioning_info(
7403 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007404 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007405 }
7406
Jouni Malinen75ecf522011-06-27 15:19:46 -07007407 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007408
7409 if (wpa_s == wpa_s->global->p2p_group_formation) {
7410 /*
7411 * Allow some time for the failed WPS negotiation exchange to
7412 * complete, but remove the group since group formation cannot
7413 * succeed after provisioning failure.
7414 */
7415 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7416 wpa_s->global->p2p_fail_on_wps_complete = 1;
7417 eloop_deplete_timeout(0, 50000,
7418 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007419 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007420 }
7421}
7422
7423
7424int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7425{
7426 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7427 !wpa_s->p2p_in_provisioning)
7428 return 0;
7429
7430 wpas_p2p_grpform_fail_after_wps(wpa_s);
7431
7432 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007433}
7434
7435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007436int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007437 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007438 enum wpas_p2p_prov_disc_use use,
7439 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007440{
7441 u16 config_methods;
7442
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007443 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007444 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007445 wpa_s->p2p_fallback_to_go_neg = 0;
7446 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007447 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7448 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007449 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7450 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7451
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007452 wpa_printf(MSG_DEBUG,
7453 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7454 __func__, p2ps_prov->conncap,
7455 p2ps_prov->adv_id, p2ps_prov->conncap,
7456 p2ps_prov->status, p2ps_prov->info);
7457
7458 config_methods = 0;
7459 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007460 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007461 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007462 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7464 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007465 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007466 else {
7467 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007468 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007469 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007470 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471
Dmitry Shmidt04949592012-07-19 12:16:46 -07007472 if (use == WPAS_P2P_PD_AUTO) {
7473 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7474 wpa_s->pending_pd_config_methods = config_methods;
7475 wpa_s->p2p_auto_pd = 1;
7476 wpa_s->p2p_auto_join = 0;
7477 wpa_s->pending_pd_before_join = 0;
7478 wpa_s->auto_pd_scan_retry = 0;
7479 wpas_p2p_stop_find(wpa_s);
7480 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007481 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007482 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7483 wpa_s->p2p_auto_started.sec,
7484 wpa_s->p2p_auto_started.usec);
7485 wpas_p2p_join_scan(wpa_s, NULL);
7486 return 0;
7487 }
7488
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007489 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7490 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007491 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007493
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007494 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007495 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007496 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007497}
7498
7499
7500int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7501 char *end)
7502{
7503 return p2p_scan_result_text(ies, ies_len, buf, end);
7504}
7505
7506
7507static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7508{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007509 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007510 return;
7511
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007512 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007513 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007514 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7515 wpa_s, NULL);
7516 offchannel_send_action_done(wpa_s);
7517 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007519 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7520 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007521 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007522}
7523
7524
7525int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7526 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007527 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007528 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007529 u8 seek_cnt, const char **seek_string, int freq,
7530 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531{
7532 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007533 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534
Dmitry Shmidt04949592012-07-19 12:16:46 -07007535 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007536 wpa_s->p2p_in_provisioning) {
7537 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7538 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7539 " (P2P disabled)" : "",
7540 wpa_s->p2p_in_provisioning ?
7541 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007542 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007543 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007545 wpa_supplicant_cancel_sched_scan(wpa_s);
7546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007548 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007549 search_delay, seek_cnt, seek_string, freq,
7550 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551}
7552
7553
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007554static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7555 struct wpa_scan_results *scan_res)
7556{
7557 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7558
7559 if (wpa_s->p2p_scan_work) {
7560 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7561 wpa_s->p2p_scan_work = NULL;
7562 radio_work_done(work);
7563 }
7564
7565 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7566 return;
7567
7568 /*
7569 * Indicate that results have been processed so that the P2P module can
7570 * continue pending tasks.
7571 */
Hai Shalom60840252021-02-19 19:02:11 -08007572 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007573}
7574
7575
7576static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577{
7578 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007579 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007580 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7581 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582
7583 if (wpa_s->global->p2p)
7584 p2p_stop_find(wpa_s->global->p2p);
7585
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007586 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7587 wpa_printf(MSG_DEBUG,
7588 "P2P: Do not consider the scan results after stop_find");
7589 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7590 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007591}
7592
7593
7594void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7595{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007596 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007597 if (!wpa_s->global->pending_group_iface_for_p2ps)
7598 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599}
7600
7601
7602static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7603{
7604 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007605 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606}
7607
7608
7609int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7610{
7611 int res;
7612
7613 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7614 return -1;
7615
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007616 if (wpa_s->p2p_lo_started) {
7617 wpa_printf(MSG_DEBUG,
7618 "P2P: Cannot start P2P listen, it is offloaded");
7619 return -1;
7620 }
7621
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007622 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007623 wpas_p2p_clear_pending_action_tx(wpa_s);
7624
7625 if (timeout == 0) {
7626 /*
7627 * This is a request for unlimited Listen state. However, at
7628 * least for now, this is mapped to a Listen state for one
7629 * hour.
7630 */
7631 timeout = 3600;
7632 }
7633 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007634 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007636 /*
7637 * Stop previous find/listen operation to avoid trying to request a new
7638 * remain-on-channel operation while the driver is still running the
7639 * previous one.
7640 */
7641 if (wpa_s->global->p2p)
7642 p2p_stop_find(wpa_s->global->p2p);
7643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7645 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007646 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647 eloop_register_timeout(timeout, 0,
7648 wpas_p2p_long_listen_timeout,
7649 wpa_s, NULL);
7650 }
7651
7652 return res;
7653}
7654
7655
7656int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7657 u8 *buf, size_t len, int p2p_group)
7658{
7659 struct wpabuf *p2p_ie;
7660 int ret;
7661
7662 if (wpa_s->global->p2p_disabled)
7663 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007664 /*
7665 * Advertize mandatory cross connection capability even on
7666 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7667 */
7668 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007669 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007670 if (wpa_s->global->p2p == NULL)
7671 return -1;
7672 if (bss == NULL)
7673 return -1;
7674
7675 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7676 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7677 p2p_group, p2p_ie);
7678 wpabuf_free(p2p_ie);
7679
7680 return ret;
7681}
7682
7683
7684int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007685 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007686 const u8 *ie, size_t ie_len,
7687 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688{
7689 if (wpa_s->global->p2p_disabled)
7690 return 0;
7691 if (wpa_s->global->p2p == NULL)
7692 return 0;
7693
Dmitry Shmidt04949592012-07-19 12:16:46 -07007694 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007695 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007696 case P2P_PREQ_NOT_P2P:
7697 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7698 ssi_signal);
7699 /* fall through */
7700 case P2P_PREQ_MALFORMED:
7701 case P2P_PREQ_NOT_LISTEN:
7702 case P2P_PREQ_NOT_PROCESSED:
7703 default: /* make gcc happy */
7704 return 0;
7705 case P2P_PREQ_PROCESSED:
7706 return 1;
7707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007708}
7709
7710
7711void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7712 const u8 *sa, const u8 *bssid,
7713 u8 category, const u8 *data, size_t len, int freq)
7714{
7715 if (wpa_s->global->p2p_disabled)
7716 return;
7717 if (wpa_s->global->p2p == NULL)
7718 return;
7719
7720 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7721 freq);
7722}
7723
7724
7725void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7726{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007727 unsigned int bands;
7728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007729 if (wpa_s->global->p2p_disabled)
7730 return;
7731 if (wpa_s->global->p2p == NULL)
7732 return;
7733
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007734 bands = wpas_get_bands(wpa_s, NULL);
7735 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736}
7737
7738
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007739static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007740{
7741 p2p_group_deinit(wpa_s->p2p_group);
7742 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007743
7744 wpa_s->ap_configured_cb = NULL;
7745 wpa_s->ap_configured_cb_ctx = NULL;
7746 wpa_s->ap_configured_cb_data = NULL;
7747 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007748}
7749
7750
7751int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7752{
Hai Shalomfdcde762020-04-02 11:19:20 -07007753 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7756 return -1;
7757
7758 return p2p_reject(wpa_s->global->p2p, addr);
7759}
7760
7761
7762/* Invite to reinvoke a persistent group */
7763int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007764 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007765 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007766 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007767{
7768 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007769 u8 *bssid = NULL;
7770 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007771 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007772 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007773 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7774 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007775
Hai Shalomc1a21442022-02-04 13:43:00 -08007776 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7777 return -1;
7778
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007779 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007780 if (peer_addr)
7781 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7782 else
7783 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007784
Jouni Malinen31be0a42012-08-31 21:20:51 +03007785 wpa_s->p2p_persistent_go_freq = freq;
7786 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007787 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007788 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007789 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7790 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007791 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792 if (ssid->mode == WPAS_MODE_P2P_GO) {
7793 role = P2P_INVITE_ROLE_GO;
7794 if (peer_addr == NULL) {
7795 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7796 "address in invitation command");
7797 return -1;
7798 }
7799 if (wpas_p2p_create_iface(wpa_s)) {
7800 if (wpas_p2p_add_group_interface(wpa_s,
7801 WPA_IF_P2P_GO) < 0) {
7802 wpa_printf(MSG_ERROR, "P2P: Failed to "
7803 "allocate a new interface for the "
7804 "group");
7805 return -1;
7806 }
7807 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007808 } else if (wpa_s->p2p_mgmt)
7809 bssid = wpa_s->parent->own_addr;
7810 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007811 bssid = wpa_s->own_addr;
7812 } else {
7813 role = P2P_INVITE_ROLE_CLIENT;
7814 peer_addr = ssid->bssid;
7815 }
7816 wpa_s->pending_invite_ssid_id = ssid->id;
7817
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007818 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007819 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007820 role == P2P_INVITE_ROLE_GO,
7821 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007822 if (res)
7823 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007825 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7826 return -1;
7827
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007828 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7829
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007830 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7831 no_pref_freq_given && pref_freq > 0 &&
7832 wpa_s->num_multichan_concurrent > 1 &&
7833 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7834 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7835 pref_freq);
7836 pref_freq = 0;
7837 }
7838
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007839 /*
7840 * Stop any find/listen operations before invitation and possibly
7841 * connection establishment.
7842 */
7843 wpas_p2p_stop_find_oper(wpa_s);
7844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007845 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007846 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007847 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848}
7849
7850
7851/* Invite to join an active group */
7852int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007853 const u8 *peer_addr, const u8 *go_dev_addr,
7854 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007855{
7856 struct wpa_global *global = wpa_s->global;
7857 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007858 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007859 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007860 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007861 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007862 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007863 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7864 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007865
Jouni Malinen31be0a42012-08-31 21:20:51 +03007866 wpa_s->p2p_persistent_go_freq = 0;
7867 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007868 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007869 wpa_s->p2p_go_vht_center_freq2 = 0;
7870 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007871 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007873 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7874 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7875 break;
7876 }
7877 if (wpa_s == NULL) {
7878 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7879 return -1;
7880 }
7881
7882 ssid = wpa_s->current_ssid;
7883 if (ssid == NULL) {
7884 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7885 "invitation");
7886 return -1;
7887 }
7888
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007889 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007890 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007891 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007892 ssid->ssid, ssid->ssid_len);
7893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007894 if (ssid->mode == WPAS_MODE_P2P_GO) {
7895 role = P2P_INVITE_ROLE_ACTIVE_GO;
7896 bssid = wpa_s->own_addr;
7897 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007898 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007899 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007900 } else {
7901 role = P2P_INVITE_ROLE_CLIENT;
7902 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7903 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7904 "invite to current group");
7905 return -1;
7906 }
7907 bssid = wpa_s->bssid;
7908 if (go_dev_addr == NULL &&
7909 !is_zero_ether_addr(wpa_s->go_dev_addr))
7910 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007911 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7912 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007913 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007914 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007916 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7917 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007918 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7919 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007920
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007921 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007922 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007923 role == P2P_INVITE_ROLE_ACTIVE_GO,
7924 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007925 if (res)
7926 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007927 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007929 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007930 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007931 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007932}
7933
7934
7935void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7936{
7937 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007938 u8 go_dev_addr[ETH_ALEN];
7939 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007940 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007941 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007942 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007943
Dmitry Shmidt04949592012-07-19 12:16:46 -07007944 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7945 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007946 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007947 }
7948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007949 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007950 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007951
7952 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007953 if (!wpa_s->p2p_go_group_formation_completed &&
7954 wpa_s->global->p2p_group_formation == wpa_s) {
7955 wpa_dbg(wpa_s, MSG_DEBUG,
7956 "P2P: Marking group formation completed on client on data connection");
7957 wpa_s->p2p_go_group_formation_completed = 1;
7958 wpa_s->global->p2p_group_formation = NULL;
7959 wpa_s->p2p_in_provisioning = 0;
7960 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00007961 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007962 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007964 os_memset(go_dev_addr, 0, ETH_ALEN);
7965 if (ssid->bssid_set)
7966 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7967 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7968 ssid->ssid_len);
7969 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7970
7971 if (wpa_s->global->p2p_group_formation == wpa_s)
7972 wpa_s->global->p2p_group_formation = NULL;
7973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007974 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7975 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007976
7977 ip_addr[0] = '\0';
7978 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007979 int res;
7980
7981 res = os_snprintf(ip_addr, sizeof(ip_addr),
7982 " ip_addr=%u.%u.%u.%u "
7983 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7984 ip[0], ip[1], ip[2], ip[3],
7985 ip[4], ip[5], ip[6], ip[7],
7986 ip[8], ip[9], ip[10], ip[11]);
7987 if (os_snprintf_error(sizeof(ip_addr), res))
7988 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007989 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007990 }
7991
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007992 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7993 ssid->passphrase == NULL && ssid->psk_set ?
7994 ssid->psk : NULL,
7995 ssid->passphrase, go_dev_addr, persistent,
7996 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007997
7998 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007999 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
8000 ssid, go_dev_addr);
8001
Hai Shalom5f92bc92019-04-18 11:54:11 -07008002 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003}
8004
8005
8006int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
8007 u32 interval1, u32 duration2, u32 interval2)
8008{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008009 int ret;
8010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008011 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8012 return -1;
8013
8014 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
8015 wpa_s->current_ssid == NULL ||
8016 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
8017 return -1;
8018
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008019 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
8020 wpa_s->own_addr, wpa_s->assoc_freq,
8021 duration1, interval1, duration2, interval2);
8022 if (ret == 0)
8023 wpa_s->waiting_presence_resp = 1;
8024
8025 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008026}
8027
8028
8029int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
8030 unsigned int interval)
8031{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008032 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8033 return -1;
8034
8035 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
8036}
8037
8038
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008039static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
8040{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008041 if (wpa_s->current_ssid == NULL) {
8042 /*
8043 * current_ssid can be cleared when P2P client interface gets
8044 * disconnected, so assume this interface was used as P2P
8045 * client.
8046 */
8047 return 1;
8048 }
8049 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008050 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
8051}
8052
8053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008054static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
8055{
8056 struct wpa_supplicant *wpa_s = eloop_ctx;
8057
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008058 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008059 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
8060 "disabled");
8061 return;
8062 }
8063
Dmitry Shmidt04949592012-07-19 12:16:46 -07008064 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
8065 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008066 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008067}
8068
8069
8070static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
8071{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008072 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008073
Dmitry Shmidt04949592012-07-19 12:16:46 -07008074 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8075 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008077 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8078 return;
8079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008080 timeout = wpa_s->conf->p2p_group_idle;
8081 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8082 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8083 timeout = P2P_MAX_CLIENT_IDLE;
8084
8085 if (timeout == 0)
8086 return;
8087
Dmitry Shmidt04949592012-07-19 12:16:46 -07008088 if (timeout < 0) {
8089 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8090 timeout = 0; /* special client mode no-timeout */
8091 else
8092 return;
8093 }
8094
8095 if (wpa_s->p2p_in_provisioning) {
8096 /*
8097 * Use the normal group formation timeout during the
8098 * provisioning phase to avoid terminating this process too
8099 * early due to group idle timeout.
8100 */
8101 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8102 "during provisioning");
8103 return;
8104 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308105
Dmitry Shmidt04949592012-07-19 12:16:46 -07008106 if (wpa_s->show_group_started) {
8107 /*
8108 * Use the normal group formation timeout between the end of
8109 * the provisioning phase and completion of 4-way handshake to
8110 * avoid terminating this process too early due to group idle
8111 * timeout.
8112 */
8113 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8114 "while waiting for initial 4-way handshake to "
8115 "complete");
8116 return;
8117 }
8118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008119 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008120 timeout);
8121 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8122 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008123}
8124
8125
Jouni Malinen2b89da82012-08-31 22:04:41 +03008126/* Returns 1 if the interface was removed */
8127int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8128 u16 reason_code, const u8 *ie, size_t ie_len,
8129 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008130{
8131 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008132 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008133
Dmitry Shmidt04949592012-07-19 12:16:46 -07008134 if (!locally_generated)
8135 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8136 ie_len);
8137
8138 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8139 wpa_s->current_ssid &&
8140 wpa_s->current_ssid->p2p_group &&
8141 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8142 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8143 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008144 if (wpas_p2p_group_delete(wpa_s,
8145 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8146 > 0)
8147 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008148 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008149
8150 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008151}
8152
8153
8154void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008155 u16 reason_code, const u8 *ie, size_t ie_len,
8156 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008157{
8158 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8159 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008160
Dmitry Shmidt04949592012-07-19 12:16:46 -07008161 if (!locally_generated)
8162 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8163 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008164}
8165
8166
8167void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8168{
8169 struct p2p_data *p2p = wpa_s->global->p2p;
8170
8171 if (p2p == NULL)
8172 return;
8173
8174 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8175 return;
8176
8177 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8178 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8179
8180 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8181 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8182
8183 if (wpa_s->wps &&
8184 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8185 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8186
8187 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8188 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8189
8190 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8191 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8192 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8193 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8194 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8195 }
8196
8197 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8198 p2p_set_sec_dev_types(p2p,
8199 (void *) wpa_s->conf->sec_device_type,
8200 wpa_s->conf->num_sec_device_types);
8201
8202 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8203 int i;
8204 p2p_remove_wps_vendor_extensions(p2p);
8205 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8206 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8207 continue;
8208 p2p_add_wps_vendor_extension(
8209 p2p, wpa_s->conf->wps_vendor_ext[i]);
8210 }
8211 }
8212
8213 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8214 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8215 char country[3];
8216 country[0] = wpa_s->conf->country[0];
8217 country[1] = wpa_s->conf->country[1];
8218 country[2] = 0x04;
8219 p2p_set_country(p2p, country);
8220 }
8221
8222 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8223 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8224 wpa_s->conf->p2p_ssid_postfix ?
8225 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8226 0);
8227 }
8228
8229 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8230 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008231
8232 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8233 u8 reg_class, channel;
8234 int ret;
8235 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008236 u8 channel_forced;
8237
Jouni Malinen75ecf522011-06-27 15:19:46 -07008238 if (wpa_s->conf->p2p_listen_reg_class &&
8239 wpa_s->conf->p2p_listen_channel) {
8240 reg_class = wpa_s->conf->p2p_listen_reg_class;
8241 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008242 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008243 } else {
8244 reg_class = 81;
8245 /*
8246 * Pick one of the social channels randomly as the
8247 * listen channel.
8248 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008249 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8250 channel = 1;
8251 else
8252 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008253 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008254 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008255 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8256 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008257 if (ret)
8258 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8259 "failed: %d", ret);
8260 }
8261 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8262 u8 op_reg_class, op_channel, cfg_op_channel;
8263 int ret = 0;
8264 unsigned int r;
8265 if (wpa_s->conf->p2p_oper_reg_class &&
8266 wpa_s->conf->p2p_oper_channel) {
8267 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8268 op_channel = wpa_s->conf->p2p_oper_channel;
8269 cfg_op_channel = 1;
8270 } else {
8271 op_reg_class = 81;
8272 /*
8273 * Use random operation channel from (1, 6, 11)
8274 *if no other preference is indicated.
8275 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008276 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8277 op_channel = 1;
8278 else
8279 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008280 cfg_op_channel = 0;
8281 }
8282 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8283 cfg_op_channel);
8284 if (ret)
8285 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8286 "failed: %d", ret);
8287 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008288
8289 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8290 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8291 wpa_s->conf->p2p_pref_chan) < 0) {
8292 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8293 "update failed");
8294 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008295
8296 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8297 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8298 "update failed");
8299 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008300 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008301
8302 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8303 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008304}
8305
8306
8307int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8308 int duration)
8309{
8310 if (!wpa_s->ap_iface)
8311 return -1;
8312 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8313 duration);
8314}
8315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008316
8317int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8318{
8319 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8320 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008321
8322 wpa_s->global->cross_connection = enabled;
8323 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8324
8325 if (!enabled) {
8326 struct wpa_supplicant *iface;
8327
8328 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8329 {
8330 if (iface->cross_connect_enabled == 0)
8331 continue;
8332
8333 iface->cross_connect_enabled = 0;
8334 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008335 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008336 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8337 iface->ifname,
8338 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008339 }
8340 }
8341
8342 return 0;
8343}
8344
8345
8346static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8347{
8348 struct wpa_supplicant *iface;
8349
8350 if (!uplink->global->cross_connection)
8351 return;
8352
8353 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8354 if (!iface->cross_connect_enabled)
8355 continue;
8356 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8357 0)
8358 continue;
8359 if (iface->ap_iface == NULL)
8360 continue;
8361 if (iface->cross_connect_in_use)
8362 continue;
8363
8364 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008365 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008366 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8367 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008368 }
8369}
8370
8371
8372static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8373{
8374 struct wpa_supplicant *iface;
8375
8376 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8377 if (!iface->cross_connect_enabled)
8378 continue;
8379 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8380 0)
8381 continue;
8382 if (!iface->cross_connect_in_use)
8383 continue;
8384
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008385 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008386 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8387 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008388 iface->cross_connect_in_use = 0;
8389 }
8390}
8391
8392
8393void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8394{
8395 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8396 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8397 wpa_s->cross_connect_disallowed)
8398 wpas_p2p_disable_cross_connect(wpa_s);
8399 else
8400 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008401 if (!wpa_s->ap_iface &&
8402 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8403 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008404}
8405
8406
8407void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8408{
8409 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008410 if (!wpa_s->ap_iface &&
8411 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8412 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008413 wpas_p2p_set_group_idle_timeout(wpa_s);
8414}
8415
8416
8417static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8418{
8419 struct wpa_supplicant *iface;
8420
8421 if (!wpa_s->global->cross_connection)
8422 return;
8423
8424 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8425 if (iface == wpa_s)
8426 continue;
8427 if (iface->drv_flags &
8428 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8429 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008430 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8431 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008432 continue;
8433
8434 wpa_s->cross_connect_enabled = 1;
8435 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8436 sizeof(wpa_s->cross_connect_uplink));
8437 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8438 "%s to %s whenever uplink is available",
8439 wpa_s->ifname, wpa_s->cross_connect_uplink);
8440
8441 if (iface->ap_iface || iface->current_ssid == NULL ||
8442 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8443 iface->cross_connect_disallowed ||
8444 iface->wpa_state != WPA_COMPLETED)
8445 break;
8446
8447 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008448 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008449 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8450 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008451 break;
8452 }
8453}
8454
8455
Sunil Ravi036cec52023-03-29 11:35:17 -07008456static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008457{
8458 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8459 !wpa_s->p2p_in_provisioning)
8460 return 0; /* not P2P client operation */
8461
8462 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8463 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008464 if (wpa_s != wpa_s->p2pdev)
8465 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008466 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008467 return 1;
8468}
8469
8470
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008471void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8472{
8473 struct wpa_supplicant *wpa_s = eloop_ctx;
8474 wpas_p2p_notif_pbc_overlap(wpa_s);
8475}
8476
8477
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008478void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8479 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008480{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008481 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008482 struct wpa_used_freq_data *freqs = NULL;
8483 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008484
8485 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8486 return;
8487
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008488 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8489 if (!freqs)
8490 return;
8491
Sunil Ravi77d572f2023-01-17 23:58:31 +00008492 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008494 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008495 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008496 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8497 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008498 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8499 "channel list");
8500 return;
8501 }
8502
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008503 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008504
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008505 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008506
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008507 /*
8508 * The used frequencies map changed, so it is possible that a GO is
8509 * using a channel that is no longer valid for P2P use. It is also
8510 * possible that due to policy consideration, it would be preferable to
8511 * move it to a frequency already used by other station interfaces.
8512 */
8513 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8514
8515 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008516}
8517
8518
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008519static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8520 struct wpa_scan_results *scan_res)
8521{
8522 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8523}
8524
8525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008526int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8527{
8528 struct wpa_global *global = wpa_s->global;
8529 int found = 0;
8530 const u8 *peer;
8531
8532 if (global->p2p == NULL)
8533 return -1;
8534
8535 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8536
8537 if (wpa_s->pending_interface_name[0] &&
8538 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8539 found = 1;
8540
8541 peer = p2p_get_go_neg_peer(global->p2p);
8542 if (peer) {
8543 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8544 MACSTR, MAC2STR(peer));
8545 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008546 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008547 }
8548
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008549 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8550 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8551 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8552 found = 1;
8553 }
8554
8555 if (wpa_s->pending_pd_before_join) {
8556 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8557 wpa_s->pending_pd_before_join = 0;
8558 found = 1;
8559 }
8560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008561 wpas_p2p_stop_find(wpa_s);
8562
8563 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8564 if (wpa_s == global->p2p_group_formation &&
8565 (wpa_s->p2p_in_provisioning ||
8566 wpa_s->parent->pending_interface_type ==
8567 WPA_IF_P2P_CLIENT)) {
8568 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8569 "formation found - cancelling",
8570 wpa_s->ifname);
8571 found = 1;
8572 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008573 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008574 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008575 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008576 break;
8577 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008578 wpas_p2p_group_delete(wpa_s,
8579 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008580 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008581 } else if (wpa_s->p2p_in_invitation) {
8582 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8583 wpa_s->ifname);
8584 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008585 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008586 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008587 }
8588 }
8589
8590 if (!found) {
8591 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8592 return -1;
8593 }
8594
8595 return 0;
8596}
8597
8598
8599void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8600{
8601 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8602 return;
8603
8604 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8605 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008606 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008607}
8608
8609
8610void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8611 int freq_24, int freq_5, int freq_overall)
8612{
8613 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008614 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008615 return;
8616 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8617}
8618
8619
8620int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8621{
8622 u8 peer[ETH_ALEN];
8623 struct p2p_data *p2p = wpa_s->global->p2p;
8624
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008625 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008626 return -1;
8627
8628 if (hwaddr_aton(addr, peer))
8629 return -1;
8630
8631 return p2p_unauthorize(p2p, peer);
8632}
8633
8634
8635/**
8636 * wpas_p2p_disconnect - Disconnect from a P2P Group
8637 * @wpa_s: Pointer to wpa_supplicant data
8638 * Returns: 0 on success, -1 on failure
8639 *
8640 * This can be used to disconnect from a group in which the local end is a P2P
8641 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8642 * virtual network interface was created for this group, that interface will be
8643 * removed. Otherwise, only the configured P2P group network will be removed
8644 * from the interface.
8645 */
8646int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8647{
8648
8649 if (wpa_s == NULL)
8650 return -1;
8651
Jouni Malinen2b89da82012-08-31 22:04:41 +03008652 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8653 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008654}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008655
8656
8657int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8658{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008659 int ret;
8660
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008661 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8662 return 0;
8663
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008664 ret = p2p_in_progress(wpa_s->global->p2p);
8665 if (ret == 0) {
8666 /*
8667 * Check whether there is an ongoing WPS provisioning step (or
8668 * other parts of group formation) on another interface since
8669 * p2p_in_progress() does not report this to avoid issues for
8670 * scans during such provisioning step.
8671 */
8672 if (wpa_s->global->p2p_group_formation &&
8673 wpa_s->global->p2p_group_formation != wpa_s) {
8674 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8675 "in group formation",
8676 wpa_s->global->p2p_group_formation->ifname);
8677 ret = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008678 } else if (wpa_s->global->p2p_group_formation == wpa_s) {
8679 wpa_dbg(wpa_s, MSG_DEBUG,
8680 "P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
8681 ret = 2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008682 }
8683 }
8684
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008685 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008686 struct os_reltime now;
8687 os_get_reltime(&now);
8688 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8689 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008690 /* Wait for the first client has expired */
8691 wpa_s->global->p2p_go_wait_client.sec = 0;
8692 } else {
8693 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8694 ret = 1;
8695 }
8696 }
8697
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008698 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008699}
8700
8701
8702void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8703 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008704{
8705 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8706 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008707 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008708 /**
8709 * Remove the network by scheduling the group formation
8710 * timeout to happen immediately. The teardown code
8711 * needs to be scheduled to run asynch later so that we
8712 * don't delete data from under ourselves unexpectedly.
8713 * Calling wpas_p2p_group_formation_timeout directly
8714 * causes a series of crashes in WPS failure scenarios.
8715 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008716 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8717 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008718 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008719 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008720 }
8721}
8722
8723
8724struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008725 const u8 *addr, const u8 *ssid,
8726 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008727{
8728 struct wpa_ssid *s;
8729 size_t i;
8730
8731 for (s = wpa_s->conf->ssid; s; s = s->next) {
8732 if (s->disabled != 2)
8733 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008734 if (ssid &&
8735 (ssid_len != s->ssid_len ||
8736 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8737 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008738 if (addr == NULL) {
8739 if (s->mode == WPAS_MODE_P2P_GO)
8740 return s;
8741 continue;
8742 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008743 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8744 return s; /* peer is GO in the persistent group */
8745 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8746 continue;
8747 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008748 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008749 addr, ETH_ALEN) == 0)
8750 return s; /* peer is P2P client in persistent
8751 * group */
8752 }
8753 }
8754
8755 return NULL;
8756}
8757
8758
8759void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8760 const u8 *addr)
8761{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008762 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008763 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008764 /*
8765 * This can happen if WPS provisioning step is not terminated
8766 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8767 * peer was able to connect, there is no need to time out group
8768 * formation after this, though. In addition, this is used with
8769 * the initial connection wait on the GO as a separate formation
8770 * timeout and as such, expected to be hit after the initial WPS
8771 * provisioning step.
8772 */
8773 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008774
8775 if (!wpa_s->p2p_go_group_formation_completed &&
8776 !wpa_s->group_formation_reported) {
8777 /*
8778 * GO has not yet notified group formation success since
8779 * the WPS step was not completed cleanly. Do that
8780 * notification now since the P2P Client was able to
8781 * connect and as such, must have received the
8782 * credential from the WPS step.
8783 */
8784 if (wpa_s->global->p2p)
8785 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008786 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008787 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008788 }
8789 if (!wpa_s->p2p_go_group_formation_completed) {
8790 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8791 wpa_s->p2p_go_group_formation_completed = 1;
8792 wpa_s->global->p2p_group_formation = NULL;
8793 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008794 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008795 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008796 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008797 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008798 if (addr == NULL)
8799 return;
8800 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8801}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008802
Dmitry Shmidt04949592012-07-19 12:16:46 -07008803
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008804static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8805 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008806{
8807 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008808 int ret = 0;
8809
Dmitry Shmidt04949592012-07-19 12:16:46 -07008810 if (wpa_s->global->p2p_group_formation)
8811 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008812 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008813 offchannel_send_action_done(wpa_s);
8814 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008815 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008816 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8817 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8818 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8819 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008820 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008821 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008822 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008823 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008824 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008825 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008826 wpa_s->p2p_go_he,
8827 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008828 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008829 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008830}
8831
8832
8833int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8834{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008835 int res;
8836
Dmitry Shmidt04949592012-07-19 12:16:46 -07008837 if (!wpa_s->p2p_fallback_to_go_neg ||
8838 wpa_s->p2p_in_provisioning <= 5)
8839 return 0;
8840
8841 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8842 return 0; /* peer operating as a GO */
8843
8844 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8845 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008846 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008847 "reason=GO-not-found");
8848 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008849
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008850 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008851}
8852
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008853
8854unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8855{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008856 struct wpa_supplicant *ifs;
8857
8858 if (wpa_s->wpa_state > WPA_SCANNING) {
8859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8860 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008861 wpa_s->conf->p2p_search_delay);
8862 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008863 }
8864
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008865 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8866 radio_list) {
8867 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8869 "delay due to concurrent operation on "
8870 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008871 wpa_s->conf->p2p_search_delay,
8872 ifs->ifname);
8873 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008874 }
8875 }
8876
8877 return 0;
8878}
8879
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008880
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008881static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8882 struct wpa_ssid *s, const u8 *addr,
8883 int iface_addr)
8884{
8885 struct psk_list_entry *psk, *tmp;
8886 int changed = 0;
8887
8888 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8889 list) {
8890 if ((iface_addr && !psk->p2p &&
8891 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8892 (!iface_addr && psk->p2p &&
8893 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8894 wpa_dbg(wpa_s, MSG_DEBUG,
8895 "P2P: Remove persistent group PSK list entry for "
8896 MACSTR " p2p=%u",
8897 MAC2STR(psk->addr), psk->p2p);
8898 dl_list_del(&psk->list);
8899 os_free(psk);
8900 changed++;
8901 }
8902 }
8903
8904 return changed;
8905}
8906
8907
8908void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8909 const u8 *p2p_dev_addr,
8910 const u8 *psk, size_t psk_len)
8911{
8912 struct wpa_ssid *ssid = wpa_s->current_ssid;
8913 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008914 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008915
8916 if (psk_len != sizeof(p->psk))
8917 return;
8918
8919 if (p2p_dev_addr) {
8920 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8921 " p2p_dev_addr=" MACSTR,
8922 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8923 if (is_zero_ether_addr(p2p_dev_addr))
8924 p2p_dev_addr = NULL;
8925 } else {
8926 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8927 MAC2STR(mac_addr));
8928 }
8929
8930 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8931 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8932 /* To be added to persistent group once created */
8933 if (wpa_s->global->add_psk == NULL) {
8934 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8935 if (wpa_s->global->add_psk == NULL)
8936 return;
8937 }
8938 p = wpa_s->global->add_psk;
8939 if (p2p_dev_addr) {
8940 p->p2p = 1;
8941 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8942 } else {
8943 p->p2p = 0;
8944 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8945 }
8946 os_memcpy(p->psk, psk, psk_len);
8947 return;
8948 }
8949
8950 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8951 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8952 return;
8953 }
8954
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008955 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008956 ssid->ssid_len);
8957 if (!persistent) {
8958 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8959 return;
8960 }
8961
8962 p = os_zalloc(sizeof(*p));
8963 if (p == NULL)
8964 return;
8965 if (p2p_dev_addr) {
8966 p->p2p = 1;
8967 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8968 } else {
8969 p->p2p = 0;
8970 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8971 }
8972 os_memcpy(p->psk, psk, psk_len);
8973
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008974 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8975 (last = dl_list_last(&persistent->psk_list,
8976 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8978 MACSTR " (p2p=%u) to make room for a new one",
8979 MAC2STR(last->addr), last->p2p);
8980 dl_list_del(&last->list);
8981 os_free(last);
8982 }
8983
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008984 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008985 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8986 p2p_dev_addr == NULL);
8987 if (p2p_dev_addr) {
8988 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8989 MACSTR, MAC2STR(p2p_dev_addr));
8990 } else {
8991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8992 MAC2STR(mac_addr));
8993 }
8994 dl_list_add(&persistent->psk_list, &p->list);
8995
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008996 if (wpa_s->p2pdev->conf->update_config &&
8997 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008998 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008999}
9000
9001
9002static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
9003 struct wpa_ssid *s, const u8 *addr,
9004 int iface_addr)
9005{
9006 int res;
9007
9008 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009009 if (res > 0 && wpa_s->conf->update_config &&
9010 wpa_config_write(wpa_s->confname, wpa_s->conf))
9011 wpa_dbg(wpa_s, MSG_DEBUG,
9012 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009013}
9014
9015
9016static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
9017 const u8 *peer, int iface_addr)
9018{
9019 struct hostapd_data *hapd;
9020 struct hostapd_wpa_psk *psk, *prev, *rem;
9021 struct sta_info *sta;
9022
9023 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
9024 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
9025 return;
9026
9027 /* Remove per-station PSK entry */
9028 hapd = wpa_s->ap_iface->bss[0];
9029 prev = NULL;
9030 psk = hapd->conf->ssid.wpa_psk;
9031 while (psk) {
9032 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
9033 (!iface_addr &&
9034 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
9035 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
9036 MACSTR " iface_addr=%d",
9037 MAC2STR(peer), iface_addr);
9038 if (prev)
9039 prev->next = psk->next;
9040 else
9041 hapd->conf->ssid.wpa_psk = psk->next;
9042 rem = psk;
9043 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07009044 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009045 } else {
9046 prev = psk;
9047 psk = psk->next;
9048 }
9049 }
9050
9051 /* Disconnect from group */
9052 if (iface_addr)
9053 sta = ap_get_sta(hapd, peer);
9054 else
9055 sta = ap_get_sta_p2p(hapd, peer);
9056 if (sta) {
9057 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
9058 " (iface_addr=%d) from group",
9059 MAC2STR(peer), iface_addr);
9060 hostapd_drv_sta_deauth(hapd, sta->addr,
9061 WLAN_REASON_DEAUTH_LEAVING);
9062 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
9063 }
9064}
9065
9066
9067void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
9068 int iface_addr)
9069{
9070 struct wpa_ssid *s;
9071 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009072 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009073
9074 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
9075
9076 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009077 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009078 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9079 continue;
9080 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009081 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9082 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009083 }
9084
9085 /* Remove from any operating group */
9086 for (w = wpa_s->global->ifaces; w; w = w->next)
9087 wpas_p2p_remove_client_go(w, peer, iface_addr);
9088}
9089
9090
9091static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9092{
9093 struct wpa_supplicant *wpa_s = eloop_ctx;
9094 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9095}
9096
9097
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009098static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9099{
9100 struct wpa_supplicant *wpa_s = eloop_ctx;
9101
9102 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9103 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9104}
9105
9106
9107int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9108 struct wpa_ssid *ssid)
9109{
9110 struct wpa_supplicant *iface;
9111
9112 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9113 if (!iface->current_ssid ||
9114 iface->current_ssid->frequency == freq ||
9115 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9116 !iface->current_ssid->p2p_group))
9117 continue;
9118
9119 /* Remove the connection with least priority */
9120 if (!wpas_is_p2p_prioritized(iface)) {
9121 /* STA connection has priority over existing
9122 * P2P connection, so remove the interface. */
9123 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9124 eloop_register_timeout(0, 0,
9125 wpas_p2p_group_freq_conflict,
9126 iface, NULL);
9127 /* If connection in progress is P2P connection, do not
9128 * proceed for the connection. */
9129 if (wpa_s == iface)
9130 return -1;
9131 else
9132 return 0;
9133 } else {
9134 /* P2P connection has priority, disable the STA network
9135 */
9136 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9137 ssid);
9138 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9139 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9140 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9141 ETH_ALEN);
9142 /* If P2P connection is in progress, continue
9143 * connecting...*/
9144 if (wpa_s == iface)
9145 return 0;
9146 else
9147 return -1;
9148 }
9149 }
9150
9151 return 0;
9152}
9153
9154
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009155int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9156{
9157 struct wpa_ssid *ssid = wpa_s->current_ssid;
9158
9159 if (ssid == NULL || !ssid->p2p_group)
9160 return 0;
9161
9162 if (wpa_s->p2p_last_4way_hs_fail &&
9163 wpa_s->p2p_last_4way_hs_fail == ssid) {
9164 u8 go_dev_addr[ETH_ALEN];
9165 struct wpa_ssid *persistent;
9166
9167 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9168 ssid->ssid,
9169 ssid->ssid_len) <= 0) {
9170 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9171 goto disconnect;
9172 }
9173
9174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9175 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009176 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009177 ssid->ssid,
9178 ssid->ssid_len);
9179 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9180 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9181 goto disconnect;
9182 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009183 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009184 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9185 persistent->id);
9186 disconnect:
9187 wpa_s->p2p_last_4way_hs_fail = NULL;
9188 /*
9189 * Remove the group from a timeout to avoid issues with caller
9190 * continuing to use the interface if this is on a P2P group
9191 * interface.
9192 */
9193 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9194 wpa_s, NULL);
9195 return 1;
9196 }
9197
9198 wpa_s->p2p_last_4way_hs_fail = ssid;
9199 return 0;
9200}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009201
9202
9203#ifdef CONFIG_WPS_NFC
9204
9205static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9206 struct wpabuf *p2p)
9207{
9208 struct wpabuf *ret;
9209 size_t wsc_len;
9210
9211 if (p2p == NULL) {
9212 wpabuf_free(wsc);
9213 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9214 return NULL;
9215 }
9216
9217 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9218 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9219 if (ret == NULL) {
9220 wpabuf_free(wsc);
9221 wpabuf_free(p2p);
9222 return NULL;
9223 }
9224
9225 wpabuf_put_be16(ret, wsc_len);
9226 if (wsc)
9227 wpabuf_put_buf(ret, wsc);
9228 wpabuf_put_be16(ret, wpabuf_len(p2p));
9229 wpabuf_put_buf(ret, p2p);
9230
9231 wpabuf_free(wsc);
9232 wpabuf_free(p2p);
9233 wpa_hexdump_buf(MSG_DEBUG,
9234 "P2P: Generated NFC connection handover message", ret);
9235
9236 if (ndef && ret) {
9237 struct wpabuf *tmp;
9238 tmp = ndef_build_p2p(ret);
9239 wpabuf_free(ret);
9240 if (tmp == NULL) {
9241 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9242 return NULL;
9243 }
9244 ret = tmp;
9245 }
9246
9247 return ret;
9248}
9249
9250
9251static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9252 struct wpa_ssid **ssid, u8 *go_dev_addr)
9253{
9254 struct wpa_supplicant *iface;
9255
9256 if (go_dev_addr)
9257 os_memset(go_dev_addr, 0, ETH_ALEN);
9258 if (ssid)
9259 *ssid = NULL;
9260 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9261 if (iface->wpa_state < WPA_ASSOCIATING ||
9262 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9263 !iface->current_ssid->p2p_group ||
9264 iface->current_ssid->mode != WPAS_MODE_INFRA)
9265 continue;
9266 if (ssid)
9267 *ssid = iface->current_ssid;
9268 if (go_dev_addr)
9269 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9270 return iface->assoc_freq;
9271 }
9272 return 0;
9273}
9274
9275
9276struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9277 int ndef)
9278{
9279 struct wpabuf *wsc, *p2p;
9280 struct wpa_ssid *ssid;
9281 u8 go_dev_addr[ETH_ALEN];
9282 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9283
9284 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9285 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9286 return NULL;
9287 }
9288
9289 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9290 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9291 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9292 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9293 return NULL;
9294 }
9295
9296 if (cli_freq == 0) {
9297 wsc = wps_build_nfc_handover_req_p2p(
9298 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9299 } else
9300 wsc = NULL;
9301 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9302 go_dev_addr, ssid ? ssid->ssid : NULL,
9303 ssid ? ssid->ssid_len : 0);
9304
9305 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9306}
9307
9308
9309struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9310 int ndef, int tag)
9311{
9312 struct wpabuf *wsc, *p2p;
9313 struct wpa_ssid *ssid;
9314 u8 go_dev_addr[ETH_ALEN];
9315 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9316
9317 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9318 return NULL;
9319
9320 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9321 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9322 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9323 return NULL;
9324
9325 if (cli_freq == 0) {
9326 wsc = wps_build_nfc_handover_sel_p2p(
9327 wpa_s->parent->wps,
9328 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9329 DEV_PW_NFC_CONNECTION_HANDOVER,
9330 wpa_s->conf->wps_nfc_dh_pubkey,
9331 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9332 } else
9333 wsc = NULL;
9334 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9335 go_dev_addr, ssid ? ssid->ssid : NULL,
9336 ssid ? ssid->ssid_len : 0);
9337
9338 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9339}
9340
9341
9342static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9343 struct p2p_nfc_params *params)
9344{
9345 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9346 "connection handover (freq=%d)",
9347 params->go_freq);
9348
9349 if (params->go_freq && params->go_ssid_len) {
9350 wpa_s->p2p_wps_method = WPS_NFC;
9351 wpa_s->pending_join_wps_method = WPS_NFC;
9352 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9353 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9354 ETH_ALEN);
9355 return wpas_p2p_join_start(wpa_s, params->go_freq,
9356 params->go_ssid,
9357 params->go_ssid_len);
9358 }
9359
9360 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9361 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009362 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009363 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009364 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009365 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009366 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009367}
9368
9369
9370static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9371 struct p2p_nfc_params *params, int tag)
9372{
9373 int res, persistent;
9374 struct wpa_ssid *ssid;
9375
9376 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9377 "connection handover");
9378 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9379 ssid = wpa_s->current_ssid;
9380 if (ssid == NULL)
9381 continue;
9382 if (ssid->mode != WPAS_MODE_P2P_GO)
9383 continue;
9384 if (wpa_s->ap_iface == NULL)
9385 continue;
9386 break;
9387 }
9388 if (wpa_s == NULL) {
9389 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9390 return -1;
9391 }
9392
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009393 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009394 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009395 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009396 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9397 return -1;
9398 }
9399 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009400 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9401 wpa_s->p2pdev->p2p_oob_dev_pw,
9402 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9403 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009404 if (res)
9405 return res;
9406
9407 if (!tag) {
9408 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9409 return 0;
9410 }
9411
9412 if (!params->peer ||
9413 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9414 return 0;
9415
9416 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9417 " to join", MAC2STR(params->peer->p2p_device_addr));
9418
9419 wpa_s->global->p2p_invite_group = wpa_s;
9420 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009421 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009422 params->peer->p2p_device_addr,
9423 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009424 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009425
9426 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9427 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9428 ssid->ssid, ssid->ssid_len, ssid->frequency,
9429 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009430 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009431}
9432
9433
9434static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9435 struct p2p_nfc_params *params,
9436 int forced_freq)
9437{
9438 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9439 "connection handover");
9440 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9441 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009442 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009443 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009444 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009445 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009446}
9447
9448
9449static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9450 struct p2p_nfc_params *params,
9451 int forced_freq)
9452{
9453 int res;
9454
9455 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9456 "connection handover");
9457 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9458 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009459 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009460 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009461 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009462 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009463 if (res)
9464 return res;
9465
9466 res = wpas_p2p_listen(wpa_s, 60);
9467 if (res) {
9468 p2p_unauthorize(wpa_s->global->p2p,
9469 params->peer->p2p_device_addr);
9470 }
9471
9472 return res;
9473}
9474
9475
9476static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9477 const struct wpabuf *data,
9478 int sel, int tag, int forced_freq)
9479{
9480 const u8 *pos, *end;
9481 u16 len, id;
9482 struct p2p_nfc_params params;
9483 int res;
9484
9485 os_memset(&params, 0, sizeof(params));
9486 params.sel = sel;
9487
9488 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9489
9490 pos = wpabuf_head(data);
9491 end = pos + wpabuf_len(data);
9492
9493 if (end - pos < 2) {
9494 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9495 "attributes");
9496 return -1;
9497 }
9498 len = WPA_GET_BE16(pos);
9499 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009500 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009501 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9502 "attributes");
9503 return -1;
9504 }
9505 params.wsc_attr = pos;
9506 params.wsc_len = len;
9507 pos += len;
9508
9509 if (end - pos < 2) {
9510 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9511 "attributes");
9512 return -1;
9513 }
9514 len = WPA_GET_BE16(pos);
9515 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009516 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009517 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9518 "attributes");
9519 return -1;
9520 }
9521 params.p2p_attr = pos;
9522 params.p2p_len = len;
9523 pos += len;
9524
9525 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9526 params.wsc_attr, params.wsc_len);
9527 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9528 params.p2p_attr, params.p2p_len);
9529 if (pos < end) {
9530 wpa_hexdump(MSG_DEBUG,
9531 "P2P: Ignored extra data after P2P attributes",
9532 pos, end - pos);
9533 }
9534
9535 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9536 if (res)
9537 return res;
9538
9539 if (params.next_step == NO_ACTION)
9540 return 0;
9541
9542 if (params.next_step == BOTH_GO) {
9543 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9544 MAC2STR(params.peer->p2p_device_addr));
9545 return 0;
9546 }
9547
9548 if (params.next_step == PEER_CLIENT) {
9549 if (!is_zero_ether_addr(params.go_dev_addr)) {
9550 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9551 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9552 " ssid=\"%s\"",
9553 MAC2STR(params.peer->p2p_device_addr),
9554 params.go_freq,
9555 MAC2STR(params.go_dev_addr),
9556 wpa_ssid_txt(params.go_ssid,
9557 params.go_ssid_len));
9558 } else {
9559 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9560 "peer=" MACSTR " freq=%d",
9561 MAC2STR(params.peer->p2p_device_addr),
9562 params.go_freq);
9563 }
9564 return 0;
9565 }
9566
9567 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9568 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9569 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9570 return 0;
9571 }
9572
9573 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9574 wpa_s->p2p_oob_dev_pw = NULL;
9575
9576 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9577 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9578 "received");
9579 return -1;
9580 }
9581
9582 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9583 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9584 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9585 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9586 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9587 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9588 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9589
9590 if (tag) {
9591 if (id < 0x10) {
9592 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9593 "peer OOB Device Password Id %u", id);
9594 return -1;
9595 }
9596 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9597 "Device Password Id %u", id);
9598 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9599 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9600 params.oob_dev_pw_len -
9601 WPS_OOB_PUBKEY_HASH_LEN - 2);
9602 wpa_s->p2p_oob_dev_pw_id = id;
9603 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9604 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9605 params.oob_dev_pw_len -
9606 WPS_OOB_PUBKEY_HASH_LEN - 2);
9607 if (wpa_s->p2p_oob_dev_pw == NULL)
9608 return -1;
9609
9610 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9611 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9612 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9613 return -1;
9614 } else {
9615 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9616 "without Device Password");
9617 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9618 }
9619
9620 switch (params.next_step) {
9621 case NO_ACTION:
9622 case BOTH_GO:
9623 case PEER_CLIENT:
9624 /* already covered above */
9625 return 0;
9626 case JOIN_GROUP:
9627 return wpas_p2p_nfc_join_group(wpa_s, &params);
9628 case AUTH_JOIN:
9629 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9630 case INIT_GO_NEG:
9631 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9632 case RESP_GO_NEG:
9633 /* TODO: use own OOB Dev Pw */
9634 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9635 }
9636
9637 return -1;
9638}
9639
9640
9641int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9642 const struct wpabuf *data, int forced_freq)
9643{
9644 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9645 return -1;
9646
9647 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9648}
9649
9650
9651int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9652 const struct wpabuf *req,
9653 const struct wpabuf *sel, int forced_freq)
9654{
9655 struct wpabuf *tmp;
9656 int ret;
9657
9658 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9659 return -1;
9660
9661 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9662
9663 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9664 wpabuf_head(req), wpabuf_len(req));
9665 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9666 wpabuf_head(sel), wpabuf_len(sel));
9667 if (forced_freq)
9668 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9669 tmp = ndef_parse_p2p(init ? sel : req);
9670 if (tmp == NULL) {
9671 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9672 return -1;
9673 }
9674
9675 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9676 forced_freq);
9677 wpabuf_free(tmp);
9678
9679 return ret;
9680}
9681
9682
9683int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9684{
9685 const u8 *if_addr;
9686 int go_intent = wpa_s->conf->p2p_go_intent;
9687 struct wpa_supplicant *iface;
9688
9689 if (wpa_s->global->p2p == NULL)
9690 return -1;
9691
9692 if (!enabled) {
9693 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9694 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9695 {
9696 if (!iface->ap_iface)
9697 continue;
9698 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9699 }
9700 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9701 0, NULL);
9702 if (wpa_s->p2p_nfc_tag_enabled)
9703 wpas_p2p_remove_pending_group_interface(wpa_s);
9704 wpa_s->p2p_nfc_tag_enabled = 0;
9705 return 0;
9706 }
9707
9708 if (wpa_s->global->p2p_disabled)
9709 return -1;
9710
9711 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9712 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9713 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9714 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9715 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9716 "to allow static handover cases");
9717 return -1;
9718 }
9719
9720 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9721
9722 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9723 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9724 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9725 if (wpa_s->p2p_oob_dev_pw == NULL)
9726 return -1;
9727 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9728
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009729 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9730 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9731 /*
9732 * P2P Group Interface present and the command came on group
9733 * interface, so enable the token for the current interface.
9734 */
9735 wpa_s->create_p2p_iface = 0;
9736 } else {
9737 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9738 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009739
9740 if (wpa_s->create_p2p_iface) {
9741 enum wpa_driver_if_type iftype;
9742 /* Prepare to add a new interface for the group */
9743 iftype = WPA_IF_P2P_GROUP;
9744 if (go_intent == 15)
9745 iftype = WPA_IF_P2P_GO;
9746 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9747 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9748 "interface for the group");
9749 return -1;
9750 }
9751
9752 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009753 } else if (wpa_s->p2p_mgmt)
9754 if_addr = wpa_s->parent->own_addr;
9755 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009756 if_addr = wpa_s->own_addr;
9757
9758 wpa_s->p2p_nfc_tag_enabled = enabled;
9759
9760 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9761 struct hostapd_data *hapd;
9762 if (iface->ap_iface == NULL)
9763 continue;
9764 hapd = iface->ap_iface->bss[0];
9765 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9766 hapd->conf->wps_nfc_dh_pubkey =
9767 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9768 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9769 hapd->conf->wps_nfc_dh_privkey =
9770 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9771 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9772 hapd->conf->wps_nfc_dev_pw =
9773 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9774 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9775
9776 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9777 wpa_dbg(iface, MSG_DEBUG,
9778 "P2P: Failed to enable NFC Tag for GO");
9779 }
9780 }
9781 p2p_set_authorized_oob_dev_pw_id(
9782 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9783 if_addr);
9784
9785 return 0;
9786}
9787
9788#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009789
9790
9791static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9792 struct wpa_used_freq_data *freqs,
9793 unsigned int num)
9794{
9795 u8 curr_chan, cand, chan;
9796 unsigned int i;
9797
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009798 /*
9799 * If possible, optimize the Listen channel to be a channel that is
9800 * already used by one of the other interfaces.
9801 */
9802 if (!wpa_s->conf->p2p_optimize_listen_chan)
9803 return;
9804
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009805 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9806 for (i = 0, cand = 0; i < num; i++) {
9807 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9808 if (curr_chan == chan) {
9809 cand = 0;
9810 break;
9811 }
9812
9813 if (chan == 1 || chan == 6 || chan == 11)
9814 cand = chan;
9815 }
9816
9817 if (cand) {
9818 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009819 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009820 cand);
9821 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9822 }
9823}
9824
9825
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009826static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009827{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009828 struct hostapd_config *conf;
9829 struct p2p_go_neg_results params;
9830 struct csa_settings csa_settings;
9831 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9832 int old_freq = current_ssid->frequency;
9833 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009834
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009835 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9836 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9837 return -1;
9838 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009839
9840 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009841 * TODO: This function may not always work correctly. For example,
9842 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009843 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009844 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9845 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009846 wpa_dbg(wpa_s, MSG_DEBUG,
9847 "P2P CSA: Failed to select new frequency for GO");
9848 return -1;
9849 }
9850
9851 if (current_ssid->frequency == params.freq) {
9852 wpa_dbg(wpa_s, MSG_DEBUG,
9853 "P2P CSA: Selected same frequency - not moving GO");
9854 return 0;
9855 }
9856
9857 conf = hostapd_config_defaults();
9858 if (!conf) {
9859 wpa_dbg(wpa_s, MSG_DEBUG,
9860 "P2P CSA: Failed to allocate default config");
9861 return -1;
9862 }
9863
9864 current_ssid->frequency = params.freq;
9865 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9866 wpa_dbg(wpa_s, MSG_DEBUG,
9867 "P2P CSA: Failed to create new GO config");
9868 ret = -1;
9869 goto out;
9870 }
9871
Sunil4a3f9f52022-07-19 22:04:39 +00009872 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9873 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009874 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009875 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9876 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009877 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9878 wpa_s->ap_iface->current_mode->mode,
9879 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9880 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009881 ret = -1;
9882 goto out;
9883 }
9884
9885 os_memset(&csa_settings, 0, sizeof(csa_settings));
9886 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9887 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9888 csa_settings.freq_params.freq = params.freq;
9889 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9890 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9891 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9892
9893 if (conf->ieee80211ac) {
9894 int freq1 = 0, freq2 = 0;
9895 u8 chan, opclass;
9896
9897 if (ieee80211_freq_to_channel_ext(params.freq,
9898 conf->secondary_channel,
9899 conf->vht_oper_chwidth,
9900 &opclass, &chan) ==
9901 NUM_HOSTAPD_MODES) {
9902 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9903 ret = -1;
9904 goto out;
9905 }
9906
9907 if (conf->vht_oper_centr_freq_seg0_idx)
9908 freq1 = ieee80211_chan_to_freq(
9909 NULL, opclass,
9910 conf->vht_oper_centr_freq_seg0_idx);
9911
9912 if (conf->vht_oper_centr_freq_seg1_idx)
9913 freq2 = ieee80211_chan_to_freq(
9914 NULL, opclass,
9915 conf->vht_oper_centr_freq_seg1_idx);
9916
9917 if (freq1 < 0 || freq2 < 0) {
9918 wpa_dbg(wpa_s, MSG_DEBUG,
9919 "P2P CSA: Selected invalid VHT center freqs");
9920 ret = -1;
9921 goto out;
9922 }
9923
9924 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9925 csa_settings.freq_params.center_freq1 = freq1;
9926 csa_settings.freq_params.center_freq2 = freq2;
9927
9928 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009929 case CONF_OPER_CHWIDTH_80MHZ:
9930 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009931 csa_settings.freq_params.bandwidth = 80;
9932 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009933 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009934 csa_settings.freq_params.bandwidth = 160;
9935 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009936 default:
9937 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009938 }
9939 }
9940
9941 ret = ap_switch_channel(wpa_s, &csa_settings);
9942out:
9943 current_ssid->frequency = old_freq;
9944 hostapd_config_free(conf);
9945 return ret;
9946}
9947
9948
9949static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9950{
9951 struct p2p_go_neg_results params;
9952 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009953 void (*ap_configured_cb)(void *ctx, void *data);
9954 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009955
9956 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9957
9958 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9959 current_ssid->frequency);
9960
9961 /* Stop the AP functionality */
9962 /* TODO: Should do this in a way that does not indicated to possible
9963 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009964 /* If this action occurs before a group is started, the callback should
9965 * be preserved, or GROUP-STARTED event would be lost. If this action
9966 * occurs after a group is started, these pointers are all NULL and
9967 * harmless. */
9968 ap_configured_cb = wpa_s->ap_configured_cb;
9969 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9970 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009971 wpa_supplicant_ap_deinit(wpa_s);
9972
9973 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009974 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9975 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009976 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9977 wpas_p2p_group_delete(wpa_s,
9978 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9979 return;
9980 }
9981 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9982 params.freq);
9983
9984 if (params.freq &&
9985 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9986 wpa_printf(MSG_DEBUG,
9987 "P2P: Selected freq (%u MHz) is not valid for P2P",
9988 params.freq);
9989 wpas_p2p_group_delete(wpa_s,
9990 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9991 return;
9992 }
9993
Hai Shalom899fcc72020-10-19 14:38:18 -07009994 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009995 wpa_s->ap_configured_cb = ap_configured_cb;
9996 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9997 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009998
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009999 /* Update the frequency */
10000 current_ssid->frequency = params.freq;
10001 wpa_s->connect_without_scan = current_ssid;
10002 wpa_s->reassociate = 1;
10003 wpa_s->disconnected = 0;
10004 wpa_supplicant_req_scan(wpa_s, 0, 0);
10005}
10006
10007
10008static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
10009{
10010 struct wpa_supplicant *wpa_s = eloop_ctx;
10011
10012 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010013 return;
10014
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010015 wpas_p2p_go_update_common_freqs(wpa_s);
10016
10017 /* Do not move GO in the middle of a CSA */
10018 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10019 wpa_printf(MSG_DEBUG,
10020 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010021 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010022 }
10023
10024 /*
10025 * First, try a channel switch flow. If it is not supported or fails,
10026 * take down the GO and bring it up again.
10027 */
10028 if (wpas_p2p_move_go_csa(wpa_s) < 0)
10029 wpas_p2p_move_go_no_csa(wpa_s);
10030}
10031
10032
10033static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
10034{
10035 struct wpa_supplicant *wpa_s = eloop_ctx;
10036 struct wpa_used_freq_data *freqs = NULL;
10037 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010038
10039 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
10040 if (!freqs)
10041 return;
10042
Sunil Ravi77d572f2023-01-17 23:58:31 +000010043 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010044
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010045 /* Previous attempt to move a GO was not possible -- try again. */
10046 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
10047 WPAS_P2P_CHANNEL_UPDATE_ANY);
10048
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010049 os_free(freqs);
10050}
10051
10052
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010053/*
10054 * Consider moving a GO from its currently used frequency:
10055 * 1. It is possible that due to regulatory consideration the frequency
10056 * can no longer be used and there is a need to evacuate the GO.
10057 * 2. It is possible that due to MCC considerations, it would be preferable
10058 * to move the GO to a channel that is currently used by some other
10059 * station interface.
10060 *
10061 * In case a frequency that became invalid is once again valid, cancel a
10062 * previously initiated GO frequency change.
10063 */
10064static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
10065 struct wpa_used_freq_data *freqs,
10066 unsigned int num)
10067{
10068 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
10069 unsigned int timeout;
10070 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010071 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010072
10073 wpas_p2p_go_update_common_freqs(wpa_s);
10074
10075 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010076 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010077 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010078 for (i = 0, invalid_freq = 0; i < num; i++) {
10079 if (freqs[i].freq == freq) {
10080 flags = freqs[i].flags;
10081
10082 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010083 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10084 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010085 wpa_dbg(wpa_s, MSG_DEBUG,
10086 "P2P: Freq=%d MHz no longer valid for GO",
10087 freq);
10088 invalid_freq = 1;
10089 }
10090 } else if (freqs[i].flags == 0) {
10091 /* Freq is not used by any other station interface */
10092 continue;
10093 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010094 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010095 /* Freq is not valid for P2P use cases */
10096 continue;
10097 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10098 P2P_GO_FREQ_MOVE_SCM) {
10099 policy_move = 1;
10100 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10101 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10102 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10103 policy_move = 1;
10104 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10105 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10106 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10107 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10108 policy_move = 1;
10109 } else if ((wpa_s->drv_flags &
10110 WPA_DRIVER_FLAGS_AP_CSA) &&
10111 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10112 u8 chan;
10113
10114 /*
10115 * We do not support CSA between bands, so move
10116 * GO only within the same band.
10117 */
10118 if (wpa_s->ap_iface->current_mode->mode ==
10119 ieee80211_freq_to_chan(freqs[i].freq,
10120 &chan))
10121 policy_move = 1;
10122 }
10123 }
10124 }
10125
10126 wpa_dbg(wpa_s, MSG_DEBUG,
10127 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10128 invalid_freq, policy_move, flags);
10129
10130 /*
10131 * The channel is valid, or we are going to have a policy move, so
10132 * cancel timeout.
10133 */
10134 if (!invalid_freq || policy_move) {
10135 wpa_dbg(wpa_s, MSG_DEBUG,
10136 "P2P: Cancel a GO move from freq=%d MHz", freq);
10137 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10138
10139 if (wpas_p2p_in_progress(wpa_s)) {
10140 wpa_dbg(wpa_s, MSG_DEBUG,
10141 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10142 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10143 wpa_s, NULL);
10144 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10145 wpas_p2p_reconsider_moving_go,
10146 wpa_s, NULL);
10147 return;
10148 }
10149 }
10150
10151 if (!invalid_freq && (!policy_move || flags != 0)) {
10152 wpa_dbg(wpa_s, MSG_DEBUG,
10153 "P2P: Not initiating a GO frequency change");
10154 return;
10155 }
10156
10157 /*
10158 * Do not consider moving GO if it is in the middle of a CSA. When the
10159 * CSA is finished this flow should be retriggered.
10160 */
10161 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10162 wpa_dbg(wpa_s, MSG_DEBUG,
10163 "P2P: Not initiating a GO frequency change - CSA is in progress");
10164 return;
10165 }
10166
10167 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10168 timeout = P2P_GO_FREQ_CHANGE_TIME;
10169 else
10170 timeout = 0;
10171
10172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10173 freq, timeout);
10174 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10175 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10176}
10177
10178
10179static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10180 struct wpa_used_freq_data *freqs,
10181 unsigned int num,
10182 enum wpas_p2p_channel_update_trig trig)
10183{
10184 struct wpa_supplicant *ifs;
10185
10186 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10187 NULL);
10188
10189 /*
10190 * Travers all the radio interfaces, and for each GO interface, check
10191 * if there is a need to move the GO from the frequency it is using,
10192 * or in case the frequency is valid again, cancel the evacuation flow.
10193 */
10194 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10195 radio_list) {
10196 if (ifs->current_ssid == NULL ||
10197 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10198 continue;
10199
10200 /*
10201 * The GO was just started or completed channel switch, no need
10202 * to move it.
10203 */
10204 if (wpa_s == ifs &&
10205 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10206 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10207 wpa_dbg(wpa_s, MSG_DEBUG,
10208 "P2P: GO move - schedule re-consideration");
10209 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10210 wpas_p2p_reconsider_moving_go,
10211 wpa_s, NULL);
10212 continue;
10213 }
10214
10215 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10216 }
10217}
10218
10219
10220void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10221{
10222 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10223 return;
10224
10225 wpas_p2p_update_channel_list(wpa_s,
10226 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10227}
10228
10229
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010230void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10231{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010232 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10234 "the management interface is being removed");
10235 wpas_p2p_deinit_global(wpa_s->global);
10236 }
10237}
10238
10239
10240void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10241{
10242 if (wpa_s->ap_iface->bss)
10243 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10244 wpas_p2p_group_deinit(wpa_s);
10245}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010246
10247
10248int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10249 unsigned int period, unsigned int interval,
10250 unsigned int count)
10251{
10252 struct p2p_data *p2p = wpa_s->global->p2p;
10253 u8 *device_types;
10254 size_t dev_types_len;
10255 struct wpabuf *buf;
10256 int ret;
10257
10258 if (wpa_s->p2p_lo_started) {
10259 wpa_dbg(wpa_s, MSG_DEBUG,
10260 "P2P Listen offload is already started");
10261 return 0;
10262 }
10263
10264 if (wpa_s->global->p2p == NULL ||
10265 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10266 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10267 return -1;
10268 }
10269
10270 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10271 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10272 freq);
10273 return -1;
10274 }
10275
10276 /* Get device type */
10277 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10278 WPS_DEV_TYPE_LEN;
10279 device_types = os_malloc(dev_types_len);
10280 if (!device_types)
10281 return -1;
10282 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10283 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10284 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10285
10286 /* Get Probe Response IE(s) */
10287 buf = p2p_build_probe_resp_template(p2p, freq);
10288 if (!buf) {
10289 os_free(device_types);
10290 return -1;
10291 }
10292
10293 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10294 device_types, dev_types_len,
10295 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10296 if (ret < 0)
10297 wpa_dbg(wpa_s, MSG_DEBUG,
10298 "P2P: Failed to start P2P listen offload");
10299
10300 os_free(device_types);
10301 wpabuf_free(buf);
10302
10303 if (ret == 0) {
10304 wpa_s->p2p_lo_started = 1;
10305
10306 /* Stop current P2P listen if any */
10307 wpas_stop_listen(wpa_s);
10308 }
10309
10310 return ret;
10311}
10312
10313
10314int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10315{
10316 int ret;
10317
10318 if (!wpa_s->p2p_lo_started)
10319 return 0;
10320
10321 ret = wpa_drv_p2p_lo_stop(wpa_s);
10322 if (ret < 0)
10323 wpa_dbg(wpa_s, MSG_DEBUG,
10324 "P2P: Failed to stop P2P listen offload");
10325
10326 wpa_s->p2p_lo_started = 0;
10327 return ret;
10328}