blob: a505e4fe78e195be041b3fb00803e0563fbbfab7 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +0000132 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
169
Hai Shalomc1a21442022-02-04 13:43:00 -0800170static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
171{
172 int he_capab = 0;
173
174 if (mode)
175 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
176 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
177 return he_capab;
178}
179
180
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700181/*
182 * Get the number of concurrent channels that the HW can operate, but that are
183 * currently not in use by any of the wpa_supplicant interfaces.
184 */
185static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
186{
187 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800188 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700189
190 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
191 if (!freqs)
192 return -1;
193
194 num = get_shared_radio_freqs(wpa_s, freqs,
195 wpa_s->num_multichan_concurrent);
196 os_free(freqs);
197
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800198 unused = wpa_s->num_multichan_concurrent - num;
199 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
200 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700201}
202
203
204/*
205 * Get the frequencies that are currently in use by one or more of the virtual
206 * interfaces, and that are also valid for P2P operation.
207 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700208static unsigned int
209wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
210 struct wpa_used_freq_data *p2p_freqs,
211 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700212{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 unsigned int num, i, j;
215
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700216 freqs = os_calloc(wpa_s->num_multichan_concurrent,
217 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700218 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700220
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700221 num = get_shared_radio_freqs_data(wpa_s, freqs,
222 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700223
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700224 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700225
226 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700227 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228 p2p_freqs[j++] = freqs[i];
229 }
230
231 os_free(freqs);
232
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700233 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800234
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700235 return j;
236}
237
238
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
240 int freq)
241{
242 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
243 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700244
245 /* Use the wpa_s used to control the P2P Device operation */
246 wpa_s = wpa_s->global->p2p_init_wpa_s;
247
248 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800249 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
250 wpas_p2p_num_unused_channels(wpa_s) > 0) {
251 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
252 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700253 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800254 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700255 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
256}
257
258
Hai Shalom60840252021-02-19 19:02:11 -0800259static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
260{
261 unsigned int delay = wpas_p2p_search_delay(wpa_s);
262
263 /* In case of concurrent P2P and external scans, delay P2P search. */
264 if (external_scan_running(wpa_s->radio)) {
265 delay = wpa_s->conf->p2p_search_delay;
266 wpa_printf(MSG_DEBUG,
267 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
268 delay);
269 }
270
271 p2p_scan_res_handled(wpa_s->global->p2p, delay);
272}
273
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
276 struct wpa_scan_results *scan_res)
277{
278 size_t i;
279
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800280 if (wpa_s->p2p_scan_work) {
281 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
282 wpa_s->p2p_scan_work = NULL;
283 radio_work_done(work);
284 }
285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
287 return;
288
289 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
290 (int) scan_res->num);
291
292 for (i = 0; i < scan_res->num; i++) {
293 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700295 const u8 *ies;
296 size_t ies_len;
297
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800298 time_tmp_age.sec = bss->age / 1000;
299 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700301
302 ies = (const u8 *) (bss + 1);
303 ies_len = bss->ie_len;
304 if (bss->beacon_ie_len > 0 &&
305 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
306 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
307 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
308 MACSTR, MAC2STR(bss->bssid));
309 ies = ies + ies_len;
310 ies_len = bss->beacon_ie_len;
311 }
312
313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800315 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700316 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 break;
318 }
319
Hai Shalom60840252021-02-19 19:02:11 -0800320 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321}
322
323
Hai Shalom60840252021-02-19 19:02:11 -0800324static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700325{
Hai Shalom60840252021-02-19 19:02:11 -0800326 if (wpa_s->p2p_scan_work) {
327 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700328
Hai Shalom60840252021-02-19 19:02:11 -0800329 wpa_s->p2p_scan_work = NULL;
330 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700331 }
332
Hai Shalom60840252021-02-19 19:02:11 -0800333 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
334 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700335
Hai Shalom60840252021-02-19 19:02:11 -0800336 wpa_dbg(wpa_s, MSG_DEBUG,
337 "P2P: Failed to get scan results - try to continue");
338 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700339}
340
341
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800342static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
343{
344 struct wpa_supplicant *wpa_s = work->wpa_s;
345 struct wpa_driver_scan_params *params = work->ctx;
346 int ret;
347
348 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800349 if (!work->started) {
350 wpa_scan_free_params(params);
351 return;
352 }
353
354 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800355 return;
356 }
357
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800358 if (wpa_s->clear_driver_scan_cache) {
359 wpa_printf(MSG_DEBUG,
360 "Request driver to clear scan cache due to local BSS flush");
361 params->only_new_results = 1;
362 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700363
Hai Shalomc1a21442022-02-04 13:43:00 -0800364 if (!params->p2p_include_6ghz && !params->freqs) {
Hai Shalom899fcc72020-10-19 14:38:18 -0700365 wpa_printf(MSG_DEBUG,
Hai Shalomc1a21442022-02-04 13:43:00 -0800366 "P2P: Exclude 6 GHz channels - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -0800367 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
Matthew Wang36173112022-11-07 21:48:44 -0800368 false, false);
Hai Shalom60840252021-02-19 19:02:11 -0800369 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
Matthew Wang36173112022-11-07 21:48:44 -0800370 false, false);
Hai Shalom899fcc72020-10-19 14:38:18 -0700371 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800373 if (ret == 0)
374 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 wpa_scan_free_params(params);
376 work->ctx = NULL;
377 if (ret) {
378 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800380 return;
381 }
382
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800383 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800384 os_get_reltime(&wpa_s->scan_trigger_time);
385 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800386 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800387 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800388 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800389 wpa_s->p2p_scan_work = work;
390}
391
392
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800393static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
394 int freq)
395{
396 if (wpa_s->global->p2p_24ghz_social_channels &&
397 (freq == 2412 || freq == 2437 || freq == 2462)) {
398 /*
399 * Search all social channels regardless of whether these have
400 * been disabled for P2P operating channel use to avoid missing
401 * peers.
402 */
403 return 1;
404 }
405 return p2p_supported_freq(wpa_s->global->p2p, freq);
406}
407
408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
410 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800411 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
412 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413{
414 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800415 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700417 unsigned int num_channels = 0;
418 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800419 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700420 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800421 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422
423 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
424 return -1;
425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800426 if (wpa_s->p2p_scan_work) {
427 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
428 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 }
430
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800431 params = os_zalloc(sizeof(*params));
432 if (params == NULL)
433 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434
435 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800436 params->num_ssids = 1;
437 n = os_malloc(P2P_WILDCARD_SSID_LEN);
438 if (n == NULL)
439 goto fail;
440 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
441 params->ssids[0].ssid = n;
442 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443
444 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
446 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 num_req_dev_types, req_dev_types);
448 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800449 goto fail;
Hai Shalomc1a21442022-02-04 13:43:00 -0800450 if (!wpa_s->conf->p2p_6ghz_disable)
451 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 switch (type) {
453 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700454 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
455 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800456 if (params->freqs == NULL)
457 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700458 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800459 if (wpas_p2p_search_social_channel(
460 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700461 params->freqs[num_channels++] =
462 social_channels_freq[i];
463 }
464 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 break;
466 case P2P_SCAN_FULL:
467 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800468 case P2P_SCAN_SPECIFIC:
469 params->freqs = os_calloc(2, sizeof(int));
470 if (params->freqs == NULL)
471 goto fail;
472 params->freqs[0] = freq;
473 params->freqs[1] = 0;
474 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
477 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800478 if (params->freqs == NULL)
479 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700480 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481 if (wpas_p2p_search_social_channel(
482 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700483 params->freqs[num_channels++] =
484 social_channels_freq[i];
485 }
486 if (p2p_supported_freq(wpa_s->global->p2p, freq))
487 params->freqs[num_channels++] = freq;
488 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489 break;
490 }
491
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800492 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
493 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
494 if (ies == NULL) {
495 wpabuf_free(wps_ie);
496 goto fail;
497 }
498 wpabuf_put_buf(ies, wps_ie);
499 wpabuf_free(wps_ie);
500
501 bands = wpas_get_bands(wpa_s, params->freqs);
502 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
503
504 params->p2p_probe = 1;
505 n = os_malloc(wpabuf_len(ies));
506 if (n == NULL) {
507 wpabuf_free(ies);
508 goto fail;
509 }
510 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
511 params->extra_ies = n;
512 params->extra_ies_len = wpabuf_len(ies);
513 wpabuf_free(ies);
514
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800515 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800516 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
517 params) < 0)
518 goto fail;
519 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800521fail:
522 wpa_scan_free_params(params);
523 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524}
525
526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
528{
529 switch (p2p_group_interface) {
530 case P2P_GROUP_INTERFACE_PENDING:
531 return WPA_IF_P2P_GROUP;
532 case P2P_GROUP_INTERFACE_GO:
533 return WPA_IF_P2P_GO;
534 case P2P_GROUP_INTERFACE_CLIENT:
535 return WPA_IF_P2P_CLIENT;
536 }
537
538 return WPA_IF_P2P_GROUP;
539}
540
541
542static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
543 const u8 *ssid,
544 size_t ssid_len, int *go)
545{
546 struct wpa_ssid *s;
547
548 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
549 for (s = wpa_s->conf->ssid; s; s = s->next) {
550 if (s->disabled != 0 || !s->p2p_group ||
551 s->ssid_len != ssid_len ||
552 os_memcmp(ssid, s->ssid, ssid_len) != 0)
553 continue;
554 if (s->mode == WPAS_MODE_P2P_GO &&
555 s != wpa_s->current_ssid)
556 continue;
557 if (go)
558 *go = s->mode == WPAS_MODE_P2P_GO;
559 return wpa_s;
560 }
561 }
562
563 return NULL;
564}
565
566
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800567static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
568{
569 struct wpa_supplicant *wpa_s = eloop_ctx;
570 wpa_printf(MSG_DEBUG,
571 "P2P: Complete previously requested removal of %s",
572 wpa_s->ifname);
573 wpas_p2p_disconnect(wpa_s);
574}
575
576
577static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
578 struct wpa_supplicant *calling_wpa_s)
579{
580 if (calling_wpa_s == wpa_s && wpa_s &&
581 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
582 /*
583 * The calling wpa_s instance is going to be removed. Do that
584 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700585 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800586 * control interface responses on the per-interface socket.
587 */
588 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
589 wpa_s, NULL) < 0)
590 return -1;
591 return 0;
592 }
593
594 return wpas_p2p_disconnect(wpa_s);
595}
596
597
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800598/* Determine total number of clients in active groups where we are the GO */
599static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
600{
601 unsigned int count = 0;
602 struct wpa_ssid *s;
603
604 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
605 for (s = wpa_s->conf->ssid; s; s = s->next) {
606 wpa_printf(MSG_DEBUG,
607 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
608 wpa_s, s, s->disabled, s->p2p_group,
609 s->mode);
610 if (!s->disabled && s->p2p_group &&
611 s->mode == WPAS_MODE_P2P_GO) {
612 count += p2p_get_group_num_members(
613 wpa_s->p2p_group);
614 }
615 }
616 }
617
618 return count;
619}
620
621
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800622static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
623{
624 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
625 !wpa_s->current_ssid->disabled &&
626 wpa_s->current_ssid->p2p_group &&
627 wpa_s->current_ssid->p2p_persistent_group;
628}
629
630
631static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
632{
633 return p2p_is_active_persistent_group(wpa_s) &&
634 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
635}
636
637
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800638/* Find an interface for a P2P group where we are the GO */
639static struct wpa_supplicant *
640wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
641{
642 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800643
644 if (!wpa_s)
645 return NULL;
646
647 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 if (!p2p_is_active_persistent_go(wpa_s))
649 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800650
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800651 /* Prefer a group with connected clients */
652 if (p2p_get_group_num_members(wpa_s->p2p_group))
653 return wpa_s;
654 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800655 }
656
657 /* No group with connected clients, so pick the one without (if any) */
658 return save;
659}
660
661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800662static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800663{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800664 return p2p_is_active_persistent_group(wpa_s) &&
665 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
666}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800667
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800668
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800669/* Find an interface for a P2P group where we are the P2P Client */
670static struct wpa_supplicant *
671wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
672{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800673 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800674 if (p2p_is_active_persistent_cli(wpa_s))
675 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800676 }
677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800679}
680
681
682/* Find a persistent group where we are the GO */
683static struct wpa_ssid *
684wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
685{
686 struct wpa_ssid *s;
687
688 for (s = wpa_s->conf->ssid; s; s = s->next) {
689 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
690 return s;
691 }
692
693 return NULL;
694}
695
696
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800697static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
698 unsigned int *force_freq,
699 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800700{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800701 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800702 struct wpa_ssid *s;
703 u8 conncap = P2PS_SETUP_NONE;
704 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800705 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800706 struct wpa_ssid *persistent_go;
707 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000708 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
709 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800710
711 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
712
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800713 if (force_freq)
714 *force_freq = 0;
715 if (pref_freq)
716 *pref_freq = 0;
717
718 size = P2P_MAX_PREF_CHANNELS;
719 if (force_freq && pref_freq &&
720 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
721 (int *) pref_freq, 0, pref_freq_list, &size))
722 wpas_p2p_set_own_freq_preference(wpa_s,
723 *force_freq ? *force_freq :
724 *pref_freq);
725
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800726 /*
727 * For non-concurrent capable devices:
728 * If persistent_go, then no new.
729 * If GO, then no client.
730 * If client, then no GO.
731 */
732 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800733 if (go_wpa_s)
734 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800735 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800736 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
737 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800738
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800739 wpa_printf(MSG_DEBUG,
740 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
741 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800742
743 /* If not concurrent, restrict our choices */
744 if (p2p_no_group_iface) {
745 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
746
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800747 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800748 return P2PS_SETUP_NONE;
749
750 if (go_wpa_s) {
751 if (role == P2PS_SETUP_CLIENT ||
752 incoming == P2PS_SETUP_GROUP_OWNER ||
753 p2p_client_limit_reached(go_wpa_s->p2p_group))
754 return P2PS_SETUP_NONE;
755
756 return P2PS_SETUP_GROUP_OWNER;
757 }
758
759 if (persistent_go) {
760 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
761 if (!incoming)
762 return P2PS_SETUP_GROUP_OWNER |
763 P2PS_SETUP_CLIENT;
764 if (incoming == P2PS_SETUP_NEW) {
765 u8 r;
766
767 if (os_get_random(&r, sizeof(r)) < 0 ||
768 (r & 1))
769 return P2PS_SETUP_CLIENT;
770 return P2PS_SETUP_GROUP_OWNER;
771 }
772 }
773 }
774 }
775
776 /* If a required role has been specified, handle it here */
777 if (role && role != P2PS_SETUP_NEW) {
778 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800779 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
780 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
781 /*
782 * Peer has an active GO, so if the role allows it and
783 * we do not have any active roles, become client.
784 */
785 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
786 !cli_wpa_s)
787 return P2PS_SETUP_CLIENT;
788
789 /* fall through */
790
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800791 case P2PS_SETUP_NONE:
792 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800793 conncap = role;
794 goto grp_owner;
795
796 case P2PS_SETUP_GROUP_OWNER:
797 /*
798 * Must be a complimentary role - cannot be a client to
799 * more than one peer.
800 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800802 return P2PS_SETUP_NONE;
803
804 return P2PS_SETUP_CLIENT;
805
806 case P2PS_SETUP_CLIENT:
807 /* Must be a complimentary role */
808 if (incoming != role) {
809 conncap = P2PS_SETUP_GROUP_OWNER;
810 goto grp_owner;
811 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700812 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813
814 default:
815 return P2PS_SETUP_NONE;
816 }
817 }
818
819 /*
820 * For now, we only will support ownership of one group, and being a
821 * client of one group. Therefore, if we have either an existing GO
822 * group, or an existing client group, we will not do a new GO
823 * negotiation, but rather try to re-use the existing groups.
824 */
825 switch (incoming) {
826 case P2PS_SETUP_NONE:
827 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800828 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800829 conncap = P2PS_SETUP_GROUP_OWNER;
830 else if (!owned_members)
831 conncap = P2PS_SETUP_NEW;
832 else if (incoming == P2PS_SETUP_NONE)
833 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
834 else
835 conncap = P2PS_SETUP_CLIENT;
836 break;
837
838 case P2PS_SETUP_CLIENT:
839 conncap = P2PS_SETUP_GROUP_OWNER;
840 break;
841
842 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800843 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800844 conncap = P2PS_SETUP_CLIENT;
845 break;
846
847 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
848 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800849 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800850 conncap = P2PS_SETUP_GROUP_OWNER;
851 else {
852 u8 r;
853
854 if (os_get_random(&r, sizeof(r)) < 0 ||
855 (r & 1))
856 conncap = P2PS_SETUP_CLIENT;
857 else
858 conncap = P2PS_SETUP_GROUP_OWNER;
859 }
860 break;
861
862 default:
863 return P2PS_SETUP_NONE;
864 }
865
866grp_owner:
867 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
868 (!incoming && (conncap & P2PS_SETUP_NEW))) {
869 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
870 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800871
872 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800873 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800874 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800875 wpa_s->p2p_mgmt ?
876 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800877 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800878 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800879 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800880 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800881 wpa_printf(MSG_ERROR,
882 "P2P: Failed to allocate a new interface for the group");
883 return P2PS_SETUP_NONE;
884 }
885 wpa_s->global->pending_group_iface_for_p2ps = 1;
886 p2p_set_intended_addr(wpa_s->global->p2p,
887 wpa_s->pending_interface_addr);
888 }
889 }
890
891 return conncap;
892}
893
894
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
896 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897{
898 struct wpa_ssid *ssid;
899 char *gtype;
900 const char *reason;
901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 ssid = wpa_s->current_ssid;
903 if (ssid == NULL) {
904 /*
905 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700906 * pending P2P group interface waiting for provisioning or a
907 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 */
909 ssid = wpa_s->conf->ssid;
910 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700911 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 break;
913 ssid = ssid->next;
914 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300915 if (ssid == NULL &&
916 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
917 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700918 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
919 "not found");
920 return -1;
921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 }
923 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
924 gtype = "GO";
925 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
926 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
927 wpa_s->reassociate = 0;
928 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 gtype = "client";
930 } else
931 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700932
933 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
934 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
935
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800936 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700937 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800938 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
939 wpa_s, NULL)) {
940 wpa_printf(MSG_DEBUG,
941 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
942 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
943 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
944 wpa_s, NULL);
945 }
946 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 if (wpa_s->cross_connect_in_use) {
949 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800950 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700951 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
952 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 case P2P_GROUP_REMOVAL_REQUESTED:
956 reason = " reason=REQUESTED";
957 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700958 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
959 reason = " reason=FORMATION_FAILED";
960 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
962 reason = " reason=IDLE";
963 break;
964 case P2P_GROUP_REMOVAL_UNAVAILABLE:
965 reason = " reason=UNAVAILABLE";
966 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
968 reason = " reason=GO_ENDING_SESSION";
969 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700970 case P2P_GROUP_REMOVAL_PSK_FAILURE:
971 reason = " reason=PSK_FAILURE";
972 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800973 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
974 reason = " reason=FREQ_CONFLICT";
975 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 default:
977 reason = "";
978 break;
979 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800981 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700982 P2P_EVENT_GROUP_REMOVED "%s %s%s",
983 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800986 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
987 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700988 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
989 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800990 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800991 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800992 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
993 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700994 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800995 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700996 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997
Dmitry Shmidt15907092014-03-25 10:42:57 -0700998 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +0000999 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001000 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1001 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001002
Dmitry Shmidt96571392013-10-14 12:54:46 -07001003 /*
1004 * Make sure wait for the first client does not remain active after the
1005 * group has been removed.
1006 */
1007 wpa_s->global->p2p_go_wait_client.sec = 0;
1008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1010 struct wpa_global *global;
1011 char *ifname;
1012 enum wpa_driver_if_type type;
1013 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1014 wpa_s->ifname);
1015 global = wpa_s->global;
1016 ifname = os_strdup(wpa_s->ifname);
1017 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001018 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001019 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 wpa_s = global->ifaces;
1021 if (wpa_s && ifname)
1022 wpa_drv_if_remove(wpa_s, type, ifname);
1023 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001024 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 }
1026
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001027 /*
1028 * The primary interface was used for P2P group operations, so
1029 * need to reset its p2pdev.
1030 */
1031 wpa_s->p2pdev = wpa_s->parent;
1032
Dmitry Shmidt96571392013-10-14 12:54:46 -07001033 if (!wpa_s->p2p_go_group_formation_completed) {
1034 wpa_s->global->p2p_group_formation = NULL;
1035 wpa_s->p2p_in_provisioning = 0;
1036 }
1037
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001038 wpa_s->show_group_started = 0;
1039 os_free(wpa_s->go_params);
1040 wpa_s->go_params = NULL;
1041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001042 os_free(wpa_s->p2p_group_common_freqs);
1043 wpa_s->p2p_group_common_freqs = NULL;
1044 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001045 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001046 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001047
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001048 wpa_s->waiting_presence_resp = 0;
1049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1051 if (ssid && (ssid->p2p_group ||
1052 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1053 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1054 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001055 if (ssid == wpa_s->current_ssid) {
1056 wpa_sm_set_config(wpa_s->wpa, NULL);
1057 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001059 }
1060 /*
1061 * Networks objects created during any P2P activities are not
1062 * exposed out as they might/will confuse certain non-P2P aware
1063 * applications since these network objects won't behave like
1064 * regular ones.
1065 *
1066 * Likewise, we don't send out network removed signals for such
1067 * network objects.
1068 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 wpa_config_remove_network(wpa_s->conf, id);
1070 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001071 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 } else {
1073 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1074 "found");
1075 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001076 if (wpa_s->ap_iface)
1077 wpa_supplicant_ap_deinit(wpa_s);
1078 else
1079 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001080
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001081 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1082
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001083 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084}
1085
1086
1087static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1088 u8 *go_dev_addr,
1089 const u8 *ssid, size_t ssid_len)
1090{
1091 struct wpa_bss *bss;
1092 const u8 *bssid;
1093 struct wpabuf *p2p;
1094 u8 group_capab;
1095 const u8 *addr;
1096
1097 if (wpa_s->go_params)
1098 bssid = wpa_s->go_params->peer_interface_addr;
1099 else
1100 bssid = wpa_s->bssid;
1101
1102 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001103 if (bss == NULL && wpa_s->go_params &&
1104 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1105 bss = wpa_bss_get_p2p_dev_addr(
1106 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 if (bss == NULL) {
1108 u8 iface_addr[ETH_ALEN];
1109 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1110 iface_addr) == 0)
1111 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1112 }
1113 if (bss == NULL) {
1114 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1115 "group is persistent - BSS " MACSTR " not found",
1116 MAC2STR(bssid));
1117 return 0;
1118 }
1119
1120 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001121 if (p2p == NULL)
1122 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1123 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 if (p2p == NULL) {
1125 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1126 "group is persistent - BSS " MACSTR
1127 " did not include P2P IE", MAC2STR(bssid));
1128 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001129 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001131 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 bss->beacon_ie_len);
1133 return 0;
1134 }
1135
1136 group_capab = p2p_get_group_capab(p2p);
1137 addr = p2p_get_go_dev_addr(p2p);
1138 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1139 "group_capab=0x%x", group_capab);
1140 if (addr) {
1141 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1142 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1143 MAC2STR(addr));
1144 } else
1145 os_memset(go_dev_addr, 0, ETH_ALEN);
1146 wpabuf_free(p2p);
1147
1148 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1149 "go_dev_addr=" MACSTR,
1150 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1151
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001152 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153}
1154
1155
Jouni Malinen75ecf522011-06-27 15:19:46 -07001156static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1157 struct wpa_ssid *ssid,
1158 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159{
1160 struct wpa_ssid *s;
1161 int changed = 0;
1162
1163 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1164 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1165 for (s = wpa_s->conf->ssid; s; s = s->next) {
1166 if (s->disabled == 2 &&
1167 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1168 s->ssid_len == ssid->ssid_len &&
1169 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1170 break;
1171 }
1172
1173 if (s) {
1174 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1175 "entry");
1176 if (ssid->passphrase && !s->passphrase)
1177 changed = 1;
1178 else if (ssid->passphrase && s->passphrase &&
1179 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1180 changed = 1;
1181 } else {
1182 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1183 "entry");
1184 changed = 1;
1185 s = wpa_config_add_network(wpa_s->conf);
1186 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001187 return -1;
1188
1189 /*
1190 * Instead of network_added we emit persistent_group_added
1191 * notification. Also to keep the defense checks in
1192 * persistent_group obj registration method, we set the
1193 * relevant flags in s to designate it as a persistent group.
1194 */
1195 s->p2p_group = 1;
1196 s->p2p_persistent_group = 1;
1197 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 wpa_config_set_network_defaults(s);
1199 }
1200
1201 s->p2p_group = 1;
1202 s->p2p_persistent_group = 1;
1203 s->disabled = 2;
1204 s->bssid_set = 1;
1205 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1206 s->mode = ssid->mode;
1207 s->auth_alg = WPA_AUTH_ALG_OPEN;
1208 s->key_mgmt = WPA_KEY_MGMT_PSK;
1209 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001210 s->pbss = ssid->pbss;
1211 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 s->export_keys = 1;
1213 if (ssid->passphrase) {
1214 os_free(s->passphrase);
1215 s->passphrase = os_strdup(ssid->passphrase);
1216 }
1217 if (ssid->psk_set) {
1218 s->psk_set = 1;
1219 os_memcpy(s->psk, ssid->psk, 32);
1220 }
1221 if (s->passphrase && !s->psk_set)
1222 wpa_config_update_psk(s);
1223 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1224 os_free(s->ssid);
1225 s->ssid = os_malloc(ssid->ssid_len);
1226 }
1227 if (s->ssid) {
1228 s->ssid_len = ssid->ssid_len;
1229 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1230 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001231 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1232 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1233 wpa_s->global->add_psk = NULL;
1234 changed = 1;
1235 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 if (changed && wpa_s->conf->update_config &&
1238 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1239 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1240 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001241
1242 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243}
1244
1245
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001246static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1247 const u8 *addr)
1248{
1249 struct wpa_ssid *ssid, *s;
1250 u8 *n;
1251 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001252 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001253 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254
1255 ssid = wpa_s->current_ssid;
1256 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1257 !ssid->p2p_persistent_group)
1258 return;
1259
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001260 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001261 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1262 continue;
1263
1264 if (s->ssid_len == ssid->ssid_len &&
1265 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1266 break;
1267 }
1268
1269 if (s == NULL)
1270 return;
1271
1272 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001273 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001274 ETH_ALEN) != 0)
1275 continue;
1276
1277 if (i == s->num_p2p_clients - 1)
1278 return; /* already the most recent entry */
1279
1280 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001281 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1282 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1283 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001284 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001285 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1286 ETH_ALEN);
1287 os_memset(s->p2p_client_list +
1288 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1289 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001290 found = 1;
1291 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001292 }
1293
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001294 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1295 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001296 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001297 if (n == NULL)
1298 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001299 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1300 ETH_ALEN);
1301 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1302 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001303 s->p2p_client_list = n;
1304 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001305 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001306 /* Not enough room for an additional entry - drop the oldest
1307 * entry */
1308 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001309 s->p2p_client_list + 2 * ETH_ALEN,
1310 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001311 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001312 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001313 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001314 os_memset(s->p2p_client_list +
1315 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1316 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001317 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001318
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001319 if (p2p_wpa_s->conf->update_config &&
1320 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001321 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322}
1323
1324
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001325static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1326 int go, struct wpa_ssid *ssid, int freq,
1327 const u8 *psk, const char *passphrase,
1328 const u8 *go_dev_addr, int persistent,
1329 const char *extra)
1330{
1331 const char *ssid_txt;
1332 char psk_txt[65];
1333
1334 if (psk)
1335 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1336 else
1337 psk_txt[0] = '\0';
1338
1339 if (ssid)
1340 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1341 else
1342 ssid_txt = "";
1343
1344 if (passphrase && passphrase[0] == '\0')
1345 passphrase = NULL;
1346
1347 /*
1348 * Include PSK/passphrase only in the control interface message and
1349 * leave it out from the debug log entry.
1350 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001351 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001352 P2P_EVENT_GROUP_STARTED
1353 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1354 MACSTR "%s%s",
1355 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1356 psk ? " psk=" : "", psk_txt,
1357 passphrase ? " passphrase=\"" : "",
1358 passphrase ? passphrase : "",
1359 passphrase ? "\"" : "",
1360 MAC2STR(go_dev_addr),
1361 persistent ? " [PERSISTENT]" : "", extra);
1362 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1363 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1364 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1365 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1366 extra);
1367}
1368
1369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001371 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372{
1373 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 int client;
1375 int persistent;
1376 u8 go_dev_addr[ETH_ALEN];
1377
1378 /*
1379 * This callback is likely called for the main interface. Update wpa_s
1380 * to use the group interface if a new interface was created for the
1381 * group.
1382 */
1383 if (wpa_s->global->p2p_group_formation)
1384 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001385 if (wpa_s->p2p_go_group_formation_completed) {
1386 wpa_s->global->p2p_group_formation = NULL;
1387 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001388 } else if (wpa_s->p2p_in_provisioning && !success) {
1389 wpa_msg(wpa_s, MSG_DEBUG,
1390 "P2P: Stop provisioning state due to failure");
1391 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001392 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001393 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001394 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001395 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396
1397 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001398 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001399 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001400 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001401 if (already_deleted)
1402 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001403 wpas_p2p_group_delete(wpa_s,
1404 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 return;
1406 }
1407
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001408 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001409 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410
1411 ssid = wpa_s->current_ssid;
1412 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1413 ssid->mode = WPAS_MODE_P2P_GO;
1414 p2p_group_notif_formation_done(wpa_s->p2p_group);
1415 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1416 }
1417
1418 persistent = 0;
1419 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 client = ssid->mode == WPAS_MODE_INFRA;
1421 if (ssid->mode == WPAS_MODE_P2P_GO) {
1422 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001423 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1424 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001425 } else {
1426 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 persistent = wpas_p2p_persistent_group(wpa_s,
1428 go_dev_addr,
1429 ssid->ssid,
1430 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 client = wpa_s->p2p_group_interface ==
1434 P2P_GROUP_INTERFACE_CLIENT;
1435 os_memset(go_dev_addr, 0, ETH_ALEN);
1436 }
1437
1438 wpa_s->show_group_started = 0;
1439 if (client) {
1440 /*
1441 * Indicate event only after successfully completed 4-way
1442 * handshake, i.e., when the interface is ready for data
1443 * packets.
1444 */
1445 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001447 wpas_p2p_group_started(wpa_s, 1, ssid,
1448 ssid ? ssid->frequency : 0,
1449 ssid && ssid->passphrase == NULL &&
1450 ssid->psk_set ? ssid->psk : NULL,
1451 ssid ? ssid->passphrase : NULL,
1452 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 wpas_p2p_cross_connect_setup(wpa_s);
1454 wpas_p2p_set_group_idle_timeout(wpa_s);
1455 }
1456
1457 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001458 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1459 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001460 else {
1461 os_free(wpa_s->global->add_psk);
1462 wpa_s->global->add_psk = NULL;
1463 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001464
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001465 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001466 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001467 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001468 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001469}
1470
1471
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001472struct send_action_work {
1473 unsigned int freq;
1474 u8 dst[ETH_ALEN];
1475 u8 src[ETH_ALEN];
1476 u8 bssid[ETH_ALEN];
1477 size_t len;
1478 unsigned int wait_time;
1479 u8 buf[0];
1480};
1481
1482
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001483static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1484{
1485 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1486
1487 wpa_printf(MSG_DEBUG,
1488 "P2P: Free Action frame radio work @%p (freq=%u dst="
1489 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1490 wpa_s->p2p_send_action_work, awork->freq,
1491 MAC2STR(awork->dst), MAC2STR(awork->src),
1492 MAC2STR(awork->bssid), awork->wait_time);
1493 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1494 awork->buf, awork->len);
1495 os_free(awork);
1496 wpa_s->p2p_send_action_work->ctx = NULL;
1497 radio_work_done(wpa_s->p2p_send_action_work);
1498 wpa_s->p2p_send_action_work = NULL;
1499}
1500
1501
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001502static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1503 void *timeout_ctx)
1504{
1505 struct wpa_supplicant *wpa_s = eloop_ctx;
1506
1507 if (!wpa_s->p2p_send_action_work)
1508 return;
1509
1510 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001511 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001512}
1513
1514
Dmitry Shmidt03658832014-08-13 11:03:49 -07001515static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001517 if (wpa_s->p2p_send_action_work) {
1518 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001519
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001520 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001521 wpa_printf(MSG_DEBUG,
1522 "P2P: Clear Action TX work @%p (wait_time=%u)",
1523 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001525 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001526 } else {
1527 /*
1528 * In theory, this should not be needed, but number of
1529 * places in the P2P code is still using non-zero wait
1530 * time for the last Action frame in the sequence and
1531 * some of these do not call send_action_done().
1532 */
1533 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1534 wpa_s, NULL);
1535 eloop_register_timeout(
1536 0, awork->wait_time * 1000,
1537 wpas_p2p_send_action_work_timeout,
1538 wpa_s, NULL);
1539 }
1540 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001541}
1542
1543
1544static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1545 unsigned int freq,
1546 const u8 *dst, const u8 *src,
1547 const u8 *bssid,
1548 const u8 *data, size_t data_len,
1549 enum offchannel_send_action_result
1550 result)
1551{
1552 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1553
1554 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1557 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001559 switch (result) {
1560 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1561 res = P2P_SEND_ACTION_SUCCESS;
1562 break;
1563 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1564 res = P2P_SEND_ACTION_NO_ACK;
1565 break;
1566 case OFFCHANNEL_SEND_ACTION_FAILED:
1567 res = P2P_SEND_ACTION_FAILED;
1568 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 }
1570
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001571 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001573 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1574 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001575 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001576 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1577 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001579 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1580 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001581 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001582 P2P_EVENT_FALLBACK_TO_GO_NEG
1583 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001584 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1585 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586 }
1587}
1588
1589
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001590static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1591{
1592 struct wpa_supplicant *wpa_s = work->wpa_s;
1593 struct send_action_work *awork = work->ctx;
1594
1595 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001596 if (work->started) {
1597 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1598 wpa_s, NULL);
1599 wpa_s->p2p_send_action_work = NULL;
1600 offchannel_send_action_done(wpa_s);
1601 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001602 os_free(awork);
1603 return;
1604 }
1605
1606 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1607 awork->bssid, awork->buf, awork->len,
1608 awork->wait_time,
1609 wpas_p2p_send_action_tx_status, 1) < 0) {
1610 os_free(awork);
1611 radio_work_done(work);
1612 return;
1613 }
1614 wpa_s->p2p_send_action_work = work;
1615}
1616
1617
1618static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1619 unsigned int freq, const u8 *dst,
1620 const u8 *src, const u8 *bssid, const u8 *buf,
1621 size_t len, unsigned int wait_time)
1622{
1623 struct send_action_work *awork;
1624
Hai Shalom81f62d82019-07-22 12:10:00 -07001625 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001626 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1627 return -1;
1628 }
1629
1630 awork = os_zalloc(sizeof(*awork) + len);
1631 if (awork == NULL)
1632 return -1;
1633
1634 awork->freq = freq;
1635 os_memcpy(awork->dst, dst, ETH_ALEN);
1636 os_memcpy(awork->src, src, ETH_ALEN);
1637 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1638 awork->len = len;
1639 awork->wait_time = wait_time;
1640 os_memcpy(awork->buf, buf, len);
1641
Hai Shalom81f62d82019-07-22 12:10:00 -07001642 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001643 wpas_send_action_cb, awork) < 0) {
1644 os_free(awork);
1645 return -1;
1646 }
1647
1648 return 0;
1649}
1650
1651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1653 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001654 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655{
1656 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001657 int listen_freq = -1, send_freq = -1;
1658
Hai Shalom021b0b52019-04-10 11:17:58 -07001659 if (scheduled)
1660 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001661 if (wpa_s->p2p_listen_work)
1662 listen_freq = wpa_s->p2p_listen_work->freq;
1663 if (wpa_s->p2p_send_action_work)
1664 send_freq = wpa_s->p2p_send_action_work->freq;
1665 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001666 int res;
1667
1668 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1669 listen_freq, send_freq, freq);
1670 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1671 len, wait_time);
1672 if (res == 0 && scheduled)
1673 *scheduled = 1;
1674 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001675 }
1676
1677 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001678 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1679 wait_time,
1680 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681}
1682
1683
1684static void wpas_send_action_done(void *ctx)
1685{
1686 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001687
1688 if (wpa_s->p2p_send_action_work) {
1689 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1690 wpa_s, NULL);
1691 os_free(wpa_s->p2p_send_action_work->ctx);
1692 radio_work_done(wpa_s->p2p_send_action_work);
1693 wpa_s->p2p_send_action_work = NULL;
1694 }
1695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001696 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697}
1698
1699
1700static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1701 struct p2p_go_neg_results *params)
1702{
1703 if (wpa_s->go_params == NULL) {
1704 wpa_s->go_params = os_malloc(sizeof(*params));
1705 if (wpa_s->go_params == NULL)
1706 return -1;
1707 }
1708 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1709 return 0;
1710}
1711
1712
1713static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1714 struct p2p_go_neg_results *res)
1715{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001716 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001717 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1718 " dev_addr " MACSTR " wps_method %d",
1719 MAC2STR(res->peer_interface_addr),
1720 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1722 res->ssid, res->ssid_len);
1723 wpa_supplicant_ap_deinit(wpa_s);
1724 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001725 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001726 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001727#ifdef CONFIG_WPS_NFC
1728 } else if (res->wps_method == WPS_NFC) {
1729 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1730 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001731 wpa_s->p2pdev->p2p_oob_dev_pw,
1732 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1733 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001734 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001735 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001736 NULL,
1737 NULL, 0, 0);
1738#endif /* CONFIG_WPS_NFC */
1739 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001741 if (wpa_s->p2p_wps_method == WPS_P2PS)
1742 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1744 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1745 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1746 wpa_s->p2p_pin, 1, dev_pw_id);
1747 }
1748}
1749
1750
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001751static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1752 struct wpa_ssid *ssid)
1753{
1754 struct wpa_ssid *persistent;
1755 struct psk_list_entry *psk;
1756 struct hostapd_data *hapd;
1757
1758 if (!wpa_s->ap_iface)
1759 return;
1760
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001761 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001762 ssid->ssid_len);
1763 if (persistent == NULL)
1764 return;
1765
1766 hapd = wpa_s->ap_iface->bss[0];
1767
1768 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1769 list) {
1770 struct hostapd_wpa_psk *hpsk;
1771
1772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1773 MACSTR " psk=%d",
1774 MAC2STR(psk->addr), psk->p2p);
1775 hpsk = os_zalloc(sizeof(*hpsk));
1776 if (hpsk == NULL)
1777 break;
1778 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1779 if (psk->p2p)
1780 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1781 else
1782 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1783 hpsk->next = hapd->conf->ssid.wpa_psk;
1784 hapd->conf->ssid.wpa_psk = hpsk;
1785 }
1786}
1787
1788
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001789static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1790{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001791 char buf[20 + P2P_MAX_CHANNELS * 6];
1792 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001793 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001794 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001795
Roshan Pius3a1667e2018-07-03 15:17:14 -07001796 pos = buf;
1797 end = pos + sizeof(buf);
1798 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1799 res = os_snprintf(pos, end - pos, " %d",
1800 wpa_s->p2p_group_common_freqs[i]);
1801 if (os_snprintf_error(end - pos, res))
1802 break;
1803 pos += res;
1804 }
1805 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001806
Roshan Pius3a1667e2018-07-03 15:17:14 -07001807 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001808}
1809
1810
1811static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1812 struct p2p_go_neg_results *params)
1813{
1814 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1815
1816 wpa_s->p2p_group_common_freqs_num = 0;
1817 os_free(wpa_s->p2p_group_common_freqs);
1818 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1819 if (!wpa_s->p2p_group_common_freqs)
1820 return;
1821
1822 for (i = 0; i < len; i++) {
1823 if (!wpa_s->go_params->freq_list[i])
1824 break;
1825 wpa_s->p2p_group_common_freqs[i] =
1826 wpa_s->go_params->freq_list[i];
1827 }
1828 wpa_s->p2p_group_common_freqs_num = i;
1829}
1830
1831
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001832static void p2p_config_write(struct wpa_supplicant *wpa_s)
1833{
1834#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001835 if (wpa_s->p2pdev->conf->update_config &&
1836 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001837 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1838#endif /* CONFIG_NO_CONFIG_WRITE */
1839}
1840
1841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842static void p2p_go_configured(void *ctx, void *data)
1843{
1844 struct wpa_supplicant *wpa_s = ctx;
1845 struct p2p_go_neg_results *params = data;
1846 struct wpa_ssid *ssid;
1847
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001848 wpa_s->ap_configured_cb = NULL;
1849 wpa_s->ap_configured_cb_ctx = NULL;
1850 wpa_s->ap_configured_cb_data = NULL;
1851 if (!wpa_s->go_params) {
1852 wpa_printf(MSG_ERROR,
1853 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1854 return;
1855 }
1856
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001857 p2p_go_save_group_common_freqs(wpa_s, params);
1858 p2p_go_dump_common_freqs(wpa_s);
1859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860 ssid = wpa_s->current_ssid;
1861 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1862 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1863 if (wpa_s->global->p2p_group_formation == wpa_s)
1864 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001865 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1866 params->passphrase[0] == '\0' ?
1867 params->psk : NULL,
1868 params->passphrase,
1869 wpa_s->global->p2p_dev_addr,
1870 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001871 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001872
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001873 if (wpa_s->p2pdev->p2ps_method_config_any) {
1874 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001875 wpa_dbg(wpa_s, MSG_DEBUG,
1876 "P2PS: Setting default PIN for ANY");
1877 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1878 "12345670", NULL, 0,
1879 0);
1880 } else {
1881 wpa_dbg(wpa_s, MSG_DEBUG,
1882 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001883 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001884 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001885 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001886 "12345670", NULL, 0, 0);
1887 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001888 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001889 }
1890
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001891 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001892 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001893 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001894 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001895 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001896 wpas_p2p_add_psk_list(wpa_s, ssid);
1897 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001898
1899 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001900 params->persistent_group, 0,
1901 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902 wpas_p2p_cross_connect_setup(wpa_s);
1903 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001904
1905 if (wpa_s->p2p_first_connection_timeout) {
1906 wpa_dbg(wpa_s, MSG_DEBUG,
1907 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1908 wpa_s->p2p_first_connection_timeout);
1909 wpa_s->p2p_go_group_formation_completed = 0;
1910 wpa_s->global->p2p_group_formation = wpa_s;
1911 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001912 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001913 eloop_register_timeout(
1914 wpa_s->p2p_first_connection_timeout, 0,
1915 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001916 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001917 }
1918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 return;
1920 }
1921
1922 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1923 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1924 params->peer_interface_addr)) {
1925 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1926 "filtering");
1927 return;
1928 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001929 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001931 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001932#ifdef CONFIG_WPS_NFC
1933 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001934 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001935 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001936 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001937 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1938 return;
1939 }
1940 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001941 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1942 wpa_s->p2pdev->p2p_oob_dev_pw,
1943 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1944 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001945#endif /* CONFIG_WPS_NFC */
1946 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001948 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 os_free(wpa_s->go_params);
1950 wpa_s->go_params = NULL;
1951}
1952
1953
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001954/**
1955 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1956 * @freq: Frequency (MHz) to convert
1957 * @op_class: Buffer for returning operating class
1958 * @op_edmg_channel: Buffer for returning channel number
1959 * Returns: 0 on success, -1 on failure
1960 *
1961 * This can be used to find the highest channel bonding which includes the
1962 * specified frequency.
1963 */
1964static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1965 unsigned int freq,
1966 u8 *op_class, u8 *op_edmg_channel)
1967{
1968 struct hostapd_hw_modes *hwmode;
1969 struct ieee80211_edmg_config edmg;
1970 unsigned int i;
1971 enum chan_width chanwidth[] = {
1972 CHAN_WIDTH_8640,
1973 CHAN_WIDTH_6480,
1974 CHAN_WIDTH_4320,
1975 };
1976
1977 if (!wpa_s->hw.modes)
1978 return -1;
1979
1980 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08001981 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001982 if (!hwmode) {
1983 wpa_printf(MSG_ERROR,
1984 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1985 return -1;
1986 }
1987
1988 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1989 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1990 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1991 op_class,
1992 op_edmg_channel) < 0)
1993 continue;
1994
1995 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1996 if (edmg.channels &&
1997 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1998 wpa_printf(MSG_DEBUG,
1999 "Freq %u to EDMG channel %u at opclass %u",
2000 freq, *op_edmg_channel, *op_class);
2001 return 0;
2002 }
2003 }
2004
2005 return -1;
2006}
2007
2008
2009int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2010 struct p2p_go_neg_results *params)
2011{
2012 u8 op_channel, op_class;
2013 int freq;
2014
2015 /* Try social channel as primary channel frequency */
2016 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2017
2018 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2019 &op_channel) == 0) {
2020 wpa_printf(MSG_DEBUG,
2021 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2022 freq, op_channel, op_class);
2023 params->freq = freq;
2024 return 0;
2025 }
2026
2027 return -1;
2028}
2029
2030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2032 struct p2p_go_neg_results *params,
2033 int group_formation)
2034{
2035 struct wpa_ssid *ssid;
2036
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002037 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2038 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2039 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2040 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043
2044 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002045 if (ssid == NULL) {
2046 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002048 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002050 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002051 wpa_s->p2p_go_group_formation_completed = 0;
2052 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002053 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 wpa_config_set_network_defaults(ssid);
2056 ssid->temporary = 1;
2057 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002058 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2060 WPAS_MODE_P2P_GO;
2061 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002062 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002063 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002064 ssid->max_oper_chwidth = params->max_oper_chwidth;
2065 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002066 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002067 if (params->edmg) {
2068 u8 op_channel, op_class;
2069
2070 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2071 &op_class, &op_channel)) {
2072 ssid->edmg_channel = op_channel;
2073 ssid->enable_edmg = params->edmg;
2074 } else {
2075 wpa_dbg(wpa_s, MSG_DEBUG,
2076 "P2P: Could not match EDMG channel, freq %d, for GO",
2077 params->freq);
2078 }
2079 }
2080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 ssid->ssid = os_zalloc(params->ssid_len + 1);
2082 if (ssid->ssid) {
2083 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2084 ssid->ssid_len = params->ssid_len;
2085 }
2086 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2087 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002088 if (is_6ghz_freq(ssid->frequency) &&
2089 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2090 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2091 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2092 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2093 ssid->sae_pwe = 1;
2094 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2095 } else {
2096 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2097 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 ssid->proto = WPA_PROTO_RSN;
2099 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002100 ssid->group_cipher = WPA_CIPHER_CCMP;
2101 if (params->freq > 56160) {
2102 /*
2103 * Enable GCMP instead of CCMP as pairwise_cipher and
2104 * group_cipher in 60 GHz.
2105 */
2106 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2107 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002108 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2109 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002110 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002111 if (os_strlen(params->passphrase) > 0) {
2112 ssid->passphrase = os_strdup(params->passphrase);
2113 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002114 wpa_msg_global(wpa_s, MSG_ERROR,
2115 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002116 wpa_config_remove_network(wpa_s->conf, ssid->id);
2117 return;
2118 }
2119 } else
2120 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002121 ssid->psk_set = params->psk_set;
2122 if (ssid->psk_set)
2123 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002124 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002125 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002126 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127
2128 wpa_s->ap_configured_cb = p2p_go_configured;
2129 wpa_s->ap_configured_cb_ctx = wpa_s;
2130 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002131 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002132 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133 wpa_s->reassociate = 1;
2134 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002135 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2136 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 wpa_supplicant_req_scan(wpa_s, 0, 0);
2138}
2139
2140
2141static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2142 const struct wpa_supplicant *src)
2143{
2144 struct wpa_config *d;
2145 const struct wpa_config *s;
2146
2147 d = dst->conf;
2148 s = src->conf;
2149
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002150#define C(n) \
2151do { \
2152 if (s->n && !d->n) \
2153 d->n = os_strdup(s->n); \
2154} while (0)
2155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 C(device_name);
2157 C(manufacturer);
2158 C(model_name);
2159 C(model_number);
2160 C(serial_number);
2161 C(config_methods);
2162#undef C
2163
2164 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2165 os_memcpy(d->sec_device_type, s->sec_device_type,
2166 sizeof(d->sec_device_type));
2167 d->num_sec_device_types = s->num_sec_device_types;
2168
2169 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002170 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002172 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002173 d->max_num_sta = s->max_num_sta;
2174 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002175 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002176 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002177 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002178 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002179 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002180 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002181 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002182 d->pmf = s->pmf;
2183 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002184
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002185 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2186 !d->wps_nfc_pw_from_config) {
2187 wpabuf_free(d->wps_nfc_dh_privkey);
2188 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002189 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2190 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2191 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002192 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002193 d->go_interworking = s->go_interworking;
2194 d->go_access_network_type = s->go_access_network_type;
2195 d->go_internet = s->go_internet;
2196 d->go_venue_group = s->go_venue_group;
2197 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002198 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199}
2200
2201
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002202static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2203 char *ifname, size_t len)
2204{
2205 char *ifname_ptr = wpa_s->ifname;
2206
2207 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2208 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2209 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2210 }
2211
2212 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2213 if (os_strlen(ifname) >= IFNAMSIZ &&
2214 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002215 int res;
2216
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002217 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002218 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2219 if (os_snprintf_error(len, res) && len)
2220 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002221 }
2222}
2223
2224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2226 enum wpa_driver_if_type type)
2227{
2228 char ifname[120], force_ifname[120];
2229
2230 if (wpa_s->pending_interface_name[0]) {
2231 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2232 "- skip creation of a new one");
2233 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2234 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2235 "unknown?! ifname='%s'",
2236 wpa_s->pending_interface_name);
2237 return -1;
2238 }
2239 return 0;
2240 }
2241
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002242 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 force_ifname[0] = '\0';
2244
2245 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2246 ifname);
2247 wpa_s->p2p_group_idx++;
2248
2249 wpa_s->pending_interface_type = type;
2250 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2251 wpa_s->pending_interface_addr, NULL) < 0) {
2252 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2253 "interface");
2254 return -1;
2255 }
2256
Jimmy Chen36c21992018-11-29 16:46:43 +08002257 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002258 random_mac_addr(wpa_s->pending_interface_addr);
2259 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2260 " for the group",
2261 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002262 }
2263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 if (force_ifname[0]) {
2265 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2266 force_ifname);
2267 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2268 sizeof(wpa_s->pending_interface_name));
2269 } else
2270 os_strlcpy(wpa_s->pending_interface_name, ifname,
2271 sizeof(wpa_s->pending_interface_name));
2272 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2273 MACSTR, wpa_s->pending_interface_name,
2274 MAC2STR(wpa_s->pending_interface_addr));
2275
2276 return 0;
2277}
2278
2279
2280static void wpas_p2p_remove_pending_group_interface(
2281 struct wpa_supplicant *wpa_s)
2282{
2283 if (!wpa_s->pending_interface_name[0] ||
2284 is_zero_ether_addr(wpa_s->pending_interface_addr))
2285 return; /* No pending virtual interface */
2286
2287 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2288 wpa_s->pending_interface_name);
2289 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2290 wpa_s->pending_interface_name);
2291 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2292 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002293 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002294}
2295
2296
2297static struct wpa_supplicant *
2298wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2299{
2300 struct wpa_interface iface;
2301 struct wpa_supplicant *group_wpa_s;
2302
2303 if (!wpa_s->pending_interface_name[0]) {
2304 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2305 if (!wpas_p2p_create_iface(wpa_s))
2306 return NULL;
2307 /*
2308 * Something has forced us to remove the pending interface; try
2309 * to create a new one and hope for the best that we will get
2310 * the same local address.
2311 */
2312 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2313 WPA_IF_P2P_CLIENT) < 0)
2314 return NULL;
2315 }
2316
2317 os_memset(&iface, 0, sizeof(iface));
2318 iface.ifname = wpa_s->pending_interface_name;
2319 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002320 if (wpa_s->conf->ctrl_interface == NULL &&
2321 wpa_s->parent != wpa_s &&
2322 wpa_s->p2p_mgmt &&
2323 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2324 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2325 else
2326 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002328 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329 if (group_wpa_s == NULL) {
2330 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2331 "wpa_supplicant interface");
2332 return NULL;
2333 }
2334 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2336 P2P_GROUP_INTERFACE_CLIENT;
2337 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002338 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339
2340 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2341
Jimmy Chen36c21992018-11-29 16:46:43 +08002342 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002343 if (wpa_drv_set_mac_addr(group_wpa_s,
2344 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002345 wpa_msg(group_wpa_s, MSG_INFO,
2346 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002347 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2348 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002349 return NULL;
2350 }
2351
2352 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2353 wpa_msg(group_wpa_s, MSG_INFO,
2354 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002355 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2356 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002357 return NULL;
2358 }
2359
Hai Shalom74f70d42019-02-11 14:42:39 -08002360 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2361 " for the group",
2362 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002363 }
2364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 return group_wpa_s;
2366}
2367
2368
2369static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2370 void *timeout_ctx)
2371{
2372 struct wpa_supplicant *wpa_s = eloop_ctx;
2373 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002374 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002375}
2376
2377
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002378static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2379 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002380{
2381 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002382 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 if (wpa_s->global->p2p)
2384 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002385 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386}
2387
2388
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002389static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2390{
2391 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2392 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002393 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002394 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002395 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002396 wpa_s->global->p2p_fail_on_wps_complete = 0;
2397}
2398
2399
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002400void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2401{
2402 if (wpa_s->global->p2p_group_formation != wpa_s)
2403 return;
2404 /* Speed up group formation timeout since this cannot succeed */
2405 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002406 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002407 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002408 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002409}
2410
2411
Matthew Wang06b42472022-11-10 06:56:31 +00002412bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2413{
2414 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
2415 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit) {
2416 return false;
2417 }
2418 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2419 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2420 wpa_s->p2pdev, NULL);
2421 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2422 wpa_s->p2pdev, NULL);
2423 return true;
2424}
2425
2426
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002427static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428{
2429 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002430 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431
2432 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2433 wpa_drv_cancel_remain_on_channel(wpa_s);
2434 wpa_s->off_channel_freq = 0;
2435 wpa_s->roc_waiting_drv_freq = 0;
2436 }
2437
2438 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002439 wpa_msg_global(wpa_s, MSG_INFO,
2440 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2441 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002442 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443 wpas_p2p_remove_pending_group_interface(wpa_s);
2444 return;
2445 }
2446
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002447 if (!res->role_go) {
2448 /* Inform driver of the operating channel of GO. */
2449 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2450 }
2451
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002452 if (wpa_s->p2p_go_ht40)
2453 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002454 if (wpa_s->p2p_go_vht)
2455 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002456 if (wpa_s->p2p_go_he)
2457 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002458 if (wpa_s->p2p_go_edmg)
2459 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002460 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2461 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002462
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002463 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2464 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2465 " wps_method=%s",
2466 res->role_go ? "GO" : "client", res->freq, res->ht40,
2467 MAC2STR(res->peer_device_addr),
2468 MAC2STR(res->peer_interface_addr),
2469 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002470 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471
Dmitry Shmidt04949592012-07-19 12:16:46 -07002472 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2473 struct wpa_ssid *ssid;
2474 ssid = wpa_config_get_network(wpa_s->conf,
2475 wpa_s->p2p_persistent_id);
2476 if (ssid && ssid->disabled == 2 &&
2477 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2478 size_t len = os_strlen(ssid->passphrase);
2479 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2480 "on requested persistent group");
2481 os_memcpy(res->passphrase, ssid->passphrase, len);
2482 res->passphrase[len] = '\0';
2483 }
2484 }
2485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002487 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2489 if (group_wpa_s == NULL) {
2490 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002491 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2492 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002493 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 return;
2495 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2497 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002499 group_wpa_s = wpa_s->parent;
2500 wpa_s->global->p2p_group_formation = group_wpa_s;
2501 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002502 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002505 group_wpa_s->p2p_in_provisioning = 1;
2506 group_wpa_s->p2pdev = wpa_s;
2507 if (group_wpa_s != wpa_s) {
2508 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2509 sizeof(group_wpa_s->p2p_pin));
2510 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2511 }
2512 if (res->role_go) {
2513 wpas_start_wps_go(group_wpa_s, res, 1);
2514 } else {
2515 os_get_reltime(&group_wpa_s->scan_min_time);
2516 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 }
2518
Hai Shalomfdcde762020-04-02 11:19:20 -07002519 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2521
2522 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2523 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2524 (res->peer_config_timeout % 100) * 10000,
2525 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2526}
2527
2528
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002529static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2530 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531{
2532 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002533 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002534 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2535 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002537 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538}
2539
2540
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002541static void wpas_dev_found(void *ctx, const u8 *addr,
2542 const struct p2p_peer_info *info,
2543 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002545 u8 *wfd_dev_info = NULL;
2546 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002547 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002548 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002549#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550 struct wpa_supplicant *wpa_s = ctx;
2551 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002552 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002553 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002554
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002555#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002556 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2557 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002558 if (wfd_dev_info_hex) {
2559 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2560 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2561 // Only used for notification, so not handling error.
2562 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2563 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002564
2565 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2566 WFD_SUBELEM_R2_DEVICE_INFO);
2567 if (wfd_r2_dev_info_hex) {
2568 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2569 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2570 // Only used for notification, so not handling error.
2571 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2572 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002573#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002574
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002575 if (info->p2ps_instance) {
2576 char str[256];
2577 const u8 *buf = wpabuf_head(info->p2ps_instance);
2578 size_t len = wpabuf_len(info->p2ps_instance);
2579
2580 while (len) {
2581 u32 id;
2582 u16 methods;
2583 u8 str_len;
2584
2585 if (len < 4 + 2 + 1)
2586 break;
2587 id = WPA_GET_LE32(buf);
2588 buf += sizeof(u32);
2589 methods = WPA_GET_BE16(buf);
2590 buf += sizeof(u16);
2591 str_len = *buf++;
2592 if (str_len > len - 4 - 2 - 1)
2593 break;
2594 os_memcpy(str, buf, str_len);
2595 str[str_len] = '\0';
2596 buf += str_len;
2597 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2598
2599 wpa_msg_global(wpa_s, MSG_INFO,
2600 P2P_EVENT_DEVICE_FOUND MACSTR
2601 " p2p_dev_addr=" MACSTR
2602 " pri_dev_type=%s name='%s'"
2603 " config_methods=0x%x"
2604 " dev_capab=0x%x"
2605 " group_capab=0x%x"
2606 " adv_id=%x asp_svc=%s%s",
2607 MAC2STR(addr),
2608 MAC2STR(info->p2p_device_addr),
2609 wps_dev_type_bin2str(
2610 info->pri_dev_type,
2611 devtype, sizeof(devtype)),
2612 info->device_name, methods,
2613 info->dev_capab, info->group_capab,
2614 id, str,
2615 info->vendor_elems ?
2616 " vendor_elems=1" : "");
2617 }
2618 goto done;
2619 }
2620
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002621 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2622 " p2p_dev_addr=" MACSTR
2623 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002624 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002625 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2626 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2627 sizeof(devtype)),
2628 info->device_name, info->config_methods,
2629 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002630 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002631 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002632 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2633 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002634 info->vendor_elems ? " vendor_elems=1" : "",
2635 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002637done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002638 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002639 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002640#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002641
Roshan Piusfd2fd662017-01-23 13:41:57 -08002642 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002643 wfd_dev_info_len, wfd_r2_dev_info,
2644 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002645 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646}
2647
2648
2649static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2650{
2651 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002653 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2654 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2657}
2658
2659
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002660static void wpas_find_stopped(void *ctx)
2661{
2662 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002663
2664 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2665 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2666
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002667 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002668 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002669}
2670
2671
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002672struct wpas_p2p_listen_work {
2673 unsigned int freq;
2674 unsigned int duration;
2675 struct wpabuf *probe_resp_ie;
2676};
2677
2678
2679static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2680{
2681 if (lwork == NULL)
2682 return;
2683 wpabuf_free(lwork->probe_resp_ie);
2684 os_free(lwork);
2685}
2686
2687
2688static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2689{
2690 struct wpas_p2p_listen_work *lwork;
2691
2692 if (!wpa_s->p2p_listen_work)
2693 return;
2694
2695 lwork = wpa_s->p2p_listen_work->ctx;
2696 wpas_p2p_listen_work_free(lwork);
2697 radio_work_done(wpa_s->p2p_listen_work);
2698 wpa_s->p2p_listen_work = NULL;
2699}
2700
2701
2702static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2703{
2704 struct wpa_supplicant *wpa_s = work->wpa_s;
2705 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002706 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002707
2708 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002709 if (work->started) {
2710 wpa_s->p2p_listen_work = NULL;
2711 wpas_stop_listen(wpa_s);
2712 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002713 wpas_p2p_listen_work_free(lwork);
2714 return;
2715 }
2716
2717 wpa_s->p2p_listen_work = work;
2718
2719 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2720
2721 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2722 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2723 "report received Probe Request frames");
2724 wpas_p2p_listen_work_done(wpa_s);
2725 return;
2726 }
2727
2728 wpa_s->pending_listen_freq = lwork->freq;
2729 wpa_s->pending_listen_duration = lwork->duration;
2730
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002731 duration = lwork->duration;
2732#ifdef CONFIG_TESTING_OPTIONS
2733 if (wpa_s->extra_roc_dur) {
2734 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2735 duration, duration + wpa_s->extra_roc_dur);
2736 duration += wpa_s->extra_roc_dur;
2737 }
2738#endif /* CONFIG_TESTING_OPTIONS */
2739
2740 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002741 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2742 "to remain on channel (%u MHz) for Listen "
2743 "state", lwork->freq);
2744 wpas_p2p_listen_work_done(wpa_s);
2745 wpa_s->pending_listen_freq = 0;
2746 return;
2747 }
2748 wpa_s->off_channel_freq = 0;
2749 wpa_s->roc_waiting_drv_freq = lwork->freq;
2750}
2751
2752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753static int wpas_start_listen(void *ctx, unsigned int freq,
2754 unsigned int duration,
2755 const struct wpabuf *probe_resp_ie)
2756{
2757 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002758 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002760 if (wpa_s->p2p_listen_work) {
2761 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 return -1;
2763 }
2764
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002765 lwork = os_zalloc(sizeof(*lwork));
2766 if (lwork == NULL)
2767 return -1;
2768 lwork->freq = freq;
2769 lwork->duration = duration;
2770 if (probe_resp_ie) {
2771 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2772 if (lwork->probe_resp_ie == NULL) {
2773 wpas_p2p_listen_work_free(lwork);
2774 return -1;
2775 }
2776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002778 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2779 lwork) < 0) {
2780 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 return -1;
2782 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783
2784 return 0;
2785}
2786
2787
2788static void wpas_stop_listen(void *ctx)
2789{
2790 struct wpa_supplicant *wpa_s = ctx;
2791 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2792 wpa_drv_cancel_remain_on_channel(wpa_s);
2793 wpa_s->off_channel_freq = 0;
2794 wpa_s->roc_waiting_drv_freq = 0;
2795 }
2796 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002797
2798 /*
2799 * Don't cancel Probe Request RX reporting for a connected P2P Client
2800 * handling Probe Request frames.
2801 */
2802 if (!wpa_s->p2p_cli_probe)
2803 wpa_drv_probe_req_report(wpa_s, 0);
2804
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002805 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806}
2807
2808
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002809static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2810 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811{
2812 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002813 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002814 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815}
2816
2817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2819 const u8 *peer, const char *params,
2820 unsigned int generated_pin)
2821{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002822 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2823 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824}
2825
2826
2827static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2828 const u8 *peer, const char *params)
2829{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002830 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2831 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832}
2833
2834
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002835static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2836 const u8 *dev_addr, const u8 *pri_dev_type,
2837 const char *dev_name, u16 supp_config_methods,
2838 u8 dev_capab, u8 group_capab, const u8 *group_id,
2839 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840{
2841 struct wpa_supplicant *wpa_s = ctx;
2842 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002843 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844 u8 empty_dev_type[8];
2845 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002847 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002848
2849 if (group_id) {
2850 for (group = wpa_s->global->ifaces; group; group = group->next)
2851 {
2852 struct wpa_ssid *s = group->current_ssid;
2853 if (s != NULL &&
2854 s->mode == WPAS_MODE_P2P_GO &&
2855 group_id_len - ETH_ALEN == s->ssid_len &&
2856 os_memcmp(group_id + ETH_ALEN, s->ssid,
2857 s->ssid_len) == 0)
2858 break;
2859 }
2860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861
2862 if (pri_dev_type == NULL) {
2863 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2864 pri_dev_type = empty_dev_type;
2865 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002866 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2867 " pri_dev_type=%s name='%s' config_methods=0x%x "
2868 "dev_capab=0x%x group_capab=0x%x%s%s",
2869 MAC2STR(dev_addr),
2870 wps_dev_type_bin2str(pri_dev_type, devtype,
2871 sizeof(devtype)),
2872 dev_name, supp_config_methods, dev_capab, group_capab,
2873 group ? " group=" : "",
2874 group ? group->ifname : "");
2875 if (os_snprintf_error(sizeof(params), res))
2876 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002880 if (wps_generate_pin(&generated_pin) < 0) {
2881 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2882 wpas_notify_p2p_provision_discovery(
2883 wpa_s, peer, 0 /* response */,
2884 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2885 return;
2886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887 wpas_prov_disc_local_display(wpa_s, peer, params,
2888 generated_pin);
2889 } else if (config_methods & WPS_CONFIG_KEYPAD)
2890 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2891 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002892 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2893 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002894
2895 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2896 P2P_PROV_DISC_SUCCESS,
2897 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898}
2899
2900
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002901static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902{
2903 struct wpa_supplicant *wpa_s = ctx;
2904 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002905 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002907 if (wpa_s->pending_pd_before_join &&
2908 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2909 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2910 wpa_s->pending_pd_before_join = 0;
2911 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2912 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002913 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002914 return;
2915 }
2916
Dmitry Shmidt04949592012-07-19 12:16:46 -07002917 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002918 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2919 int res;
2920
2921 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2922 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2923 if (os_snprintf_error(sizeof(params), res))
2924 params[sizeof(params) - 1] = '\0';
2925 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002926 params[0] = '\0';
2927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002928 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002929 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002931 if (wps_generate_pin(&generated_pin) < 0) {
2932 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2933 wpas_notify_p2p_provision_discovery(
2934 wpa_s, peer, 0 /* response */,
2935 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2936 return;
2937 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002938 wpas_prov_disc_local_display(wpa_s, peer, params,
2939 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002941 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2942 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943
Jouni Malinen75ecf522011-06-27 15:19:46 -07002944 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2945 P2P_PROV_DISC_SUCCESS,
2946 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002947}
2948
2949
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002950static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002951 enum p2p_prov_disc_status status,
2952 u32 adv_id, const u8 *adv_mac,
2953 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002954{
2955 struct wpa_supplicant *wpa_s = ctx;
2956
Dmitry Shmidt04949592012-07-19 12:16:46 -07002957 if (wpa_s->p2p_fallback_to_go_neg) {
2958 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2959 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002960 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002961 P2P_EVENT_FALLBACK_TO_GO_NEG
2962 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002963 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2964 return;
2965 }
2966
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002967 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002968 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002969 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2970 "join-existing-group operation (no ACK for PD "
2971 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002972 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002973 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002974 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002975
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002976 if (adv_id && adv_mac && deferred_session_resp) {
2977 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2978 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2979 " deferred_session_resp='%s'",
2980 MAC2STR(peer), status, adv_id,
2981 deferred_session_resp);
2982 } else if (adv_id && adv_mac) {
2983 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2984 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2985 MAC2STR(peer), status, adv_id);
2986 } else {
2987 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2988 " p2p_dev_addr=" MACSTR " status=%d",
2989 MAC2STR(peer), status);
2990 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002991
Jouni Malinen75ecf522011-06-27 15:19:46 -07002992 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2993 status, 0, 0);
2994}
2995
2996
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002997static int freq_included(struct wpa_supplicant *wpa_s,
2998 const struct p2p_channels *channels,
2999 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003000{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003001 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3002 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3003 return 1;
3004 return 0;
3005}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003006
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003007
3008static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3009{
3010 unsigned int num = P2P_MAX_CHANNELS;
3011 int *common_freqs;
3012 int ret;
3013
3014 p2p_go_dump_common_freqs(wpa_s);
3015 common_freqs = os_calloc(num, sizeof(int));
3016 if (!common_freqs)
3017 return;
3018
3019 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3020 if (ret < 0) {
3021 wpa_dbg(wpa_s, MSG_DEBUG,
3022 "P2P: Failed to get group common freqs");
3023 os_free(common_freqs);
3024 return;
3025 }
3026
3027 os_free(wpa_s->p2p_group_common_freqs);
3028 wpa_s->p2p_group_common_freqs = common_freqs;
3029 wpa_s->p2p_group_common_freqs_num = num;
3030 p2p_go_dump_common_freqs(wpa_s);
3031}
3032
3033
3034/*
3035 * Check if the given frequency is one of the possible operating frequencies
3036 * set after the completion of the GO Negotiation.
3037 */
3038static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3039{
3040 unsigned int i;
3041
3042 p2p_go_dump_common_freqs(wpa_s);
3043
3044 /* assume no restrictions */
3045 if (!wpa_s->p2p_group_common_freqs_num)
3046 return 1;
3047
3048 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3049 if (wpa_s->p2p_group_common_freqs[i] == freq)
3050 return 1;
3051 }
3052 return 0;
3053}
3054
3055
3056static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3057 struct sta_info *sta, void *ctx)
3058{
3059 int *ecsa_support = ctx;
3060
3061 *ecsa_support &= sta->ecsa_supported;
3062
3063 return 0;
3064}
3065
3066
3067/* Check if all the peers support eCSA */
3068static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3069{
3070 int ecsa_support = 1;
3071
3072 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3073 &ecsa_support);
3074
3075 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003076}
3077
3078
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003079/**
3080 * Pick the best frequency to use from all the currently used frequencies.
3081 */
3082static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3083 struct wpa_used_freq_data *freqs,
3084 unsigned int num)
3085{
3086 unsigned int i, c;
3087
3088 /* find a candidate freq that is supported by P2P */
3089 for (c = 0; c < num; c++)
3090 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3091 break;
3092
3093 if (c == num)
3094 return 0;
3095
3096 /* once we have a candidate, try to find a 'better' one */
3097 for (i = c + 1; i < num; i++) {
3098 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3099 continue;
3100
3101 /*
3102 * 1. Infrastructure station interfaces have higher preference.
3103 * 2. P2P Clients have higher preference.
3104 * 3. All others.
3105 */
3106 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3107 c = i;
3108 break;
3109 }
3110
3111 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3112 c = i;
3113 }
3114 return freqs[c].freq;
3115}
3116
3117
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3119 const u8 *go_dev_addr, const u8 *ssid,
3120 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003121 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003122 const struct p2p_channels *channels,
3123 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124{
3125 struct wpa_supplicant *wpa_s = ctx;
3126 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003127 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003129 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130
3131 if (!persistent_group) {
3132 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003133 " to join an active group (SSID: %s)",
3134 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3136 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3137 == 0 ||
3138 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3139 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3140 "authorized invitation");
3141 goto accept_inv;
3142 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003143
3144#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003145 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3146 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003147 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003148 wpa_s->p2p_wps_method = WPS_NFC;
3149 wpa_s->pending_join_wps_method = WPS_NFC;
3150 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003151 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003152 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003153 bssid, ETH_ALEN);
3154 goto accept_inv;
3155 }
3156#endif /* CONFIG_WPS_NFC */
3157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158 /*
3159 * Do not accept the invitation automatically; notify user and
3160 * request approval.
3161 */
3162 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3163 }
3164
3165 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3166 if (grp) {
3167 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3168 "running persistent group");
3169 if (*go)
3170 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3171 goto accept_inv;
3172 }
3173
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003174 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3175 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3176 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3177 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003178 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003179 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3181
3182 for (s = wpa_s->conf->ssid; s; s = s->next) {
3183 if (s->disabled == 2 &&
3184 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3185 s->ssid_len == ssid_len &&
3186 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3187 break;
3188 }
3189
3190 if (!s) {
3191 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3192 " requested reinvocation of an unknown group",
3193 MAC2STR(sa));
3194 return P2P_SC_FAIL_UNKNOWN_GROUP;
3195 }
3196
3197 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3198 *go = 1;
3199 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3200 wpa_printf(MSG_DEBUG, "P2P: The only available "
3201 "interface is already in use - reject "
3202 "invitation");
3203 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3204 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003205 if (wpa_s->p2p_mgmt)
3206 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3207 ETH_ALEN);
3208 else
3209 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 } else if (s->mode == WPAS_MODE_P2P_GO) {
3211 *go = 1;
3212 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3213 {
3214 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3215 "interface address for the group");
3216 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3217 }
3218 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3219 ETH_ALEN);
3220 }
3221
3222accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003223 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3224
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003225 best_freq = 0;
3226 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3227 sizeof(struct wpa_used_freq_data));
3228 if (freqs) {
3229 int num_channels = wpa_s->num_multichan_concurrent;
3230 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3231 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3232 os_free(freqs);
3233 }
3234
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003235 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003236 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003237 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003238 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003239
3240 if (wpa_s->num_multichan_concurrent < 2 ||
3241 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3242 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 -07003243 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 }
3246
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003247 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3248 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003249 if (*go == 0) {
3250 /* We are the client */
3251 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3252 "running a GO but we are capable of MCC, "
3253 "figure out the best channel to use");
3254 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003255 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003256 /* We are the GO, and *force_freq is not in the
3257 * intersection */
3258 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3259 "in intersection but we are capable of MCC, "
3260 "figure out the best channel to use",
3261 *force_freq);
3262 *force_freq = 0;
3263 }
3264 }
3265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 return P2P_SC_SUCCESS;
3267}
3268
3269
3270static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3271 const u8 *ssid, size_t ssid_len,
3272 const u8 *go_dev_addr, u8 status,
3273 int op_freq)
3274{
3275 struct wpa_supplicant *wpa_s = ctx;
3276 struct wpa_ssid *s;
3277
3278 for (s = wpa_s->conf->ssid; s; s = s->next) {
3279 if (s->disabled == 2 &&
3280 s->ssid_len == ssid_len &&
3281 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3282 break;
3283 }
3284
3285 if (status == P2P_SC_SUCCESS) {
3286 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003287 " was accepted; op_freq=%d MHz, SSID=%s",
3288 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003290 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003291 if (go) {
3292 wpa_msg_global(wpa_s, MSG_INFO,
3293 P2P_EVENT_INVITATION_ACCEPTED
3294 "sa=" MACSTR
3295 " persistent=%d freq=%d",
3296 MAC2STR(sa), s->id, op_freq);
3297 } else {
3298 wpa_msg_global(wpa_s, MSG_INFO,
3299 P2P_EVENT_INVITATION_ACCEPTED
3300 "sa=" MACSTR
3301 " persistent=%d",
3302 MAC2STR(sa), s->id);
3303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003305 wpa_s, s, go, 0, op_freq, 0,
3306 wpa_s->conf->p2p_go_ht40,
3307 wpa_s->conf->p2p_go_vht,
3308 0,
3309 wpa_s->conf->p2p_go_he,
3310 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003311 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003312 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
3313 false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003315 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003316 wpa_msg_global(wpa_s, MSG_INFO,
3317 P2P_EVENT_INVITATION_ACCEPTED
3318 "sa=" MACSTR " go_dev_addr=" MACSTR
3319 " bssid=" MACSTR " unknown-network",
3320 MAC2STR(sa), MAC2STR(go_dev_addr),
3321 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003322 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003323 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003324 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 }
3326 return;
3327 }
3328
3329 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3330 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3331 " was rejected (status %u)", MAC2STR(sa), status);
3332 return;
3333 }
3334
3335 if (!s) {
3336 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003337 wpa_msg_global(wpa_s, MSG_INFO,
3338 P2P_EVENT_INVITATION_RECEIVED
3339 "sa=" MACSTR " go_dev_addr=" MACSTR
3340 " bssid=" MACSTR " unknown-network",
3341 MAC2STR(sa), MAC2STR(go_dev_addr),
3342 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003344 wpa_msg_global(wpa_s, MSG_INFO,
3345 P2P_EVENT_INVITATION_RECEIVED
3346 "sa=" MACSTR " go_dev_addr=" MACSTR
3347 " unknown-network",
3348 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003350 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3351 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003352 return;
3353 }
3354
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003355 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003356 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3357 "sa=" MACSTR " persistent=%d freq=%d",
3358 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003359 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003360 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3361 "sa=" MACSTR " persistent=%d",
3362 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003363 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003364 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3365 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366}
3367
3368
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003369static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3370 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003371 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003372{
3373 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003374 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003375
3376 if (ssid == NULL)
3377 return;
3378
3379 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003380 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003381 ETH_ALEN) == 0)
3382 break;
3383 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003384 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003385 if (ssid->mode != WPAS_MODE_P2P_GO &&
3386 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3387 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3388 "due to invitation result", ssid->id);
3389 wpas_notify_network_removed(wpa_s, ssid);
3390 wpa_config_remove_network(wpa_s->conf, ssid->id);
3391 return;
3392 }
3393 return; /* Peer not found in client list */
3394 }
3395
3396 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003397 "group %d client list%s",
3398 MAC2STR(peer), ssid->id,
3399 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003400 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3401 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3402 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003403 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003404 if (p2p_wpa_s->conf->update_config &&
3405 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003406 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003407}
3408
3409
3410static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3411 const u8 *peer)
3412{
3413 struct wpa_ssid *ssid;
3414
3415 wpa_s = wpa_s->global->p2p_invite_group;
3416 if (wpa_s == NULL)
3417 return; /* No known invitation group */
3418 ssid = wpa_s->current_ssid;
3419 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3420 !ssid->p2p_persistent_group)
3421 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003422 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003423 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003424 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003425}
3426
3427
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003428static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003429 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003430 const u8 *peer, int neg_freq,
3431 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432{
3433 struct wpa_supplicant *wpa_s = ctx;
3434 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003435 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436
3437 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003438 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3439 "status=%d " MACSTR,
3440 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003442 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3443 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 }
3445 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3446
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003447 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3448 status, MAC2STR(peer));
3449 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003450 struct wpa_supplicant *group_if =
3451 wpa_s->global->p2p_invite_group;
3452
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003453 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3454 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003455
3456 /*
3457 * Invitation to an active group. If this is successful and we
3458 * are the GO, set the client wait to postpone some concurrent
3459 * operations and to allow provisioning and connection to happen
3460 * more quickly.
3461 */
3462 if (status == P2P_SC_SUCCESS &&
3463 group_if && group_if->current_ssid &&
3464 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3465 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3466#ifdef CONFIG_TESTING_OPTIONS
3467 if (group_if->p2p_go_csa_on_inv) {
3468 wpa_printf(MSG_DEBUG,
3469 "Testing: force P2P GO CSA after invitation");
3470 eloop_cancel_timeout(
3471 wpas_p2p_reconsider_moving_go,
3472 wpa_s, NULL);
3473 eloop_register_timeout(
3474 0, 50000,
3475 wpas_p2p_reconsider_moving_go,
3476 wpa_s, NULL);
3477 }
3478#endif /* CONFIG_TESTING_OPTIONS */
3479 }
3480 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003481 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003483 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3484 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3485 "invitation exchange to indicate readiness for "
3486 "re-invocation");
3487 }
3488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003490 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3491 ssid = wpa_config_get_network(
3492 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003493 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003494 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 wpas_p2p_remove_pending_group_interface(wpa_s);
3496 return;
3497 }
3498
3499 ssid = wpa_config_get_network(wpa_s->conf,
3500 wpa_s->pending_invite_ssid_id);
3501 if (ssid == NULL) {
3502 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3503 "data matching with invitation");
3504 return;
3505 }
3506
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003507 /*
3508 * The peer could have missed our ctrl::ack frame for Invitation
3509 * Response and continue retransmitting the frame. To reduce the
3510 * likelihood of the peer not getting successful TX status for the
3511 * Invitation Response frame, wait a short time here before starting
3512 * the persistent group so that we will remain on the current channel to
3513 * acknowledge any possible retransmission from the peer.
3514 */
3515 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3516 "starting persistent group");
3517 os_sleep(0, 50000);
3518
Dmitry Shmidt15907092014-03-25 10:42:57 -07003519 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003520 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003521 freq = neg_freq;
3522 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003523 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003524 freq = peer_oper_freq;
3525 else
3526 freq = 0;
3527
3528 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3529 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003531 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003532 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003533 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003534 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003535 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003536 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003537 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003538 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003539 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003540 ssid->mode == WPAS_MODE_P2P_GO ?
3541 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003542 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00003543 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
3544 false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545}
3546
3547
Dmitry Shmidt04949592012-07-19 12:16:46 -07003548static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3549 unsigned int freq)
3550{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003551 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3552 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003553 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003554}
3555
3556
3557static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3558{
3559 reg->channel[reg->channels] = chan;
3560 reg->channels++;
3561}
3562
3563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003565 struct p2p_channels *chan,
3566 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567{
3568 int i, cla = 0;
3569
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003570 wpa_s->global->p2p_24ghz_social_channels = 1;
3571
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003572 os_memset(cli_chan, 0, sizeof(*cli_chan));
3573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3575 "band");
3576
3577 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3578 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003579 chan->reg_class[cla].channels = 0;
3580 for (i = 0; i < 11; i++) {
3581 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3582 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3583 }
3584 if (chan->reg_class[cla].channels)
3585 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586
3587 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3588 "band");
3589
3590 /* Operating class 115 - 5 GHz, channels 36-48 */
3591 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003592 chan->reg_class[cla].channels = 0;
3593 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3594 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3595 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3596 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3597 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3598 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3599 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3600 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3601 if (chan->reg_class[cla].channels)
3602 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603
3604 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3605 "band");
3606
3607 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3608 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003609 chan->reg_class[cla].channels = 0;
3610 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3611 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3612 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3613 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3614 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3615 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3616 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3617 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3618 if (chan->reg_class[cla].channels)
3619 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620
3621 chan->reg_classes = cla;
3622 return 0;
3623}
3624
3625
Hai Shalomc1a21442022-02-04 13:43:00 -08003626static enum chan_allowed has_channel(struct wpa_global *global,
3627 struct hostapd_hw_modes *mode, u8 op_class,
3628 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629{
3630 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003631 unsigned int freq;
3632
Hai Shalomc1a21442022-02-04 13:43:00 -08003633 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003634 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003635 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636
3637 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003638 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 if (flags)
3640 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003641 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003642 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003643 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3644 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003645 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3646 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003647 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 }
3649 }
3650
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003651 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003652}
3653
3654
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003655static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3656 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003657 u8 channel, const u8 *center_channels,
3658 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003659{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003660 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003661
3662 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3663 return 0;
3664
Hai Shalomc1a21442022-02-04 13:43:00 -08003665 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003666 /*
3667 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3668 * so the center channel is 6 channels away from the start/end.
3669 */
3670 if (channel >= center_channels[i] - 6 &&
3671 channel <= center_channels[i] + 6)
3672 return center_channels[i];
3673
3674 return 0;
3675}
3676
3677
Hai Shalomc1a21442022-02-04 13:43:00 -08003678static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3679 155, 171 };
3680static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3681 119, 135, 151, 167, 183, 199,
3682 215 };
3683
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003684static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3685 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003686 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003687{
3688 u8 center_chan;
3689 int i, flags;
3690 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003691 const u8 *chans;
3692 size_t num_chans;
3693 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003694
Hai Shalomc1a21442022-02-04 13:43:00 -08003695 if (is_6ghz) {
3696 chans = center_channels_6ghz_80mhz;
3697 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3698 } else {
3699 chans = center_channels_5ghz_80mhz;
3700 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3701 }
3702 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3703 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003704 if (!center_chan)
3705 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003706 if (!wpa_s->p2p_go_allow_dfs &&
3707 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003708 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3709
3710 /* check all the channels are available */
3711 for (i = 0; i < 4; i++) {
3712 int adj_chan = center_chan - 6 + i * 4;
3713
Hai Shalomc1a21442022-02-04 13:43:00 -08003714 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3715 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003716 if (res == NOT_ALLOWED)
3717 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003718 if (res == RADAR)
3719 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003720 if (res == NO_IR)
3721 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003722 if (!is_6ghz) {
3723 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3724 return NOT_ALLOWED;
3725 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3726 return NOT_ALLOWED;
3727 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3728 return NOT_ALLOWED;
3729 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3730 return NOT_ALLOWED;
3731 } else if (is_6ghz &&
3732 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3733 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003734 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003735 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003736 }
3737
3738 return ret;
3739}
3740
3741
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003742static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3743 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003744 u8 channel, const u8 *center_channels,
3745 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003746{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003747 unsigned int i;
3748
3749 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3750 return 0;
3751
Hai Shalomc1a21442022-02-04 13:43:00 -08003752 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003753 /*
3754 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3755 * so the center channel is 14 channels away from the start/end.
3756 */
3757 if (channel >= center_channels[i] - 14 &&
3758 channel <= center_channels[i] + 14)
3759 return center_channels[i];
3760
3761 return 0;
3762}
3763
3764
Hai Shalomc1a21442022-02-04 13:43:00 -08003765static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3766static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3767 207 };
3768
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003769static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3770 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003771 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003772{
3773 u8 center_chan;
3774 int i, flags;
3775 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003776 const u8 *chans;
3777 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003778
Hai Shalomc1a21442022-02-04 13:43:00 -08003779 if (is_6ghz_op_class(op_class)) {
3780 chans = center_channels_6ghz_160mhz;
3781 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3782 } else {
3783 chans = center_channels_5ghz_160mhz;
3784 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3785 }
3786 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3787 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003788 if (!center_chan)
3789 return NOT_ALLOWED;
3790 /* VHT 160 MHz uses DFS channels in most countries. */
3791
3792 /* Check all the channels are available */
3793 for (i = 0; i < 8; i++) {
3794 int adj_chan = center_chan - 14 + i * 4;
3795
Hai Shalomc1a21442022-02-04 13:43:00 -08003796 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3797 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003798 if (res == NOT_ALLOWED)
3799 return NOT_ALLOWED;
3800
Hai Shalomc1a21442022-02-04 13:43:00 -08003801 if (res == RADAR)
3802 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003803 if (res == NO_IR)
3804 ret = NO_IR;
3805
Hai Shalomc1a21442022-02-04 13:43:00 -08003806 if (!is_6ghz_op_class(op_class)) {
3807 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3808 return NOT_ALLOWED;
3809 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3810 return NOT_ALLOWED;
3811 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3812 return NOT_ALLOWED;
3813 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3814 return NOT_ALLOWED;
3815 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3816 return NOT_ALLOWED;
3817 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3818 return NOT_ALLOWED;
3819 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3820 return NOT_ALLOWED;
3821 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3822 return NOT_ALLOWED;
3823 } else if (is_6ghz_op_class(op_class) &&
3824 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3825 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003826 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003827 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003828 }
3829
3830 return ret;
3831}
3832
3833
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003834static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3835 struct hostapd_hw_modes *mode,
3836 u8 channel)
3837{
3838 struct ieee80211_edmg_config edmg;
3839
3840 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3841 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3842 return ALLOWED;
3843
3844 return NOT_ALLOWED;
3845}
3846
3847
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003848static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3849 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003850 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003851{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003852 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003853 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003854
Sunil Ravi89eba102022-09-13 21:04:37 -07003855 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3856 p2p_channel_to_freq(op_class, channel)))
3857 return NOT_ALLOWED;
3858
Hai Shalomc1a21442022-02-04 13:43:00 -08003859 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003860 if (bw == BW40MINUS) {
3861 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3862 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003863 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3864 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003865 } else if (bw == BW40PLUS) {
3866 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3867 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003868 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3869 NULL);
3870 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3871 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3872 return NOT_ALLOWED;
3873 if (get_6ghz_sec_channel(channel) < 0)
3874 res2 = has_channel(wpa_s->global, mode, op_class,
3875 channel - 4, NULL);
3876 else
3877 res2 = has_channel(wpa_s->global, mode, op_class,
3878 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003879 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003880 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3881 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003882 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003883 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3884 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003885 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3886 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003887 }
3888
3889 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3890 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003891 if (res == NO_IR || res2 == NO_IR)
3892 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003893 if (res == RADAR || res2 == RADAR)
3894 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003895 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003896}
3897
3898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003899static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003900 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003901 struct p2p_channels *cli_chan,
3902 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003904 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003905 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003907 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3909 "of all supported channels; assume dualband "
3910 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003911 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 }
3913
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003914 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003916 for (op = 0; global_op_class[op].op_class; op++) {
3917 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003918 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003919 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920
Hai Shalom899fcc72020-10-19 14:38:18 -07003921 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003922 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003923 continue;
3924
Hai Shalomfdcde762020-04-02 11:19:20 -07003925 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3926 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927 if (mode == NULL)
3928 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003929 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3930 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003931 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003932 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003933
3934 /* Check for non-continuous jump in channel index
3935 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003936 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003937 ch < 149 && ch + o->inc > 149)
3938 ch = 149;
3939
Hai Shalomc1a21442022-02-04 13:43:00 -08003940 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
3941 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003942 if (res == ALLOWED) {
3943 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003944 if (cla == P2P_MAX_REG_CLASSES)
3945 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003946 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3947 o->op_class);
3948 reg = &chan->reg_class[cla];
3949 cla++;
3950 reg->reg_class = o->op_class;
3951 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003952 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3953 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003954 reg->channel[reg->channels] = ch;
3955 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003956 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003957 wpa_s->conf->p2p_add_cli_chan) {
3958 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003959 if (cli_cla == P2P_MAX_REG_CLASSES)
3960 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003961 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3962 o->op_class);
3963 cli_reg = &cli_chan->reg_class[cli_cla];
3964 cli_cla++;
3965 cli_reg->reg_class = o->op_class;
3966 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003967 if (cli_reg->channels ==
3968 P2P_MAX_REG_CLASS_CHANNELS)
3969 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003970 cli_reg->channel[cli_reg->channels] = ch;
3971 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 }
3974 if (reg) {
3975 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3976 reg->channel, reg->channels);
3977 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003978 if (cli_reg) {
3979 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3980 cli_reg->channel, cli_reg->channels);
3981 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982 }
3983
3984 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003985 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 return 0;
3988}
3989
3990
Hai Shalomc1a21442022-02-04 13:43:00 -08003991int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
3992 struct hostapd_hw_modes *mode,
3993 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003994{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003995 int op;
3996 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003997
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003998 for (op = 0; global_op_class[op].op_class; op++) {
3999 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08004000 u16 ch;
4001 int chan = channel;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004002
Hai Shalomc1a21442022-02-04 13:43:00 -08004003 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4004 * driver offloaded DFS. */
4005 if ((o->p2p == NO_P2P_SUPP &&
4006 (!is_dfs_global_op_class(o->op_class) ||
4007 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004008 (is_6ghz_op_class(o->op_class) &&
4009 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004010 continue;
4011
Hai Shalomc1a21442022-02-04 13:43:00 -08004012 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4013 get_6ghz_sec_channel(channel) < 0)
4014 chan = channel - 4;
4015
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004016 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4017 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004018 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4019 o->bw != BW40) ||
4020 ch != chan)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004021 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004022 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4023 ch, o->bw);
4024 if (ret == ALLOWED) {
4025 if (is_6ghz_op_class(o->op_class) &&
4026 o->bw == BW40)
4027 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004028 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004029 }
4030 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4031 /* Allow RADAR channels used for driver
4032 * offloaded DFS */
4033 return (o->bw == BW40MINUS) ? -1 : 1;
4034 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004035 }
4036 }
4037 return 0;
4038}
4039
4040
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004041int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004042 struct hostapd_hw_modes *mode, u8 channel,
4043 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004044{
Hai Shalomc1a21442022-02-04 13:43:00 -08004045 const u8 *chans;
4046 size_t num_chans;
4047 enum chan_allowed ret;
4048
4049 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4050 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004051 return 0;
4052
Hai Shalomc1a21442022-02-04 13:43:00 -08004053 if (is_6ghz_op_class(op_class)) {
4054 chans = center_channels_6ghz_80mhz;
4055 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4056 } else {
4057 chans = center_channels_5ghz_80mhz;
4058 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4059 }
4060 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4061 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004062}
4063
4064
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004065int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004066 struct hostapd_hw_modes *mode, u8 channel,
4067 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004068{
Hai Shalomc1a21442022-02-04 13:43:00 -08004069 const u8 *chans;
4070 size_t num_chans;
4071 enum chan_allowed ret;
4072
4073 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4074 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004075 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004076 if (is_6ghz_op_class(op_class)) {
4077 chans = center_channels_6ghz_160mhz;
4078 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4079 } else {
4080 chans = center_channels_5ghz_160mhz;
4081 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4082 }
4083 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4084 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004085}
4086
4087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4089 size_t buf_len)
4090{
4091 struct wpa_supplicant *wpa_s = ctx;
4092
4093 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4094 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4095 break;
4096 }
4097 if (wpa_s == NULL)
4098 return -1;
4099
4100 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4101}
4102
4103
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004104struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4105 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004106{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004107 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4108 struct wpa_ssid *s = wpa_s->current_ssid;
4109 if (s == NULL)
4110 continue;
4111 if (s->mode != WPAS_MODE_P2P_GO &&
4112 s->mode != WPAS_MODE_AP &&
4113 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4114 continue;
4115 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004116 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004117 continue;
4118 return wpa_s;
4119 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004120
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004121 return NULL;
4122
4123}
4124
4125
4126struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4127 const u8 *peer_dev_addr)
4128{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004129 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4130 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004131 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004132 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004133 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4134 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004135 }
4136
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004137 return NULL;
4138}
4139
4140
4141static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4142{
4143 struct wpa_supplicant *wpa_s = ctx;
4144
4145 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004146}
4147
4148
Dmitry Shmidt18463232014-01-24 12:29:41 -08004149static int wpas_is_concurrent_session_active(void *ctx)
4150{
4151 struct wpa_supplicant *wpa_s = ctx;
4152 struct wpa_supplicant *ifs;
4153
4154 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4155 if (ifs == wpa_s)
4156 continue;
4157 if (ifs->wpa_state > WPA_ASSOCIATED)
4158 return 1;
4159 }
4160 return 0;
4161}
4162
4163
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004164static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4165{
4166 struct wpa_supplicant *wpa_s = ctx;
4167 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4168}
4169
4170
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004171int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4172 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004173{
4174 struct wpa_interface iface;
4175 struct wpa_supplicant *p2pdev_wpa_s;
4176 char ifname[100];
4177 char force_name[100];
4178 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004179 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004180
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004181 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4182 wpa_s->ifname);
4183 if (os_snprintf_error(sizeof(ifname), ret))
4184 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004185 /* Cut length at the maximum size. Note that we don't need to ensure
4186 * collision free names here as the created interface is not a netdev.
4187 */
4188 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004189 force_name[0] = '\0';
4190 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004191
4192 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4193 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4194 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4195
4196 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004197 force_name, wpa_s->pending_interface_addr, NULL);
4198 if (ret < 0) {
4199 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4200 return ret;
4201 }
4202 os_strlcpy(wpa_s->pending_interface_name, ifname,
4203 sizeof(wpa_s->pending_interface_name));
4204
4205 os_memset(&iface, 0, sizeof(iface));
4206 iface.p2p_mgmt = 1;
4207 iface.ifname = wpa_s->pending_interface_name;
4208 iface.driver = wpa_s->driver->name;
4209 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004210
4211 /*
4212 * If a P2P Device configuration file was given, use it as the interface
4213 * configuration file (instead of using parent's configuration file.
4214 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004215 if (conf_p2p_dev) {
4216 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004217 iface.ctrl_interface = NULL;
4218 } else {
4219 iface.confname = wpa_s->confname;
4220 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4221 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004222
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004223 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004224 if (!p2pdev_wpa_s) {
4225 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4226 return -1;
4227 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004228
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004229 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004230 wpa_s->pending_interface_name[0] = '\0';
4231 return 0;
4232}
4233
4234
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004235static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4236 const u8 *noa, size_t noa_len)
4237{
4238 struct wpa_supplicant *wpa_s, *intf = ctx;
4239 char hex[100];
4240
4241 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4242 if (wpa_s->waiting_presence_resp)
4243 break;
4244 }
4245 if (!wpa_s) {
4246 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4247 return;
4248 }
4249 wpa_s->waiting_presence_resp = 0;
4250
4251 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4252 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4253 " status=%u noa=%s", MAC2STR(src), status, hex);
4254}
4255
4256
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004257static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4258 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004259 u8 *ret_ssid, size_t *ret_ssid_len,
4260 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004261{
4262 struct wpa_supplicant *wpa_s = ctx;
4263 struct wpa_ssid *s;
4264
4265 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4266 if (s) {
4267 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4268 *ret_ssid_len = s->ssid_len;
4269 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004270
4271 if (s->mode != WPAS_MODE_P2P_GO) {
4272 os_memset(intended_iface_addr, 0, ETH_ALEN);
4273 } else if (wpas_p2p_create_iface(wpa_s)) {
4274 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4275 return 0;
4276
4277 os_memcpy(intended_iface_addr,
4278 wpa_s->pending_interface_addr, ETH_ALEN);
4279 } else {
4280 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4281 ETH_ALEN);
4282 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004283 return 1;
4284 }
4285
4286 return 0;
4287}
4288
4289
4290static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004291 u8 *ssid, size_t *ssid_len, int *group_iface,
4292 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004293{
4294 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004295 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004296 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004297
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004298 /*
4299 * group_iface will be set to 1 only if a dedicated interface for P2P
4300 * role is required. First, we try to reuse an active GO. However,
4301 * if it is not present, we will try to reactivate an existing
4302 * persistent group and set group_iface to 1, so the caller will know
4303 * that the pending interface should be used.
4304 */
4305 *group_iface = 0;
4306
4307 if (freq)
4308 *freq = 0;
4309
4310 go = wpas_p2p_get_go_group(wpa_s);
4311 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004312 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004313 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004314 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004315 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4316 else
4317 return 0;
4318 } else {
4319 s = go->current_ssid;
4320 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4321 if (freq)
4322 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004323 }
4324
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004325 os_memcpy(ssid, s->ssid, s->ssid_len);
4326 *ssid_len = s->ssid_len;
4327
4328 return 1;
4329}
4330
4331
4332static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4333 const u8 *ssid, size_t ssid_len)
4334{
4335 struct wpa_supplicant *wpa_s = ctx;
4336 struct wpa_ssid *s;
4337 int save_config = 0;
4338 size_t i;
4339
4340 /* Start with our first choice of Persistent Groups */
4341 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4342 if (go && ssid && ssid_len &&
4343 s->ssid_len == ssid_len &&
4344 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4345 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4346 break;
4347
4348 /* Remove stale persistent group */
4349 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004350 wpa_dbg(wpa_s, MSG_DEBUG,
4351 "P2P: Remove stale persistent group id=%d",
4352 s->id);
4353 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004354 wpa_config_remove_network(wpa_s->conf, s->id);
4355 save_config = 1;
4356 continue;
4357 }
4358
4359 for (i = 0; i < s->num_p2p_clients; i++) {
4360 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4361 peer, ETH_ALEN) != 0)
4362 continue;
4363
4364 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4365 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4366 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4367 break;
4368 }
4369 s->num_p2p_clients--;
4370 save_config = 1;
4371 }
4372
4373 if (save_config)
4374 p2p_config_write(wpa_s);
4375
4376 /* Return TRUE if valid SSID remains */
4377 return s != NULL;
4378}
4379
4380
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004381static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4382 const u8 *feat_cap, size_t feat_cap_len)
4383{
4384 static const char pref[] = " feature_cap=";
4385 int ret;
4386
4387 buf[0] = '\0';
4388
4389 /*
4390 * We expect a feature capability to contain at least one byte to be
4391 * reported. The string buffer provided by the caller function is
4392 * expected to be big enough to contain all bytes of the attribute for
4393 * known specifications. This function truncates the reported bytes if
4394 * the feature capability data exceeds the string buffer size.
4395 */
4396 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4397 return;
4398
4399 os_memcpy(buf, pref, sizeof(pref));
4400 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4401 buf_len - sizeof(pref) + 1,
4402 feat_cap, feat_cap_len);
4403
4404 if (ret != (2 * (int) feat_cap_len))
4405 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4406}
4407
4408
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004409static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4410 const u8 *adv_mac, const u8 *ses_mac,
4411 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4412 u8 conncap, int passwd_id,
4413 const u8 *persist_ssid,
4414 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004415 int prov_start, const char *session_info,
4416 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004417 unsigned int freq,
4418 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004419{
4420 struct wpa_supplicant *wpa_s = ctx;
4421 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004422 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004423 int save_config = 0;
4424 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004425 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004426
4427 if (!dev)
4428 return;
4429
4430 os_memset(mac, 0, ETH_ALEN);
4431 if (!adv_mac)
4432 adv_mac = mac;
4433 if (!ses_mac)
4434 ses_mac = mac;
4435 if (!grp_mac)
4436 grp_mac = mac;
4437
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004438 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4439 feat_cap, feat_cap_len);
4440
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004441 if (prov_start) {
4442 if (session_info == NULL) {
4443 wpa_msg_global(wpa_s, MSG_INFO,
4444 P2P_EVENT_P2PS_PROVISION_START MACSTR
4445 " adv_id=%x conncap=%x"
4446 " adv_mac=" MACSTR
4447 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004448 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004449 MAC2STR(dev), adv_id, conncap,
4450 MAC2STR(adv_mac),
4451 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004452 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004453 } else {
4454 wpa_msg_global(wpa_s, MSG_INFO,
4455 P2P_EVENT_P2PS_PROVISION_START MACSTR
4456 " adv_id=%x conncap=%x"
4457 " adv_mac=" MACSTR
4458 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004459 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004460 MAC2STR(dev), adv_id, conncap,
4461 MAC2STR(adv_mac),
4462 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004463 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004464 }
4465 return;
4466 }
4467
4468 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4469 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4470
4471 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4472 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4473 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4474
4475 if (persistent_go && !persistent_go->num_p2p_clients) {
4476 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004477 wpa_dbg(wpa_s, MSG_DEBUG,
4478 "P2P: Remove empty persistent group id=%d",
4479 persistent_go->id);
4480 wpas_notify_persistent_group_removed(wpa_s,
4481 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004482 wpa_config_remove_network(wpa_s->conf,
4483 persistent_go->id);
4484 }
4485
4486 wpa_msg_global(wpa_s, MSG_INFO,
4487 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4488 " status=%d"
4489 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004490 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004491 MAC2STR(dev), status,
4492 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004493 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004494 return;
4495 }
4496
4497 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004498 if (persist_ssid && persist_ssid_size)
4499 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4500 persist_ssid_size);
4501
4502 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4503 is_zero_ether_addr(grp_mac)) {
4504 wpa_dbg(wpa_s, MSG_ERROR,
4505 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4506 return;
4507 }
4508
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004509 for (;;) {
4510 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4511 if (!stale)
4512 break;
4513
4514 if (s && s->ssid_len == stale->ssid_len &&
4515 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4516 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4517 break;
4518
4519 /* Remove stale persistent group */
4520 if (stale->mode != WPAS_MODE_P2P_GO ||
4521 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004522 wpa_dbg(wpa_s, MSG_DEBUG,
4523 "P2P: Remove stale persistent group id=%d",
4524 stale->id);
4525 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004526 wpa_config_remove_network(wpa_s->conf, stale->id);
4527 } else {
4528 size_t i;
4529
4530 for (i = 0; i < stale->num_p2p_clients; i++) {
4531 if (os_memcmp(stale->p2p_client_list +
4532 i * ETH_ALEN,
4533 dev, ETH_ALEN) == 0) {
4534 os_memmove(stale->p2p_client_list +
4535 i * ETH_ALEN,
4536 stale->p2p_client_list +
4537 (i + 1) * ETH_ALEN,
4538 (stale->num_p2p_clients -
4539 i - 1) * ETH_ALEN);
4540 break;
4541 }
4542 }
4543 stale->num_p2p_clients--;
4544 }
4545 save_config = 1;
4546 }
4547
4548 if (save_config)
4549 p2p_config_write(wpa_s);
4550
4551 if (s) {
4552 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4553 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4554
4555 if (persistent_go && s != persistent_go &&
4556 !persistent_go->num_p2p_clients) {
4557 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004558 wpa_dbg(wpa_s, MSG_DEBUG,
4559 "P2P: Remove empty persistent group id=%d",
4560 persistent_go->id);
4561 wpas_notify_persistent_group_removed(wpa_s,
4562 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004563 wpa_config_remove_network(wpa_s->conf,
4564 persistent_go->id);
4565 /* Save config */
4566 }
4567
4568 wpa_msg_global(wpa_s, MSG_INFO,
4569 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4570 " status=%d"
4571 " adv_id=%x adv_mac=" MACSTR
4572 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004573 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004574 MAC2STR(dev), status,
4575 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004576 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004577 return;
4578 }
4579
Hai Shalom5f92bc92019-04-18 11:54:11 -07004580 wpa_s->global->pending_p2ps_group = 0;
4581 wpa_s->global->pending_p2ps_group_freq = 0;
4582
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004583 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004584 /*
4585 * We need to copy the interface name. Simply saving a
4586 * pointer isn't enough, since if we use pending_interface_name
4587 * it will be overwritten when the group is added.
4588 */
4589 char go_ifname[100];
4590
4591 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004592 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004593 if (!response_done) {
4594 wpa_s->global->pending_p2ps_group = 1;
4595 wpa_s->global->pending_p2ps_group_freq = freq;
4596 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004597
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004598 if (!wpas_p2p_create_iface(wpa_s))
4599 os_memcpy(go_ifname, wpa_s->ifname,
4600 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004601 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004602 os_memcpy(go_ifname,
4603 wpa_s->pending_interface_name,
4604 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004605
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004606 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004607 wpas_p2ps_prov_complete(
4608 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4609 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004610 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004611 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4612 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004613 return;
4614 }
4615
4616 /* If PD Resp complete, start up the GO */
4617 if (response_done && persistent_go) {
4618 wpas_p2p_group_add_persistent(
4619 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004620 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004621 persistent_go->mode ==
4622 WPAS_MODE_P2P_GO ?
4623 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Matthew Wang124e5f42022-12-29 07:23:06 +00004624 0, 0, false, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004625 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004626 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004627 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004628 }
4629
4630 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004631 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4632 ETH_ALEN);
4633 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004634 }
4635 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004636 os_memcpy(go_ifname, go_wpa_s->ifname,
4637 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004638
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004639 if (is_zero_ether_addr(grp_mac)) {
4640 wpa_dbg(go_wpa_s, MSG_DEBUG,
4641 "P2P: Setting PIN-1 for ANY");
4642 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4643 "12345670", NULL, 0,
4644 0);
4645 } else {
4646 wpa_dbg(go_wpa_s, MSG_DEBUG,
4647 "P2P: Setting PIN-1 for " MACSTR,
4648 MAC2STR(grp_mac));
4649 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4650 "12345670", NULL, 0,
4651 0);
4652 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004653
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004654 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4655 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004656 }
4657
4658 wpa_msg_global(wpa_s, MSG_INFO,
4659 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4660 " status=%d conncap=%x"
4661 " adv_id=%x adv_mac=" MACSTR
4662 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004663 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004664 MAC2STR(dev), status, conncap,
4665 adv_id, MAC2STR(adv_mac),
4666 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004667 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004668 return;
4669 }
4670
4671 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4672 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4673
4674 if (persistent_go && !persistent_go->num_p2p_clients) {
4675 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004676 wpa_dbg(wpa_s, MSG_DEBUG,
4677 "P2P: Remove empty persistent group id=%d",
4678 persistent_go->id);
4679 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004680 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4681 }
4682
4683 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004684 char ssid_hex[32 * 2 + 1];
4685
4686 if (group_ssid)
4687 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4688 group_ssid, group_ssid_len);
4689 else
4690 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004691 wpa_msg_global(wpa_s, MSG_INFO,
4692 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4693 " status=%d conncap=%x"
4694 " adv_id=%x adv_mac=" MACSTR
4695 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004696 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004697 MAC2STR(dev), status, conncap,
4698 adv_id, MAC2STR(adv_mac),
4699 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004700 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4701 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004702 } else {
4703 wpa_msg_global(wpa_s, MSG_INFO,
4704 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4705 " status=%d conncap=%x"
4706 " adv_id=%x adv_mac=" MACSTR
4707 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004708 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004709 MAC2STR(dev), status, conncap,
4710 adv_id, MAC2STR(adv_mac),
4711 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004712 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004713 }
4714}
4715
4716
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004717static int _wpas_p2p_in_progress(void *ctx)
4718{
4719 struct wpa_supplicant *wpa_s = ctx;
4720 return wpas_p2p_in_progress(wpa_s);
4721}
4722
4723
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004724static int wpas_prov_disc_resp_cb(void *ctx)
4725{
4726 struct wpa_supplicant *wpa_s = ctx;
4727 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004728 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004729
4730 if (!wpa_s->global->pending_p2ps_group)
4731 return 0;
4732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004733 freq = wpa_s->global->pending_p2ps_group_freq;
4734 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004735 wpa_s->global->pending_p2ps_group = 0;
4736
4737 if (wpas_p2p_get_go_group(wpa_s))
4738 return 0;
4739 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4740
4741 if (persistent_go) {
4742 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004743 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4744 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004745 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004746 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00004747 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004748 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004749 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4750 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004751 }
4752
4753 return 1;
4754}
4755
4756
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004757static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4758 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004759 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004760{
4761 struct wpa_supplicant *wpa_s = ctx;
4762
4763 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4764 WPA_IF_P2P_CLIENT, len, freq_list);
4765}
4766
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004767int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4768{
JaeMan Park9de97322022-07-11 15:36:43 +09004769 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004770 u8 addr[ETH_ALEN] = {0};
4771
4772 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4773 return 0;
4774
Hai Shalom60840252021-02-19 19:02:11 -08004775 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4776 if (is_zero_ether_addr(
4777 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4778 !is_zero_ether_addr(wpa_s->own_addr)) {
4779 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4780 wpa_s->own_addr, ETH_ALEN);
4781 }
4782 return 0;
4783 }
4784
4785 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004786 if (random_mac_addr(addr) < 0) {
4787 wpa_msg(wpa_s, MSG_INFO,
4788 "Failed to generate random MAC address");
4789 return -EINVAL;
4790 }
4791
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004792 /* Store generated MAC address. */
4793 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4794 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004795 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004796 /* If there are existing saved groups, restore last MAC address.
4797 * if there is no last used MAC address, the last one is
4798 * factory MAC. */
4799 if (is_zero_ether_addr(
4800 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004801 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004802 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4803 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004804 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4805 }
4806
JaeMan Park9de97322022-07-11 15:36:43 +09004807 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4808
4809 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004810 wpa_msg(wpa_s, MSG_INFO,
4811 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004812 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004813 }
4814
JaeMan Park9de97322022-07-11 15:36:43 +09004815 ret = wpa_supplicant_update_mac_addr(wpa_s);
4816
4817 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004818 wpa_msg(wpa_s, MSG_INFO,
4819 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004820 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004821 }
4822
4823 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4824 MAC2STR(addr));
4825
4826 return 0;
4827}
4828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829/**
4830 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4831 * @global: Pointer to global data from wpa_supplicant_init()
4832 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4833 * Returns: 0 on success, -1 on failure
4834 */
4835int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4836{
4837 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838 int i;
4839
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004840 if (wpa_s->conf->p2p_disabled)
4841 return 0;
4842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4844 return 0;
4845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846 if (global->p2p)
4847 return 0;
4848
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004849 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4850 wpa_msg(wpa_s, MSG_ERROR,
4851 "Failed to initialize P2P random MAC address.");
4852 return -1;
4853 }
4854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004855 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004857 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858 p2p.p2p_scan = wpas_p2p_scan;
4859 p2p.send_action = wpas_send_action;
4860 p2p.send_action_done = wpas_send_action_done;
4861 p2p.go_neg_completed = wpas_go_neg_completed;
4862 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4863 p2p.dev_found = wpas_dev_found;
4864 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004865 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866 p2p.start_listen = wpas_start_listen;
4867 p2p.stop_listen = wpas_stop_listen;
4868 p2p.send_probe_resp = wpas_send_probe_resp;
4869 p2p.sd_request = wpas_sd_request;
4870 p2p.sd_response = wpas_sd_response;
4871 p2p.prov_disc_req = wpas_prov_disc_req;
4872 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004873 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 p2p.invitation_process = wpas_invitation_process;
4875 p2p.invitation_received = wpas_invitation_received;
4876 p2p.invitation_result = wpas_invitation_result;
4877 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004878 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004879 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004880 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004881 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004882 p2p.get_persistent_group = wpas_get_persistent_group;
4883 p2p.get_go_info = wpas_get_go_info;
4884 p2p.remove_stale_groups = wpas_remove_stale_groups;
4885 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4886 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4887 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004888 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004889 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890
4891 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004892 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 p2p.dev_name = wpa_s->conf->device_name;
4894 p2p.manufacturer = wpa_s->conf->manufacturer;
4895 p2p.model_name = wpa_s->conf->model_name;
4896 p2p.model_number = wpa_s->conf->model_number;
4897 p2p.serial_number = wpa_s->conf->serial_number;
4898 if (wpa_s->wps) {
4899 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4900 p2p.config_methods = wpa_s->wps->config_methods;
4901 }
4902
Hai Shalom60840252021-02-19 19:02:11 -08004903 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4904 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004905 wpa_printf(MSG_ERROR,
4906 "P2P: Failed to configure supported channel list");
4907 return -1;
4908 }
4909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 if (wpa_s->conf->p2p_listen_reg_class &&
4911 wpa_s->conf->p2p_listen_channel) {
4912 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4913 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004914 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 /*
4917 * Pick one of the social channels randomly as the listen
4918 * channel.
4919 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004920 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004921 &p2p.channel,
4922 &global->p2p_go_avoid_freq,
4923 &global->p2p_disallow_freq) !=
4924 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004925 wpa_printf(MSG_INFO,
4926 "P2P: No social channels supported by the driver - do not enable P2P");
4927 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004928 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004929 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004931 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4932 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933
4934 if (wpa_s->conf->p2p_oper_reg_class &&
4935 wpa_s->conf->p2p_oper_channel) {
4936 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4937 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4938 p2p.cfg_op_channel = 1;
4939 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4940 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4941
4942 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004944 * Use random operation channel from 2.4 GHz band social
4945 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4946 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004948 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004949 &p2p.op_channel, NULL,
4950 NULL) != 0) {
4951 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004952 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004953 p2p.op_reg_class = 0;
4954 p2p.op_channel = 0;
4955 /* This will be overridden during group setup in
4956 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004957 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 p2p.cfg_op_channel = 0;
4959 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4960 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4961 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004962
4963 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4964 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4965 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4966 }
4967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4969 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4970 p2p.country[2] = 0x04;
4971 } else
4972 os_memcpy(p2p.country, "XX\x04", 3);
4973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004974 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4975 WPS_DEV_TYPE_LEN);
4976
4977 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4978 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4979 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4980
4981 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4982 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4983
4984 p2p.max_peers = 100;
4985
4986 if (wpa_s->conf->p2p_ssid_postfix) {
4987 p2p.ssid_postfix_len =
4988 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4989 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4990 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4991 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4992 p2p.ssid_postfix_len);
4993 }
4994
4995 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4996
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004997 p2p.max_listen = wpa_s->max_remain_on_chan;
4998
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004999 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5000 wpa_s->conf->p2p_passphrase_len <= 63)
5001 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5002 else
5003 p2p.passphrase_len = 8;
5004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 global->p2p = p2p_init(&p2p);
5006 if (global->p2p == NULL)
5007 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005008 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009
5010 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5011 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5012 continue;
5013 p2p_add_wps_vendor_extension(
5014 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5015 }
5016
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005017 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 return 0;
5020}
5021
5022
5023/**
5024 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5025 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5026 *
5027 * This function deinitialize per-interface P2P data.
5028 */
5029void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5030{
5031 if (wpa_s->driver && wpa_s->drv_priv)
5032 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005033
5034 if (wpa_s->go_params) {
5035 /* Clear any stored provisioning info */
5036 p2p_clear_provisioning_info(
5037 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005038 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005039 }
5040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005041 os_free(wpa_s->go_params);
5042 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005043 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5045 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005046 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005047 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5048 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5049 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005050 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005051 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005052 wpas_p2p_listen_work_done(wpa_s);
5053 if (wpa_s->p2p_send_action_work) {
5054 os_free(wpa_s->p2p_send_action_work->ctx);
5055 radio_work_done(wpa_s->p2p_send_action_work);
5056 wpa_s->p2p_send_action_work = NULL;
5057 }
5058 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005060 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5061 wpa_s->p2p_oob_dev_pw = NULL;
5062
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005063 os_free(wpa_s->p2p_group_common_freqs);
5064 wpa_s->p2p_group_common_freqs = NULL;
5065 wpa_s->p2p_group_common_freqs_num = 0;
5066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067 /* TODO: remove group interface from the driver if this wpa_s instance
5068 * is on top of a P2P group interface */
5069}
5070
5071
5072/**
5073 * wpas_p2p_deinit_global - Deinitialize global P2P module
5074 * @global: Pointer to global data from wpa_supplicant_init()
5075 *
5076 * This function deinitializes the global (per device) P2P module.
5077 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005078static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079{
5080 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005082 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005083
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005084 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085
5086 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5088 wpa_s = wpa_s->next;
5089 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090 tmp = global->ifaces;
5091 while (tmp &&
5092 (tmp == wpa_s ||
5093 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5094 tmp = tmp->next;
5095 }
5096 if (tmp == NULL)
5097 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005098 /* Disconnect from the P2P group and deinit the interface */
5099 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 }
5101
5102 /*
5103 * Deinit GO data on any possibly remaining interface (if main
5104 * interface is used as GO).
5105 */
5106 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5107 if (wpa_s->ap_iface)
5108 wpas_p2p_group_deinit(wpa_s);
5109 }
5110
5111 p2p_deinit(global->p2p);
5112 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005113 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114}
5115
5116
5117static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5118{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005119 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005120 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 if (wpa_s->drv_flags &
5122 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5123 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5124 return 1; /* P2P group requires a new interface in every case
5125 */
5126 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5127 return 0; /* driver does not support concurrent operations */
5128 if (wpa_s->global->ifaces->next)
5129 return 1; /* more that one interface already in use */
5130 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5131 return 1; /* this interface is already in use */
5132 return 0;
5133}
5134
5135
5136static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5137 const u8 *peer_addr,
5138 enum p2p_wps_method wps_method,
5139 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005140 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005141 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005143 if (persistent_group && wpa_s->conf->persistent_reconnect)
5144 persistent_group = 2;
5145
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005146 /*
5147 * Increase GO config timeout if HT40 is used since it takes some time
5148 * to scan channels for coex purposes before the BSS can be started.
5149 */
5150 p2p_set_config_timeout(wpa_s->global->p2p,
5151 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5154 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005155 persistent_group, ssid ? ssid->ssid : NULL,
5156 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005157 wpa_s->p2p_pd_before_go_neg, pref_freq,
5158 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5159 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160}
5161
5162
5163static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5164 const u8 *peer_addr,
5165 enum p2p_wps_method wps_method,
5166 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005167 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005168 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005170 if (persistent_group && wpa_s->conf->persistent_reconnect)
5171 persistent_group = 2;
5172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5174 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005175 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005176 ssid ? ssid->ssid_len : 0, pref_freq,
5177 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5178 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005179}
5180
5181
5182static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5183{
5184 wpa_s->p2p_join_scan_count++;
5185 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5186 wpa_s->p2p_join_scan_count);
5187 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5188 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5189 " for join operationg - stop join attempt",
5190 MAC2STR(wpa_s->pending_join_iface_addr));
5191 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005192 if (wpa_s->p2p_auto_pd) {
5193 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005194 wpa_msg_global(wpa_s, MSG_INFO,
5195 P2P_EVENT_PROV_DISC_FAILURE
5196 " p2p_dev_addr=" MACSTR " status=N/A",
5197 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005198 return;
5199 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005200 if (wpa_s->p2p_fallback_to_go_neg) {
5201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5202 "failed - fall back to GO Negotiation");
5203 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5204 P2P_EVENT_FALLBACK_TO_GO_NEG
5205 "reason=join-failed");
5206 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5207 return;
5208 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005209 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005210 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005211 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005212 }
5213}
5214
5215
Dmitry Shmidt04949592012-07-19 12:16:46 -07005216static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5217{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005218 int res;
5219 unsigned int num, i;
5220 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005221
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005222 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5223 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005224 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005225 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005226
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005227 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5228 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005229 if (!freqs)
5230 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005231
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005232 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5233 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005234
5235 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005236 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005237 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5238 freq);
5239 res = 0;
5240 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005241 }
5242 }
5243
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005244 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005245 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005246
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005247exit_free:
5248 os_free(freqs);
5249 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005250}
5251
5252
5253static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5254 const u8 *peer_dev_addr)
5255{
5256 struct wpa_bss *bss;
5257 int updated;
5258
5259 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5260 if (bss == NULL)
5261 return -1;
5262 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5263 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5264 "last scan");
5265 return 0;
5266 }
5267
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005268 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5269 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005270 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5271 "%ld.%06ld (%supdated in last scan)",
5272 bss->last_update.sec, bss->last_update.usec,
5273 updated ? "": "not ");
5274
5275 return updated;
5276}
5277
5278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005279static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5280 struct wpa_scan_results *scan_res)
5281{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005282 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005283 int freq;
5284 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5287
5288 if (wpa_s->global->p2p_disabled)
5289 return;
5290
Dmitry Shmidt04949592012-07-19 12:16:46 -07005291 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5292 scan_res ? (int) scan_res->num : -1,
5293 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005294
5295 if (scan_res)
5296 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5297
Dmitry Shmidt04949592012-07-19 12:16:46 -07005298 if (wpa_s->p2p_auto_pd) {
5299 int join = wpas_p2p_peer_go(wpa_s,
5300 wpa_s->pending_join_dev_addr);
5301 if (join == 0 &&
5302 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5303 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005304 bss = wpa_bss_get_bssid_latest(
5305 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005306 if (bss) {
5307 freq = bss->freq;
5308 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5309 "the peer " MACSTR " at %d MHz",
5310 wpa_s->auto_pd_scan_retry,
5311 MAC2STR(wpa_s->
5312 pending_join_dev_addr),
5313 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005314 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005315 return;
5316 }
5317 }
5318
5319 if (join < 0)
5320 join = 0;
5321
5322 wpa_s->p2p_auto_pd = 0;
5323 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5324 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5325 MAC2STR(wpa_s->pending_join_dev_addr), join);
5326 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005327 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005328 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005329 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005330 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005331 wpa_msg_global(wpa_s, MSG_INFO,
5332 P2P_EVENT_PROV_DISC_FAILURE
5333 " p2p_dev_addr=" MACSTR " status=N/A",
5334 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005335 }
5336 return;
5337 }
5338
5339 if (wpa_s->p2p_auto_join) {
5340 int join = wpas_p2p_peer_go(wpa_s,
5341 wpa_s->pending_join_dev_addr);
5342 if (join < 0) {
5343 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5344 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005345 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005346 P2P_EVENT_FALLBACK_TO_GO_NEG
5347 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005348 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5349 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5350 wpa_s->p2p_persistent_group, 0, 0, 0,
5351 wpa_s->p2p_go_intent,
5352 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005353 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005354 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005355 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005356 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005357 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005358 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005359 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005360 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005361 NULL, 0,
5362 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363 return;
5364 }
5365
5366 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5367 "try to join the group", join ? "" :
5368 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005369 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005370 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005371 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005372 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005373 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005374 }
5375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005376 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5377 wpa_s->pending_join_iface_addr);
5378 if (freq < 0 &&
5379 p2p_get_interface_addr(wpa_s->global->p2p,
5380 wpa_s->pending_join_dev_addr,
5381 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005382 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5383 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5385 "address for join from " MACSTR " to " MACSTR
5386 " based on newly discovered P2P peer entry",
5387 MAC2STR(wpa_s->pending_join_iface_addr),
5388 MAC2STR(iface_addr));
5389 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5390 ETH_ALEN);
5391
5392 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5393 wpa_s->pending_join_iface_addr);
5394 }
5395 if (freq >= 0) {
5396 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5397 "from P2P peer table: %d MHz", freq);
5398 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005399 if (wpa_s->p2p_join_ssid_len) {
5400 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5401 MACSTR " and SSID %s",
5402 MAC2STR(wpa_s->pending_join_iface_addr),
5403 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5404 wpa_s->p2p_join_ssid_len));
5405 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5406 wpa_s->p2p_join_ssid,
5407 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005408 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005409 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5410 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5411 bss = wpa_bss_get_bssid_latest(wpa_s,
5412 wpa_s->pending_join_iface_addr);
5413 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005415 u8 dev_addr[ETH_ALEN];
5416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 freq = bss->freq;
5418 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005419 "from BSS table: %d MHz (SSID %s)", freq,
5420 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005421 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005422 dev_addr) == 0 &&
5423 os_memcmp(wpa_s->pending_join_dev_addr,
5424 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5425 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5426 ETH_ALEN) != 0) {
5427 wpa_printf(MSG_DEBUG,
5428 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5429 MAC2STR(dev_addr),
5430 MAC2STR(wpa_s->pending_join_dev_addr));
5431 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5432 ETH_ALEN);
5433 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434 }
5435 if (freq > 0) {
5436 u16 method;
5437
Dmitry Shmidt04949592012-07-19 12:16:46 -07005438 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005439 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005440 P2P_EVENT_GROUP_FORMATION_FAILURE
5441 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005442 wpas_notify_p2p_group_formation_failure(
5443 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005444 return;
5445 }
5446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5448 "prior to joining an existing group (GO " MACSTR
5449 " freq=%u MHz)",
5450 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5451 wpa_s->pending_pd_before_join = 1;
5452
5453 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454 case WPS_PIN_DISPLAY:
5455 method = WPS_CONFIG_KEYPAD;
5456 break;
5457 case WPS_PIN_KEYPAD:
5458 method = WPS_CONFIG_DISPLAY;
5459 break;
5460 case WPS_PBC:
5461 method = WPS_CONFIG_PUSHBUTTON;
5462 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005463 case WPS_P2PS:
5464 method = WPS_CONFIG_P2PS;
5465 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466 default:
5467 method = 0;
5468 break;
5469 }
5470
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005471 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5472 wpa_s->pending_join_dev_addr) ==
5473 method)) {
5474 /*
5475 * We have already performed provision discovery for
5476 * joining the group. Proceed directly to join
5477 * operation without duplicated provision discovery. */
5478 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5479 "with " MACSTR " already done - proceed to "
5480 "join",
5481 MAC2STR(wpa_s->pending_join_dev_addr));
5482 wpa_s->pending_pd_before_join = 0;
5483 goto start;
5484 }
5485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005487 wpa_s->pending_join_dev_addr,
5488 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005489 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5491 "Discovery Request before joining an "
5492 "existing group");
5493 wpa_s->pending_pd_before_join = 0;
5494 goto start;
5495 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 return;
5497 }
5498
5499 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5500 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5501 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5502 wpas_p2p_check_join_scan_limit(wpa_s);
5503 return;
5504
5505start:
5506 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005507 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5508 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509}
5510
5511
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005512static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5513 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515 int ret;
5516 struct wpa_driver_scan_params params;
5517 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005518 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005519 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005520 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521
5522 os_memset(&params, 0, sizeof(params));
5523
5524 /* P2P Wildcard SSID */
5525 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005526 if (ssid && ssid_len) {
5527 params.ssids[0].ssid = ssid;
5528 params.ssids[0].ssid_len = ssid_len;
5529 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5530 wpa_s->p2p_join_ssid_len = ssid_len;
5531 } else {
5532 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5533 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5534 wpa_s->p2p_join_ssid_len = 0;
5535 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536
5537 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5539 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5540 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541 if (wps_ie == NULL) {
5542 wpas_p2p_scan_res_join(wpa_s, NULL);
5543 return;
5544 }
5545
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005546 if (!freq) {
5547 int oper_freq;
5548 /*
5549 * If freq is not provided, check the operating freq of the GO
5550 * and use a single channel scan on if possible.
5551 */
5552 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5553 wpa_s->pending_join_iface_addr);
5554 if (oper_freq > 0)
5555 freq = oper_freq;
5556 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005557 if (freq > 0) {
5558 freqs[0] = freq;
5559 params.freqs = freqs;
Hai Shalomc1a21442022-02-04 13:43:00 -08005560 } else if (wpa_s->conf->p2p_6ghz_disable ||
5561 !is_p2p_allow_6ghz(wpa_s->global->p2p)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07005562 wpa_printf(MSG_DEBUG,
5563 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005564 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
Matthew Wang36173112022-11-07 21:48:44 -08005565 false, false);
Hai Shalom60840252021-02-19 19:02:11 -08005566 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
Matthew Wang36173112022-11-07 21:48:44 -08005567 false, false);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005568 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005570 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5571 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5572 if (ies == NULL) {
5573 wpabuf_free(wps_ie);
5574 wpas_p2p_scan_res_join(wpa_s, NULL);
5575 return;
5576 }
5577 wpabuf_put_buf(ies, wps_ie);
5578 wpabuf_free(wps_ie);
5579
5580 bands = wpas_get_bands(wpa_s, freqs);
5581 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5582
5583 params.p2p_probe = 1;
5584 params.extra_ies = wpabuf_head(ies);
5585 params.extra_ies_len = wpabuf_len(ies);
5586
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005587 if (wpa_s->clear_driver_scan_cache) {
5588 wpa_printf(MSG_DEBUG,
5589 "Request driver to clear scan cache due to local BSS flush");
5590 params.only_new_results = 1;
5591 }
5592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 /*
5594 * Run a scan to update BSS table and start Provision Discovery once
5595 * the new scan results become available.
5596 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005597 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005598 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005599 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005600 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005601 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005602 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005603 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005604 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606
5607 wpabuf_free(ies);
5608
5609 if (ret) {
5610 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5611 "try again later");
5612 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5613 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5614 wpas_p2p_check_join_scan_limit(wpa_s);
5615 }
5616}
5617
5618
Dmitry Shmidt04949592012-07-19 12:16:46 -07005619static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5620{
5621 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005622 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005623}
5624
5625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005627 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005628 int auto_join, int op_freq,
5629 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005630{
5631 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005632 MACSTR " dev " MACSTR " op_freq=%d)%s",
5633 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005634 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005635 if (ssid && ssid_len) {
5636 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5637 wpa_ssid_txt(ssid, ssid_len));
5638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005639
Dmitry Shmidt04949592012-07-19 12:16:46 -07005640 wpa_s->p2p_auto_pd = 0;
5641 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5643 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5644 wpa_s->pending_join_wps_method = wps_method;
5645
5646 /* Make sure we are not running find during connection establishment */
5647 wpas_p2p_stop_find(wpa_s);
5648
5649 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005650 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651 return 0;
5652}
5653
5654
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005655static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5656 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005657{
5658 struct wpa_supplicant *group;
5659 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005660 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661
5662 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5663 if (group == NULL)
5664 return -1;
5665 if (group != wpa_s) {
5666 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5667 sizeof(group->p2p_pin));
5668 group->p2p_wps_method = wpa_s->p2p_wps_method;
5669 }
5670
Hai Shalom74f70d42019-02-11 14:42:39 -08005671 /*
5672 * Need to mark the current interface for p2p_group_formation
5673 * when a separate group interface is not used. This is needed
5674 * to allow p2p_cancel stop a pending p2p_connect-join.
5675 * wpas_p2p_init_group_interface() addresses this for the case
5676 * where a separate group interface is used.
5677 */
5678 if (group == wpa_s->parent)
5679 wpa_s->global->p2p_group_formation = group;
5680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005682 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683
5684 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005685 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5687 ETH_ALEN);
5688 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005689 if (freq && ssid && ssid_len) {
5690 res.freq = freq;
5691 res.ssid_len = ssid_len;
5692 os_memcpy(res.ssid, ssid, ssid_len);
5693 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005694 if (ssid && ssid_len) {
5695 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5696 ssid, ssid_len);
5697 } else {
5698 bss = wpa_bss_get_bssid_latest(
5699 wpa_s, wpa_s->pending_join_iface_addr);
5700 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005701 if (bss) {
5702 res.freq = bss->freq;
5703 res.ssid_len = bss->ssid_len;
5704 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5705 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5706 bss->freq,
5707 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005708 } else if (ssid && ssid_len) {
5709 res.ssid_len = ssid_len;
5710 os_memcpy(res.ssid, ssid, ssid_len);
5711 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5712 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005713 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005714 }
5715
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005716 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5717 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5718 "starting client");
5719 wpa_drv_cancel_remain_on_channel(wpa_s);
5720 wpa_s->off_channel_freq = 0;
5721 wpa_s->roc_waiting_drv_freq = 0;
5722 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005723 wpas_start_wps_enrollee(group, &res);
5724
5725 /*
5726 * Allow a longer timeout for join-a-running-group than normal 15
5727 * second group formation timeout since the GO may not have authorized
5728 * our connection yet.
5729 */
5730 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5731 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5732 wpa_s, NULL);
5733
5734 return 0;
5735}
5736
5737
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005738static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005739 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005740 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005741 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005742{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005743 struct wpa_used_freq_data *freqs;
5744 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005745 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305746 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005747
5748 max_pref_freq = *num_pref_freq;
5749 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005750
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005751 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5752 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005753 if (!freqs)
5754 return -1;
5755
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005756 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5757 wpa_s->num_multichan_concurrent);
5758
5759 /*
5760 * It is possible that the total number of used frequencies is bigger
5761 * than the number of frequencies used for P2P, so get the system wide
5762 * number of unused frequencies.
5763 */
5764 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5765
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005766 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005767 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5768 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005769
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005770 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005771 int ret;
5772 if (go)
5773 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5774 else
5775 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5776 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005777 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005778 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5779 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005780 /*
5781 * If freq is a DFS channel and DFS is offloaded
5782 * to the driver, allow P2P GO to use it.
5783 */
5784 wpa_printf(MSG_DEBUG,
5785 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5786 freq);
5787 } else {
5788 wpa_printf(MSG_DEBUG,
5789 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5790 freq);
5791 res = -3;
5792 goto exit_free;
5793 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005794 }
5795
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005796 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005797 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005798 freq_in_use = 1;
5799 }
5800
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005801 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005802 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5803 freq);
5804 res = -2;
5805 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005806 }
5807 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5808 "requested channel (%u MHz)", freq);
5809 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005810 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005811 }
5812
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005813 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005814
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305815 if (*pref_freq == 0) {
5816 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005817 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305818 while (i < wpa_s->conf->num_p2p_pref_chan) {
5819 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5820 wpa_s->conf->p2p_pref_chan[i].op_class,
5821 wpa_s->conf->p2p_pref_chan[i].chan);
5822
5823 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5824 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5825 best_freq = p2p_pref_freq;
5826 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5827 "from P2P preferred channel list", best_freq);
5828 break;
5829 } else {
5830 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5831 "frequency (%u MHz) ", p2p_pref_freq);
5832 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005833 i++;
5834 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305835 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305836 enum wpa_driver_if_type iface_type;
5837
5838 if (go)
5839 iface_type = WPA_IF_P2P_GO;
5840 else
5841 iface_type = WPA_IF_P2P_CLIENT;
5842
5843 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5844 best_freq, go);
5845
5846 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5847 &max_pref_freq,
5848 pref_freq_list);
5849 if (!res && max_pref_freq > 0) {
5850 *num_pref_freq = max_pref_freq;
5851 i = 0;
5852 while (i < *num_pref_freq &&
5853 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005854 pref_freq_list[i].freq) ||
5855 wpas_p2p_disallowed_freq(
5856 wpa_s->global,
5857 pref_freq_list[i].freq) ||
5858 !p2p_pref_freq_allowed(&pref_freq_list[i],
5859 go))) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305860 wpa_printf(MSG_DEBUG,
5861 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00005862 i, pref_freq_list[i].freq);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305863 i++;
5864 }
5865 if (i != *num_pref_freq) {
Sunil8cd6f4d2022-06-28 18:40:46 +00005866 best_freq = pref_freq_list[i].freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305867 wpa_printf(MSG_DEBUG,
5868 "P2P: Using preferred_freq_list[%d]=%d",
5869 i, best_freq);
5870 } else {
5871 wpa_printf(MSG_DEBUG,
5872 "P2P: All driver preferred frequencies are "
5873 "disallowed for P2P use");
5874 *num_pref_freq = 0;
5875 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005876 } else {
5877 wpa_printf(MSG_DEBUG,
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305878 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005879 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005880 }
5881 }
5882
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005883 /* We have a candidate frequency to use */
5884 if (best_freq > 0) {
5885 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005886 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005887 best_freq);
5888 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005889 } else {
5890 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 -07005891 best_freq);
5892 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005893 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005894 } else if (num_unused > 0) {
5895 wpa_printf(MSG_DEBUG,
5896 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5897 *force_freq = 0;
5898 } else {
5899 wpa_printf(MSG_DEBUG,
5900 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5901 res = -2;
5902 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005903 }
5904
5905exit_ok:
5906 res = 0;
5907exit_free:
5908 os_free(freqs);
5909 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005910}
5911
5912
Hai Shalomc1a21442022-02-04 13:43:00 -08005913static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5914 const u8 *peer_addr)
5915{
5916 if (wpa_s->conf->p2p_6ghz_disable ||
5917 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5918 HOSTAPD_MODE_IEEE80211A, true))
5919 return false;
5920
5921 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5922 return false;
5923 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5924 return false;
5925
5926 return true;
5927}
5928
5929
5930static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5931 const u8 *peer_addr, bool allow_6ghz, int freq)
5932{
5933 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5934 wpa_printf(MSG_DEBUG,
5935 "P2P: Allow connection on 6 GHz channels");
5936 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5937 } else {
5938 if (is_6ghz_freq(freq))
5939 return -2;
5940 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5941 }
5942
5943 return 0;
5944}
5945
5946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005947/**
5948 * wpas_p2p_connect - Request P2P Group Formation to be started
5949 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5950 * @peer_addr: Address of the peer P2P Device
5951 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5952 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005953 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005954 * @join: Whether to join an existing group (as a client) instead of starting
5955 * Group Owner negotiation; @peer_addr is BSSID in that case
5956 * @auth: Whether to only authorize the connection instead of doing that and
5957 * initiating Group Owner negotiation
5958 * @go_intent: GO Intent or -1 to use default
5959 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005960 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005961 * @persistent_id: Persistent group credentials to use for forcing GO
5962 * parameters or -1 to generate new values (SSID/passphrase)
5963 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5964 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005965 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005966 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005967 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005968 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005969 * @group_ssid: Specific Group SSID for join or %NULL if not set
5970 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08005971 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5973 * failure, -2 on failure due to channel not currently available,
5974 * -3 if forced channel is not supported
5975 */
5976int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5977 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005978 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005979 int go_intent, int freq, unsigned int vht_center_freq2,
5980 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005981 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005982 const u8 *group_ssid, size_t group_ssid_len,
5983 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005985 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005986 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005988 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005989 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00005990 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
5991 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992
5993 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5994 return -1;
5995
Dmitry Shmidt04949592012-07-19 12:16:46 -07005996 if (persistent_id >= 0) {
5997 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5998 if (ssid == NULL || ssid->disabled != 2 ||
5999 ssid->mode != WPAS_MODE_P2P_GO)
6000 return -1;
6001 }
6002
Hai Shalomc1a21442022-02-04 13:43:00 -08006003 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006004 return -2;
6005
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006006 os_free(wpa_s->global->add_psk);
6007 wpa_s->global->add_psk = NULL;
6008
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006009 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006010 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006011 wpa_s->global->pending_p2ps_group_freq = 0;
6012 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006014 if (go_intent < 0)
6015 go_intent = wpa_s->conf->p2p_go_intent;
6016
6017 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006018 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006019
6020 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006021 wpa_s->p2p_persistent_group = !!persistent_group;
6022 wpa_s->p2p_persistent_id = persistent_id;
6023 wpa_s->p2p_go_intent = go_intent;
6024 wpa_s->p2p_connect_freq = freq;
6025 wpa_s->p2p_fallback_to_go_neg = 0;
6026 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006027 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006028 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006029 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6030 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006031 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006032 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006033
6034 if (pin)
6035 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6036 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006037 if (wps_generate_pin((unsigned int *) &ret) < 0)
6038 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006039 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6040 "%08d", ret);
6041 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6042 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6044 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006045 } else if (wps_method == WPS_P2PS) {
6046 /* Force the P2Ps default PIN to be used */
6047 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006048 } else
6049 wpa_s->p2p_pin[0] = '\0';
6050
Dmitry Shmidt04949592012-07-19 12:16:46 -07006051 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006052 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6053 if (auth) {
6054 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6055 "connect a running group from " MACSTR,
6056 MAC2STR(peer_addr));
6057 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6058 return ret;
6059 }
6060 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6061 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6062 iface_addr) < 0) {
6063 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6064 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6065 dev_addr);
6066 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006067 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006068 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006069 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6070 "%ld.%06ld",
6071 wpa_s->p2p_auto_started.sec,
6072 wpa_s->p2p_auto_started.usec);
6073 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006074 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006075 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006076 auto_join, freq,
6077 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006078 return -1;
6079 return ret;
6080 }
6081
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006082 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006083 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006084 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006085 if (res)
6086 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006087 wpas_p2p_set_own_freq_preference(wpa_s,
6088 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006090 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006092 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6093
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006094 if (wpa_s->create_p2p_iface) {
6095 /* Prepare to add a new interface for the group */
6096 iftype = WPA_IF_P2P_GROUP;
6097 if (go_intent == 15)
6098 iftype = WPA_IF_P2P_GO;
6099 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6100 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6101 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006105 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006106 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006107 if (wpa_s->p2p_mgmt)
6108 if_addr = wpa_s->parent->own_addr;
6109 else
6110 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006111 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113
6114 if (auth) {
6115 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006116 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006117 force_freq, persistent_group, ssid,
6118 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006119 return -1;
6120 return ret;
6121 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006122
6123 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6124 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006125 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006126 if (wpa_s->create_p2p_iface)
6127 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006128 return -1;
6129 }
6130 return ret;
6131}
6132
6133
6134/**
6135 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6136 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6137 * @freq: Frequency of the channel in MHz
6138 * @duration: Duration of the stay on the channel in milliseconds
6139 *
6140 * This callback is called when the driver indicates that it has started the
6141 * requested remain-on-channel duration.
6142 */
6143void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6144 unsigned int freq, unsigned int duration)
6145{
6146 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6147 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006148 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)",
6149 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6150 wpa_s->roc_waiting_drv_freq, freq, duration);
6151 if (wpa_s->off_channel_freq &&
6152 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6154 wpa_s->pending_listen_duration);
6155 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006156 } else {
6157 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6158 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6159 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006160 }
6161}
6162
6163
Jithu Jance57cea1a2014-08-20 10:22:04 -07006164int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006165{
6166 /* Limit maximum Listen state time based on driver limitation. */
6167 if (timeout > wpa_s->max_remain_on_chan)
6168 timeout = wpa_s->max_remain_on_chan;
6169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 return p2p_listen(wpa_s->global->p2p, timeout);
6171}
6172
6173
6174/**
6175 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6176 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6177 * @freq: Frequency of the channel in MHz
6178 *
6179 * This callback is called when the driver indicates that a remain-on-channel
6180 * operation has been completed, i.e., the duration on the requested channel
6181 * has timed out.
6182 */
6183void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6184 unsigned int freq)
6185{
6186 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6187 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006188 wpa_s->global->p2p_long_listen,
6189 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006190 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6192 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006193 if (wpa_s->global->p2p_long_listen > 0)
6194 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006195 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6196 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006197 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006199 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006201 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006202 } else {
6203 /*
6204 * When listen duration is over, stop listen & update p2p_state
6205 * to IDLE.
6206 */
6207 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006208 }
6209}
6210
6211
6212/**
6213 * wpas_p2p_group_remove - Remove a P2P group
6214 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6215 * @ifname: Network interface name of the group interface or "*" to remove all
6216 * groups
6217 * Returns: 0 on success, -1 on failure
6218 *
6219 * This function is used to remove a P2P group. This can be used to disconnect
6220 * from a group in which the local end is a P2P Client or to end a P2P Group in
6221 * case the local end is the Group Owner. If a virtual network interface was
6222 * created for this group, that interface will be removed. Otherwise, only the
6223 * configured P2P group network will be removed from the interface.
6224 */
6225int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6226{
6227 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006228 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229
6230 if (os_strcmp(ifname, "*") == 0) {
6231 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006232 bool calling_wpa_s_group_removed = false;
6233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006234 wpa_s = global->ifaces;
6235 while (wpa_s) {
6236 prev = wpa_s;
6237 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006238 if (prev->p2p_group_interface !=
6239 NOT_P2P_GROUP_INTERFACE ||
6240 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006241 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006242 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006243 if (prev == calling_wpa_s)
6244 calling_wpa_s_group_removed = true;
6245 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006247
6248 if (!calling_wpa_s_group_removed &&
6249 (calling_wpa_s->p2p_group_interface !=
6250 NOT_P2P_GROUP_INTERFACE ||
6251 (calling_wpa_s->current_ssid &&
6252 calling_wpa_s->current_ssid->p2p_group))) {
6253 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6254 wpas_p2p_disconnect_safely(calling_wpa_s,
6255 calling_wpa_s);
6256 }
6257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006258 return 0;
6259 }
6260
6261 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6262 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6263 break;
6264 }
6265
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006266 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006267}
6268
6269
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006270static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6271{
6272 unsigned int r;
6273
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006274 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6275 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006276 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006277 int res;
6278
6279 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6280 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006281 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6282 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6283
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006284 if (!res && size > 0) {
6285 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006286 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006287 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006288 pref_freq_list[i].freq) ||
6289 wpas_p2p_disallowed_freq(
6290 wpa_s->global,
6291 pref_freq_list[i].freq) ||
6292 !p2p_pref_freq_allowed(&pref_freq_list[i],
6293 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006294 wpa_printf(MSG_DEBUG,
6295 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006296 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006297 i++;
6298 }
6299 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006300 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006301 wpa_printf(MSG_DEBUG,
6302 "P2P: Using preferred_freq_list[%d]=%d",
6303 i, freq);
6304 } else {
6305 wpa_printf(MSG_DEBUG,
6306 "P2P: All driver preferred frequencies are disallowed for P2P use");
6307 }
6308 } else {
6309 wpa_printf(MSG_DEBUG,
6310 "P2P: No preferred frequency list available");
6311 }
6312 }
6313
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006314 if (freq == 2) {
6315 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6316 "band");
6317 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006318 p2p_supported_freq_go(wpa_s->global->p2p,
6319 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006320 freq = wpa_s->best_24_freq;
6321 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6322 "channel: %d MHz", freq);
6323 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006324 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6325 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006326 int possible_2g_freqs[] = {
6327 /* operating class 81 */
6328 2412, 2437, 2462,
6329 };
6330 int possible_2g_freqs_num =
6331 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6332 int i;
6333 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6334 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6335 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6336 break;
6337 }
6338 }
6339
6340 if (i >= possible_2g_freqs_num) {
6341 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6342 "2.4 GHz channel for P2P group");
6343 return -1;
6344 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006345 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6346 "channel: %d MHz", freq);
6347 }
6348 }
6349
6350 if (freq == 5) {
6351 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6352 "band");
6353 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006354 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006355 wpa_s->best_5_freq)) {
6356 freq = wpa_s->best_5_freq;
6357 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6358 "channel: %d MHz", freq);
6359 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006360 const int freqs[] = {
6361 /* operating class 115 */
6362 5180, 5200, 5220, 5240,
6363 /* operating class 124 */
6364 5745, 5765, 5785, 5805,
6365 };
6366 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6367
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006368 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6369 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006370
6371 /*
6372 * most of 5G channels are DFS, only operating class 115 and 124
6373 * are available possibly, randomly pick a start to check them.
6374 */
6375 int possible_5g_freqs[] = {
6376 /* operating class 115 */
6377 5180, 5200, 5220, 5240,
6378 /* operating class 124 */
6379 5745, 5765, 5785, 5805,
6380 };
6381 int possible_5g_freqs_num =
6382 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6383
Jimmy Chen64b90632020-09-29 17:27:34 +08006384 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6385 if (p2p_supported_freq_go(
6386 wpa_s->global->p2p,
6387 possible_5g_freqs[r % possible_5g_freqs_num])) {
6388 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6389 break;
6390 }
6391 }
6392
6393 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006394 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6395 "5 GHz channel for P2P group");
6396 return -1;
6397 }
6398 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6399 "channel: %d MHz", freq);
6400 }
6401 }
6402
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006403 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006404 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006405 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6406 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006407 /*
6408 * If freq is a DFS channel and DFS is offloaded to the
6409 * driver, allow P2P GO to use it.
6410 */
6411 wpa_printf(MSG_DEBUG, "P2P: "
6412 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6413 __func__, freq);
6414 return freq;
6415 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006416 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6417 "(%u MHz) is not supported for P2P uses",
6418 freq);
6419 return -1;
6420 }
6421
6422 return freq;
6423}
6424
6425
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006426static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6427 const struct p2p_channels *channels,
6428 int freq)
6429{
6430 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6431 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6432 freq_included(wpa_s, channels, freq))
6433 return 1;
6434 return 0;
6435}
6436
6437
6438static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6439 struct p2p_go_neg_results *params,
6440 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006441{
6442 unsigned int i, r;
6443
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006444 /* try all channels in operating class 115 */
6445 for (i = 0; i < 4; i++) {
6446 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006447 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006448 goto out;
6449 }
6450
6451 /* try all channels in operating class 124 */
6452 for (i = 0; i < 4; i++) {
6453 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006454 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006455 goto out;
6456 }
6457
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006458 /* try social channel class 180 channel 2 */
6459 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006460 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006461 goto out;
6462
6463 /* try all channels in reg. class 180 */
6464 for (i = 0; i < 4; i++) {
6465 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006466 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006467 goto out;
6468 }
6469
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006470 /* try some random selection of the social channels */
6471 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6472 return;
6473
6474 for (i = 0; i < 3; i++) {
6475 params->freq = 2412 + ((r + i) % 3) * 25;
6476 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6477 goto out;
6478 }
6479
6480 /* try all other channels in operating class 81 */
6481 for (i = 0; i < 11; i++) {
6482 params->freq = 2412 + i * 5;
6483
6484 /* skip social channels; covered in the previous loop */
6485 if (params->freq == 2412 ||
6486 params->freq == 2437 ||
6487 params->freq == 2462)
6488 continue;
6489
6490 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6491 goto out;
6492 }
6493
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006494 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006495 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006496 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006497out:
6498 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6499 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006500}
6501
6502
Roshan Pius3a1667e2018-07-03 15:17:14 -07006503static int wpas_same_band(int freq1, int freq2)
6504{
6505 enum hostapd_hw_mode mode1, mode2;
6506 u8 chan1, chan2;
6507
6508 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6509 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6510 if (mode1 == NUM_HOSTAPD_MODES)
6511 return 0;
6512 return mode1 == mode2;
6513}
6514
6515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006516static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6517 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006518 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006519 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006520 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006521 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006522{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006523 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006524 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006525 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006526 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006527 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528
6529 os_memset(params, 0, sizeof(*params));
6530 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006531 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006532 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006533 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006534 params->max_oper_chwidth = max_oper_chwidth;
6535 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006536 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006537
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006538 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6539 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006540 if (!freqs)
6541 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006543 num = get_shared_radio_freqs_data(wpa_s, freqs,
6544 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006545
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006546 if (wpa_s->current_ssid &&
6547 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6548 wpa_s->wpa_state == WPA_COMPLETED) {
6549 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6550 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006551
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006552 /*
6553 * If the frequency selection is done for an active P2P GO that
6554 * is not sharing a frequency, allow to select a new frequency
6555 * even if there are no unused frequencies as we are about to
6556 * move the P2P GO so its frequency can be re-used.
6557 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006558 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006559 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6560 freqs[i].flags == 0) {
6561 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006562 break;
6563 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006564 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006565 }
6566
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006567 /* Try to use EDMG channel */
6568 if (params->edmg) {
6569 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6570 goto success;
6571 params->edmg = 0;
6572 }
6573
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006574 /* try using the forced freq */
6575 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006576 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6577 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006578 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006579 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006580 freq);
6581 goto fail;
6582 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006583 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6584 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006585 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6586 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006587 /*
6588 * If freq is a DFS channel and DFS is offloaded
6589 * to the driver, allow P2P GO to use it.
6590 */
6591 wpa_printf(MSG_DEBUG,
6592 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6593 __func__, freq);
6594 } else {
6595 wpa_printf(MSG_DEBUG,
6596 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6597 freq);
6598 goto fail;
6599 }
6600 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006601
6602 for (i = 0; i < num; i++) {
6603 if (freqs[i].freq == freq) {
6604 wpa_printf(MSG_DEBUG,
6605 "P2P: forced freq (%d MHz) is also shared",
6606 freq);
6607 params->freq = freq;
6608 goto success;
6609 }
6610 }
6611
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006612 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006613 wpa_printf(MSG_DEBUG,
6614 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6615 freq);
6616 goto fail;
6617 }
6618
6619 wpa_printf(MSG_DEBUG,
6620 "P2P: force GO freq (%d MHz) on a free channel",
6621 freq);
6622 params->freq = freq;
6623 goto success;
6624 }
6625
6626 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006627 if (num &&
6628 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006629 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6630 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6631 wpa_printf(MSG_DEBUG,
6632 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006633 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006634 params->freq = cand;
6635 goto success;
6636 }
6637
6638 /* try using one of the shared freqs */
6639 for (i = 0; i < num; i++) {
6640 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6641 freqs[i].freq)) {
6642 wpa_printf(MSG_DEBUG,
6643 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006644 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006645 params->freq = freqs[i].freq;
6646 goto success;
6647 }
6648 }
6649 }
6650
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006651 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006652 wpa_printf(MSG_DEBUG,
6653 "P2P: Cannot force GO on any of the channels we are already using");
6654 goto fail;
6655 }
6656
6657 /* try using the setting from the configuration file */
6658 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6659 wpa_s->conf->p2p_oper_channel >= 1 &&
6660 wpa_s->conf->p2p_oper_channel <= 11 &&
6661 wpas_p2p_supported_freq_go(
6662 wpa_s, channels,
6663 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6664 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6665 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6666 "frequency %d MHz", params->freq);
6667 goto success;
6668 }
6669
6670 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6671 wpa_s->conf->p2p_oper_reg_class == 116 ||
6672 wpa_s->conf->p2p_oper_reg_class == 117 ||
6673 wpa_s->conf->p2p_oper_reg_class == 124 ||
6674 wpa_s->conf->p2p_oper_reg_class == 125 ||
6675 wpa_s->conf->p2p_oper_reg_class == 126 ||
6676 wpa_s->conf->p2p_oper_reg_class == 127) &&
6677 wpas_p2p_supported_freq_go(wpa_s, channels,
6678 5000 +
6679 5 * wpa_s->conf->p2p_oper_channel)) {
6680 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6681 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6682 "frequency %d MHz", params->freq);
6683 goto success;
6684 }
6685
6686 /* Try using best channels */
6687 if (wpa_s->conf->p2p_oper_channel == 0 &&
6688 wpa_s->best_overall_freq > 0 &&
6689 wpas_p2p_supported_freq_go(wpa_s, channels,
6690 wpa_s->best_overall_freq)) {
6691 params->freq = wpa_s->best_overall_freq;
6692 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6693 "channel %d MHz", params->freq);
6694 goto success;
6695 }
6696
6697 if (wpa_s->conf->p2p_oper_channel == 0 &&
6698 wpa_s->best_24_freq > 0 &&
6699 wpas_p2p_supported_freq_go(wpa_s, channels,
6700 wpa_s->best_24_freq)) {
6701 params->freq = wpa_s->best_24_freq;
6702 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6703 "channel %d MHz", params->freq);
6704 goto success;
6705 }
6706
6707 if (wpa_s->conf->p2p_oper_channel == 0 &&
6708 wpa_s->best_5_freq > 0 &&
6709 wpas_p2p_supported_freq_go(wpa_s, channels,
6710 wpa_s->best_5_freq)) {
6711 params->freq = wpa_s->best_5_freq;
6712 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6713 "channel %d MHz", params->freq);
6714 goto success;
6715 }
6716
6717 /* try using preferred channels */
6718 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6719 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6720 params->freq = cand;
6721 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6722 "channels", params->freq);
6723 goto success;
6724 }
6725
Roshan Pius3a1667e2018-07-03 15:17:14 -07006726 /* Try using a channel that allows VHT to be used with 80 MHz */
6727 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6728 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6729 enum hostapd_hw_mode mode;
6730 struct hostapd_hw_modes *hwmode;
6731 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006732 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006733
6734 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006735 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006736 mode = ieee80211_freq_to_chan(cand, &chan);
6737 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006738 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006739 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006740 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6741 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006742 continue;
6743 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6744 params->freq = cand;
6745 wpa_printf(MSG_DEBUG,
6746 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6747 params->freq);
6748 goto success;
6749 }
6750 }
6751 }
6752
6753 /* Try using a channel that allows HT to be used with 40 MHz on the same
6754 * band so that CSA can be used */
6755 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6756 wpa_s->p2p_group_common_freqs) {
6757 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6758 enum hostapd_hw_mode mode;
6759 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006760 u8 chan, op_class;
6761 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006762
Hai Shalomc1a21442022-02-04 13:43:00 -08006763 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006764 cand = wpa_s->p2p_group_common_freqs[i];
6765 mode = ieee80211_freq_to_chan(cand, &chan);
6766 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006767 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006768 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6769 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006770 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006771 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006772 if (is_6ghz &&
6773 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6774 BW40) == ALLOWED)
6775 supported = true;
6776
6777 if (!is_6ghz &&
6778 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006779 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6780 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006781 wpas_p2p_verify_channel(
6782 wpa_s, hwmode, op_class, chan,
6783 BW40MINUS) == ALLOWED)
6784 supported = true;
6785
6786 if (!supported && !is_6ghz &&
6787 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006788 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6789 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006790 wpas_p2p_verify_channel(
6791 wpa_s, hwmode, op_class, chan,
6792 BW40PLUS) == ALLOWED)
6793 supported = true;
6794
6795 if (!supported)
6796 continue;
6797
Roshan Pius3a1667e2018-07-03 15:17:14 -07006798 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6799 params->freq = cand;
6800 wpa_printf(MSG_DEBUG,
6801 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6802 params->freq);
6803 goto success;
6804 }
6805 }
6806 }
6807
6808 /* Try using one of the group common freqs on the same band so that CSA
6809 * can be used */
6810 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6811 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6812 cand = wpa_s->p2p_group_common_freqs[i];
6813 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6814 cand))
6815 continue;
6816 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6817 params->freq = cand;
6818 wpa_printf(MSG_DEBUG,
6819 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6820 params->freq);
6821 goto success;
6822 }
6823 }
6824 }
6825
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006826 /* Try using one of the group common freqs */
6827 if (wpa_s->p2p_group_common_freqs) {
6828 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6829 cand = wpa_s->p2p_group_common_freqs[i];
6830 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6831 params->freq = cand;
6832 wpa_printf(MSG_DEBUG,
6833 "P2P: Use freq %d MHz common with the peer",
6834 params->freq);
6835 goto success;
6836 }
6837 }
6838 }
6839
6840 /* no preference, select some channel */
6841 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6842
6843 if (params->freq == 0) {
6844 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6845 goto fail;
6846 }
6847
6848success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006849 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006850 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006851fail:
6852 os_free(freqs);
6853 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006854}
6855
6856
6857static struct wpa_supplicant *
6858wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6859 int go)
6860{
6861 struct wpa_supplicant *group_wpa_s;
6862
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006863 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006864 if (wpa_s->p2p_mgmt) {
6865 /*
6866 * We may be called on the p2p_dev interface which
6867 * cannot be used for group operations, so always use
6868 * the primary interface.
6869 */
6870 wpa_s->parent->p2pdev = wpa_s;
6871 wpa_s = wpa_s->parent;
6872 }
6873 wpa_dbg(wpa_s, MSG_DEBUG,
6874 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006875 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006876 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006877 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006878 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006879 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006880
6881 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006882 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006883 wpa_msg_global(wpa_s, MSG_ERROR,
6884 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006885 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006887 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6888 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006889 wpa_msg_global(wpa_s, MSG_ERROR,
6890 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006891 wpas_p2p_remove_pending_group_interface(wpa_s);
6892 return NULL;
6893 }
6894
Roshan Pius3a1667e2018-07-03 15:17:14 -07006895 if (go && wpa_s->p2p_go_do_acs) {
6896 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6897 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6898 wpa_s->p2p_go_do_acs = 0;
6899 }
6900
Hai Shalomc1a21442022-02-04 13:43:00 -08006901 if (go && wpa_s->p2p_go_allow_dfs) {
6902 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6903 wpa_s->p2p_go_allow_dfs = 0;
6904 }
6905
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006906 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6907 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006908 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006909 return group_wpa_s;
6910}
6911
6912
6913/**
6914 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6915 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6916 * @persistent_group: Whether to create a persistent group
6917 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006918 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006919 * @ht40: Start GO with 40 MHz channel width
6920 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006921 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006922 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006923 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 * Returns: 0 on success, -1 on failure
6925 *
6926 * This function creates a new P2P group with the local end as the Group Owner,
6927 * i.e., without using Group Owner Negotiation.
6928 */
6929int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006930 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006931 int max_oper_chwidth, int he, int edmg,
6932 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933{
6934 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006935 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936
6937 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6938 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006939 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6940 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006942 os_free(wpa_s->global->add_psk);
6943 wpa_s->global->add_psk = NULL;
6944
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006945 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006946 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006947 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006948
Roshan Pius3a1667e2018-07-03 15:17:14 -07006949 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00006950 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
6951 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006952 return -1;
6953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006954
Sunil Ravi77a0f092022-10-03 00:53:41 +00006955 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006956 ht40, vht, max_oper_chwidth, he, edmg,
6957 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006958 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006960 p2p_go_params(wpa_s->global->p2p, &params);
6961 params.persistent_group = persistent_group;
6962
6963 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6964 if (wpa_s == NULL)
6965 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006966 if (freq > 0)
6967 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006968 wpas_start_wps_go(wpa_s, &params, 0);
6969
6970 return 0;
6971}
6972
6973
6974static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006975 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00006976 int freq, int force_scan, int retry_limit,
6977 bool force_go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006978{
6979 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07006980 int other_iface_found = 0;
6981 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006982
6983 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6984 if (wpa_s == NULL)
6985 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006986 if (force_scan)
6987 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006988 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006989
6990 wpa_supplicant_ap_deinit(wpa_s);
6991
6992 ssid = wpa_config_add_network(wpa_s->conf);
6993 if (ssid == NULL)
6994 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006995 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006996 wpa_config_set_network_defaults(ssid);
6997 ssid->temporary = 1;
6998 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006999 ssid->pbss = params->pbss;
7000 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7001 WPA_CIPHER_CCMP;
7002 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007003 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
7004 ssid->ssid = os_malloc(params->ssid_len);
7005 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007006 wpa_config_remove_network(wpa_s->conf, ssid->id);
7007 return -1;
7008 }
7009 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7010 ssid->ssid_len = params->ssid_len;
7011 ssid->p2p_group = 1;
7012 ssid->export_keys = 1;
7013 if (params->psk_set) {
7014 os_memcpy(ssid->psk, params->psk, 32);
7015 ssid->psk_set = 1;
7016 }
7017 if (params->passphrase)
7018 ssid->passphrase = os_strdup(params->passphrase);
7019
Matthew Wang124e5f42022-12-29 07:23:06 +00007020 if (force_go_bssid && params->bssid_set) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007021 ssid->bssid_set = 1;
7022 os_memcpy(ssid->bssid, params->bssid, ETH_ALEN);
7023 }
7024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007025 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007026 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007027 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007028 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007029 wpa_s->p2p_go_group_formation_completed = 0;
7030 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007031
Matthew Wang9ae940b2022-09-14 21:25:34 -07007032 /*
7033 * Get latest scan results from driver in case cached scan results from
7034 * interfaces on the same wiphy allow us to skip the next scan by fast
7035 * associating. Also update the scan time to the most recent scan result
7036 * fetch time on the same radio so it reflects the actual time the last
7037 * scan result event occurred.
7038 */
7039 wpa_supplicant_update_scan_results(wpa_s);
7040 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7041 radio_list) {
7042 if (ifs == wpa_s)
7043 continue;
7044 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7045 &ifs->last_scan)) {
7046 other_iface_found = 1;
7047 wpa_s->last_scan.sec = ifs->last_scan.sec;
7048 wpa_s->last_scan.usec = ifs->last_scan.usec;
7049 }
7050 }
7051
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007052 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007053 NULL);
7054 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7055 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007056 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007057 wpa_supplicant_select_network(wpa_s, ssid);
7058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007059 return 0;
7060}
7061
7062
7063int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7064 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007065 int force_freq, int neg_freq,
7066 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007067 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007068 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007069 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007070 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00007071 bool allow_6ghz, int retry_limit,
7072 bool force_go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007073{
7074 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007075 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007076
7077 if (ssid->disabled != 2 || ssid->ssid == NULL)
7078 return -1;
7079
7080 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7081 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7082 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7083 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007084 if (go == 0 &&
7085 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007086 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007087 /*
7088 * This can happen if Invitation Response frame was lost
7089 * and the peer (GO of a persistent group) tries to
7090 * invite us again. Reschedule the timeout to avoid
7091 * terminating the wait for the connection too early
7092 * since we now know that the peer is still trying to
7093 * invite us instead of having already started the GO.
7094 */
7095 wpa_printf(MSG_DEBUG,
7096 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7097 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7098 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007099 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007100 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007101 return 0;
7102 }
7103
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007104 os_free(wpa_s->global->add_psk);
7105 wpa_s->global->add_psk = NULL;
7106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007108 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109
Dmitry Shmidt04949592012-07-19 12:16:46 -07007110 wpa_s->p2p_fallback_to_go_neg = 0;
7111
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007112 if (ssid->mode == WPAS_MODE_P2P_GO) {
7113 if (force_freq > 0) {
7114 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7115 if (freq < 0)
7116 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007117 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007118 } else {
7119 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7120 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007121 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007122 freq = 0;
7123 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007124 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007125 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007126 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007127 struct os_reltime now;
7128 struct wpa_bss *bss =
7129 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007130
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007131 os_get_reltime(&now);
7132 if (bss &&
7133 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007134 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007135 freq = bss->freq;
7136 else
7137 freq = 0;
7138 }
7139
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007140 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Matthew Wang124e5f42022-12-29 07:23:06 +00007141 force_scan, retry_limit,
7142 force_go_bssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007143 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007144 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007145 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007146
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007147 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007148 ht40, vht, max_oper_chwidth, he, edmg,
7149 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007150 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007151
7152 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007153 params.psk_set = ssid->psk_set;
7154 if (params.psk_set)
7155 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007156 if (ssid->passphrase) {
7157 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7158 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7159 "persistent group");
7160 return -1;
7161 }
7162 os_strlcpy(params.passphrase, ssid->passphrase,
7163 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7166 params.ssid_len = ssid->ssid_len;
7167 params.persistent_group = 1;
7168
7169 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7170 if (wpa_s == NULL)
7171 return -1;
7172
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007173 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7174
Dmitry Shmidt56052862013-10-04 10:23:25 -07007175 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007176 wpas_start_wps_go(wpa_s, &params, 0);
7177
7178 return 0;
7179}
7180
7181
7182static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7183 struct wpabuf *proberesp_ies)
7184{
7185 struct wpa_supplicant *wpa_s = ctx;
7186 if (wpa_s->ap_iface) {
7187 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007188 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7189 wpabuf_free(beacon_ies);
7190 wpabuf_free(proberesp_ies);
7191 return;
7192 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007193 if (beacon_ies) {
7194 wpabuf_free(hapd->p2p_beacon_ie);
7195 hapd->p2p_beacon_ie = beacon_ies;
7196 }
7197 wpabuf_free(hapd->p2p_probe_resp_ie);
7198 hapd->p2p_probe_resp_ie = proberesp_ies;
7199 } else {
7200 wpabuf_free(beacon_ies);
7201 wpabuf_free(proberesp_ies);
7202 }
7203 wpa_supplicant_ap_update_beacon(wpa_s);
7204}
7205
7206
7207static void wpas_p2p_idle_update(void *ctx, int idle)
7208{
7209 struct wpa_supplicant *wpa_s = ctx;
7210 if (!wpa_s->ap_iface)
7211 return;
7212 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007213 if (idle) {
7214 if (wpa_s->global->p2p_fail_on_wps_complete &&
7215 wpa_s->p2p_in_provisioning) {
7216 wpas_p2p_grpform_fail_after_wps(wpa_s);
7217 return;
7218 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007219 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007220 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007221 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7222}
7223
7224
7225struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007226 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007227{
7228 struct p2p_group *group;
7229 struct p2p_group_config *cfg;
7230
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007231 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7232 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233 return NULL;
7234
7235 cfg = os_zalloc(sizeof(*cfg));
7236 if (cfg == NULL)
7237 return NULL;
7238
Dmitry Shmidt04949592012-07-19 12:16:46 -07007239 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007240 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007241 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007242 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007243 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7244 if (wpa_s->max_stations &&
7245 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7246 cfg->max_clients = wpa_s->max_stations;
7247 else
7248 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007249 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7250 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007251 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007252 cfg->cb_ctx = wpa_s;
7253 cfg->ie_update = wpas_p2p_ie_update;
7254 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007255 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7256 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007257
7258 group = p2p_group_init(wpa_s->global->p2p, cfg);
7259 if (group == NULL)
7260 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007261 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007262 p2p_group_notif_formation_done(group);
7263 wpa_s->p2p_group = group;
7264 return group;
7265}
7266
7267
7268void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7269 int registrar)
7270{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007271 struct wpa_ssid *ssid = wpa_s->current_ssid;
7272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007273 if (!wpa_s->p2p_in_provisioning) {
7274 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7275 "provisioning not in progress");
7276 return;
7277 }
7278
Dmitry Shmidt04949592012-07-19 12:16:46 -07007279 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7280 u8 go_dev_addr[ETH_ALEN];
7281 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7282 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7283 ssid->ssid_len);
7284 /* Clear any stored provisioning info */
7285 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7286 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007287
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007288 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007289 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007290 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007291 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7292 /*
7293 * Use a separate timeout for initial data connection to
7294 * complete to allow the group to be removed automatically if
7295 * something goes wrong in this step before the P2P group idle
7296 * timeout mechanism is taken into use.
7297 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007298 wpa_dbg(wpa_s, MSG_DEBUG,
7299 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7300 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007301 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7302 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007303 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007304 /* Complete group formation on successful data connection. */
7305 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007306 } else if (ssid) {
7307 /*
7308 * Use a separate timeout for initial data connection to
7309 * complete to allow the group to be removed automatically if
7310 * the client does not complete data connection successfully.
7311 */
7312 wpa_dbg(wpa_s, MSG_DEBUG,
7313 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7314 P2P_MAX_INITIAL_CONN_WAIT_GO);
7315 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7316 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007317 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007318 /*
7319 * Complete group formation on first successful data connection
7320 */
7321 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323 if (wpa_s->global->p2p)
7324 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007325 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007326}
7327
7328
Jouni Malinen75ecf522011-06-27 15:19:46 -07007329void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7330 struct wps_event_fail *fail)
7331{
7332 if (!wpa_s->p2p_in_provisioning) {
7333 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7334 "provisioning not in progress");
7335 return;
7336 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007337
7338 if (wpa_s->go_params) {
7339 p2p_clear_provisioning_info(
7340 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007341 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007342 }
7343
Jouni Malinen75ecf522011-06-27 15:19:46 -07007344 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007345
7346 if (wpa_s == wpa_s->global->p2p_group_formation) {
7347 /*
7348 * Allow some time for the failed WPS negotiation exchange to
7349 * complete, but remove the group since group formation cannot
7350 * succeed after provisioning failure.
7351 */
7352 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7353 wpa_s->global->p2p_fail_on_wps_complete = 1;
7354 eloop_deplete_timeout(0, 50000,
7355 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007356 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007357 }
7358}
7359
7360
7361int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7362{
7363 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7364 !wpa_s->p2p_in_provisioning)
7365 return 0;
7366
7367 wpas_p2p_grpform_fail_after_wps(wpa_s);
7368
7369 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007370}
7371
7372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007373int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007374 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007375 enum wpas_p2p_prov_disc_use use,
7376 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007377{
7378 u16 config_methods;
7379
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007380 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007381 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007382 wpa_s->p2p_fallback_to_go_neg = 0;
7383 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007384 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7385 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007386 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7387 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7388
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007389 wpa_printf(MSG_DEBUG,
7390 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7391 __func__, p2ps_prov->conncap,
7392 p2ps_prov->adv_id, p2ps_prov->conncap,
7393 p2ps_prov->status, p2ps_prov->info);
7394
7395 config_methods = 0;
7396 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007397 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007398 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007399 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007400 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7401 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007402 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007403 else {
7404 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007405 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007406 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007407 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408
Dmitry Shmidt04949592012-07-19 12:16:46 -07007409 if (use == WPAS_P2P_PD_AUTO) {
7410 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7411 wpa_s->pending_pd_config_methods = config_methods;
7412 wpa_s->p2p_auto_pd = 1;
7413 wpa_s->p2p_auto_join = 0;
7414 wpa_s->pending_pd_before_join = 0;
7415 wpa_s->auto_pd_scan_retry = 0;
7416 wpas_p2p_stop_find(wpa_s);
7417 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007418 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007419 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7420 wpa_s->p2p_auto_started.sec,
7421 wpa_s->p2p_auto_started.usec);
7422 wpas_p2p_join_scan(wpa_s, NULL);
7423 return 0;
7424 }
7425
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007426 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7427 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007430
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007431 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007432 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007433 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007434}
7435
7436
7437int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7438 char *end)
7439{
7440 return p2p_scan_result_text(ies, ies_len, buf, end);
7441}
7442
7443
7444static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7445{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007446 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007447 return;
7448
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007449 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007450 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007451 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7452 wpa_s, NULL);
7453 offchannel_send_action_done(wpa_s);
7454 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007456 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7457 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007458 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007459}
7460
7461
7462int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7463 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007464 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007465 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007466 u8 seek_cnt, const char **seek_string, int freq,
7467 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007468{
7469 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007470 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471
Dmitry Shmidt04949592012-07-19 12:16:46 -07007472 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007473 wpa_s->p2p_in_provisioning) {
7474 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7475 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7476 " (P2P disabled)" : "",
7477 wpa_s->p2p_in_provisioning ?
7478 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007479 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007480 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007481
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007482 wpa_supplicant_cancel_sched_scan(wpa_s);
7483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007484 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007485 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007486 search_delay, seek_cnt, seek_string, freq,
7487 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007488}
7489
7490
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007491static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7492 struct wpa_scan_results *scan_res)
7493{
7494 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7495
7496 if (wpa_s->p2p_scan_work) {
7497 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7498 wpa_s->p2p_scan_work = NULL;
7499 radio_work_done(work);
7500 }
7501
7502 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7503 return;
7504
7505 /*
7506 * Indicate that results have been processed so that the P2P module can
7507 * continue pending tasks.
7508 */
Hai Shalom60840252021-02-19 19:02:11 -08007509 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007510}
7511
7512
7513static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007514{
7515 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007516 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007517 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7518 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007519
7520 if (wpa_s->global->p2p)
7521 p2p_stop_find(wpa_s->global->p2p);
7522
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007523 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7524 wpa_printf(MSG_DEBUG,
7525 "P2P: Do not consider the scan results after stop_find");
7526 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7527 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007528}
7529
7530
7531void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7532{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007533 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007534 if (!wpa_s->global->pending_group_iface_for_p2ps)
7535 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536}
7537
7538
7539static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7540{
7541 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007542 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543}
7544
7545
7546int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7547{
7548 int res;
7549
7550 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7551 return -1;
7552
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007553 if (wpa_s->p2p_lo_started) {
7554 wpa_printf(MSG_DEBUG,
7555 "P2P: Cannot start P2P listen, it is offloaded");
7556 return -1;
7557 }
7558
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007559 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007560 wpas_p2p_clear_pending_action_tx(wpa_s);
7561
7562 if (timeout == 0) {
7563 /*
7564 * This is a request for unlimited Listen state. However, at
7565 * least for now, this is mapped to a Listen state for one
7566 * hour.
7567 */
7568 timeout = 3600;
7569 }
7570 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007571 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007573 /*
7574 * Stop previous find/listen operation to avoid trying to request a new
7575 * remain-on-channel operation while the driver is still running the
7576 * previous one.
7577 */
7578 if (wpa_s->global->p2p)
7579 p2p_stop_find(wpa_s->global->p2p);
7580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7582 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007583 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007584 eloop_register_timeout(timeout, 0,
7585 wpas_p2p_long_listen_timeout,
7586 wpa_s, NULL);
7587 }
7588
7589 return res;
7590}
7591
7592
7593int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7594 u8 *buf, size_t len, int p2p_group)
7595{
7596 struct wpabuf *p2p_ie;
7597 int ret;
7598
7599 if (wpa_s->global->p2p_disabled)
7600 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007601 /*
7602 * Advertize mandatory cross connection capability even on
7603 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7604 */
7605 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007606 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007607 if (wpa_s->global->p2p == NULL)
7608 return -1;
7609 if (bss == NULL)
7610 return -1;
7611
7612 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7613 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7614 p2p_group, p2p_ie);
7615 wpabuf_free(p2p_ie);
7616
7617 return ret;
7618}
7619
7620
7621int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007622 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007623 const u8 *ie, size_t ie_len,
7624 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007625{
7626 if (wpa_s->global->p2p_disabled)
7627 return 0;
7628 if (wpa_s->global->p2p == NULL)
7629 return 0;
7630
Dmitry Shmidt04949592012-07-19 12:16:46 -07007631 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007632 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007633 case P2P_PREQ_NOT_P2P:
7634 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7635 ssi_signal);
7636 /* fall through */
7637 case P2P_PREQ_MALFORMED:
7638 case P2P_PREQ_NOT_LISTEN:
7639 case P2P_PREQ_NOT_PROCESSED:
7640 default: /* make gcc happy */
7641 return 0;
7642 case P2P_PREQ_PROCESSED:
7643 return 1;
7644 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007645}
7646
7647
7648void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7649 const u8 *sa, const u8 *bssid,
7650 u8 category, const u8 *data, size_t len, int freq)
7651{
7652 if (wpa_s->global->p2p_disabled)
7653 return;
7654 if (wpa_s->global->p2p == NULL)
7655 return;
7656
7657 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7658 freq);
7659}
7660
7661
7662void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7663{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007664 unsigned int bands;
7665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007666 if (wpa_s->global->p2p_disabled)
7667 return;
7668 if (wpa_s->global->p2p == NULL)
7669 return;
7670
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007671 bands = wpas_get_bands(wpa_s, NULL);
7672 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007673}
7674
7675
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007676static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007677{
7678 p2p_group_deinit(wpa_s->p2p_group);
7679 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007680
7681 wpa_s->ap_configured_cb = NULL;
7682 wpa_s->ap_configured_cb_ctx = NULL;
7683 wpa_s->ap_configured_cb_data = NULL;
7684 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007685}
7686
7687
7688int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7689{
Hai Shalomfdcde762020-04-02 11:19:20 -07007690 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007692 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7693 return -1;
7694
7695 return p2p_reject(wpa_s->global->p2p, addr);
7696}
7697
7698
7699/* Invite to reinvoke a persistent group */
7700int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007701 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007702 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007703 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007704{
7705 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007706 u8 *bssid = NULL;
7707 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007708 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007709 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007710 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7711 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007712
Hai Shalomc1a21442022-02-04 13:43:00 -08007713 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7714 return -1;
7715
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007716 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007717 if (peer_addr)
7718 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7719 else
7720 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007721
Jouni Malinen31be0a42012-08-31 21:20:51 +03007722 wpa_s->p2p_persistent_go_freq = freq;
7723 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007724 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007725 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007726 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7727 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007728 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007729 if (ssid->mode == WPAS_MODE_P2P_GO) {
7730 role = P2P_INVITE_ROLE_GO;
7731 if (peer_addr == NULL) {
7732 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7733 "address in invitation command");
7734 return -1;
7735 }
7736 if (wpas_p2p_create_iface(wpa_s)) {
7737 if (wpas_p2p_add_group_interface(wpa_s,
7738 WPA_IF_P2P_GO) < 0) {
7739 wpa_printf(MSG_ERROR, "P2P: Failed to "
7740 "allocate a new interface for the "
7741 "group");
7742 return -1;
7743 }
7744 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007745 } else if (wpa_s->p2p_mgmt)
7746 bssid = wpa_s->parent->own_addr;
7747 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007748 bssid = wpa_s->own_addr;
7749 } else {
7750 role = P2P_INVITE_ROLE_CLIENT;
7751 peer_addr = ssid->bssid;
7752 }
7753 wpa_s->pending_invite_ssid_id = ssid->id;
7754
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007755 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007756 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007757 role == P2P_INVITE_ROLE_GO,
7758 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007759 if (res)
7760 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007762 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7763 return -1;
7764
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007765 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7766
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007767 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7768 no_pref_freq_given && pref_freq > 0 &&
7769 wpa_s->num_multichan_concurrent > 1 &&
7770 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7771 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7772 pref_freq);
7773 pref_freq = 0;
7774 }
7775
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007776 /*
7777 * Stop any find/listen operations before invitation and possibly
7778 * connection establishment.
7779 */
7780 wpas_p2p_stop_find_oper(wpa_s);
7781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007782 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007783 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007784 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785}
7786
7787
7788/* Invite to join an active group */
7789int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007790 const u8 *peer_addr, const u8 *go_dev_addr,
7791 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792{
7793 struct wpa_global *global = wpa_s->global;
7794 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007795 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007796 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007797 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007798 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007799 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007800 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7801 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007802
Jouni Malinen31be0a42012-08-31 21:20:51 +03007803 wpa_s->p2p_persistent_go_freq = 0;
7804 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007805 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007806 wpa_s->p2p_go_vht_center_freq2 = 0;
7807 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007808 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007810 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7811 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7812 break;
7813 }
7814 if (wpa_s == NULL) {
7815 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7816 return -1;
7817 }
7818
7819 ssid = wpa_s->current_ssid;
7820 if (ssid == NULL) {
7821 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7822 "invitation");
7823 return -1;
7824 }
7825
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007826 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007827 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007828 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007829 ssid->ssid, ssid->ssid_len);
7830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007831 if (ssid->mode == WPAS_MODE_P2P_GO) {
7832 role = P2P_INVITE_ROLE_ACTIVE_GO;
7833 bssid = wpa_s->own_addr;
7834 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007835 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007836 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007837 } else {
7838 role = P2P_INVITE_ROLE_CLIENT;
7839 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7840 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7841 "invite to current group");
7842 return -1;
7843 }
7844 bssid = wpa_s->bssid;
7845 if (go_dev_addr == NULL &&
7846 !is_zero_ether_addr(wpa_s->go_dev_addr))
7847 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007848 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7849 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007850 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007851 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007853 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7854 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007855 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7856 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007857
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007858 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007859 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007860 role == P2P_INVITE_ROLE_ACTIVE_GO,
7861 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007862 if (res)
7863 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007864 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007866 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007867 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007868 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007869}
7870
7871
7872void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7873{
7874 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007875 u8 go_dev_addr[ETH_ALEN];
7876 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007877 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007878 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007879 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007880
Dmitry Shmidt04949592012-07-19 12:16:46 -07007881 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7882 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007883 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007884 }
7885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007886 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007887 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007888
7889 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007890 if (!wpa_s->p2p_go_group_formation_completed &&
7891 wpa_s->global->p2p_group_formation == wpa_s) {
7892 wpa_dbg(wpa_s, MSG_DEBUG,
7893 "P2P: Marking group formation completed on client on data connection");
7894 wpa_s->p2p_go_group_formation_completed = 1;
7895 wpa_s->global->p2p_group_formation = NULL;
7896 wpa_s->p2p_in_provisioning = 0;
7897 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00007898 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007899 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007901 os_memset(go_dev_addr, 0, ETH_ALEN);
7902 if (ssid->bssid_set)
7903 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7904 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7905 ssid->ssid_len);
7906 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7907
7908 if (wpa_s->global->p2p_group_formation == wpa_s)
7909 wpa_s->global->p2p_group_formation = NULL;
7910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007911 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7912 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007913
7914 ip_addr[0] = '\0';
7915 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007916 int res;
7917
7918 res = os_snprintf(ip_addr, sizeof(ip_addr),
7919 " ip_addr=%u.%u.%u.%u "
7920 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7921 ip[0], ip[1], ip[2], ip[3],
7922 ip[4], ip[5], ip[6], ip[7],
7923 ip[8], ip[9], ip[10], ip[11]);
7924 if (os_snprintf_error(sizeof(ip_addr), res))
7925 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007926 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007927 }
7928
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007929 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7930 ssid->passphrase == NULL && ssid->psk_set ?
7931 ssid->psk : NULL,
7932 ssid->passphrase, go_dev_addr, persistent,
7933 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007934
7935 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007936 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7937 ssid, go_dev_addr);
7938
Hai Shalom5f92bc92019-04-18 11:54:11 -07007939 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007940}
7941
7942
7943int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7944 u32 interval1, u32 duration2, u32 interval2)
7945{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007946 int ret;
7947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007948 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7949 return -1;
7950
7951 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7952 wpa_s->current_ssid == NULL ||
7953 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7954 return -1;
7955
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007956 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7957 wpa_s->own_addr, wpa_s->assoc_freq,
7958 duration1, interval1, duration2, interval2);
7959 if (ret == 0)
7960 wpa_s->waiting_presence_resp = 1;
7961
7962 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007963}
7964
7965
7966int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7967 unsigned int interval)
7968{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007969 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7970 return -1;
7971
7972 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7973}
7974
7975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007976static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7977{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007978 if (wpa_s->current_ssid == NULL) {
7979 /*
7980 * current_ssid can be cleared when P2P client interface gets
7981 * disconnected, so assume this interface was used as P2P
7982 * client.
7983 */
7984 return 1;
7985 }
7986 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007987 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7988}
7989
7990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007991static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7992{
7993 struct wpa_supplicant *wpa_s = eloop_ctx;
7994
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007995 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007996 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7997 "disabled");
7998 return;
7999 }
8000
Dmitry Shmidt04949592012-07-19 12:16:46 -07008001 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
8002 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008003 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008004}
8005
8006
8007static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
8008{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008009 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008010
Dmitry Shmidt04949592012-07-19 12:16:46 -07008011 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8012 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008014 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8015 return;
8016
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008017 timeout = wpa_s->conf->p2p_group_idle;
8018 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8019 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8020 timeout = P2P_MAX_CLIENT_IDLE;
8021
8022 if (timeout == 0)
8023 return;
8024
Dmitry Shmidt04949592012-07-19 12:16:46 -07008025 if (timeout < 0) {
8026 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8027 timeout = 0; /* special client mode no-timeout */
8028 else
8029 return;
8030 }
8031
8032 if (wpa_s->p2p_in_provisioning) {
8033 /*
8034 * Use the normal group formation timeout during the
8035 * provisioning phase to avoid terminating this process too
8036 * early due to group idle timeout.
8037 */
8038 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8039 "during provisioning");
8040 return;
8041 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308042
Dmitry Shmidt04949592012-07-19 12:16:46 -07008043 if (wpa_s->show_group_started) {
8044 /*
8045 * Use the normal group formation timeout between the end of
8046 * the provisioning phase and completion of 4-way handshake to
8047 * avoid terminating this process too early due to group idle
8048 * timeout.
8049 */
8050 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8051 "while waiting for initial 4-way handshake to "
8052 "complete");
8053 return;
8054 }
8055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008056 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008057 timeout);
8058 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8059 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008060}
8061
8062
Jouni Malinen2b89da82012-08-31 22:04:41 +03008063/* Returns 1 if the interface was removed */
8064int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8065 u16 reason_code, const u8 *ie, size_t ie_len,
8066 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008067{
8068 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008069 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008070
Dmitry Shmidt04949592012-07-19 12:16:46 -07008071 if (!locally_generated)
8072 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8073 ie_len);
8074
8075 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8076 wpa_s->current_ssid &&
8077 wpa_s->current_ssid->p2p_group &&
8078 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8079 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8080 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008081 if (wpas_p2p_group_delete(wpa_s,
8082 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8083 > 0)
8084 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008085 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008086
8087 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008088}
8089
8090
8091void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008092 u16 reason_code, const u8 *ie, size_t ie_len,
8093 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008094{
8095 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8096 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008097
Dmitry Shmidt04949592012-07-19 12:16:46 -07008098 if (!locally_generated)
8099 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8100 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008101}
8102
8103
8104void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8105{
8106 struct p2p_data *p2p = wpa_s->global->p2p;
8107
8108 if (p2p == NULL)
8109 return;
8110
8111 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8112 return;
8113
8114 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8115 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8116
8117 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8118 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8119
8120 if (wpa_s->wps &&
8121 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8122 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8123
8124 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8125 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8126
8127 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8128 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8129 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8130 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8131 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8132 }
8133
8134 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8135 p2p_set_sec_dev_types(p2p,
8136 (void *) wpa_s->conf->sec_device_type,
8137 wpa_s->conf->num_sec_device_types);
8138
8139 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8140 int i;
8141 p2p_remove_wps_vendor_extensions(p2p);
8142 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8143 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8144 continue;
8145 p2p_add_wps_vendor_extension(
8146 p2p, wpa_s->conf->wps_vendor_ext[i]);
8147 }
8148 }
8149
8150 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8151 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8152 char country[3];
8153 country[0] = wpa_s->conf->country[0];
8154 country[1] = wpa_s->conf->country[1];
8155 country[2] = 0x04;
8156 p2p_set_country(p2p, country);
8157 }
8158
8159 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8160 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8161 wpa_s->conf->p2p_ssid_postfix ?
8162 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8163 0);
8164 }
8165
8166 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8167 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008168
8169 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8170 u8 reg_class, channel;
8171 int ret;
8172 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008173 u8 channel_forced;
8174
Jouni Malinen75ecf522011-06-27 15:19:46 -07008175 if (wpa_s->conf->p2p_listen_reg_class &&
8176 wpa_s->conf->p2p_listen_channel) {
8177 reg_class = wpa_s->conf->p2p_listen_reg_class;
8178 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008179 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008180 } else {
8181 reg_class = 81;
8182 /*
8183 * Pick one of the social channels randomly as the
8184 * listen channel.
8185 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008186 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8187 channel = 1;
8188 else
8189 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008190 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008191 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008192 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8193 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008194 if (ret)
8195 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8196 "failed: %d", ret);
8197 }
8198 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8199 u8 op_reg_class, op_channel, cfg_op_channel;
8200 int ret = 0;
8201 unsigned int r;
8202 if (wpa_s->conf->p2p_oper_reg_class &&
8203 wpa_s->conf->p2p_oper_channel) {
8204 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8205 op_channel = wpa_s->conf->p2p_oper_channel;
8206 cfg_op_channel = 1;
8207 } else {
8208 op_reg_class = 81;
8209 /*
8210 * Use random operation channel from (1, 6, 11)
8211 *if no other preference is indicated.
8212 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008213 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8214 op_channel = 1;
8215 else
8216 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008217 cfg_op_channel = 0;
8218 }
8219 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8220 cfg_op_channel);
8221 if (ret)
8222 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8223 "failed: %d", ret);
8224 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008225
8226 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8227 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8228 wpa_s->conf->p2p_pref_chan) < 0) {
8229 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8230 "update failed");
8231 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008232
8233 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8234 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8235 "update failed");
8236 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008237 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008238
8239 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8240 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008241}
8242
8243
8244int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8245 int duration)
8246{
8247 if (!wpa_s->ap_iface)
8248 return -1;
8249 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8250 duration);
8251}
8252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008253
8254int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8255{
8256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8257 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008258
8259 wpa_s->global->cross_connection = enabled;
8260 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8261
8262 if (!enabled) {
8263 struct wpa_supplicant *iface;
8264
8265 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8266 {
8267 if (iface->cross_connect_enabled == 0)
8268 continue;
8269
8270 iface->cross_connect_enabled = 0;
8271 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008272 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008273 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8274 iface->ifname,
8275 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008276 }
8277 }
8278
8279 return 0;
8280}
8281
8282
8283static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8284{
8285 struct wpa_supplicant *iface;
8286
8287 if (!uplink->global->cross_connection)
8288 return;
8289
8290 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8291 if (!iface->cross_connect_enabled)
8292 continue;
8293 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8294 0)
8295 continue;
8296 if (iface->ap_iface == NULL)
8297 continue;
8298 if (iface->cross_connect_in_use)
8299 continue;
8300
8301 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008302 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008303 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8304 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008305 }
8306}
8307
8308
8309static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8310{
8311 struct wpa_supplicant *iface;
8312
8313 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8314 if (!iface->cross_connect_enabled)
8315 continue;
8316 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8317 0)
8318 continue;
8319 if (!iface->cross_connect_in_use)
8320 continue;
8321
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008322 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008323 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8324 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008325 iface->cross_connect_in_use = 0;
8326 }
8327}
8328
8329
8330void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8331{
8332 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8333 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8334 wpa_s->cross_connect_disallowed)
8335 wpas_p2p_disable_cross_connect(wpa_s);
8336 else
8337 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008338 if (!wpa_s->ap_iface &&
8339 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8340 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008341}
8342
8343
8344void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8345{
8346 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008347 if (!wpa_s->ap_iface &&
8348 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8349 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008350 wpas_p2p_set_group_idle_timeout(wpa_s);
8351}
8352
8353
8354static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8355{
8356 struct wpa_supplicant *iface;
8357
8358 if (!wpa_s->global->cross_connection)
8359 return;
8360
8361 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8362 if (iface == wpa_s)
8363 continue;
8364 if (iface->drv_flags &
8365 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8366 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008367 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8368 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008369 continue;
8370
8371 wpa_s->cross_connect_enabled = 1;
8372 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8373 sizeof(wpa_s->cross_connect_uplink));
8374 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8375 "%s to %s whenever uplink is available",
8376 wpa_s->ifname, wpa_s->cross_connect_uplink);
8377
8378 if (iface->ap_iface || iface->current_ssid == NULL ||
8379 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8380 iface->cross_connect_disallowed ||
8381 iface->wpa_state != WPA_COMPLETED)
8382 break;
8383
8384 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008385 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008386 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8387 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008388 break;
8389 }
8390}
8391
8392
8393int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8394{
8395 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8396 !wpa_s->p2p_in_provisioning)
8397 return 0; /* not P2P client operation */
8398
8399 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8400 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008401 if (wpa_s != wpa_s->p2pdev)
8402 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008403 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008404 return 1;
8405}
8406
8407
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008408void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8409{
8410 struct wpa_supplicant *wpa_s = eloop_ctx;
8411 wpas_p2p_notif_pbc_overlap(wpa_s);
8412}
8413
8414
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008415void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8416 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008417{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008418 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008419 struct wpa_used_freq_data *freqs = NULL;
8420 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008421
8422 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8423 return;
8424
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008425 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8426 if (!freqs)
8427 return;
8428
8429 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008431 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008432 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008433 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8434 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008435 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8436 "channel list");
8437 return;
8438 }
8439
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008440 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008441
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008442 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008443
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008444 /*
8445 * The used frequencies map changed, so it is possible that a GO is
8446 * using a channel that is no longer valid for P2P use. It is also
8447 * possible that due to policy consideration, it would be preferable to
8448 * move it to a frequency already used by other station interfaces.
8449 */
8450 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8451
8452 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008453}
8454
8455
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008456static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8457 struct wpa_scan_results *scan_res)
8458{
8459 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8460}
8461
8462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008463int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8464{
8465 struct wpa_global *global = wpa_s->global;
8466 int found = 0;
8467 const u8 *peer;
8468
8469 if (global->p2p == NULL)
8470 return -1;
8471
8472 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8473
8474 if (wpa_s->pending_interface_name[0] &&
8475 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8476 found = 1;
8477
8478 peer = p2p_get_go_neg_peer(global->p2p);
8479 if (peer) {
8480 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8481 MACSTR, MAC2STR(peer));
8482 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008483 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008484 }
8485
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008486 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8487 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8488 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8489 found = 1;
8490 }
8491
8492 if (wpa_s->pending_pd_before_join) {
8493 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8494 wpa_s->pending_pd_before_join = 0;
8495 found = 1;
8496 }
8497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008498 wpas_p2p_stop_find(wpa_s);
8499
8500 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8501 if (wpa_s == global->p2p_group_formation &&
8502 (wpa_s->p2p_in_provisioning ||
8503 wpa_s->parent->pending_interface_type ==
8504 WPA_IF_P2P_CLIENT)) {
8505 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8506 "formation found - cancelling",
8507 wpa_s->ifname);
8508 found = 1;
8509 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008510 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008511 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008512 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008513 break;
8514 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008515 wpas_p2p_group_delete(wpa_s,
8516 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008517 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008518 } else if (wpa_s->p2p_in_invitation) {
8519 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8520 wpa_s->ifname);
8521 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008522 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008523 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008524 }
8525 }
8526
8527 if (!found) {
8528 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8529 return -1;
8530 }
8531
8532 return 0;
8533}
8534
8535
8536void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8537{
8538 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8539 return;
8540
8541 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8542 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008543 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008544}
8545
8546
8547void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8548 int freq_24, int freq_5, int freq_overall)
8549{
8550 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008551 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008552 return;
8553 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8554}
8555
8556
8557int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8558{
8559 u8 peer[ETH_ALEN];
8560 struct p2p_data *p2p = wpa_s->global->p2p;
8561
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008562 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008563 return -1;
8564
8565 if (hwaddr_aton(addr, peer))
8566 return -1;
8567
8568 return p2p_unauthorize(p2p, peer);
8569}
8570
8571
8572/**
8573 * wpas_p2p_disconnect - Disconnect from a P2P Group
8574 * @wpa_s: Pointer to wpa_supplicant data
8575 * Returns: 0 on success, -1 on failure
8576 *
8577 * This can be used to disconnect from a group in which the local end is a P2P
8578 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8579 * virtual network interface was created for this group, that interface will be
8580 * removed. Otherwise, only the configured P2P group network will be removed
8581 * from the interface.
8582 */
8583int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8584{
8585
8586 if (wpa_s == NULL)
8587 return -1;
8588
Jouni Malinen2b89da82012-08-31 22:04:41 +03008589 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8590 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008591}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008592
8593
8594int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8595{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008596 int ret;
8597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008598 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8599 return 0;
8600
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008601 ret = p2p_in_progress(wpa_s->global->p2p);
8602 if (ret == 0) {
8603 /*
8604 * Check whether there is an ongoing WPS provisioning step (or
8605 * other parts of group formation) on another interface since
8606 * p2p_in_progress() does not report this to avoid issues for
8607 * scans during such provisioning step.
8608 */
8609 if (wpa_s->global->p2p_group_formation &&
8610 wpa_s->global->p2p_group_formation != wpa_s) {
8611 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8612 "in group formation",
8613 wpa_s->global->p2p_group_formation->ifname);
8614 ret = 1;
8615 }
8616 }
8617
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008618 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008619 struct os_reltime now;
8620 os_get_reltime(&now);
8621 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8622 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008623 /* Wait for the first client has expired */
8624 wpa_s->global->p2p_go_wait_client.sec = 0;
8625 } else {
8626 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8627 ret = 1;
8628 }
8629 }
8630
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008631 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008632}
8633
8634
8635void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8636 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008637{
8638 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8639 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008640 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008641 /**
8642 * Remove the network by scheduling the group formation
8643 * timeout to happen immediately. The teardown code
8644 * needs to be scheduled to run asynch later so that we
8645 * don't delete data from under ourselves unexpectedly.
8646 * Calling wpas_p2p_group_formation_timeout directly
8647 * causes a series of crashes in WPS failure scenarios.
8648 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008649 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8650 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008651 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008652 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008653 }
8654}
8655
8656
8657struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008658 const u8 *addr, const u8 *ssid,
8659 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008660{
8661 struct wpa_ssid *s;
8662 size_t i;
8663
8664 for (s = wpa_s->conf->ssid; s; s = s->next) {
8665 if (s->disabled != 2)
8666 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008667 if (ssid &&
8668 (ssid_len != s->ssid_len ||
8669 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8670 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008671 if (addr == NULL) {
8672 if (s->mode == WPAS_MODE_P2P_GO)
8673 return s;
8674 continue;
8675 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008676 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8677 return s; /* peer is GO in the persistent group */
8678 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8679 continue;
8680 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008681 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008682 addr, ETH_ALEN) == 0)
8683 return s; /* peer is P2P client in persistent
8684 * group */
8685 }
8686 }
8687
8688 return NULL;
8689}
8690
8691
8692void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8693 const u8 *addr)
8694{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008695 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008696 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008697 /*
8698 * This can happen if WPS provisioning step is not terminated
8699 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8700 * peer was able to connect, there is no need to time out group
8701 * formation after this, though. In addition, this is used with
8702 * the initial connection wait on the GO as a separate formation
8703 * timeout and as such, expected to be hit after the initial WPS
8704 * provisioning step.
8705 */
8706 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008707
8708 if (!wpa_s->p2p_go_group_formation_completed &&
8709 !wpa_s->group_formation_reported) {
8710 /*
8711 * GO has not yet notified group formation success since
8712 * the WPS step was not completed cleanly. Do that
8713 * notification now since the P2P Client was able to
8714 * connect and as such, must have received the
8715 * credential from the WPS step.
8716 */
8717 if (wpa_s->global->p2p)
8718 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008719 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008720 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008721 }
8722 if (!wpa_s->p2p_go_group_formation_completed) {
8723 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8724 wpa_s->p2p_go_group_formation_completed = 1;
8725 wpa_s->global->p2p_group_formation = NULL;
8726 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008727 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008728 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008729 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008730 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008731 if (addr == NULL)
8732 return;
8733 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8734}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008735
Dmitry Shmidt04949592012-07-19 12:16:46 -07008736
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008737static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8738 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008739{
8740 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008741 int ret = 0;
8742
Dmitry Shmidt04949592012-07-19 12:16:46 -07008743 if (wpa_s->global->p2p_group_formation)
8744 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008745 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008746 offchannel_send_action_done(wpa_s);
8747 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008748 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008749 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8750 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8751 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8752 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008753 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008754 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008755 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008756 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008757 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008758 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008759 wpa_s->p2p_go_he,
8760 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008761 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008762 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008763}
8764
8765
8766int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8767{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008768 int res;
8769
Dmitry Shmidt04949592012-07-19 12:16:46 -07008770 if (!wpa_s->p2p_fallback_to_go_neg ||
8771 wpa_s->p2p_in_provisioning <= 5)
8772 return 0;
8773
8774 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8775 return 0; /* peer operating as a GO */
8776
8777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8778 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008779 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008780 "reason=GO-not-found");
8781 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008782
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008783 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008784}
8785
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008786
8787unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8788{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008789 struct wpa_supplicant *ifs;
8790
8791 if (wpa_s->wpa_state > WPA_SCANNING) {
8792 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8793 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008794 wpa_s->conf->p2p_search_delay);
8795 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008796 }
8797
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008798 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8799 radio_list) {
8800 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008801 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8802 "delay due to concurrent operation on "
8803 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008804 wpa_s->conf->p2p_search_delay,
8805 ifs->ifname);
8806 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008807 }
8808 }
8809
8810 return 0;
8811}
8812
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008813
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008814static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8815 struct wpa_ssid *s, const u8 *addr,
8816 int iface_addr)
8817{
8818 struct psk_list_entry *psk, *tmp;
8819 int changed = 0;
8820
8821 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8822 list) {
8823 if ((iface_addr && !psk->p2p &&
8824 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8825 (!iface_addr && psk->p2p &&
8826 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8827 wpa_dbg(wpa_s, MSG_DEBUG,
8828 "P2P: Remove persistent group PSK list entry for "
8829 MACSTR " p2p=%u",
8830 MAC2STR(psk->addr), psk->p2p);
8831 dl_list_del(&psk->list);
8832 os_free(psk);
8833 changed++;
8834 }
8835 }
8836
8837 return changed;
8838}
8839
8840
8841void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8842 const u8 *p2p_dev_addr,
8843 const u8 *psk, size_t psk_len)
8844{
8845 struct wpa_ssid *ssid = wpa_s->current_ssid;
8846 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008847 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008848
8849 if (psk_len != sizeof(p->psk))
8850 return;
8851
8852 if (p2p_dev_addr) {
8853 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8854 " p2p_dev_addr=" MACSTR,
8855 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8856 if (is_zero_ether_addr(p2p_dev_addr))
8857 p2p_dev_addr = NULL;
8858 } else {
8859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8860 MAC2STR(mac_addr));
8861 }
8862
8863 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8864 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8865 /* To be added to persistent group once created */
8866 if (wpa_s->global->add_psk == NULL) {
8867 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8868 if (wpa_s->global->add_psk == NULL)
8869 return;
8870 }
8871 p = wpa_s->global->add_psk;
8872 if (p2p_dev_addr) {
8873 p->p2p = 1;
8874 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8875 } else {
8876 p->p2p = 0;
8877 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8878 }
8879 os_memcpy(p->psk, psk, psk_len);
8880 return;
8881 }
8882
8883 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8885 return;
8886 }
8887
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008888 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008889 ssid->ssid_len);
8890 if (!persistent) {
8891 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8892 return;
8893 }
8894
8895 p = os_zalloc(sizeof(*p));
8896 if (p == NULL)
8897 return;
8898 if (p2p_dev_addr) {
8899 p->p2p = 1;
8900 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8901 } else {
8902 p->p2p = 0;
8903 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8904 }
8905 os_memcpy(p->psk, psk, psk_len);
8906
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008907 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8908 (last = dl_list_last(&persistent->psk_list,
8909 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008910 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8911 MACSTR " (p2p=%u) to make room for a new one",
8912 MAC2STR(last->addr), last->p2p);
8913 dl_list_del(&last->list);
8914 os_free(last);
8915 }
8916
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008917 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008918 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8919 p2p_dev_addr == NULL);
8920 if (p2p_dev_addr) {
8921 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8922 MACSTR, MAC2STR(p2p_dev_addr));
8923 } else {
8924 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8925 MAC2STR(mac_addr));
8926 }
8927 dl_list_add(&persistent->psk_list, &p->list);
8928
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008929 if (wpa_s->p2pdev->conf->update_config &&
8930 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008931 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008932}
8933
8934
8935static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8936 struct wpa_ssid *s, const u8 *addr,
8937 int iface_addr)
8938{
8939 int res;
8940
8941 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008942 if (res > 0 && wpa_s->conf->update_config &&
8943 wpa_config_write(wpa_s->confname, wpa_s->conf))
8944 wpa_dbg(wpa_s, MSG_DEBUG,
8945 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008946}
8947
8948
8949static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8950 const u8 *peer, int iface_addr)
8951{
8952 struct hostapd_data *hapd;
8953 struct hostapd_wpa_psk *psk, *prev, *rem;
8954 struct sta_info *sta;
8955
8956 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8957 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8958 return;
8959
8960 /* Remove per-station PSK entry */
8961 hapd = wpa_s->ap_iface->bss[0];
8962 prev = NULL;
8963 psk = hapd->conf->ssid.wpa_psk;
8964 while (psk) {
8965 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8966 (!iface_addr &&
8967 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8968 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8969 MACSTR " iface_addr=%d",
8970 MAC2STR(peer), iface_addr);
8971 if (prev)
8972 prev->next = psk->next;
8973 else
8974 hapd->conf->ssid.wpa_psk = psk->next;
8975 rem = psk;
8976 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07008977 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008978 } else {
8979 prev = psk;
8980 psk = psk->next;
8981 }
8982 }
8983
8984 /* Disconnect from group */
8985 if (iface_addr)
8986 sta = ap_get_sta(hapd, peer);
8987 else
8988 sta = ap_get_sta_p2p(hapd, peer);
8989 if (sta) {
8990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8991 " (iface_addr=%d) from group",
8992 MAC2STR(peer), iface_addr);
8993 hostapd_drv_sta_deauth(hapd, sta->addr,
8994 WLAN_REASON_DEAUTH_LEAVING);
8995 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8996 }
8997}
8998
8999
9000void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
9001 int iface_addr)
9002{
9003 struct wpa_ssid *s;
9004 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009005 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009006
9007 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
9008
9009 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009010 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009011 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9012 continue;
9013 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009014 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9015 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009016 }
9017
9018 /* Remove from any operating group */
9019 for (w = wpa_s->global->ifaces; w; w = w->next)
9020 wpas_p2p_remove_client_go(w, peer, iface_addr);
9021}
9022
9023
9024static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9025{
9026 struct wpa_supplicant *wpa_s = eloop_ctx;
9027 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9028}
9029
9030
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009031static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9032{
9033 struct wpa_supplicant *wpa_s = eloop_ctx;
9034
9035 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9036 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9037}
9038
9039
9040int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9041 struct wpa_ssid *ssid)
9042{
9043 struct wpa_supplicant *iface;
9044
9045 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9046 if (!iface->current_ssid ||
9047 iface->current_ssid->frequency == freq ||
9048 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9049 !iface->current_ssid->p2p_group))
9050 continue;
9051
9052 /* Remove the connection with least priority */
9053 if (!wpas_is_p2p_prioritized(iface)) {
9054 /* STA connection has priority over existing
9055 * P2P connection, so remove the interface. */
9056 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9057 eloop_register_timeout(0, 0,
9058 wpas_p2p_group_freq_conflict,
9059 iface, NULL);
9060 /* If connection in progress is P2P connection, do not
9061 * proceed for the connection. */
9062 if (wpa_s == iface)
9063 return -1;
9064 else
9065 return 0;
9066 } else {
9067 /* P2P connection has priority, disable the STA network
9068 */
9069 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9070 ssid);
9071 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9072 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9073 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9074 ETH_ALEN);
9075 /* If P2P connection is in progress, continue
9076 * connecting...*/
9077 if (wpa_s == iface)
9078 return 0;
9079 else
9080 return -1;
9081 }
9082 }
9083
9084 return 0;
9085}
9086
9087
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009088int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9089{
9090 struct wpa_ssid *ssid = wpa_s->current_ssid;
9091
9092 if (ssid == NULL || !ssid->p2p_group)
9093 return 0;
9094
9095 if (wpa_s->p2p_last_4way_hs_fail &&
9096 wpa_s->p2p_last_4way_hs_fail == ssid) {
9097 u8 go_dev_addr[ETH_ALEN];
9098 struct wpa_ssid *persistent;
9099
9100 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9101 ssid->ssid,
9102 ssid->ssid_len) <= 0) {
9103 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9104 goto disconnect;
9105 }
9106
9107 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9108 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009109 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009110 ssid->ssid,
9111 ssid->ssid_len);
9112 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9113 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9114 goto disconnect;
9115 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009116 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009117 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9118 persistent->id);
9119 disconnect:
9120 wpa_s->p2p_last_4way_hs_fail = NULL;
9121 /*
9122 * Remove the group from a timeout to avoid issues with caller
9123 * continuing to use the interface if this is on a P2P group
9124 * interface.
9125 */
9126 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9127 wpa_s, NULL);
9128 return 1;
9129 }
9130
9131 wpa_s->p2p_last_4way_hs_fail = ssid;
9132 return 0;
9133}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009134
9135
9136#ifdef CONFIG_WPS_NFC
9137
9138static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9139 struct wpabuf *p2p)
9140{
9141 struct wpabuf *ret;
9142 size_t wsc_len;
9143
9144 if (p2p == NULL) {
9145 wpabuf_free(wsc);
9146 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9147 return NULL;
9148 }
9149
9150 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9151 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9152 if (ret == NULL) {
9153 wpabuf_free(wsc);
9154 wpabuf_free(p2p);
9155 return NULL;
9156 }
9157
9158 wpabuf_put_be16(ret, wsc_len);
9159 if (wsc)
9160 wpabuf_put_buf(ret, wsc);
9161 wpabuf_put_be16(ret, wpabuf_len(p2p));
9162 wpabuf_put_buf(ret, p2p);
9163
9164 wpabuf_free(wsc);
9165 wpabuf_free(p2p);
9166 wpa_hexdump_buf(MSG_DEBUG,
9167 "P2P: Generated NFC connection handover message", ret);
9168
9169 if (ndef && ret) {
9170 struct wpabuf *tmp;
9171 tmp = ndef_build_p2p(ret);
9172 wpabuf_free(ret);
9173 if (tmp == NULL) {
9174 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9175 return NULL;
9176 }
9177 ret = tmp;
9178 }
9179
9180 return ret;
9181}
9182
9183
9184static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9185 struct wpa_ssid **ssid, u8 *go_dev_addr)
9186{
9187 struct wpa_supplicant *iface;
9188
9189 if (go_dev_addr)
9190 os_memset(go_dev_addr, 0, ETH_ALEN);
9191 if (ssid)
9192 *ssid = NULL;
9193 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9194 if (iface->wpa_state < WPA_ASSOCIATING ||
9195 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9196 !iface->current_ssid->p2p_group ||
9197 iface->current_ssid->mode != WPAS_MODE_INFRA)
9198 continue;
9199 if (ssid)
9200 *ssid = iface->current_ssid;
9201 if (go_dev_addr)
9202 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9203 return iface->assoc_freq;
9204 }
9205 return 0;
9206}
9207
9208
9209struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9210 int ndef)
9211{
9212 struct wpabuf *wsc, *p2p;
9213 struct wpa_ssid *ssid;
9214 u8 go_dev_addr[ETH_ALEN];
9215 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9216
9217 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9218 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9219 return NULL;
9220 }
9221
9222 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9223 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9224 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9225 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9226 return NULL;
9227 }
9228
9229 if (cli_freq == 0) {
9230 wsc = wps_build_nfc_handover_req_p2p(
9231 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9232 } else
9233 wsc = NULL;
9234 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9235 go_dev_addr, ssid ? ssid->ssid : NULL,
9236 ssid ? ssid->ssid_len : 0);
9237
9238 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9239}
9240
9241
9242struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9243 int ndef, int tag)
9244{
9245 struct wpabuf *wsc, *p2p;
9246 struct wpa_ssid *ssid;
9247 u8 go_dev_addr[ETH_ALEN];
9248 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9249
9250 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9251 return NULL;
9252
9253 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9254 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9255 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9256 return NULL;
9257
9258 if (cli_freq == 0) {
9259 wsc = wps_build_nfc_handover_sel_p2p(
9260 wpa_s->parent->wps,
9261 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9262 DEV_PW_NFC_CONNECTION_HANDOVER,
9263 wpa_s->conf->wps_nfc_dh_pubkey,
9264 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9265 } else
9266 wsc = NULL;
9267 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9268 go_dev_addr, ssid ? ssid->ssid : NULL,
9269 ssid ? ssid->ssid_len : 0);
9270
9271 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9272}
9273
9274
9275static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9276 struct p2p_nfc_params *params)
9277{
9278 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9279 "connection handover (freq=%d)",
9280 params->go_freq);
9281
9282 if (params->go_freq && params->go_ssid_len) {
9283 wpa_s->p2p_wps_method = WPS_NFC;
9284 wpa_s->pending_join_wps_method = WPS_NFC;
9285 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9286 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9287 ETH_ALEN);
9288 return wpas_p2p_join_start(wpa_s, params->go_freq,
9289 params->go_ssid,
9290 params->go_ssid_len);
9291 }
9292
9293 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9294 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009295 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009296 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009297 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009298 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009299 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009300}
9301
9302
9303static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9304 struct p2p_nfc_params *params, int tag)
9305{
9306 int res, persistent;
9307 struct wpa_ssid *ssid;
9308
9309 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9310 "connection handover");
9311 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9312 ssid = wpa_s->current_ssid;
9313 if (ssid == NULL)
9314 continue;
9315 if (ssid->mode != WPAS_MODE_P2P_GO)
9316 continue;
9317 if (wpa_s->ap_iface == NULL)
9318 continue;
9319 break;
9320 }
9321 if (wpa_s == NULL) {
9322 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9323 return -1;
9324 }
9325
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009326 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009327 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009328 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009329 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9330 return -1;
9331 }
9332 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009333 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9334 wpa_s->p2pdev->p2p_oob_dev_pw,
9335 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9336 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009337 if (res)
9338 return res;
9339
9340 if (!tag) {
9341 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9342 return 0;
9343 }
9344
9345 if (!params->peer ||
9346 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9347 return 0;
9348
9349 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9350 " to join", MAC2STR(params->peer->p2p_device_addr));
9351
9352 wpa_s->global->p2p_invite_group = wpa_s;
9353 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009354 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009355 params->peer->p2p_device_addr,
9356 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009357 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009358
9359 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9360 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9361 ssid->ssid, ssid->ssid_len, ssid->frequency,
9362 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009363 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009364}
9365
9366
9367static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9368 struct p2p_nfc_params *params,
9369 int forced_freq)
9370{
9371 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9372 "connection handover");
9373 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9374 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009375 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009376 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009377 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009378 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009379}
9380
9381
9382static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9383 struct p2p_nfc_params *params,
9384 int forced_freq)
9385{
9386 int res;
9387
9388 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9389 "connection handover");
9390 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9391 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009392 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009393 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009394 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009395 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009396 if (res)
9397 return res;
9398
9399 res = wpas_p2p_listen(wpa_s, 60);
9400 if (res) {
9401 p2p_unauthorize(wpa_s->global->p2p,
9402 params->peer->p2p_device_addr);
9403 }
9404
9405 return res;
9406}
9407
9408
9409static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9410 const struct wpabuf *data,
9411 int sel, int tag, int forced_freq)
9412{
9413 const u8 *pos, *end;
9414 u16 len, id;
9415 struct p2p_nfc_params params;
9416 int res;
9417
9418 os_memset(&params, 0, sizeof(params));
9419 params.sel = sel;
9420
9421 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9422
9423 pos = wpabuf_head(data);
9424 end = pos + wpabuf_len(data);
9425
9426 if (end - pos < 2) {
9427 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9428 "attributes");
9429 return -1;
9430 }
9431 len = WPA_GET_BE16(pos);
9432 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009433 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009434 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9435 "attributes");
9436 return -1;
9437 }
9438 params.wsc_attr = pos;
9439 params.wsc_len = len;
9440 pos += len;
9441
9442 if (end - pos < 2) {
9443 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9444 "attributes");
9445 return -1;
9446 }
9447 len = WPA_GET_BE16(pos);
9448 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009449 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009450 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9451 "attributes");
9452 return -1;
9453 }
9454 params.p2p_attr = pos;
9455 params.p2p_len = len;
9456 pos += len;
9457
9458 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9459 params.wsc_attr, params.wsc_len);
9460 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9461 params.p2p_attr, params.p2p_len);
9462 if (pos < end) {
9463 wpa_hexdump(MSG_DEBUG,
9464 "P2P: Ignored extra data after P2P attributes",
9465 pos, end - pos);
9466 }
9467
9468 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9469 if (res)
9470 return res;
9471
9472 if (params.next_step == NO_ACTION)
9473 return 0;
9474
9475 if (params.next_step == BOTH_GO) {
9476 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9477 MAC2STR(params.peer->p2p_device_addr));
9478 return 0;
9479 }
9480
9481 if (params.next_step == PEER_CLIENT) {
9482 if (!is_zero_ether_addr(params.go_dev_addr)) {
9483 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9484 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9485 " ssid=\"%s\"",
9486 MAC2STR(params.peer->p2p_device_addr),
9487 params.go_freq,
9488 MAC2STR(params.go_dev_addr),
9489 wpa_ssid_txt(params.go_ssid,
9490 params.go_ssid_len));
9491 } else {
9492 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9493 "peer=" MACSTR " freq=%d",
9494 MAC2STR(params.peer->p2p_device_addr),
9495 params.go_freq);
9496 }
9497 return 0;
9498 }
9499
9500 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9501 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9502 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9503 return 0;
9504 }
9505
9506 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9507 wpa_s->p2p_oob_dev_pw = NULL;
9508
9509 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9510 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9511 "received");
9512 return -1;
9513 }
9514
9515 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9516 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9517 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9518 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9519 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9520 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9521 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9522
9523 if (tag) {
9524 if (id < 0x10) {
9525 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9526 "peer OOB Device Password Id %u", id);
9527 return -1;
9528 }
9529 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9530 "Device Password Id %u", id);
9531 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9532 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9533 params.oob_dev_pw_len -
9534 WPS_OOB_PUBKEY_HASH_LEN - 2);
9535 wpa_s->p2p_oob_dev_pw_id = id;
9536 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9537 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9538 params.oob_dev_pw_len -
9539 WPS_OOB_PUBKEY_HASH_LEN - 2);
9540 if (wpa_s->p2p_oob_dev_pw == NULL)
9541 return -1;
9542
9543 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9544 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9545 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9546 return -1;
9547 } else {
9548 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9549 "without Device Password");
9550 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9551 }
9552
9553 switch (params.next_step) {
9554 case NO_ACTION:
9555 case BOTH_GO:
9556 case PEER_CLIENT:
9557 /* already covered above */
9558 return 0;
9559 case JOIN_GROUP:
9560 return wpas_p2p_nfc_join_group(wpa_s, &params);
9561 case AUTH_JOIN:
9562 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9563 case INIT_GO_NEG:
9564 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9565 case RESP_GO_NEG:
9566 /* TODO: use own OOB Dev Pw */
9567 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9568 }
9569
9570 return -1;
9571}
9572
9573
9574int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9575 const struct wpabuf *data, int forced_freq)
9576{
9577 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9578 return -1;
9579
9580 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9581}
9582
9583
9584int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9585 const struct wpabuf *req,
9586 const struct wpabuf *sel, int forced_freq)
9587{
9588 struct wpabuf *tmp;
9589 int ret;
9590
9591 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9592 return -1;
9593
9594 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9595
9596 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9597 wpabuf_head(req), wpabuf_len(req));
9598 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9599 wpabuf_head(sel), wpabuf_len(sel));
9600 if (forced_freq)
9601 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9602 tmp = ndef_parse_p2p(init ? sel : req);
9603 if (tmp == NULL) {
9604 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9605 return -1;
9606 }
9607
9608 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9609 forced_freq);
9610 wpabuf_free(tmp);
9611
9612 return ret;
9613}
9614
9615
9616int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9617{
9618 const u8 *if_addr;
9619 int go_intent = wpa_s->conf->p2p_go_intent;
9620 struct wpa_supplicant *iface;
9621
9622 if (wpa_s->global->p2p == NULL)
9623 return -1;
9624
9625 if (!enabled) {
9626 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9627 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9628 {
9629 if (!iface->ap_iface)
9630 continue;
9631 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9632 }
9633 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9634 0, NULL);
9635 if (wpa_s->p2p_nfc_tag_enabled)
9636 wpas_p2p_remove_pending_group_interface(wpa_s);
9637 wpa_s->p2p_nfc_tag_enabled = 0;
9638 return 0;
9639 }
9640
9641 if (wpa_s->global->p2p_disabled)
9642 return -1;
9643
9644 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9645 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9646 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9647 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9648 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9649 "to allow static handover cases");
9650 return -1;
9651 }
9652
9653 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9654
9655 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9656 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9657 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9658 if (wpa_s->p2p_oob_dev_pw == NULL)
9659 return -1;
9660 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9661
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009662 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9663 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9664 /*
9665 * P2P Group Interface present and the command came on group
9666 * interface, so enable the token for the current interface.
9667 */
9668 wpa_s->create_p2p_iface = 0;
9669 } else {
9670 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9671 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009672
9673 if (wpa_s->create_p2p_iface) {
9674 enum wpa_driver_if_type iftype;
9675 /* Prepare to add a new interface for the group */
9676 iftype = WPA_IF_P2P_GROUP;
9677 if (go_intent == 15)
9678 iftype = WPA_IF_P2P_GO;
9679 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9680 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9681 "interface for the group");
9682 return -1;
9683 }
9684
9685 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009686 } else if (wpa_s->p2p_mgmt)
9687 if_addr = wpa_s->parent->own_addr;
9688 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009689 if_addr = wpa_s->own_addr;
9690
9691 wpa_s->p2p_nfc_tag_enabled = enabled;
9692
9693 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9694 struct hostapd_data *hapd;
9695 if (iface->ap_iface == NULL)
9696 continue;
9697 hapd = iface->ap_iface->bss[0];
9698 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9699 hapd->conf->wps_nfc_dh_pubkey =
9700 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9701 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9702 hapd->conf->wps_nfc_dh_privkey =
9703 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9704 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9705 hapd->conf->wps_nfc_dev_pw =
9706 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9707 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9708
9709 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9710 wpa_dbg(iface, MSG_DEBUG,
9711 "P2P: Failed to enable NFC Tag for GO");
9712 }
9713 }
9714 p2p_set_authorized_oob_dev_pw_id(
9715 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9716 if_addr);
9717
9718 return 0;
9719}
9720
9721#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009722
9723
9724static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9725 struct wpa_used_freq_data *freqs,
9726 unsigned int num)
9727{
9728 u8 curr_chan, cand, chan;
9729 unsigned int i;
9730
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009731 /*
9732 * If possible, optimize the Listen channel to be a channel that is
9733 * already used by one of the other interfaces.
9734 */
9735 if (!wpa_s->conf->p2p_optimize_listen_chan)
9736 return;
9737
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009738 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9739 for (i = 0, cand = 0; i < num; i++) {
9740 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9741 if (curr_chan == chan) {
9742 cand = 0;
9743 break;
9744 }
9745
9746 if (chan == 1 || chan == 6 || chan == 11)
9747 cand = chan;
9748 }
9749
9750 if (cand) {
9751 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009752 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009753 cand);
9754 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9755 }
9756}
9757
9758
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009759static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009760{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009761 struct hostapd_config *conf;
9762 struct p2p_go_neg_results params;
9763 struct csa_settings csa_settings;
9764 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9765 int old_freq = current_ssid->frequency;
9766 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009767
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009768 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9769 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9770 return -1;
9771 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009772
9773 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009774 * TODO: This function may not always work correctly. For example,
9775 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009776 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009777 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9778 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009779 wpa_dbg(wpa_s, MSG_DEBUG,
9780 "P2P CSA: Failed to select new frequency for GO");
9781 return -1;
9782 }
9783
9784 if (current_ssid->frequency == params.freq) {
9785 wpa_dbg(wpa_s, MSG_DEBUG,
9786 "P2P CSA: Selected same frequency - not moving GO");
9787 return 0;
9788 }
9789
9790 conf = hostapd_config_defaults();
9791 if (!conf) {
9792 wpa_dbg(wpa_s, MSG_DEBUG,
9793 "P2P CSA: Failed to allocate default config");
9794 return -1;
9795 }
9796
9797 current_ssid->frequency = params.freq;
9798 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9799 wpa_dbg(wpa_s, MSG_DEBUG,
9800 "P2P CSA: Failed to create new GO config");
9801 ret = -1;
9802 goto out;
9803 }
9804
Sunil4a3f9f52022-07-19 22:04:39 +00009805 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9806 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009807 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009808 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9809 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009810 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9811 wpa_s->ap_iface->current_mode->mode,
9812 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9813 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009814 ret = -1;
9815 goto out;
9816 }
9817
9818 os_memset(&csa_settings, 0, sizeof(csa_settings));
9819 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9820 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9821 csa_settings.freq_params.freq = params.freq;
9822 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9823 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9824 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9825
9826 if (conf->ieee80211ac) {
9827 int freq1 = 0, freq2 = 0;
9828 u8 chan, opclass;
9829
9830 if (ieee80211_freq_to_channel_ext(params.freq,
9831 conf->secondary_channel,
9832 conf->vht_oper_chwidth,
9833 &opclass, &chan) ==
9834 NUM_HOSTAPD_MODES) {
9835 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9836 ret = -1;
9837 goto out;
9838 }
9839
9840 if (conf->vht_oper_centr_freq_seg0_idx)
9841 freq1 = ieee80211_chan_to_freq(
9842 NULL, opclass,
9843 conf->vht_oper_centr_freq_seg0_idx);
9844
9845 if (conf->vht_oper_centr_freq_seg1_idx)
9846 freq2 = ieee80211_chan_to_freq(
9847 NULL, opclass,
9848 conf->vht_oper_centr_freq_seg1_idx);
9849
9850 if (freq1 < 0 || freq2 < 0) {
9851 wpa_dbg(wpa_s, MSG_DEBUG,
9852 "P2P CSA: Selected invalid VHT center freqs");
9853 ret = -1;
9854 goto out;
9855 }
9856
9857 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9858 csa_settings.freq_params.center_freq1 = freq1;
9859 csa_settings.freq_params.center_freq2 = freq2;
9860
9861 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009862 case CONF_OPER_CHWIDTH_80MHZ:
9863 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009864 csa_settings.freq_params.bandwidth = 80;
9865 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009866 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009867 csa_settings.freq_params.bandwidth = 160;
9868 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009869 default:
9870 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009871 }
9872 }
9873
9874 ret = ap_switch_channel(wpa_s, &csa_settings);
9875out:
9876 current_ssid->frequency = old_freq;
9877 hostapd_config_free(conf);
9878 return ret;
9879}
9880
9881
9882static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9883{
9884 struct p2p_go_neg_results params;
9885 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009886 void (*ap_configured_cb)(void *ctx, void *data);
9887 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009888
9889 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9890
9891 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9892 current_ssid->frequency);
9893
9894 /* Stop the AP functionality */
9895 /* TODO: Should do this in a way that does not indicated to possible
9896 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009897 /* If this action occurs before a group is started, the callback should
9898 * be preserved, or GROUP-STARTED event would be lost. If this action
9899 * occurs after a group is started, these pointers are all NULL and
9900 * harmless. */
9901 ap_configured_cb = wpa_s->ap_configured_cb;
9902 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9903 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009904 wpa_supplicant_ap_deinit(wpa_s);
9905
9906 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009907 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9908 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009909 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9910 wpas_p2p_group_delete(wpa_s,
9911 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9912 return;
9913 }
9914 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9915 params.freq);
9916
9917 if (params.freq &&
9918 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9919 wpa_printf(MSG_DEBUG,
9920 "P2P: Selected freq (%u MHz) is not valid for P2P",
9921 params.freq);
9922 wpas_p2p_group_delete(wpa_s,
9923 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9924 return;
9925 }
9926
Hai Shalom899fcc72020-10-19 14:38:18 -07009927 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009928 wpa_s->ap_configured_cb = ap_configured_cb;
9929 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9930 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009931
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009932 /* Update the frequency */
9933 current_ssid->frequency = params.freq;
9934 wpa_s->connect_without_scan = current_ssid;
9935 wpa_s->reassociate = 1;
9936 wpa_s->disconnected = 0;
9937 wpa_supplicant_req_scan(wpa_s, 0, 0);
9938}
9939
9940
9941static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9942{
9943 struct wpa_supplicant *wpa_s = eloop_ctx;
9944
9945 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009946 return;
9947
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009948 wpas_p2p_go_update_common_freqs(wpa_s);
9949
9950 /* Do not move GO in the middle of a CSA */
9951 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9952 wpa_printf(MSG_DEBUG,
9953 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009954 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009955 }
9956
9957 /*
9958 * First, try a channel switch flow. If it is not supported or fails,
9959 * take down the GO and bring it up again.
9960 */
9961 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9962 wpas_p2p_move_go_no_csa(wpa_s);
9963}
9964
9965
9966static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9967{
9968 struct wpa_supplicant *wpa_s = eloop_ctx;
9969 struct wpa_used_freq_data *freqs = NULL;
9970 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009971
9972 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9973 if (!freqs)
9974 return;
9975
9976 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9977
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009978 /* Previous attempt to move a GO was not possible -- try again. */
9979 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9980 WPAS_P2P_CHANNEL_UPDATE_ANY);
9981
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009982 os_free(freqs);
9983}
9984
9985
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009986/*
9987 * Consider moving a GO from its currently used frequency:
9988 * 1. It is possible that due to regulatory consideration the frequency
9989 * can no longer be used and there is a need to evacuate the GO.
9990 * 2. It is possible that due to MCC considerations, it would be preferable
9991 * to move the GO to a channel that is currently used by some other
9992 * station interface.
9993 *
9994 * In case a frequency that became invalid is once again valid, cancel a
9995 * previously initiated GO frequency change.
9996 */
9997static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9998 struct wpa_used_freq_data *freqs,
9999 unsigned int num)
10000{
10001 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
10002 unsigned int timeout;
10003 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010004 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010005
10006 wpas_p2p_go_update_common_freqs(wpa_s);
10007
10008 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010009 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010010 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010011 for (i = 0, invalid_freq = 0; i < num; i++) {
10012 if (freqs[i].freq == freq) {
10013 flags = freqs[i].flags;
10014
10015 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010016 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10017 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010018 wpa_dbg(wpa_s, MSG_DEBUG,
10019 "P2P: Freq=%d MHz no longer valid for GO",
10020 freq);
10021 invalid_freq = 1;
10022 }
10023 } else if (freqs[i].flags == 0) {
10024 /* Freq is not used by any other station interface */
10025 continue;
10026 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010027 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010028 /* Freq is not valid for P2P use cases */
10029 continue;
10030 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10031 P2P_GO_FREQ_MOVE_SCM) {
10032 policy_move = 1;
10033 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10034 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10035 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10036 policy_move = 1;
10037 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10038 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10039 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10040 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10041 policy_move = 1;
10042 } else if ((wpa_s->drv_flags &
10043 WPA_DRIVER_FLAGS_AP_CSA) &&
10044 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10045 u8 chan;
10046
10047 /*
10048 * We do not support CSA between bands, so move
10049 * GO only within the same band.
10050 */
10051 if (wpa_s->ap_iface->current_mode->mode ==
10052 ieee80211_freq_to_chan(freqs[i].freq,
10053 &chan))
10054 policy_move = 1;
10055 }
10056 }
10057 }
10058
10059 wpa_dbg(wpa_s, MSG_DEBUG,
10060 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10061 invalid_freq, policy_move, flags);
10062
10063 /*
10064 * The channel is valid, or we are going to have a policy move, so
10065 * cancel timeout.
10066 */
10067 if (!invalid_freq || policy_move) {
10068 wpa_dbg(wpa_s, MSG_DEBUG,
10069 "P2P: Cancel a GO move from freq=%d MHz", freq);
10070 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10071
10072 if (wpas_p2p_in_progress(wpa_s)) {
10073 wpa_dbg(wpa_s, MSG_DEBUG,
10074 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10075 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10076 wpa_s, NULL);
10077 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10078 wpas_p2p_reconsider_moving_go,
10079 wpa_s, NULL);
10080 return;
10081 }
10082 }
10083
10084 if (!invalid_freq && (!policy_move || flags != 0)) {
10085 wpa_dbg(wpa_s, MSG_DEBUG,
10086 "P2P: Not initiating a GO frequency change");
10087 return;
10088 }
10089
10090 /*
10091 * Do not consider moving GO if it is in the middle of a CSA. When the
10092 * CSA is finished this flow should be retriggered.
10093 */
10094 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10095 wpa_dbg(wpa_s, MSG_DEBUG,
10096 "P2P: Not initiating a GO frequency change - CSA is in progress");
10097 return;
10098 }
10099
10100 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10101 timeout = P2P_GO_FREQ_CHANGE_TIME;
10102 else
10103 timeout = 0;
10104
10105 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10106 freq, timeout);
10107 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10108 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10109}
10110
10111
10112static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10113 struct wpa_used_freq_data *freqs,
10114 unsigned int num,
10115 enum wpas_p2p_channel_update_trig trig)
10116{
10117 struct wpa_supplicant *ifs;
10118
10119 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10120 NULL);
10121
10122 /*
10123 * Travers all the radio interfaces, and for each GO interface, check
10124 * if there is a need to move the GO from the frequency it is using,
10125 * or in case the frequency is valid again, cancel the evacuation flow.
10126 */
10127 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10128 radio_list) {
10129 if (ifs->current_ssid == NULL ||
10130 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10131 continue;
10132
10133 /*
10134 * The GO was just started or completed channel switch, no need
10135 * to move it.
10136 */
10137 if (wpa_s == ifs &&
10138 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10139 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10140 wpa_dbg(wpa_s, MSG_DEBUG,
10141 "P2P: GO move - schedule re-consideration");
10142 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10143 wpas_p2p_reconsider_moving_go,
10144 wpa_s, NULL);
10145 continue;
10146 }
10147
10148 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10149 }
10150}
10151
10152
10153void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10154{
10155 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10156 return;
10157
10158 wpas_p2p_update_channel_list(wpa_s,
10159 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10160}
10161
10162
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010163void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10164{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010165 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10166 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10167 "the management interface is being removed");
10168 wpas_p2p_deinit_global(wpa_s->global);
10169 }
10170}
10171
10172
10173void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10174{
10175 if (wpa_s->ap_iface->bss)
10176 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10177 wpas_p2p_group_deinit(wpa_s);
10178}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010179
10180
10181int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10182 unsigned int period, unsigned int interval,
10183 unsigned int count)
10184{
10185 struct p2p_data *p2p = wpa_s->global->p2p;
10186 u8 *device_types;
10187 size_t dev_types_len;
10188 struct wpabuf *buf;
10189 int ret;
10190
10191 if (wpa_s->p2p_lo_started) {
10192 wpa_dbg(wpa_s, MSG_DEBUG,
10193 "P2P Listen offload is already started");
10194 return 0;
10195 }
10196
10197 if (wpa_s->global->p2p == NULL ||
10198 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10199 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10200 return -1;
10201 }
10202
10203 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10204 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10205 freq);
10206 return -1;
10207 }
10208
10209 /* Get device type */
10210 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10211 WPS_DEV_TYPE_LEN;
10212 device_types = os_malloc(dev_types_len);
10213 if (!device_types)
10214 return -1;
10215 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10216 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10217 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10218
10219 /* Get Probe Response IE(s) */
10220 buf = p2p_build_probe_resp_template(p2p, freq);
10221 if (!buf) {
10222 os_free(device_types);
10223 return -1;
10224 }
10225
10226 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10227 device_types, dev_types_len,
10228 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10229 if (ret < 0)
10230 wpa_dbg(wpa_s, MSG_DEBUG,
10231 "P2P: Failed to start P2P listen offload");
10232
10233 os_free(device_types);
10234 wpabuf_free(buf);
10235
10236 if (ret == 0) {
10237 wpa_s->p2p_lo_started = 1;
10238
10239 /* Stop current P2P listen if any */
10240 wpas_stop_listen(wpa_s);
10241 }
10242
10243 return ret;
10244}
10245
10246
10247int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10248{
10249 int ret;
10250
10251 if (!wpa_s->p2p_lo_started)
10252 return 0;
10253
10254 ret = wpa_drv_p2p_lo_stop(wpa_s);
10255 if (ret < 0)
10256 wpa_dbg(wpa_s, MSG_DEBUG,
10257 "P2P: Failed to stop P2P listen offload");
10258
10259 wpa_s->p2p_lo_started = 0;
10260 return ret;
10261}