blob: 0d59b797858c66dce445858edb5881af52f9b829 [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,
368 0);
369 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
370 0);
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 Wang06b42472022-11-10 06:56:31 +00003312 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003314 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003315 wpa_msg_global(wpa_s, MSG_INFO,
3316 P2P_EVENT_INVITATION_ACCEPTED
3317 "sa=" MACSTR " go_dev_addr=" MACSTR
3318 " bssid=" MACSTR " unknown-network",
3319 MAC2STR(sa), MAC2STR(go_dev_addr),
3320 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003322 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003323 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324 }
3325 return;
3326 }
3327
3328 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3329 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3330 " was rejected (status %u)", MAC2STR(sa), status);
3331 return;
3332 }
3333
3334 if (!s) {
3335 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003336 wpa_msg_global(wpa_s, MSG_INFO,
3337 P2P_EVENT_INVITATION_RECEIVED
3338 "sa=" MACSTR " go_dev_addr=" MACSTR
3339 " bssid=" MACSTR " unknown-network",
3340 MAC2STR(sa), MAC2STR(go_dev_addr),
3341 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003343 wpa_msg_global(wpa_s, MSG_INFO,
3344 P2P_EVENT_INVITATION_RECEIVED
3345 "sa=" MACSTR " go_dev_addr=" MACSTR
3346 " unknown-network",
3347 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003349 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3350 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351 return;
3352 }
3353
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003354 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003355 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3356 "sa=" MACSTR " persistent=%d freq=%d",
3357 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003358 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003359 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3360 "sa=" MACSTR " persistent=%d",
3361 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003362 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003363 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3364 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365}
3366
3367
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003368static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3369 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003370 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003371{
3372 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003373 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003374
3375 if (ssid == NULL)
3376 return;
3377
3378 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003379 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003380 ETH_ALEN) == 0)
3381 break;
3382 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003383 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003384 if (ssid->mode != WPAS_MODE_P2P_GO &&
3385 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3386 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3387 "due to invitation result", ssid->id);
3388 wpas_notify_network_removed(wpa_s, ssid);
3389 wpa_config_remove_network(wpa_s->conf, ssid->id);
3390 return;
3391 }
3392 return; /* Peer not found in client list */
3393 }
3394
3395 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003396 "group %d client list%s",
3397 MAC2STR(peer), ssid->id,
3398 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003399 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3400 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3401 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003402 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003403 if (p2p_wpa_s->conf->update_config &&
3404 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003405 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003406}
3407
3408
3409static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3410 const u8 *peer)
3411{
3412 struct wpa_ssid *ssid;
3413
3414 wpa_s = wpa_s->global->p2p_invite_group;
3415 if (wpa_s == NULL)
3416 return; /* No known invitation group */
3417 ssid = wpa_s->current_ssid;
3418 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3419 !ssid->p2p_persistent_group)
3420 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003421 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003422 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003423 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003424}
3425
3426
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003427static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003428 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003429 const u8 *peer, int neg_freq,
3430 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431{
3432 struct wpa_supplicant *wpa_s = ctx;
3433 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003434 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435
3436 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003437 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3438 "status=%d " MACSTR,
3439 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003441 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3442 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 }
3444 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3445
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003446 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3447 status, MAC2STR(peer));
3448 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003449 struct wpa_supplicant *group_if =
3450 wpa_s->global->p2p_invite_group;
3451
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003452 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3453 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003454
3455 /*
3456 * Invitation to an active group. If this is successful and we
3457 * are the GO, set the client wait to postpone some concurrent
3458 * operations and to allow provisioning and connection to happen
3459 * more quickly.
3460 */
3461 if (status == P2P_SC_SUCCESS &&
3462 group_if && group_if->current_ssid &&
3463 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3464 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3465#ifdef CONFIG_TESTING_OPTIONS
3466 if (group_if->p2p_go_csa_on_inv) {
3467 wpa_printf(MSG_DEBUG,
3468 "Testing: force P2P GO CSA after invitation");
3469 eloop_cancel_timeout(
3470 wpas_p2p_reconsider_moving_go,
3471 wpa_s, NULL);
3472 eloop_register_timeout(
3473 0, 50000,
3474 wpas_p2p_reconsider_moving_go,
3475 wpa_s, NULL);
3476 }
3477#endif /* CONFIG_TESTING_OPTIONS */
3478 }
3479 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003480 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003481
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003482 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3483 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3484 "invitation exchange to indicate readiness for "
3485 "re-invocation");
3486 }
3487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003489 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3490 ssid = wpa_config_get_network(
3491 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003492 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494 wpas_p2p_remove_pending_group_interface(wpa_s);
3495 return;
3496 }
3497
3498 ssid = wpa_config_get_network(wpa_s->conf,
3499 wpa_s->pending_invite_ssid_id);
3500 if (ssid == NULL) {
3501 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3502 "data matching with invitation");
3503 return;
3504 }
3505
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003506 /*
3507 * The peer could have missed our ctrl::ack frame for Invitation
3508 * Response and continue retransmitting the frame. To reduce the
3509 * likelihood of the peer not getting successful TX status for the
3510 * Invitation Response frame, wait a short time here before starting
3511 * the persistent group so that we will remain on the current channel to
3512 * acknowledge any possible retransmission from the peer.
3513 */
3514 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3515 "starting persistent group");
3516 os_sleep(0, 50000);
3517
Dmitry Shmidt15907092014-03-25 10:42:57 -07003518 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003519 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003520 freq = neg_freq;
3521 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003522 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003523 freq = peer_oper_freq;
3524 else
3525 freq = 0;
3526
3527 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3528 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003530 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003531 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003532 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003533 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003534 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003535 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003536 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003537 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003538 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003539 ssid->mode == WPAS_MODE_P2P_GO ?
3540 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003541 0, 1,
Matthew Wang06b42472022-11-10 06:56:31 +00003542 is_p2p_allow_6ghz(wpa_s->global->p2p), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543}
3544
3545
Dmitry Shmidt04949592012-07-19 12:16:46 -07003546static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3547 unsigned int freq)
3548{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003549 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3550 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003551 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003552}
3553
3554
3555static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3556{
3557 reg->channel[reg->channels] = chan;
3558 reg->channels++;
3559}
3560
3561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003562static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003563 struct p2p_channels *chan,
3564 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565{
3566 int i, cla = 0;
3567
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003568 wpa_s->global->p2p_24ghz_social_channels = 1;
3569
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003570 os_memset(cli_chan, 0, sizeof(*cli_chan));
3571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003572 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3573 "band");
3574
3575 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3576 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003577 chan->reg_class[cla].channels = 0;
3578 for (i = 0; i < 11; i++) {
3579 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3580 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3581 }
3582 if (chan->reg_class[cla].channels)
3583 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584
3585 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3586 "band");
3587
3588 /* Operating class 115 - 5 GHz, channels 36-48 */
3589 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003590 chan->reg_class[cla].channels = 0;
3591 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3592 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3593 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3594 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3595 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3596 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3597 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3598 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3599 if (chan->reg_class[cla].channels)
3600 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601
3602 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3603 "band");
3604
3605 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3606 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003607 chan->reg_class[cla].channels = 0;
3608 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3609 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3610 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3611 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3612 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3613 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3614 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3615 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3616 if (chan->reg_class[cla].channels)
3617 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618
3619 chan->reg_classes = cla;
3620 return 0;
3621}
3622
3623
Hai Shalomc1a21442022-02-04 13:43:00 -08003624static enum chan_allowed has_channel(struct wpa_global *global,
3625 struct hostapd_hw_modes *mode, u8 op_class,
3626 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627{
3628 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003629 unsigned int freq;
3630
Hai Shalomc1a21442022-02-04 13:43:00 -08003631 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003632 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003633 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634
3635 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003636 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 if (flags)
3638 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003639 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003640 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003641 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3642 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003643 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3644 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003645 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646 }
3647 }
3648
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003649 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650}
3651
3652
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003653static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3654 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003655 u8 channel, const u8 *center_channels,
3656 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003657{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003658 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003659
3660 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3661 return 0;
3662
Hai Shalomc1a21442022-02-04 13:43:00 -08003663 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003664 /*
3665 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3666 * so the center channel is 6 channels away from the start/end.
3667 */
3668 if (channel >= center_channels[i] - 6 &&
3669 channel <= center_channels[i] + 6)
3670 return center_channels[i];
3671
3672 return 0;
3673}
3674
3675
Hai Shalomc1a21442022-02-04 13:43:00 -08003676static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3677 155, 171 };
3678static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3679 119, 135, 151, 167, 183, 199,
3680 215 };
3681
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003682static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3683 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003684 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003685{
3686 u8 center_chan;
3687 int i, flags;
3688 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003689 const u8 *chans;
3690 size_t num_chans;
3691 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003692
Hai Shalomc1a21442022-02-04 13:43:00 -08003693 if (is_6ghz) {
3694 chans = center_channels_6ghz_80mhz;
3695 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3696 } else {
3697 chans = center_channels_5ghz_80mhz;
3698 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3699 }
3700 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3701 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003702 if (!center_chan)
3703 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003704 if (!wpa_s->p2p_go_allow_dfs &&
3705 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003706 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3707
3708 /* check all the channels are available */
3709 for (i = 0; i < 4; i++) {
3710 int adj_chan = center_chan - 6 + i * 4;
3711
Hai Shalomc1a21442022-02-04 13:43:00 -08003712 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3713 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003714 if (res == NOT_ALLOWED)
3715 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003716 if (res == RADAR)
3717 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003718 if (res == NO_IR)
3719 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003720 if (!is_6ghz) {
3721 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3722 return NOT_ALLOWED;
3723 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3724 return NOT_ALLOWED;
3725 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3726 return NOT_ALLOWED;
3727 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3728 return NOT_ALLOWED;
3729 } else if (is_6ghz &&
3730 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3731 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003732 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003733 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003734 }
3735
3736 return ret;
3737}
3738
3739
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003740static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3741 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003742 u8 channel, const u8 *center_channels,
3743 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003744{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003745 unsigned int i;
3746
3747 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3748 return 0;
3749
Hai Shalomc1a21442022-02-04 13:43:00 -08003750 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003751 /*
3752 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3753 * so the center channel is 14 channels away from the start/end.
3754 */
3755 if (channel >= center_channels[i] - 14 &&
3756 channel <= center_channels[i] + 14)
3757 return center_channels[i];
3758
3759 return 0;
3760}
3761
3762
Hai Shalomc1a21442022-02-04 13:43:00 -08003763static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3764static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3765 207 };
3766
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003767static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3768 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003769 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003770{
3771 u8 center_chan;
3772 int i, flags;
3773 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003774 const u8 *chans;
3775 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003776
Hai Shalomc1a21442022-02-04 13:43:00 -08003777 if (is_6ghz_op_class(op_class)) {
3778 chans = center_channels_6ghz_160mhz;
3779 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3780 } else {
3781 chans = center_channels_5ghz_160mhz;
3782 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3783 }
3784 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3785 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003786 if (!center_chan)
3787 return NOT_ALLOWED;
3788 /* VHT 160 MHz uses DFS channels in most countries. */
3789
3790 /* Check all the channels are available */
3791 for (i = 0; i < 8; i++) {
3792 int adj_chan = center_chan - 14 + i * 4;
3793
Hai Shalomc1a21442022-02-04 13:43:00 -08003794 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3795 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003796 if (res == NOT_ALLOWED)
3797 return NOT_ALLOWED;
3798
Hai Shalomc1a21442022-02-04 13:43:00 -08003799 if (res == RADAR)
3800 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003801 if (res == NO_IR)
3802 ret = NO_IR;
3803
Hai Shalomc1a21442022-02-04 13:43:00 -08003804 if (!is_6ghz_op_class(op_class)) {
3805 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3806 return NOT_ALLOWED;
3807 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3808 return NOT_ALLOWED;
3809 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3810 return NOT_ALLOWED;
3811 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3812 return NOT_ALLOWED;
3813 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3814 return NOT_ALLOWED;
3815 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3816 return NOT_ALLOWED;
3817 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3818 return NOT_ALLOWED;
3819 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3820 return NOT_ALLOWED;
3821 } else if (is_6ghz_op_class(op_class) &&
3822 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3823 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003824 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003825 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003826 }
3827
3828 return ret;
3829}
3830
3831
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003832static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3833 struct hostapd_hw_modes *mode,
3834 u8 channel)
3835{
3836 struct ieee80211_edmg_config edmg;
3837
3838 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3839 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3840 return ALLOWED;
3841
3842 return NOT_ALLOWED;
3843}
3844
3845
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003846static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3847 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003848 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003849{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003850 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003851 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003852
Sunil Ravi89eba102022-09-13 21:04:37 -07003853 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3854 p2p_channel_to_freq(op_class, channel)))
3855 return NOT_ALLOWED;
3856
Hai Shalomc1a21442022-02-04 13:43:00 -08003857 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003858 if (bw == BW40MINUS) {
3859 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3860 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003861 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3862 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003863 } else if (bw == BW40PLUS) {
3864 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3865 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003866 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3867 NULL);
3868 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3869 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3870 return NOT_ALLOWED;
3871 if (get_6ghz_sec_channel(channel) < 0)
3872 res2 = has_channel(wpa_s->global, mode, op_class,
3873 channel - 4, NULL);
3874 else
3875 res2 = has_channel(wpa_s->global, mode, op_class,
3876 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003877 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003878 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3879 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003880 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003881 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3882 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003883 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3884 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003885 }
3886
3887 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3888 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003889 if (res == NO_IR || res2 == NO_IR)
3890 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003891 if (res == RADAR || res2 == RADAR)
3892 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003893 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003894}
3895
3896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003898 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003899 struct p2p_channels *cli_chan,
3900 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003902 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003903 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003905 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003906 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3907 "of all supported channels; assume dualband "
3908 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003909 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910 }
3911
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003912 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003914 for (op = 0; global_op_class[op].op_class; op++) {
3915 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003916 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003917 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918
Hai Shalom899fcc72020-10-19 14:38:18 -07003919 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003920 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003921 continue;
3922
Hai Shalomfdcde762020-04-02 11:19:20 -07003923 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3924 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 if (mode == NULL)
3926 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003927 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3928 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003930 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003931
3932 /* Check for non-continuous jump in channel index
3933 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003934 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003935 ch < 149 && ch + o->inc > 149)
3936 ch = 149;
3937
Hai Shalomc1a21442022-02-04 13:43:00 -08003938 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
3939 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003940 if (res == ALLOWED) {
3941 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003942 if (cla == P2P_MAX_REG_CLASSES)
3943 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003944 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3945 o->op_class);
3946 reg = &chan->reg_class[cla];
3947 cla++;
3948 reg->reg_class = o->op_class;
3949 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003950 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3951 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003952 reg->channel[reg->channels] = ch;
3953 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003954 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003955 wpa_s->conf->p2p_add_cli_chan) {
3956 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003957 if (cli_cla == P2P_MAX_REG_CLASSES)
3958 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003959 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3960 o->op_class);
3961 cli_reg = &cli_chan->reg_class[cli_cla];
3962 cli_cla++;
3963 cli_reg->reg_class = o->op_class;
3964 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003965 if (cli_reg->channels ==
3966 P2P_MAX_REG_CLASS_CHANNELS)
3967 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003968 cli_reg->channel[cli_reg->channels] = ch;
3969 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 }
3972 if (reg) {
3973 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3974 reg->channel, reg->channels);
3975 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003976 if (cli_reg) {
3977 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3978 cli_reg->channel, cli_reg->channels);
3979 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980 }
3981
3982 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003983 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985 return 0;
3986}
3987
3988
Hai Shalomc1a21442022-02-04 13:43:00 -08003989int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
3990 struct hostapd_hw_modes *mode,
3991 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003992{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003993 int op;
3994 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003995
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003996 for (op = 0; global_op_class[op].op_class; op++) {
3997 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003998 u16 ch;
3999 int chan = channel;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004000
Hai Shalomc1a21442022-02-04 13:43:00 -08004001 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4002 * driver offloaded DFS. */
4003 if ((o->p2p == NO_P2P_SUPP &&
4004 (!is_dfs_global_op_class(o->op_class) ||
4005 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004006 (is_6ghz_op_class(o->op_class) &&
4007 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004008 continue;
4009
Hai Shalomc1a21442022-02-04 13:43:00 -08004010 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4011 get_6ghz_sec_channel(channel) < 0)
4012 chan = channel - 4;
4013
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004014 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4015 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004016 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4017 o->bw != BW40) ||
4018 ch != chan)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004019 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004020 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4021 ch, o->bw);
4022 if (ret == ALLOWED) {
4023 if (is_6ghz_op_class(o->op_class) &&
4024 o->bw == BW40)
4025 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004026 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004027 }
4028 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4029 /* Allow RADAR channels used for driver
4030 * offloaded DFS */
4031 return (o->bw == BW40MINUS) ? -1 : 1;
4032 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004033 }
4034 }
4035 return 0;
4036}
4037
4038
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004039int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004040 struct hostapd_hw_modes *mode, u8 channel,
4041 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004042{
Hai Shalomc1a21442022-02-04 13:43:00 -08004043 const u8 *chans;
4044 size_t num_chans;
4045 enum chan_allowed ret;
4046
4047 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4048 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004049 return 0;
4050
Hai Shalomc1a21442022-02-04 13:43:00 -08004051 if (is_6ghz_op_class(op_class)) {
4052 chans = center_channels_6ghz_80mhz;
4053 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4054 } else {
4055 chans = center_channels_5ghz_80mhz;
4056 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4057 }
4058 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4059 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004060}
4061
4062
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004063int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004064 struct hostapd_hw_modes *mode, u8 channel,
4065 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004066{
Hai Shalomc1a21442022-02-04 13:43:00 -08004067 const u8 *chans;
4068 size_t num_chans;
4069 enum chan_allowed ret;
4070
4071 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4072 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004073 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004074 if (is_6ghz_op_class(op_class)) {
4075 chans = center_channels_6ghz_160mhz;
4076 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4077 } else {
4078 chans = center_channels_5ghz_160mhz;
4079 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4080 }
4081 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4082 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004083}
4084
4085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4087 size_t buf_len)
4088{
4089 struct wpa_supplicant *wpa_s = ctx;
4090
4091 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4092 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4093 break;
4094 }
4095 if (wpa_s == NULL)
4096 return -1;
4097
4098 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4099}
4100
4101
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004102struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4103 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004104{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004105 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4106 struct wpa_ssid *s = wpa_s->current_ssid;
4107 if (s == NULL)
4108 continue;
4109 if (s->mode != WPAS_MODE_P2P_GO &&
4110 s->mode != WPAS_MODE_AP &&
4111 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4112 continue;
4113 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004114 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004115 continue;
4116 return wpa_s;
4117 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004118
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004119 return NULL;
4120
4121}
4122
4123
4124struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4125 const u8 *peer_dev_addr)
4126{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004127 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4128 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004129 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004130 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004131 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4132 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004133 }
4134
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004135 return NULL;
4136}
4137
4138
4139static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4140{
4141 struct wpa_supplicant *wpa_s = ctx;
4142
4143 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004144}
4145
4146
Dmitry Shmidt18463232014-01-24 12:29:41 -08004147static int wpas_is_concurrent_session_active(void *ctx)
4148{
4149 struct wpa_supplicant *wpa_s = ctx;
4150 struct wpa_supplicant *ifs;
4151
4152 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4153 if (ifs == wpa_s)
4154 continue;
4155 if (ifs->wpa_state > WPA_ASSOCIATED)
4156 return 1;
4157 }
4158 return 0;
4159}
4160
4161
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004162static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4163{
4164 struct wpa_supplicant *wpa_s = ctx;
4165 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4166}
4167
4168
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004169int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4170 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004171{
4172 struct wpa_interface iface;
4173 struct wpa_supplicant *p2pdev_wpa_s;
4174 char ifname[100];
4175 char force_name[100];
4176 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004177 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004178
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004179 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4180 wpa_s->ifname);
4181 if (os_snprintf_error(sizeof(ifname), ret))
4182 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004183 /* Cut length at the maximum size. Note that we don't need to ensure
4184 * collision free names here as the created interface is not a netdev.
4185 */
4186 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004187 force_name[0] = '\0';
4188 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004189
4190 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4191 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4192 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4193
4194 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004195 force_name, wpa_s->pending_interface_addr, NULL);
4196 if (ret < 0) {
4197 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4198 return ret;
4199 }
4200 os_strlcpy(wpa_s->pending_interface_name, ifname,
4201 sizeof(wpa_s->pending_interface_name));
4202
4203 os_memset(&iface, 0, sizeof(iface));
4204 iface.p2p_mgmt = 1;
4205 iface.ifname = wpa_s->pending_interface_name;
4206 iface.driver = wpa_s->driver->name;
4207 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004208
4209 /*
4210 * If a P2P Device configuration file was given, use it as the interface
4211 * configuration file (instead of using parent's configuration file.
4212 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004213 if (conf_p2p_dev) {
4214 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004215 iface.ctrl_interface = NULL;
4216 } else {
4217 iface.confname = wpa_s->confname;
4218 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4219 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004220
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004221 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004222 if (!p2pdev_wpa_s) {
4223 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4224 return -1;
4225 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004226
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004227 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004228 wpa_s->pending_interface_name[0] = '\0';
4229 return 0;
4230}
4231
4232
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004233static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4234 const u8 *noa, size_t noa_len)
4235{
4236 struct wpa_supplicant *wpa_s, *intf = ctx;
4237 char hex[100];
4238
4239 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4240 if (wpa_s->waiting_presence_resp)
4241 break;
4242 }
4243 if (!wpa_s) {
4244 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4245 return;
4246 }
4247 wpa_s->waiting_presence_resp = 0;
4248
4249 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4250 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4251 " status=%u noa=%s", MAC2STR(src), status, hex);
4252}
4253
4254
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004255static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4256 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004257 u8 *ret_ssid, size_t *ret_ssid_len,
4258 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004259{
4260 struct wpa_supplicant *wpa_s = ctx;
4261 struct wpa_ssid *s;
4262
4263 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4264 if (s) {
4265 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4266 *ret_ssid_len = s->ssid_len;
4267 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004268
4269 if (s->mode != WPAS_MODE_P2P_GO) {
4270 os_memset(intended_iface_addr, 0, ETH_ALEN);
4271 } else if (wpas_p2p_create_iface(wpa_s)) {
4272 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4273 return 0;
4274
4275 os_memcpy(intended_iface_addr,
4276 wpa_s->pending_interface_addr, ETH_ALEN);
4277 } else {
4278 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4279 ETH_ALEN);
4280 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004281 return 1;
4282 }
4283
4284 return 0;
4285}
4286
4287
4288static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004289 u8 *ssid, size_t *ssid_len, int *group_iface,
4290 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004291{
4292 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004293 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004294 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004295
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004296 /*
4297 * group_iface will be set to 1 only if a dedicated interface for P2P
4298 * role is required. First, we try to reuse an active GO. However,
4299 * if it is not present, we will try to reactivate an existing
4300 * persistent group and set group_iface to 1, so the caller will know
4301 * that the pending interface should be used.
4302 */
4303 *group_iface = 0;
4304
4305 if (freq)
4306 *freq = 0;
4307
4308 go = wpas_p2p_get_go_group(wpa_s);
4309 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004310 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004311 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004312 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004313 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4314 else
4315 return 0;
4316 } else {
4317 s = go->current_ssid;
4318 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4319 if (freq)
4320 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004321 }
4322
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004323 os_memcpy(ssid, s->ssid, s->ssid_len);
4324 *ssid_len = s->ssid_len;
4325
4326 return 1;
4327}
4328
4329
4330static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4331 const u8 *ssid, size_t ssid_len)
4332{
4333 struct wpa_supplicant *wpa_s = ctx;
4334 struct wpa_ssid *s;
4335 int save_config = 0;
4336 size_t i;
4337
4338 /* Start with our first choice of Persistent Groups */
4339 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4340 if (go && ssid && ssid_len &&
4341 s->ssid_len == ssid_len &&
4342 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4343 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4344 break;
4345
4346 /* Remove stale persistent group */
4347 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004348 wpa_dbg(wpa_s, MSG_DEBUG,
4349 "P2P: Remove stale persistent group id=%d",
4350 s->id);
4351 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004352 wpa_config_remove_network(wpa_s->conf, s->id);
4353 save_config = 1;
4354 continue;
4355 }
4356
4357 for (i = 0; i < s->num_p2p_clients; i++) {
4358 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4359 peer, ETH_ALEN) != 0)
4360 continue;
4361
4362 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4363 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4364 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4365 break;
4366 }
4367 s->num_p2p_clients--;
4368 save_config = 1;
4369 }
4370
4371 if (save_config)
4372 p2p_config_write(wpa_s);
4373
4374 /* Return TRUE if valid SSID remains */
4375 return s != NULL;
4376}
4377
4378
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004379static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4380 const u8 *feat_cap, size_t feat_cap_len)
4381{
4382 static const char pref[] = " feature_cap=";
4383 int ret;
4384
4385 buf[0] = '\0';
4386
4387 /*
4388 * We expect a feature capability to contain at least one byte to be
4389 * reported. The string buffer provided by the caller function is
4390 * expected to be big enough to contain all bytes of the attribute for
4391 * known specifications. This function truncates the reported bytes if
4392 * the feature capability data exceeds the string buffer size.
4393 */
4394 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4395 return;
4396
4397 os_memcpy(buf, pref, sizeof(pref));
4398 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4399 buf_len - sizeof(pref) + 1,
4400 feat_cap, feat_cap_len);
4401
4402 if (ret != (2 * (int) feat_cap_len))
4403 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4404}
4405
4406
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004407static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4408 const u8 *adv_mac, const u8 *ses_mac,
4409 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4410 u8 conncap, int passwd_id,
4411 const u8 *persist_ssid,
4412 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004413 int prov_start, const char *session_info,
4414 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004415 unsigned int freq,
4416 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004417{
4418 struct wpa_supplicant *wpa_s = ctx;
4419 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004420 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004421 int save_config = 0;
4422 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004423 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004424
4425 if (!dev)
4426 return;
4427
4428 os_memset(mac, 0, ETH_ALEN);
4429 if (!adv_mac)
4430 adv_mac = mac;
4431 if (!ses_mac)
4432 ses_mac = mac;
4433 if (!grp_mac)
4434 grp_mac = mac;
4435
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004436 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4437 feat_cap, feat_cap_len);
4438
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004439 if (prov_start) {
4440 if (session_info == NULL) {
4441 wpa_msg_global(wpa_s, MSG_INFO,
4442 P2P_EVENT_P2PS_PROVISION_START MACSTR
4443 " adv_id=%x conncap=%x"
4444 " adv_mac=" MACSTR
4445 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004446 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004447 MAC2STR(dev), adv_id, conncap,
4448 MAC2STR(adv_mac),
4449 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004450 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004451 } else {
4452 wpa_msg_global(wpa_s, MSG_INFO,
4453 P2P_EVENT_P2PS_PROVISION_START MACSTR
4454 " adv_id=%x conncap=%x"
4455 " adv_mac=" MACSTR
4456 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004457 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004458 MAC2STR(dev), adv_id, conncap,
4459 MAC2STR(adv_mac),
4460 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004461 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004462 }
4463 return;
4464 }
4465
4466 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4467 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4468
4469 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4470 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4471 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4472
4473 if (persistent_go && !persistent_go->num_p2p_clients) {
4474 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004475 wpa_dbg(wpa_s, MSG_DEBUG,
4476 "P2P: Remove empty persistent group id=%d",
4477 persistent_go->id);
4478 wpas_notify_persistent_group_removed(wpa_s,
4479 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004480 wpa_config_remove_network(wpa_s->conf,
4481 persistent_go->id);
4482 }
4483
4484 wpa_msg_global(wpa_s, MSG_INFO,
4485 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4486 " status=%d"
4487 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004488 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004489 MAC2STR(dev), status,
4490 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004491 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004492 return;
4493 }
4494
4495 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004496 if (persist_ssid && persist_ssid_size)
4497 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4498 persist_ssid_size);
4499
4500 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4501 is_zero_ether_addr(grp_mac)) {
4502 wpa_dbg(wpa_s, MSG_ERROR,
4503 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4504 return;
4505 }
4506
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004507 for (;;) {
4508 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4509 if (!stale)
4510 break;
4511
4512 if (s && s->ssid_len == stale->ssid_len &&
4513 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4514 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4515 break;
4516
4517 /* Remove stale persistent group */
4518 if (stale->mode != WPAS_MODE_P2P_GO ||
4519 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004520 wpa_dbg(wpa_s, MSG_DEBUG,
4521 "P2P: Remove stale persistent group id=%d",
4522 stale->id);
4523 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004524 wpa_config_remove_network(wpa_s->conf, stale->id);
4525 } else {
4526 size_t i;
4527
4528 for (i = 0; i < stale->num_p2p_clients; i++) {
4529 if (os_memcmp(stale->p2p_client_list +
4530 i * ETH_ALEN,
4531 dev, ETH_ALEN) == 0) {
4532 os_memmove(stale->p2p_client_list +
4533 i * ETH_ALEN,
4534 stale->p2p_client_list +
4535 (i + 1) * ETH_ALEN,
4536 (stale->num_p2p_clients -
4537 i - 1) * ETH_ALEN);
4538 break;
4539 }
4540 }
4541 stale->num_p2p_clients--;
4542 }
4543 save_config = 1;
4544 }
4545
4546 if (save_config)
4547 p2p_config_write(wpa_s);
4548
4549 if (s) {
4550 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4551 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4552
4553 if (persistent_go && s != persistent_go &&
4554 !persistent_go->num_p2p_clients) {
4555 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004556 wpa_dbg(wpa_s, MSG_DEBUG,
4557 "P2P: Remove empty persistent group id=%d",
4558 persistent_go->id);
4559 wpas_notify_persistent_group_removed(wpa_s,
4560 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004561 wpa_config_remove_network(wpa_s->conf,
4562 persistent_go->id);
4563 /* Save config */
4564 }
4565
4566 wpa_msg_global(wpa_s, MSG_INFO,
4567 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4568 " status=%d"
4569 " adv_id=%x adv_mac=" MACSTR
4570 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004571 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004572 MAC2STR(dev), status,
4573 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004574 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004575 return;
4576 }
4577
Hai Shalom5f92bc92019-04-18 11:54:11 -07004578 wpa_s->global->pending_p2ps_group = 0;
4579 wpa_s->global->pending_p2ps_group_freq = 0;
4580
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004581 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004582 /*
4583 * We need to copy the interface name. Simply saving a
4584 * pointer isn't enough, since if we use pending_interface_name
4585 * it will be overwritten when the group is added.
4586 */
4587 char go_ifname[100];
4588
4589 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004590 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004591 if (!response_done) {
4592 wpa_s->global->pending_p2ps_group = 1;
4593 wpa_s->global->pending_p2ps_group_freq = freq;
4594 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004596 if (!wpas_p2p_create_iface(wpa_s))
4597 os_memcpy(go_ifname, wpa_s->ifname,
4598 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004599 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004600 os_memcpy(go_ifname,
4601 wpa_s->pending_interface_name,
4602 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004603
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004604 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004605 wpas_p2ps_prov_complete(
4606 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4607 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004608 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004609 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4610 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004611 return;
4612 }
4613
4614 /* If PD Resp complete, start up the GO */
4615 if (response_done && persistent_go) {
4616 wpas_p2p_group_add_persistent(
4617 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004618 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004619 persistent_go->mode ==
4620 WPAS_MODE_P2P_GO ?
4621 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Matthew Wang06b42472022-11-10 06:56:31 +00004622 0, 0, false, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004623 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004624 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004625 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004626 }
4627
4628 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004629 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4630 ETH_ALEN);
4631 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004632 }
4633 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004634 os_memcpy(go_ifname, go_wpa_s->ifname,
4635 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004637 if (is_zero_ether_addr(grp_mac)) {
4638 wpa_dbg(go_wpa_s, MSG_DEBUG,
4639 "P2P: Setting PIN-1 for ANY");
4640 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4641 "12345670", NULL, 0,
4642 0);
4643 } else {
4644 wpa_dbg(go_wpa_s, MSG_DEBUG,
4645 "P2P: Setting PIN-1 for " MACSTR,
4646 MAC2STR(grp_mac));
4647 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4648 "12345670", NULL, 0,
4649 0);
4650 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004651
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004652 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4653 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004654 }
4655
4656 wpa_msg_global(wpa_s, MSG_INFO,
4657 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4658 " status=%d conncap=%x"
4659 " adv_id=%x adv_mac=" MACSTR
4660 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004661 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004662 MAC2STR(dev), status, conncap,
4663 adv_id, MAC2STR(adv_mac),
4664 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004665 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004666 return;
4667 }
4668
4669 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4670 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4671
4672 if (persistent_go && !persistent_go->num_p2p_clients) {
4673 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004674 wpa_dbg(wpa_s, MSG_DEBUG,
4675 "P2P: Remove empty persistent group id=%d",
4676 persistent_go->id);
4677 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004678 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4679 }
4680
4681 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004682 char ssid_hex[32 * 2 + 1];
4683
4684 if (group_ssid)
4685 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4686 group_ssid, group_ssid_len);
4687 else
4688 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004689 wpa_msg_global(wpa_s, MSG_INFO,
4690 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4691 " status=%d conncap=%x"
4692 " adv_id=%x adv_mac=" MACSTR
4693 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004694 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004695 MAC2STR(dev), status, conncap,
4696 adv_id, MAC2STR(adv_mac),
4697 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004698 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4699 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004700 } else {
4701 wpa_msg_global(wpa_s, MSG_INFO,
4702 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4703 " status=%d conncap=%x"
4704 " adv_id=%x adv_mac=" MACSTR
4705 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004706 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004707 MAC2STR(dev), status, conncap,
4708 adv_id, MAC2STR(adv_mac),
4709 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004710 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004711 }
4712}
4713
4714
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004715static int _wpas_p2p_in_progress(void *ctx)
4716{
4717 struct wpa_supplicant *wpa_s = ctx;
4718 return wpas_p2p_in_progress(wpa_s);
4719}
4720
4721
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004722static int wpas_prov_disc_resp_cb(void *ctx)
4723{
4724 struct wpa_supplicant *wpa_s = ctx;
4725 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004726 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004727
4728 if (!wpa_s->global->pending_p2ps_group)
4729 return 0;
4730
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004731 freq = wpa_s->global->pending_p2ps_group_freq;
4732 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004733 wpa_s->global->pending_p2ps_group = 0;
4734
4735 if (wpas_p2p_get_go_group(wpa_s))
4736 return 0;
4737 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4738
4739 if (persistent_go) {
4740 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004741 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4742 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004743 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004744 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Matthew Wang06b42472022-11-10 06:56:31 +00004745 is_p2p_allow_6ghz(wpa_s->global->p2p), 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004746 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004747 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4748 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004749 }
4750
4751 return 1;
4752}
4753
4754
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004755static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4756 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004757 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004758{
4759 struct wpa_supplicant *wpa_s = ctx;
4760
4761 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4762 WPA_IF_P2P_CLIENT, len, freq_list);
4763}
4764
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004765int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4766{
JaeMan Park9de97322022-07-11 15:36:43 +09004767 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004768 u8 addr[ETH_ALEN] = {0};
4769
4770 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4771 return 0;
4772
Hai Shalom60840252021-02-19 19:02:11 -08004773 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4774 if (is_zero_ether_addr(
4775 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4776 !is_zero_ether_addr(wpa_s->own_addr)) {
4777 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4778 wpa_s->own_addr, ETH_ALEN);
4779 }
4780 return 0;
4781 }
4782
4783 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004784 if (random_mac_addr(addr) < 0) {
4785 wpa_msg(wpa_s, MSG_INFO,
4786 "Failed to generate random MAC address");
4787 return -EINVAL;
4788 }
4789
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004790 /* Store generated MAC address. */
4791 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4792 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004793 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004794 /* If there are existing saved groups, restore last MAC address.
4795 * if there is no last used MAC address, the last one is
4796 * factory MAC. */
4797 if (is_zero_ether_addr(
4798 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004799 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004800 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4801 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004802 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4803 }
4804
JaeMan Park9de97322022-07-11 15:36:43 +09004805 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4806
4807 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004808 wpa_msg(wpa_s, MSG_INFO,
4809 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004810 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004811 }
4812
JaeMan Park9de97322022-07-11 15:36:43 +09004813 ret = wpa_supplicant_update_mac_addr(wpa_s);
4814
4815 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004816 wpa_msg(wpa_s, MSG_INFO,
4817 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004818 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004819 }
4820
4821 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4822 MAC2STR(addr));
4823
4824 return 0;
4825}
4826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827/**
4828 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4829 * @global: Pointer to global data from wpa_supplicant_init()
4830 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4831 * Returns: 0 on success, -1 on failure
4832 */
4833int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4834{
4835 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 int i;
4837
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004838 if (wpa_s->conf->p2p_disabled)
4839 return 0;
4840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4842 return 0;
4843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844 if (global->p2p)
4845 return 0;
4846
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004847 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4848 wpa_msg(wpa_s, MSG_ERROR,
4849 "Failed to initialize P2P random MAC address.");
4850 return -1;
4851 }
4852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004855 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856 p2p.p2p_scan = wpas_p2p_scan;
4857 p2p.send_action = wpas_send_action;
4858 p2p.send_action_done = wpas_send_action_done;
4859 p2p.go_neg_completed = wpas_go_neg_completed;
4860 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4861 p2p.dev_found = wpas_dev_found;
4862 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004863 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 p2p.start_listen = wpas_start_listen;
4865 p2p.stop_listen = wpas_stop_listen;
4866 p2p.send_probe_resp = wpas_send_probe_resp;
4867 p2p.sd_request = wpas_sd_request;
4868 p2p.sd_response = wpas_sd_response;
4869 p2p.prov_disc_req = wpas_prov_disc_req;
4870 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004871 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 p2p.invitation_process = wpas_invitation_process;
4873 p2p.invitation_received = wpas_invitation_received;
4874 p2p.invitation_result = wpas_invitation_result;
4875 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004876 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004877 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004878 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004879 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004880 p2p.get_persistent_group = wpas_get_persistent_group;
4881 p2p.get_go_info = wpas_get_go_info;
4882 p2p.remove_stale_groups = wpas_remove_stale_groups;
4883 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4884 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4885 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004886 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004887 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888
4889 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004890 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891 p2p.dev_name = wpa_s->conf->device_name;
4892 p2p.manufacturer = wpa_s->conf->manufacturer;
4893 p2p.model_name = wpa_s->conf->model_name;
4894 p2p.model_number = wpa_s->conf->model_number;
4895 p2p.serial_number = wpa_s->conf->serial_number;
4896 if (wpa_s->wps) {
4897 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4898 p2p.config_methods = wpa_s->wps->config_methods;
4899 }
4900
Hai Shalom60840252021-02-19 19:02:11 -08004901 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4902 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004903 wpa_printf(MSG_ERROR,
4904 "P2P: Failed to configure supported channel list");
4905 return -1;
4906 }
4907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 if (wpa_s->conf->p2p_listen_reg_class &&
4909 wpa_s->conf->p2p_listen_channel) {
4910 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4911 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004912 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914 /*
4915 * Pick one of the social channels randomly as the listen
4916 * channel.
4917 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004918 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004919 &p2p.channel,
4920 &global->p2p_go_avoid_freq,
4921 &global->p2p_disallow_freq) !=
4922 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004923 wpa_printf(MSG_INFO,
4924 "P2P: No social channels supported by the driver - do not enable P2P");
4925 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004926 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004927 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004929 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4930 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931
4932 if (wpa_s->conf->p2p_oper_reg_class &&
4933 wpa_s->conf->p2p_oper_channel) {
4934 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4935 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4936 p2p.cfg_op_channel = 1;
4937 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4938 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4939
4940 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004942 * Use random operation channel from 2.4 GHz band social
4943 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4944 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004946 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004947 &p2p.op_channel, NULL,
4948 NULL) != 0) {
4949 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004950 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004951 p2p.op_reg_class = 0;
4952 p2p.op_channel = 0;
4953 /* This will be overridden during group setup in
4954 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004955 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956 p2p.cfg_op_channel = 0;
4957 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4958 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4959 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004960
4961 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4962 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4963 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4964 }
4965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4967 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4968 p2p.country[2] = 0x04;
4969 } else
4970 os_memcpy(p2p.country, "XX\x04", 3);
4971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4973 WPS_DEV_TYPE_LEN);
4974
4975 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4976 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4977 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4978
4979 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4980 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4981
4982 p2p.max_peers = 100;
4983
4984 if (wpa_s->conf->p2p_ssid_postfix) {
4985 p2p.ssid_postfix_len =
4986 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4987 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4988 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4989 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4990 p2p.ssid_postfix_len);
4991 }
4992
4993 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4994
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004995 p2p.max_listen = wpa_s->max_remain_on_chan;
4996
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004997 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4998 wpa_s->conf->p2p_passphrase_len <= 63)
4999 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5000 else
5001 p2p.passphrase_len = 8;
5002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 global->p2p = p2p_init(&p2p);
5004 if (global->p2p == NULL)
5005 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005006 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007
5008 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5009 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5010 continue;
5011 p2p_add_wps_vendor_extension(
5012 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5013 }
5014
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005015 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017 return 0;
5018}
5019
5020
5021/**
5022 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5023 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5024 *
5025 * This function deinitialize per-interface P2P data.
5026 */
5027void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5028{
5029 if (wpa_s->driver && wpa_s->drv_priv)
5030 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005031
5032 if (wpa_s->go_params) {
5033 /* Clear any stored provisioning info */
5034 p2p_clear_provisioning_info(
5035 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005036 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005037 }
5038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039 os_free(wpa_s->go_params);
5040 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005041 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5043 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005044 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5046 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5047 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005048 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005049 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005050 wpas_p2p_listen_work_done(wpa_s);
5051 if (wpa_s->p2p_send_action_work) {
5052 os_free(wpa_s->p2p_send_action_work->ctx);
5053 radio_work_done(wpa_s->p2p_send_action_work);
5054 wpa_s->p2p_send_action_work = NULL;
5055 }
5056 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005058 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5059 wpa_s->p2p_oob_dev_pw = NULL;
5060
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005061 os_free(wpa_s->p2p_group_common_freqs);
5062 wpa_s->p2p_group_common_freqs = NULL;
5063 wpa_s->p2p_group_common_freqs_num = 0;
5064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065 /* TODO: remove group interface from the driver if this wpa_s instance
5066 * is on top of a P2P group interface */
5067}
5068
5069
5070/**
5071 * wpas_p2p_deinit_global - Deinitialize global P2P module
5072 * @global: Pointer to global data from wpa_supplicant_init()
5073 *
5074 * This function deinitializes the global (per device) P2P module.
5075 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005076static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077{
5078 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005080 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005081
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005082 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083
5084 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5086 wpa_s = wpa_s->next;
5087 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 tmp = global->ifaces;
5089 while (tmp &&
5090 (tmp == wpa_s ||
5091 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5092 tmp = tmp->next;
5093 }
5094 if (tmp == NULL)
5095 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005096 /* Disconnect from the P2P group and deinit the interface */
5097 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 }
5099
5100 /*
5101 * Deinit GO data on any possibly remaining interface (if main
5102 * interface is used as GO).
5103 */
5104 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5105 if (wpa_s->ap_iface)
5106 wpas_p2p_group_deinit(wpa_s);
5107 }
5108
5109 p2p_deinit(global->p2p);
5110 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005111 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112}
5113
5114
5115static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5116{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005117 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005118 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119 if (wpa_s->drv_flags &
5120 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5121 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5122 return 1; /* P2P group requires a new interface in every case
5123 */
5124 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5125 return 0; /* driver does not support concurrent operations */
5126 if (wpa_s->global->ifaces->next)
5127 return 1; /* more that one interface already in use */
5128 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5129 return 1; /* this interface is already in use */
5130 return 0;
5131}
5132
5133
5134static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5135 const u8 *peer_addr,
5136 enum p2p_wps_method wps_method,
5137 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005138 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005139 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005141 if (persistent_group && wpa_s->conf->persistent_reconnect)
5142 persistent_group = 2;
5143
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005144 /*
5145 * Increase GO config timeout if HT40 is used since it takes some time
5146 * to scan channels for coex purposes before the BSS can be started.
5147 */
5148 p2p_set_config_timeout(wpa_s->global->p2p,
5149 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5152 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005153 persistent_group, ssid ? ssid->ssid : NULL,
5154 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005155 wpa_s->p2p_pd_before_go_neg, pref_freq,
5156 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5157 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158}
5159
5160
5161static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5162 const u8 *peer_addr,
5163 enum p2p_wps_method wps_method,
5164 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005165 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005166 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005168 if (persistent_group && wpa_s->conf->persistent_reconnect)
5169 persistent_group = 2;
5170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5172 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005173 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005174 ssid ? ssid->ssid_len : 0, pref_freq,
5175 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5176 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177}
5178
5179
5180static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5181{
5182 wpa_s->p2p_join_scan_count++;
5183 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5184 wpa_s->p2p_join_scan_count);
5185 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5186 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5187 " for join operationg - stop join attempt",
5188 MAC2STR(wpa_s->pending_join_iface_addr));
5189 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005190 if (wpa_s->p2p_auto_pd) {
5191 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005192 wpa_msg_global(wpa_s, MSG_INFO,
5193 P2P_EVENT_PROV_DISC_FAILURE
5194 " p2p_dev_addr=" MACSTR " status=N/A",
5195 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005196 return;
5197 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005198 if (wpa_s->p2p_fallback_to_go_neg) {
5199 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5200 "failed - fall back to GO Negotiation");
5201 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5202 P2P_EVENT_FALLBACK_TO_GO_NEG
5203 "reason=join-failed");
5204 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5205 return;
5206 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005207 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005208 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005209 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210 }
5211}
5212
5213
Dmitry Shmidt04949592012-07-19 12:16:46 -07005214static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5215{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005216 int res;
5217 unsigned int num, i;
5218 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005219
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005220 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5221 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005222 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005223 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005224
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005225 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5226 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005227 if (!freqs)
5228 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005229
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005230 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5231 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005232
5233 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005234 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005235 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5236 freq);
5237 res = 0;
5238 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005239 }
5240 }
5241
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005242 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005243 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005244
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005245exit_free:
5246 os_free(freqs);
5247 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005248}
5249
5250
5251static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5252 const u8 *peer_dev_addr)
5253{
5254 struct wpa_bss *bss;
5255 int updated;
5256
5257 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5258 if (bss == NULL)
5259 return -1;
5260 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5261 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5262 "last scan");
5263 return 0;
5264 }
5265
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005266 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5267 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005268 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5269 "%ld.%06ld (%supdated in last scan)",
5270 bss->last_update.sec, bss->last_update.usec,
5271 updated ? "": "not ");
5272
5273 return updated;
5274}
5275
5276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5278 struct wpa_scan_results *scan_res)
5279{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005280 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281 int freq;
5282 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005284 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5285
5286 if (wpa_s->global->p2p_disabled)
5287 return;
5288
Dmitry Shmidt04949592012-07-19 12:16:46 -07005289 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5290 scan_res ? (int) scan_res->num : -1,
5291 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292
5293 if (scan_res)
5294 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5295
Dmitry Shmidt04949592012-07-19 12:16:46 -07005296 if (wpa_s->p2p_auto_pd) {
5297 int join = wpas_p2p_peer_go(wpa_s,
5298 wpa_s->pending_join_dev_addr);
5299 if (join == 0 &&
5300 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5301 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005302 bss = wpa_bss_get_bssid_latest(
5303 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005304 if (bss) {
5305 freq = bss->freq;
5306 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5307 "the peer " MACSTR " at %d MHz",
5308 wpa_s->auto_pd_scan_retry,
5309 MAC2STR(wpa_s->
5310 pending_join_dev_addr),
5311 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005312 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005313 return;
5314 }
5315 }
5316
5317 if (join < 0)
5318 join = 0;
5319
5320 wpa_s->p2p_auto_pd = 0;
5321 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5322 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5323 MAC2STR(wpa_s->pending_join_dev_addr), join);
5324 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005325 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005326 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005327 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005328 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005329 wpa_msg_global(wpa_s, MSG_INFO,
5330 P2P_EVENT_PROV_DISC_FAILURE
5331 " p2p_dev_addr=" MACSTR " status=N/A",
5332 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005333 }
5334 return;
5335 }
5336
5337 if (wpa_s->p2p_auto_join) {
5338 int join = wpas_p2p_peer_go(wpa_s,
5339 wpa_s->pending_join_dev_addr);
5340 if (join < 0) {
5341 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5342 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005343 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005344 P2P_EVENT_FALLBACK_TO_GO_NEG
5345 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005346 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5347 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5348 wpa_s->p2p_persistent_group, 0, 0, 0,
5349 wpa_s->p2p_go_intent,
5350 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005351 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005352 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005353 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005354 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005355 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005356 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005357 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005358 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005359 NULL, 0,
5360 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005361 return;
5362 }
5363
5364 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5365 "try to join the group", join ? "" :
5366 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005367 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005368 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005369 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005370 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005371 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005372 }
5373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5375 wpa_s->pending_join_iface_addr);
5376 if (freq < 0 &&
5377 p2p_get_interface_addr(wpa_s->global->p2p,
5378 wpa_s->pending_join_dev_addr,
5379 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005380 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5381 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5383 "address for join from " MACSTR " to " MACSTR
5384 " based on newly discovered P2P peer entry",
5385 MAC2STR(wpa_s->pending_join_iface_addr),
5386 MAC2STR(iface_addr));
5387 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5388 ETH_ALEN);
5389
5390 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5391 wpa_s->pending_join_iface_addr);
5392 }
5393 if (freq >= 0) {
5394 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5395 "from P2P peer table: %d MHz", freq);
5396 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005397 if (wpa_s->p2p_join_ssid_len) {
5398 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5399 MACSTR " and SSID %s",
5400 MAC2STR(wpa_s->pending_join_iface_addr),
5401 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5402 wpa_s->p2p_join_ssid_len));
5403 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5404 wpa_s->p2p_join_ssid,
5405 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005406 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005407 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5408 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5409 bss = wpa_bss_get_bssid_latest(wpa_s,
5410 wpa_s->pending_join_iface_addr);
5411 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005412 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005413 u8 dev_addr[ETH_ALEN];
5414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005415 freq = bss->freq;
5416 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005417 "from BSS table: %d MHz (SSID %s)", freq,
5418 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005419 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005420 dev_addr) == 0 &&
5421 os_memcmp(wpa_s->pending_join_dev_addr,
5422 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5423 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5424 ETH_ALEN) != 0) {
5425 wpa_printf(MSG_DEBUG,
5426 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5427 MAC2STR(dev_addr),
5428 MAC2STR(wpa_s->pending_join_dev_addr));
5429 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5430 ETH_ALEN);
5431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432 }
5433 if (freq > 0) {
5434 u16 method;
5435
Dmitry Shmidt04949592012-07-19 12:16:46 -07005436 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005437 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005438 P2P_EVENT_GROUP_FORMATION_FAILURE
5439 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005440 wpas_notify_p2p_group_formation_failure(
5441 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005442 return;
5443 }
5444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5446 "prior to joining an existing group (GO " MACSTR
5447 " freq=%u MHz)",
5448 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5449 wpa_s->pending_pd_before_join = 1;
5450
5451 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452 case WPS_PIN_DISPLAY:
5453 method = WPS_CONFIG_KEYPAD;
5454 break;
5455 case WPS_PIN_KEYPAD:
5456 method = WPS_CONFIG_DISPLAY;
5457 break;
5458 case WPS_PBC:
5459 method = WPS_CONFIG_PUSHBUTTON;
5460 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005461 case WPS_P2PS:
5462 method = WPS_CONFIG_P2PS;
5463 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 default:
5465 method = 0;
5466 break;
5467 }
5468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005469 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5470 wpa_s->pending_join_dev_addr) ==
5471 method)) {
5472 /*
5473 * We have already performed provision discovery for
5474 * joining the group. Proceed directly to join
5475 * operation without duplicated provision discovery. */
5476 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5477 "with " MACSTR " already done - proceed to "
5478 "join",
5479 MAC2STR(wpa_s->pending_join_dev_addr));
5480 wpa_s->pending_pd_before_join = 0;
5481 goto start;
5482 }
5483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005485 wpa_s->pending_join_dev_addr,
5486 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005487 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5489 "Discovery Request before joining an "
5490 "existing group");
5491 wpa_s->pending_pd_before_join = 0;
5492 goto start;
5493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005494 return;
5495 }
5496
5497 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5498 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5499 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5500 wpas_p2p_check_join_scan_limit(wpa_s);
5501 return;
5502
5503start:
5504 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005505 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5506 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507}
5508
5509
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005510static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5511 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513 int ret;
5514 struct wpa_driver_scan_params params;
5515 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005516 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005517 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005518 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519
5520 os_memset(&params, 0, sizeof(params));
5521
5522 /* P2P Wildcard SSID */
5523 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005524 if (ssid && ssid_len) {
5525 params.ssids[0].ssid = ssid;
5526 params.ssids[0].ssid_len = ssid_len;
5527 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5528 wpa_s->p2p_join_ssid_len = ssid_len;
5529 } else {
5530 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5531 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5532 wpa_s->p2p_join_ssid_len = 0;
5533 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534
5535 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005536 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5537 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5538 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539 if (wps_ie == NULL) {
5540 wpas_p2p_scan_res_join(wpa_s, NULL);
5541 return;
5542 }
5543
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005544 if (!freq) {
5545 int oper_freq;
5546 /*
5547 * If freq is not provided, check the operating freq of the GO
5548 * and use a single channel scan on if possible.
5549 */
5550 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5551 wpa_s->pending_join_iface_addr);
5552 if (oper_freq > 0)
5553 freq = oper_freq;
5554 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005555 if (freq > 0) {
5556 freqs[0] = freq;
5557 params.freqs = freqs;
Hai Shalomc1a21442022-02-04 13:43:00 -08005558 } else if (wpa_s->conf->p2p_6ghz_disable ||
5559 !is_p2p_allow_6ghz(wpa_s->global->p2p)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07005560 wpa_printf(MSG_DEBUG,
5561 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005562 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5563 0);
5564 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5565 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005566 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005568 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5569 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5570 if (ies == NULL) {
5571 wpabuf_free(wps_ie);
5572 wpas_p2p_scan_res_join(wpa_s, NULL);
5573 return;
5574 }
5575 wpabuf_put_buf(ies, wps_ie);
5576 wpabuf_free(wps_ie);
5577
5578 bands = wpas_get_bands(wpa_s, freqs);
5579 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5580
5581 params.p2p_probe = 1;
5582 params.extra_ies = wpabuf_head(ies);
5583 params.extra_ies_len = wpabuf_len(ies);
5584
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005585 if (wpa_s->clear_driver_scan_cache) {
5586 wpa_printf(MSG_DEBUG,
5587 "Request driver to clear scan cache due to local BSS flush");
5588 params.only_new_results = 1;
5589 }
5590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 /*
5592 * Run a scan to update BSS table and start Provision Discovery once
5593 * the new scan results become available.
5594 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005595 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005596 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005597 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005598 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005599 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005600 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005601 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005602 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604
5605 wpabuf_free(ies);
5606
5607 if (ret) {
5608 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5609 "try again later");
5610 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5611 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5612 wpas_p2p_check_join_scan_limit(wpa_s);
5613 }
5614}
5615
5616
Dmitry Shmidt04949592012-07-19 12:16:46 -07005617static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5618{
5619 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005620 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005621}
5622
5623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005626 int auto_join, int op_freq,
5627 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628{
5629 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005630 MACSTR " dev " MACSTR " op_freq=%d)%s",
5631 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005632 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005633 if (ssid && ssid_len) {
5634 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5635 wpa_ssid_txt(ssid, ssid_len));
5636 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005637
Dmitry Shmidt04949592012-07-19 12:16:46 -07005638 wpa_s->p2p_auto_pd = 0;
5639 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5641 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5642 wpa_s->pending_join_wps_method = wps_method;
5643
5644 /* Make sure we are not running find during connection establishment */
5645 wpas_p2p_stop_find(wpa_s);
5646
5647 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005648 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005649 return 0;
5650}
5651
5652
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005653static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5654 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655{
5656 struct wpa_supplicant *group;
5657 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005658 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659
5660 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5661 if (group == NULL)
5662 return -1;
5663 if (group != wpa_s) {
5664 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5665 sizeof(group->p2p_pin));
5666 group->p2p_wps_method = wpa_s->p2p_wps_method;
5667 }
5668
Hai Shalom74f70d42019-02-11 14:42:39 -08005669 /*
5670 * Need to mark the current interface for p2p_group_formation
5671 * when a separate group interface is not used. This is needed
5672 * to allow p2p_cancel stop a pending p2p_connect-join.
5673 * wpas_p2p_init_group_interface() addresses this for the case
5674 * where a separate group interface is used.
5675 */
5676 if (group == wpa_s->parent)
5677 wpa_s->global->p2p_group_formation = group;
5678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005680 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681
5682 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005683 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5685 ETH_ALEN);
5686 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005687 if (freq && ssid && ssid_len) {
5688 res.freq = freq;
5689 res.ssid_len = ssid_len;
5690 os_memcpy(res.ssid, ssid, ssid_len);
5691 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005692 if (ssid && ssid_len) {
5693 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5694 ssid, ssid_len);
5695 } else {
5696 bss = wpa_bss_get_bssid_latest(
5697 wpa_s, wpa_s->pending_join_iface_addr);
5698 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005699 if (bss) {
5700 res.freq = bss->freq;
5701 res.ssid_len = bss->ssid_len;
5702 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5703 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5704 bss->freq,
5705 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005706 } else if (ssid && ssid_len) {
5707 res.ssid_len = ssid_len;
5708 os_memcpy(res.ssid, ssid, ssid_len);
5709 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5710 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005711 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005712 }
5713
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005714 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5715 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5716 "starting client");
5717 wpa_drv_cancel_remain_on_channel(wpa_s);
5718 wpa_s->off_channel_freq = 0;
5719 wpa_s->roc_waiting_drv_freq = 0;
5720 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721 wpas_start_wps_enrollee(group, &res);
5722
5723 /*
5724 * Allow a longer timeout for join-a-running-group than normal 15
5725 * second group formation timeout since the GO may not have authorized
5726 * our connection yet.
5727 */
5728 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5729 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5730 wpa_s, NULL);
5731
5732 return 0;
5733}
5734
5735
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005736static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005737 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005738 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005739 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005740{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005741 struct wpa_used_freq_data *freqs;
5742 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005743 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305744 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005745
5746 max_pref_freq = *num_pref_freq;
5747 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005748
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005749 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5750 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005751 if (!freqs)
5752 return -1;
5753
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005754 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5755 wpa_s->num_multichan_concurrent);
5756
5757 /*
5758 * It is possible that the total number of used frequencies is bigger
5759 * than the number of frequencies used for P2P, so get the system wide
5760 * number of unused frequencies.
5761 */
5762 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5763
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005764 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005765 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5766 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005767
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005768 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005769 int ret;
5770 if (go)
5771 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5772 else
5773 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5774 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005775 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005776 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5777 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005778 /*
5779 * If freq is a DFS channel and DFS is offloaded
5780 * to the driver, allow P2P GO to use it.
5781 */
5782 wpa_printf(MSG_DEBUG,
5783 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5784 freq);
5785 } else {
5786 wpa_printf(MSG_DEBUG,
5787 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5788 freq);
5789 res = -3;
5790 goto exit_free;
5791 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005792 }
5793
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005794 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005795 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005796 freq_in_use = 1;
5797 }
5798
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005799 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005800 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5801 freq);
5802 res = -2;
5803 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005804 }
5805 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5806 "requested channel (%u MHz)", freq);
5807 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005808 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005809 }
5810
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005811 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005812
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305813 if (*pref_freq == 0) {
5814 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005815 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305816 while (i < wpa_s->conf->num_p2p_pref_chan) {
5817 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5818 wpa_s->conf->p2p_pref_chan[i].op_class,
5819 wpa_s->conf->p2p_pref_chan[i].chan);
5820
5821 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5822 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5823 best_freq = p2p_pref_freq;
5824 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5825 "from P2P preferred channel list", best_freq);
5826 break;
5827 } else {
5828 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5829 "frequency (%u MHz) ", p2p_pref_freq);
5830 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005831 i++;
5832 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305833 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305834 enum wpa_driver_if_type iface_type;
5835
5836 if (go)
5837 iface_type = WPA_IF_P2P_GO;
5838 else
5839 iface_type = WPA_IF_P2P_CLIENT;
5840
5841 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5842 best_freq, go);
5843
5844 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5845 &max_pref_freq,
5846 pref_freq_list);
5847 if (!res && max_pref_freq > 0) {
5848 *num_pref_freq = max_pref_freq;
5849 i = 0;
5850 while (i < *num_pref_freq &&
5851 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005852 pref_freq_list[i].freq) ||
5853 wpas_p2p_disallowed_freq(
5854 wpa_s->global,
5855 pref_freq_list[i].freq) ||
5856 !p2p_pref_freq_allowed(&pref_freq_list[i],
5857 go))) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305858 wpa_printf(MSG_DEBUG,
5859 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00005860 i, pref_freq_list[i].freq);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305861 i++;
5862 }
5863 if (i != *num_pref_freq) {
Sunil8cd6f4d2022-06-28 18:40:46 +00005864 best_freq = pref_freq_list[i].freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305865 wpa_printf(MSG_DEBUG,
5866 "P2P: Using preferred_freq_list[%d]=%d",
5867 i, best_freq);
5868 } else {
5869 wpa_printf(MSG_DEBUG,
5870 "P2P: All driver preferred frequencies are "
5871 "disallowed for P2P use");
5872 *num_pref_freq = 0;
5873 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005874 } else {
5875 wpa_printf(MSG_DEBUG,
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305876 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005877 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005878 }
5879 }
5880
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005881 /* We have a candidate frequency to use */
5882 if (best_freq > 0) {
5883 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005884 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005885 best_freq);
5886 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005887 } else {
5888 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 -07005889 best_freq);
5890 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005891 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005892 } else if (num_unused > 0) {
5893 wpa_printf(MSG_DEBUG,
5894 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5895 *force_freq = 0;
5896 } else {
5897 wpa_printf(MSG_DEBUG,
5898 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5899 res = -2;
5900 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005901 }
5902
5903exit_ok:
5904 res = 0;
5905exit_free:
5906 os_free(freqs);
5907 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005908}
5909
5910
Hai Shalomc1a21442022-02-04 13:43:00 -08005911static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5912 const u8 *peer_addr)
5913{
5914 if (wpa_s->conf->p2p_6ghz_disable ||
5915 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5916 HOSTAPD_MODE_IEEE80211A, true))
5917 return false;
5918
5919 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5920 return false;
5921 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5922 return false;
5923
5924 return true;
5925}
5926
5927
5928static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5929 const u8 *peer_addr, bool allow_6ghz, int freq)
5930{
5931 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5932 wpa_printf(MSG_DEBUG,
5933 "P2P: Allow connection on 6 GHz channels");
5934 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5935 } else {
5936 if (is_6ghz_freq(freq))
5937 return -2;
5938 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5939 }
5940
5941 return 0;
5942}
5943
5944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005945/**
5946 * wpas_p2p_connect - Request P2P Group Formation to be started
5947 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5948 * @peer_addr: Address of the peer P2P Device
5949 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5950 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005951 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005952 * @join: Whether to join an existing group (as a client) instead of starting
5953 * Group Owner negotiation; @peer_addr is BSSID in that case
5954 * @auth: Whether to only authorize the connection instead of doing that and
5955 * initiating Group Owner negotiation
5956 * @go_intent: GO Intent or -1 to use default
5957 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005958 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005959 * @persistent_id: Persistent group credentials to use for forcing GO
5960 * parameters or -1 to generate new values (SSID/passphrase)
5961 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5962 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005963 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005964 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005965 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005966 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005967 * @group_ssid: Specific Group SSID for join or %NULL if not set
5968 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08005969 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5971 * failure, -2 on failure due to channel not currently available,
5972 * -3 if forced channel is not supported
5973 */
5974int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5975 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005976 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005977 int go_intent, int freq, unsigned int vht_center_freq2,
5978 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005979 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005980 const u8 *group_ssid, size_t group_ssid_len,
5981 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005983 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005984 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005985 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005986 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005987 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00005988 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
5989 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005990
5991 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5992 return -1;
5993
Dmitry Shmidt04949592012-07-19 12:16:46 -07005994 if (persistent_id >= 0) {
5995 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5996 if (ssid == NULL || ssid->disabled != 2 ||
5997 ssid->mode != WPAS_MODE_P2P_GO)
5998 return -1;
5999 }
6000
Hai Shalomc1a21442022-02-04 13:43:00 -08006001 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006002 return -2;
6003
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006004 os_free(wpa_s->global->add_psk);
6005 wpa_s->global->add_psk = NULL;
6006
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006007 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006008 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006009 wpa_s->global->pending_p2ps_group_freq = 0;
6010 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006012 if (go_intent < 0)
6013 go_intent = wpa_s->conf->p2p_go_intent;
6014
6015 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006016 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006017
6018 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006019 wpa_s->p2p_persistent_group = !!persistent_group;
6020 wpa_s->p2p_persistent_id = persistent_id;
6021 wpa_s->p2p_go_intent = go_intent;
6022 wpa_s->p2p_connect_freq = freq;
6023 wpa_s->p2p_fallback_to_go_neg = 0;
6024 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006025 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006026 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006027 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6028 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006029 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006030 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006031
6032 if (pin)
6033 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6034 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006035 if (wps_generate_pin((unsigned int *) &ret) < 0)
6036 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006037 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6038 "%08d", ret);
6039 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6040 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006041 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6042 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006043 } else if (wps_method == WPS_P2PS) {
6044 /* Force the P2Ps default PIN to be used */
6045 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 } else
6047 wpa_s->p2p_pin[0] = '\0';
6048
Dmitry Shmidt04949592012-07-19 12:16:46 -07006049 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6051 if (auth) {
6052 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6053 "connect a running group from " MACSTR,
6054 MAC2STR(peer_addr));
6055 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6056 return ret;
6057 }
6058 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6059 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6060 iface_addr) < 0) {
6061 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6062 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6063 dev_addr);
6064 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006065 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006066 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006067 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6068 "%ld.%06ld",
6069 wpa_s->p2p_auto_started.sec,
6070 wpa_s->p2p_auto_started.usec);
6071 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006072 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006073 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006074 auto_join, freq,
6075 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076 return -1;
6077 return ret;
6078 }
6079
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006080 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006081 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006082 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006083 if (res)
6084 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006085 wpas_p2p_set_own_freq_preference(wpa_s,
6086 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006087
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006088 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6091
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006092 if (wpa_s->create_p2p_iface) {
6093 /* Prepare to add a new interface for the group */
6094 iftype = WPA_IF_P2P_GROUP;
6095 if (go_intent == 15)
6096 iftype = WPA_IF_P2P_GO;
6097 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6098 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6099 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006100 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006103 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006104 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006105 if (wpa_s->p2p_mgmt)
6106 if_addr = wpa_s->parent->own_addr;
6107 else
6108 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006109 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111
6112 if (auth) {
6113 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006114 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006115 force_freq, persistent_group, ssid,
6116 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117 return -1;
6118 return ret;
6119 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006120
6121 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6122 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006123 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006124 if (wpa_s->create_p2p_iface)
6125 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126 return -1;
6127 }
6128 return ret;
6129}
6130
6131
6132/**
6133 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6134 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6135 * @freq: Frequency of the channel in MHz
6136 * @duration: Duration of the stay on the channel in milliseconds
6137 *
6138 * This callback is called when the driver indicates that it has started the
6139 * requested remain-on-channel duration.
6140 */
6141void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6142 unsigned int freq, unsigned int duration)
6143{
6144 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6145 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006146 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)",
6147 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6148 wpa_s->roc_waiting_drv_freq, freq, duration);
6149 if (wpa_s->off_channel_freq &&
6150 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6152 wpa_s->pending_listen_duration);
6153 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006154 } else {
6155 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6156 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6157 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 }
6159}
6160
6161
Jithu Jance57cea1a2014-08-20 10:22:04 -07006162int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006163{
6164 /* Limit maximum Listen state time based on driver limitation. */
6165 if (timeout > wpa_s->max_remain_on_chan)
6166 timeout = wpa_s->max_remain_on_chan;
6167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168 return p2p_listen(wpa_s->global->p2p, timeout);
6169}
6170
6171
6172/**
6173 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6174 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6175 * @freq: Frequency of the channel in MHz
6176 *
6177 * This callback is called when the driver indicates that a remain-on-channel
6178 * operation has been completed, i.e., the duration on the requested channel
6179 * has timed out.
6180 */
6181void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6182 unsigned int freq)
6183{
6184 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6185 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006186 wpa_s->global->p2p_long_listen,
6187 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006188 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6190 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006191 if (wpa_s->global->p2p_long_listen > 0)
6192 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6194 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006195 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006196 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006197 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006199 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006200 } else {
6201 /*
6202 * When listen duration is over, stop listen & update p2p_state
6203 * to IDLE.
6204 */
6205 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206 }
6207}
6208
6209
6210/**
6211 * wpas_p2p_group_remove - Remove a P2P group
6212 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6213 * @ifname: Network interface name of the group interface or "*" to remove all
6214 * groups
6215 * Returns: 0 on success, -1 on failure
6216 *
6217 * This function is used to remove a P2P group. This can be used to disconnect
6218 * from a group in which the local end is a P2P Client or to end a P2P Group in
6219 * case the local end is the Group Owner. If a virtual network interface was
6220 * created for this group, that interface will be removed. Otherwise, only the
6221 * configured P2P group network will be removed from the interface.
6222 */
6223int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6224{
6225 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006226 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006227
6228 if (os_strcmp(ifname, "*") == 0) {
6229 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006230 bool calling_wpa_s_group_removed = false;
6231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006232 wpa_s = global->ifaces;
6233 while (wpa_s) {
6234 prev = wpa_s;
6235 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006236 if (prev->p2p_group_interface !=
6237 NOT_P2P_GROUP_INTERFACE ||
6238 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006239 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006240 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006241 if (prev == calling_wpa_s)
6242 calling_wpa_s_group_removed = true;
6243 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006244 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006245
6246 if (!calling_wpa_s_group_removed &&
6247 (calling_wpa_s->p2p_group_interface !=
6248 NOT_P2P_GROUP_INTERFACE ||
6249 (calling_wpa_s->current_ssid &&
6250 calling_wpa_s->current_ssid->p2p_group))) {
6251 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6252 wpas_p2p_disconnect_safely(calling_wpa_s,
6253 calling_wpa_s);
6254 }
6255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 return 0;
6257 }
6258
6259 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6260 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6261 break;
6262 }
6263
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006264 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265}
6266
6267
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006268static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6269{
6270 unsigned int r;
6271
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006272 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6273 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006274 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006275 int res;
6276
6277 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6278 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006279 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6280 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6281
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006282 if (!res && size > 0) {
6283 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006284 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006285 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006286 pref_freq_list[i].freq) ||
6287 wpas_p2p_disallowed_freq(
6288 wpa_s->global,
6289 pref_freq_list[i].freq) ||
6290 !p2p_pref_freq_allowed(&pref_freq_list[i],
6291 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006292 wpa_printf(MSG_DEBUG,
6293 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006294 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006295 i++;
6296 }
6297 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006298 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006299 wpa_printf(MSG_DEBUG,
6300 "P2P: Using preferred_freq_list[%d]=%d",
6301 i, freq);
6302 } else {
6303 wpa_printf(MSG_DEBUG,
6304 "P2P: All driver preferred frequencies are disallowed for P2P use");
6305 }
6306 } else {
6307 wpa_printf(MSG_DEBUG,
6308 "P2P: No preferred frequency list available");
6309 }
6310 }
6311
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006312 if (freq == 2) {
6313 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6314 "band");
6315 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006316 p2p_supported_freq_go(wpa_s->global->p2p,
6317 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006318 freq = wpa_s->best_24_freq;
6319 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6320 "channel: %d MHz", freq);
6321 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006322 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6323 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006324 int possible_2g_freqs[] = {
6325 /* operating class 81 */
6326 2412, 2437, 2462,
6327 };
6328 int possible_2g_freqs_num =
6329 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6330 int i;
6331 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6332 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6333 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6334 break;
6335 }
6336 }
6337
6338 if (i >= possible_2g_freqs_num) {
6339 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6340 "2.4 GHz channel for P2P group");
6341 return -1;
6342 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006343 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6344 "channel: %d MHz", freq);
6345 }
6346 }
6347
6348 if (freq == 5) {
6349 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6350 "band");
6351 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006352 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006353 wpa_s->best_5_freq)) {
6354 freq = wpa_s->best_5_freq;
6355 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6356 "channel: %d MHz", freq);
6357 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006358 const int freqs[] = {
6359 /* operating class 115 */
6360 5180, 5200, 5220, 5240,
6361 /* operating class 124 */
6362 5745, 5765, 5785, 5805,
6363 };
6364 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6365
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006366 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6367 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006368
6369 /*
6370 * most of 5G channels are DFS, only operating class 115 and 124
6371 * are available possibly, randomly pick a start to check them.
6372 */
6373 int possible_5g_freqs[] = {
6374 /* operating class 115 */
6375 5180, 5200, 5220, 5240,
6376 /* operating class 124 */
6377 5745, 5765, 5785, 5805,
6378 };
6379 int possible_5g_freqs_num =
6380 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6381
Jimmy Chen64b90632020-09-29 17:27:34 +08006382 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6383 if (p2p_supported_freq_go(
6384 wpa_s->global->p2p,
6385 possible_5g_freqs[r % possible_5g_freqs_num])) {
6386 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6387 break;
6388 }
6389 }
6390
6391 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006392 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6393 "5 GHz channel for P2P group");
6394 return -1;
6395 }
6396 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6397 "channel: %d MHz", freq);
6398 }
6399 }
6400
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006401 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006402 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006403 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6404 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006405 /*
6406 * If freq is a DFS channel and DFS is offloaded to the
6407 * driver, allow P2P GO to use it.
6408 */
6409 wpa_printf(MSG_DEBUG, "P2P: "
6410 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6411 __func__, freq);
6412 return freq;
6413 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006414 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6415 "(%u MHz) is not supported for P2P uses",
6416 freq);
6417 return -1;
6418 }
6419
6420 return freq;
6421}
6422
6423
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006424static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6425 const struct p2p_channels *channels,
6426 int freq)
6427{
6428 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6429 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6430 freq_included(wpa_s, channels, freq))
6431 return 1;
6432 return 0;
6433}
6434
6435
6436static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6437 struct p2p_go_neg_results *params,
6438 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006439{
6440 unsigned int i, r;
6441
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006442 /* try all channels in operating class 115 */
6443 for (i = 0; i < 4; i++) {
6444 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006445 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006446 goto out;
6447 }
6448
6449 /* try all channels in operating class 124 */
6450 for (i = 0; i < 4; i++) {
6451 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006452 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006453 goto out;
6454 }
6455
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006456 /* try social channel class 180 channel 2 */
6457 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006458 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006459 goto out;
6460
6461 /* try all channels in reg. class 180 */
6462 for (i = 0; i < 4; i++) {
6463 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006464 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006465 goto out;
6466 }
6467
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006468 /* try some random selection of the social channels */
6469 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6470 return;
6471
6472 for (i = 0; i < 3; i++) {
6473 params->freq = 2412 + ((r + i) % 3) * 25;
6474 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6475 goto out;
6476 }
6477
6478 /* try all other channels in operating class 81 */
6479 for (i = 0; i < 11; i++) {
6480 params->freq = 2412 + i * 5;
6481
6482 /* skip social channels; covered in the previous loop */
6483 if (params->freq == 2412 ||
6484 params->freq == 2437 ||
6485 params->freq == 2462)
6486 continue;
6487
6488 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6489 goto out;
6490 }
6491
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006492 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006493 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006494 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006495out:
6496 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6497 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006498}
6499
6500
Roshan Pius3a1667e2018-07-03 15:17:14 -07006501static int wpas_same_band(int freq1, int freq2)
6502{
6503 enum hostapd_hw_mode mode1, mode2;
6504 u8 chan1, chan2;
6505
6506 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6507 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6508 if (mode1 == NUM_HOSTAPD_MODES)
6509 return 0;
6510 return mode1 == mode2;
6511}
6512
6513
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006514static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6515 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006516 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006517 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006518 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006519 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006520{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006521 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006522 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006523 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006524 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006525 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006526
6527 os_memset(params, 0, sizeof(*params));
6528 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006529 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006530 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006531 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006532 params->max_oper_chwidth = max_oper_chwidth;
6533 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006534 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006536 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6537 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006538 if (!freqs)
6539 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006540
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006541 num = get_shared_radio_freqs_data(wpa_s, freqs,
6542 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006543
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006544 if (wpa_s->current_ssid &&
6545 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6546 wpa_s->wpa_state == WPA_COMPLETED) {
6547 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6548 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006549
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006550 /*
6551 * If the frequency selection is done for an active P2P GO that
6552 * is not sharing a frequency, allow to select a new frequency
6553 * even if there are no unused frequencies as we are about to
6554 * move the P2P GO so its frequency can be re-used.
6555 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006556 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006557 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6558 freqs[i].flags == 0) {
6559 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006560 break;
6561 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006562 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006563 }
6564
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006565 /* Try to use EDMG channel */
6566 if (params->edmg) {
6567 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6568 goto success;
6569 params->edmg = 0;
6570 }
6571
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006572 /* try using the forced freq */
6573 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006574 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6575 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006576 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006577 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006578 freq);
6579 goto fail;
6580 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006581 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6582 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006583 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6584 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006585 /*
6586 * If freq is a DFS channel and DFS is offloaded
6587 * to the driver, allow P2P GO to use it.
6588 */
6589 wpa_printf(MSG_DEBUG,
6590 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6591 __func__, freq);
6592 } else {
6593 wpa_printf(MSG_DEBUG,
6594 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6595 freq);
6596 goto fail;
6597 }
6598 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006599
6600 for (i = 0; i < num; i++) {
6601 if (freqs[i].freq == freq) {
6602 wpa_printf(MSG_DEBUG,
6603 "P2P: forced freq (%d MHz) is also shared",
6604 freq);
6605 params->freq = freq;
6606 goto success;
6607 }
6608 }
6609
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006610 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006611 wpa_printf(MSG_DEBUG,
6612 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6613 freq);
6614 goto fail;
6615 }
6616
6617 wpa_printf(MSG_DEBUG,
6618 "P2P: force GO freq (%d MHz) on a free channel",
6619 freq);
6620 params->freq = freq;
6621 goto success;
6622 }
6623
6624 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006625 if (num &&
6626 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006627 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6628 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6629 wpa_printf(MSG_DEBUG,
6630 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006631 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006632 params->freq = cand;
6633 goto success;
6634 }
6635
6636 /* try using one of the shared freqs */
6637 for (i = 0; i < num; i++) {
6638 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6639 freqs[i].freq)) {
6640 wpa_printf(MSG_DEBUG,
6641 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006642 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006643 params->freq = freqs[i].freq;
6644 goto success;
6645 }
6646 }
6647 }
6648
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006649 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006650 wpa_printf(MSG_DEBUG,
6651 "P2P: Cannot force GO on any of the channels we are already using");
6652 goto fail;
6653 }
6654
6655 /* try using the setting from the configuration file */
6656 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6657 wpa_s->conf->p2p_oper_channel >= 1 &&
6658 wpa_s->conf->p2p_oper_channel <= 11 &&
6659 wpas_p2p_supported_freq_go(
6660 wpa_s, channels,
6661 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6662 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6663 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6664 "frequency %d MHz", params->freq);
6665 goto success;
6666 }
6667
6668 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6669 wpa_s->conf->p2p_oper_reg_class == 116 ||
6670 wpa_s->conf->p2p_oper_reg_class == 117 ||
6671 wpa_s->conf->p2p_oper_reg_class == 124 ||
6672 wpa_s->conf->p2p_oper_reg_class == 125 ||
6673 wpa_s->conf->p2p_oper_reg_class == 126 ||
6674 wpa_s->conf->p2p_oper_reg_class == 127) &&
6675 wpas_p2p_supported_freq_go(wpa_s, channels,
6676 5000 +
6677 5 * wpa_s->conf->p2p_oper_channel)) {
6678 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6679 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6680 "frequency %d MHz", params->freq);
6681 goto success;
6682 }
6683
6684 /* Try using best channels */
6685 if (wpa_s->conf->p2p_oper_channel == 0 &&
6686 wpa_s->best_overall_freq > 0 &&
6687 wpas_p2p_supported_freq_go(wpa_s, channels,
6688 wpa_s->best_overall_freq)) {
6689 params->freq = wpa_s->best_overall_freq;
6690 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6691 "channel %d MHz", params->freq);
6692 goto success;
6693 }
6694
6695 if (wpa_s->conf->p2p_oper_channel == 0 &&
6696 wpa_s->best_24_freq > 0 &&
6697 wpas_p2p_supported_freq_go(wpa_s, channels,
6698 wpa_s->best_24_freq)) {
6699 params->freq = wpa_s->best_24_freq;
6700 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6701 "channel %d MHz", params->freq);
6702 goto success;
6703 }
6704
6705 if (wpa_s->conf->p2p_oper_channel == 0 &&
6706 wpa_s->best_5_freq > 0 &&
6707 wpas_p2p_supported_freq_go(wpa_s, channels,
6708 wpa_s->best_5_freq)) {
6709 params->freq = wpa_s->best_5_freq;
6710 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6711 "channel %d MHz", params->freq);
6712 goto success;
6713 }
6714
6715 /* try using preferred channels */
6716 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6717 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6718 params->freq = cand;
6719 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6720 "channels", params->freq);
6721 goto success;
6722 }
6723
Roshan Pius3a1667e2018-07-03 15:17:14 -07006724 /* Try using a channel that allows VHT to be used with 80 MHz */
6725 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6726 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6727 enum hostapd_hw_mode mode;
6728 struct hostapd_hw_modes *hwmode;
6729 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006730 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006731
6732 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006733 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006734 mode = ieee80211_freq_to_chan(cand, &chan);
6735 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006736 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006737 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006738 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6739 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006740 continue;
6741 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6742 params->freq = cand;
6743 wpa_printf(MSG_DEBUG,
6744 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6745 params->freq);
6746 goto success;
6747 }
6748 }
6749 }
6750
6751 /* Try using a channel that allows HT to be used with 40 MHz on the same
6752 * band so that CSA can be used */
6753 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6754 wpa_s->p2p_group_common_freqs) {
6755 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6756 enum hostapd_hw_mode mode;
6757 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006758 u8 chan, op_class;
6759 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006760
Hai Shalomc1a21442022-02-04 13:43:00 -08006761 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006762 cand = wpa_s->p2p_group_common_freqs[i];
6763 mode = ieee80211_freq_to_chan(cand, &chan);
6764 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006765 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006766 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6767 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006768 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006769 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006770 if (is_6ghz &&
6771 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6772 BW40) == ALLOWED)
6773 supported = true;
6774
6775 if (!is_6ghz &&
6776 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006777 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6778 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006779 wpas_p2p_verify_channel(
6780 wpa_s, hwmode, op_class, chan,
6781 BW40MINUS) == ALLOWED)
6782 supported = true;
6783
6784 if (!supported && !is_6ghz &&
6785 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006786 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6787 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006788 wpas_p2p_verify_channel(
6789 wpa_s, hwmode, op_class, chan,
6790 BW40PLUS) == ALLOWED)
6791 supported = true;
6792
6793 if (!supported)
6794 continue;
6795
Roshan Pius3a1667e2018-07-03 15:17:14 -07006796 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6797 params->freq = cand;
6798 wpa_printf(MSG_DEBUG,
6799 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6800 params->freq);
6801 goto success;
6802 }
6803 }
6804 }
6805
6806 /* Try using one of the group common freqs on the same band so that CSA
6807 * can be used */
6808 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6809 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6810 cand = wpa_s->p2p_group_common_freqs[i];
6811 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6812 cand))
6813 continue;
6814 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6815 params->freq = cand;
6816 wpa_printf(MSG_DEBUG,
6817 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6818 params->freq);
6819 goto success;
6820 }
6821 }
6822 }
6823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006824 /* Try using one of the group common freqs */
6825 if (wpa_s->p2p_group_common_freqs) {
6826 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6827 cand = wpa_s->p2p_group_common_freqs[i];
6828 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6829 params->freq = cand;
6830 wpa_printf(MSG_DEBUG,
6831 "P2P: Use freq %d MHz common with the peer",
6832 params->freq);
6833 goto success;
6834 }
6835 }
6836 }
6837
6838 /* no preference, select some channel */
6839 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6840
6841 if (params->freq == 0) {
6842 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6843 goto fail;
6844 }
6845
6846success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006847 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006848 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006849fail:
6850 os_free(freqs);
6851 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006852}
6853
6854
6855static struct wpa_supplicant *
6856wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6857 int go)
6858{
6859 struct wpa_supplicant *group_wpa_s;
6860
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006861 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006862 if (wpa_s->p2p_mgmt) {
6863 /*
6864 * We may be called on the p2p_dev interface which
6865 * cannot be used for group operations, so always use
6866 * the primary interface.
6867 */
6868 wpa_s->parent->p2pdev = wpa_s;
6869 wpa_s = wpa_s->parent;
6870 }
6871 wpa_dbg(wpa_s, MSG_DEBUG,
6872 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006873 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006874 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006875 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006876 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006878
6879 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006880 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006881 wpa_msg_global(wpa_s, MSG_ERROR,
6882 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006883 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006885 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6886 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006887 wpa_msg_global(wpa_s, MSG_ERROR,
6888 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006889 wpas_p2p_remove_pending_group_interface(wpa_s);
6890 return NULL;
6891 }
6892
Roshan Pius3a1667e2018-07-03 15:17:14 -07006893 if (go && wpa_s->p2p_go_do_acs) {
6894 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6895 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6896 wpa_s->p2p_go_do_acs = 0;
6897 }
6898
Hai Shalomc1a21442022-02-04 13:43:00 -08006899 if (go && wpa_s->p2p_go_allow_dfs) {
6900 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6901 wpa_s->p2p_go_allow_dfs = 0;
6902 }
6903
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006904 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6905 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006906 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907 return group_wpa_s;
6908}
6909
6910
6911/**
6912 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6913 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6914 * @persistent_group: Whether to create a persistent group
6915 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006916 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006917 * @ht40: Start GO with 40 MHz channel width
6918 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006919 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006920 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006921 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006922 * Returns: 0 on success, -1 on failure
6923 *
6924 * This function creates a new P2P group with the local end as the Group Owner,
6925 * i.e., without using Group Owner Negotiation.
6926 */
6927int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006928 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006929 int max_oper_chwidth, int he, int edmg,
6930 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931{
6932 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006933 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934
6935 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6936 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006937 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6938 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006939
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006940 os_free(wpa_s->global->add_psk);
6941 wpa_s->global->add_psk = NULL;
6942
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006943 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006944 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006945 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006946
Roshan Pius3a1667e2018-07-03 15:17:14 -07006947 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00006948 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
6949 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006950 return -1;
6951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952
Sunil Ravi77a0f092022-10-03 00:53:41 +00006953 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006954 ht40, vht, max_oper_chwidth, he, edmg,
6955 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006956 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006958 p2p_go_params(wpa_s->global->p2p, &params);
6959 params.persistent_group = persistent_group;
6960
6961 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6962 if (wpa_s == NULL)
6963 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006964 if (freq > 0)
6965 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006966 wpas_start_wps_go(wpa_s, &params, 0);
6967
6968 return 0;
6969}
6970
6971
6972static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006973 struct wpa_ssid *params, int addr_allocated,
Matthew Wang06b42472022-11-10 06:56:31 +00006974 int freq, int force_scan, int retry_limit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975{
6976 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07006977 int other_iface_found = 0;
6978 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006979
6980 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6981 if (wpa_s == NULL)
6982 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006983 if (force_scan)
6984 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006985 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986
6987 wpa_supplicant_ap_deinit(wpa_s);
6988
6989 ssid = wpa_config_add_network(wpa_s->conf);
6990 if (ssid == NULL)
6991 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006992 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006993 wpa_config_set_network_defaults(ssid);
6994 ssid->temporary = 1;
6995 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006996 ssid->pbss = params->pbss;
6997 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6998 WPA_CIPHER_CCMP;
6999 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
7001 ssid->ssid = os_malloc(params->ssid_len);
7002 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007003 wpa_config_remove_network(wpa_s->conf, ssid->id);
7004 return -1;
7005 }
7006 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7007 ssid->ssid_len = params->ssid_len;
7008 ssid->p2p_group = 1;
7009 ssid->export_keys = 1;
7010 if (params->psk_set) {
7011 os_memcpy(ssid->psk, params->psk, 32);
7012 ssid->psk_set = 1;
7013 }
7014 if (params->passphrase)
7015 ssid->passphrase = os_strdup(params->passphrase);
7016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007017 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007018 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007019 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007020 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007021 wpa_s->p2p_go_group_formation_completed = 0;
7022 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023
Matthew Wang9ae940b2022-09-14 21:25:34 -07007024 /*
7025 * Get latest scan results from driver in case cached scan results from
7026 * interfaces on the same wiphy allow us to skip the next scan by fast
7027 * associating. Also update the scan time to the most recent scan result
7028 * fetch time on the same radio so it reflects the actual time the last
7029 * scan result event occurred.
7030 */
7031 wpa_supplicant_update_scan_results(wpa_s);
7032 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7033 radio_list) {
7034 if (ifs == wpa_s)
7035 continue;
7036 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7037 &ifs->last_scan)) {
7038 other_iface_found = 1;
7039 wpa_s->last_scan.sec = ifs->last_scan.sec;
7040 wpa_s->last_scan.usec = ifs->last_scan.usec;
7041 }
7042 }
7043
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007044 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007045 NULL);
7046 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7047 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007048 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007049 wpa_supplicant_select_network(wpa_s, ssid);
7050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007051 return 0;
7052}
7053
7054
7055int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7056 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007057 int force_freq, int neg_freq,
7058 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007059 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007060 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007061 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007062 int connection_timeout, int force_scan,
Matthew Wang06b42472022-11-10 06:56:31 +00007063 bool allow_6ghz, int retry_limit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007064{
7065 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007066 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067
7068 if (ssid->disabled != 2 || ssid->ssid == NULL)
7069 return -1;
7070
7071 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7072 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7073 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7074 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007075 if (go == 0 &&
7076 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007077 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007078 /*
7079 * This can happen if Invitation Response frame was lost
7080 * and the peer (GO of a persistent group) tries to
7081 * invite us again. Reschedule the timeout to avoid
7082 * terminating the wait for the connection too early
7083 * since we now know that the peer is still trying to
7084 * invite us instead of having already started the GO.
7085 */
7086 wpa_printf(MSG_DEBUG,
7087 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7088 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7089 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007090 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007092 return 0;
7093 }
7094
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007095 os_free(wpa_s->global->add_psk);
7096 wpa_s->global->add_psk = NULL;
7097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007098 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007099 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007100
Dmitry Shmidt04949592012-07-19 12:16:46 -07007101 wpa_s->p2p_fallback_to_go_neg = 0;
7102
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007103 if (ssid->mode == WPAS_MODE_P2P_GO) {
7104 if (force_freq > 0) {
7105 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7106 if (freq < 0)
7107 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007108 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007109 } else {
7110 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7111 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007112 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007113 freq = 0;
7114 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007115 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007116 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007117 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007118 struct os_reltime now;
7119 struct wpa_bss *bss =
7120 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007121
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007122 os_get_reltime(&now);
7123 if (bss &&
7124 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007125 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007126 freq = bss->freq;
7127 else
7128 freq = 0;
7129 }
7130
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007131 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Matthew Wang06b42472022-11-10 06:56:31 +00007132 force_scan, retry_limit);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007133 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007134 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007135 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007136
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007137 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007138 ht40, vht, max_oper_chwidth, he, edmg,
7139 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007140 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007141
7142 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007143 params.psk_set = ssid->psk_set;
7144 if (params.psk_set)
7145 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007146 if (ssid->passphrase) {
7147 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7148 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7149 "persistent group");
7150 return -1;
7151 }
7152 os_strlcpy(params.passphrase, ssid->passphrase,
7153 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007154 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007155 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7156 params.ssid_len = ssid->ssid_len;
7157 params.persistent_group = 1;
7158
7159 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7160 if (wpa_s == NULL)
7161 return -1;
7162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007163 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7164
Dmitry Shmidt56052862013-10-04 10:23:25 -07007165 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007166 wpas_start_wps_go(wpa_s, &params, 0);
7167
7168 return 0;
7169}
7170
7171
7172static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7173 struct wpabuf *proberesp_ies)
7174{
7175 struct wpa_supplicant *wpa_s = ctx;
7176 if (wpa_s->ap_iface) {
7177 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007178 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7179 wpabuf_free(beacon_ies);
7180 wpabuf_free(proberesp_ies);
7181 return;
7182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007183 if (beacon_ies) {
7184 wpabuf_free(hapd->p2p_beacon_ie);
7185 hapd->p2p_beacon_ie = beacon_ies;
7186 }
7187 wpabuf_free(hapd->p2p_probe_resp_ie);
7188 hapd->p2p_probe_resp_ie = proberesp_ies;
7189 } else {
7190 wpabuf_free(beacon_ies);
7191 wpabuf_free(proberesp_ies);
7192 }
7193 wpa_supplicant_ap_update_beacon(wpa_s);
7194}
7195
7196
7197static void wpas_p2p_idle_update(void *ctx, int idle)
7198{
7199 struct wpa_supplicant *wpa_s = ctx;
7200 if (!wpa_s->ap_iface)
7201 return;
7202 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007203 if (idle) {
7204 if (wpa_s->global->p2p_fail_on_wps_complete &&
7205 wpa_s->p2p_in_provisioning) {
7206 wpas_p2p_grpform_fail_after_wps(wpa_s);
7207 return;
7208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007210 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007211 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7212}
7213
7214
7215struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007216 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007217{
7218 struct p2p_group *group;
7219 struct p2p_group_config *cfg;
7220
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007221 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7222 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007223 return NULL;
7224
7225 cfg = os_zalloc(sizeof(*cfg));
7226 if (cfg == NULL)
7227 return NULL;
7228
Dmitry Shmidt04949592012-07-19 12:16:46 -07007229 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007230 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007231 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007232 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7234 if (wpa_s->max_stations &&
7235 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7236 cfg->max_clients = wpa_s->max_stations;
7237 else
7238 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007239 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7240 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007241 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007242 cfg->cb_ctx = wpa_s;
7243 cfg->ie_update = wpas_p2p_ie_update;
7244 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007245 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7246 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007247
7248 group = p2p_group_init(wpa_s->global->p2p, cfg);
7249 if (group == NULL)
7250 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007251 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007252 p2p_group_notif_formation_done(group);
7253 wpa_s->p2p_group = group;
7254 return group;
7255}
7256
7257
7258void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7259 int registrar)
7260{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007261 struct wpa_ssid *ssid = wpa_s->current_ssid;
7262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007263 if (!wpa_s->p2p_in_provisioning) {
7264 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7265 "provisioning not in progress");
7266 return;
7267 }
7268
Dmitry Shmidt04949592012-07-19 12:16:46 -07007269 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7270 u8 go_dev_addr[ETH_ALEN];
7271 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7272 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7273 ssid->ssid_len);
7274 /* Clear any stored provisioning info */
7275 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7276 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007277
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007278 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007279 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007280 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007281 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7282 /*
7283 * Use a separate timeout for initial data connection to
7284 * complete to allow the group to be removed automatically if
7285 * something goes wrong in this step before the P2P group idle
7286 * timeout mechanism is taken into use.
7287 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007288 wpa_dbg(wpa_s, MSG_DEBUG,
7289 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7290 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007291 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7292 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007293 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007294 /* Complete group formation on successful data connection. */
7295 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007296 } else if (ssid) {
7297 /*
7298 * Use a separate timeout for initial data connection to
7299 * complete to allow the group to be removed automatically if
7300 * the client does not complete data connection successfully.
7301 */
7302 wpa_dbg(wpa_s, MSG_DEBUG,
7303 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7304 P2P_MAX_INITIAL_CONN_WAIT_GO);
7305 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7306 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007307 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007308 /*
7309 * Complete group formation on first successful data connection
7310 */
7311 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007313 if (wpa_s->global->p2p)
7314 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007315 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007316}
7317
7318
Jouni Malinen75ecf522011-06-27 15:19:46 -07007319void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7320 struct wps_event_fail *fail)
7321{
7322 if (!wpa_s->p2p_in_provisioning) {
7323 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7324 "provisioning not in progress");
7325 return;
7326 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007327
7328 if (wpa_s->go_params) {
7329 p2p_clear_provisioning_info(
7330 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007331 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007332 }
7333
Jouni Malinen75ecf522011-06-27 15:19:46 -07007334 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007335
7336 if (wpa_s == wpa_s->global->p2p_group_formation) {
7337 /*
7338 * Allow some time for the failed WPS negotiation exchange to
7339 * complete, but remove the group since group formation cannot
7340 * succeed after provisioning failure.
7341 */
7342 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7343 wpa_s->global->p2p_fail_on_wps_complete = 1;
7344 eloop_deplete_timeout(0, 50000,
7345 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007346 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007347 }
7348}
7349
7350
7351int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7352{
7353 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7354 !wpa_s->p2p_in_provisioning)
7355 return 0;
7356
7357 wpas_p2p_grpform_fail_after_wps(wpa_s);
7358
7359 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007360}
7361
7362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007364 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007365 enum wpas_p2p_prov_disc_use use,
7366 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007367{
7368 u16 config_methods;
7369
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007370 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007371 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007372 wpa_s->p2p_fallback_to_go_neg = 0;
7373 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007374 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7375 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007376 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7377 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7378
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007379 wpa_printf(MSG_DEBUG,
7380 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7381 __func__, p2ps_prov->conncap,
7382 p2ps_prov->adv_id, p2ps_prov->conncap,
7383 p2ps_prov->status, p2ps_prov->info);
7384
7385 config_methods = 0;
7386 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007387 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007388 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007389 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007390 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7391 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007392 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007393 else {
7394 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007395 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007396 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007397 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007398
Dmitry Shmidt04949592012-07-19 12:16:46 -07007399 if (use == WPAS_P2P_PD_AUTO) {
7400 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7401 wpa_s->pending_pd_config_methods = config_methods;
7402 wpa_s->p2p_auto_pd = 1;
7403 wpa_s->p2p_auto_join = 0;
7404 wpa_s->pending_pd_before_join = 0;
7405 wpa_s->auto_pd_scan_retry = 0;
7406 wpas_p2p_stop_find(wpa_s);
7407 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007408 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007409 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7410 wpa_s->p2p_auto_started.sec,
7411 wpa_s->p2p_auto_started.usec);
7412 wpas_p2p_join_scan(wpa_s, NULL);
7413 return 0;
7414 }
7415
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007416 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7417 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007418 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007421 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007422 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007423 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007424}
7425
7426
7427int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7428 char *end)
7429{
7430 return p2p_scan_result_text(ies, ies_len, buf, end);
7431}
7432
7433
7434static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7435{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007436 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007437 return;
7438
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007439 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007440 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007441 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7442 wpa_s, NULL);
7443 offchannel_send_action_done(wpa_s);
7444 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7447 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007448 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007449}
7450
7451
7452int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7453 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007454 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007455 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007456 u8 seek_cnt, const char **seek_string, int freq,
7457 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458{
7459 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007460 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007461
Dmitry Shmidt04949592012-07-19 12:16:46 -07007462 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007463 wpa_s->p2p_in_provisioning) {
7464 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7465 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7466 " (P2P disabled)" : "",
7467 wpa_s->p2p_in_provisioning ?
7468 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007469 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007470 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007472 wpa_supplicant_cancel_sched_scan(wpa_s);
7473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007475 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007476 search_delay, seek_cnt, seek_string, freq,
7477 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007478}
7479
7480
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007481static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7482 struct wpa_scan_results *scan_res)
7483{
7484 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7485
7486 if (wpa_s->p2p_scan_work) {
7487 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7488 wpa_s->p2p_scan_work = NULL;
7489 radio_work_done(work);
7490 }
7491
7492 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7493 return;
7494
7495 /*
7496 * Indicate that results have been processed so that the P2P module can
7497 * continue pending tasks.
7498 */
Hai Shalom60840252021-02-19 19:02:11 -08007499 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007500}
7501
7502
7503static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007504{
7505 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007506 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7508 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007509
7510 if (wpa_s->global->p2p)
7511 p2p_stop_find(wpa_s->global->p2p);
7512
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007513 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7514 wpa_printf(MSG_DEBUG,
7515 "P2P: Do not consider the scan results after stop_find");
7516 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7517 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007518}
7519
7520
7521void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7522{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007523 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007524 if (!wpa_s->global->pending_group_iface_for_p2ps)
7525 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526}
7527
7528
7529static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7530{
7531 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007532 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007533}
7534
7535
7536int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7537{
7538 int res;
7539
7540 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7541 return -1;
7542
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007543 if (wpa_s->p2p_lo_started) {
7544 wpa_printf(MSG_DEBUG,
7545 "P2P: Cannot start P2P listen, it is offloaded");
7546 return -1;
7547 }
7548
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007549 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550 wpas_p2p_clear_pending_action_tx(wpa_s);
7551
7552 if (timeout == 0) {
7553 /*
7554 * This is a request for unlimited Listen state. However, at
7555 * least for now, this is mapped to a Listen state for one
7556 * hour.
7557 */
7558 timeout = 3600;
7559 }
7560 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007561 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007563 /*
7564 * Stop previous find/listen operation to avoid trying to request a new
7565 * remain-on-channel operation while the driver is still running the
7566 * previous one.
7567 */
7568 if (wpa_s->global->p2p)
7569 p2p_stop_find(wpa_s->global->p2p);
7570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007571 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7572 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007573 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007574 eloop_register_timeout(timeout, 0,
7575 wpas_p2p_long_listen_timeout,
7576 wpa_s, NULL);
7577 }
7578
7579 return res;
7580}
7581
7582
7583int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7584 u8 *buf, size_t len, int p2p_group)
7585{
7586 struct wpabuf *p2p_ie;
7587 int ret;
7588
7589 if (wpa_s->global->p2p_disabled)
7590 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007591 /*
7592 * Advertize mandatory cross connection capability even on
7593 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7594 */
7595 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007596 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007597 if (wpa_s->global->p2p == NULL)
7598 return -1;
7599 if (bss == NULL)
7600 return -1;
7601
7602 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7603 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7604 p2p_group, p2p_ie);
7605 wpabuf_free(p2p_ie);
7606
7607 return ret;
7608}
7609
7610
7611int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007612 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007613 const u8 *ie, size_t ie_len,
7614 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007615{
7616 if (wpa_s->global->p2p_disabled)
7617 return 0;
7618 if (wpa_s->global->p2p == NULL)
7619 return 0;
7620
Dmitry Shmidt04949592012-07-19 12:16:46 -07007621 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007622 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007623 case P2P_PREQ_NOT_P2P:
7624 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7625 ssi_signal);
7626 /* fall through */
7627 case P2P_PREQ_MALFORMED:
7628 case P2P_PREQ_NOT_LISTEN:
7629 case P2P_PREQ_NOT_PROCESSED:
7630 default: /* make gcc happy */
7631 return 0;
7632 case P2P_PREQ_PROCESSED:
7633 return 1;
7634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635}
7636
7637
7638void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7639 const u8 *sa, const u8 *bssid,
7640 u8 category, const u8 *data, size_t len, int freq)
7641{
7642 if (wpa_s->global->p2p_disabled)
7643 return;
7644 if (wpa_s->global->p2p == NULL)
7645 return;
7646
7647 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7648 freq);
7649}
7650
7651
7652void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7653{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007654 unsigned int bands;
7655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007656 if (wpa_s->global->p2p_disabled)
7657 return;
7658 if (wpa_s->global->p2p == NULL)
7659 return;
7660
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007661 bands = wpas_get_bands(wpa_s, NULL);
7662 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663}
7664
7665
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007666static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007667{
7668 p2p_group_deinit(wpa_s->p2p_group);
7669 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007670
7671 wpa_s->ap_configured_cb = NULL;
7672 wpa_s->ap_configured_cb_ctx = NULL;
7673 wpa_s->ap_configured_cb_data = NULL;
7674 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007675}
7676
7677
7678int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7679{
Hai Shalomfdcde762020-04-02 11:19:20 -07007680 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007682 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7683 return -1;
7684
7685 return p2p_reject(wpa_s->global->p2p, addr);
7686}
7687
7688
7689/* Invite to reinvoke a persistent group */
7690int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007691 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007692 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007693 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007694{
7695 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007696 u8 *bssid = NULL;
7697 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007698 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007699 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007700 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7701 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007702
Hai Shalomc1a21442022-02-04 13:43:00 -08007703 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7704 return -1;
7705
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007706 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007707 if (peer_addr)
7708 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7709 else
7710 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007711
Jouni Malinen31be0a42012-08-31 21:20:51 +03007712 wpa_s->p2p_persistent_go_freq = freq;
7713 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007714 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007715 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007716 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7717 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007718 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007719 if (ssid->mode == WPAS_MODE_P2P_GO) {
7720 role = P2P_INVITE_ROLE_GO;
7721 if (peer_addr == NULL) {
7722 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7723 "address in invitation command");
7724 return -1;
7725 }
7726 if (wpas_p2p_create_iface(wpa_s)) {
7727 if (wpas_p2p_add_group_interface(wpa_s,
7728 WPA_IF_P2P_GO) < 0) {
7729 wpa_printf(MSG_ERROR, "P2P: Failed to "
7730 "allocate a new interface for the "
7731 "group");
7732 return -1;
7733 }
7734 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007735 } else if (wpa_s->p2p_mgmt)
7736 bssid = wpa_s->parent->own_addr;
7737 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007738 bssid = wpa_s->own_addr;
7739 } else {
7740 role = P2P_INVITE_ROLE_CLIENT;
7741 peer_addr = ssid->bssid;
7742 }
7743 wpa_s->pending_invite_ssid_id = ssid->id;
7744
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007745 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007746 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007747 role == P2P_INVITE_ROLE_GO,
7748 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007749 if (res)
7750 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007752 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7753 return -1;
7754
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007755 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7756
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007757 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7758 no_pref_freq_given && pref_freq > 0 &&
7759 wpa_s->num_multichan_concurrent > 1 &&
7760 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7761 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7762 pref_freq);
7763 pref_freq = 0;
7764 }
7765
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007766 /*
7767 * Stop any find/listen operations before invitation and possibly
7768 * connection establishment.
7769 */
7770 wpas_p2p_stop_find_oper(wpa_s);
7771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007772 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007773 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007774 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007775}
7776
7777
7778/* Invite to join an active group */
7779int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007780 const u8 *peer_addr, const u8 *go_dev_addr,
7781 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007782{
7783 struct wpa_global *global = wpa_s->global;
7784 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007785 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007786 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007787 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007788 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007789 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007790 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7791 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792
Jouni Malinen31be0a42012-08-31 21:20:51 +03007793 wpa_s->p2p_persistent_go_freq = 0;
7794 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007795 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007796 wpa_s->p2p_go_vht_center_freq2 = 0;
7797 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007798 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007800 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7801 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7802 break;
7803 }
7804 if (wpa_s == NULL) {
7805 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7806 return -1;
7807 }
7808
7809 ssid = wpa_s->current_ssid;
7810 if (ssid == NULL) {
7811 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7812 "invitation");
7813 return -1;
7814 }
7815
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007816 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007817 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007818 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007819 ssid->ssid, ssid->ssid_len);
7820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821 if (ssid->mode == WPAS_MODE_P2P_GO) {
7822 role = P2P_INVITE_ROLE_ACTIVE_GO;
7823 bssid = wpa_s->own_addr;
7824 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007825 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007826 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007827 } else {
7828 role = P2P_INVITE_ROLE_CLIENT;
7829 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7830 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7831 "invite to current group");
7832 return -1;
7833 }
7834 bssid = wpa_s->bssid;
7835 if (go_dev_addr == NULL &&
7836 !is_zero_ether_addr(wpa_s->go_dev_addr))
7837 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007838 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7839 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007840 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007841 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007843 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7844 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007845 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7846 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007847
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007848 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007849 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007850 role == P2P_INVITE_ROLE_ACTIVE_GO,
7851 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007852 if (res)
7853 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007854 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007856 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007857 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007858 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007859}
7860
7861
7862void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7863{
7864 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007865 u8 go_dev_addr[ETH_ALEN];
7866 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007867 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007868 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007869 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007870
Dmitry Shmidt04949592012-07-19 12:16:46 -07007871 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7872 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007873 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007874 }
7875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007876 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007877 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007878
7879 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007880 if (!wpa_s->p2p_go_group_formation_completed &&
7881 wpa_s->global->p2p_group_formation == wpa_s) {
7882 wpa_dbg(wpa_s, MSG_DEBUG,
7883 "P2P: Marking group formation completed on client on data connection");
7884 wpa_s->p2p_go_group_formation_completed = 1;
7885 wpa_s->global->p2p_group_formation = NULL;
7886 wpa_s->p2p_in_provisioning = 0;
7887 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00007888 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007889 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007891 os_memset(go_dev_addr, 0, ETH_ALEN);
7892 if (ssid->bssid_set)
7893 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7894 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7895 ssid->ssid_len);
7896 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7897
7898 if (wpa_s->global->p2p_group_formation == wpa_s)
7899 wpa_s->global->p2p_group_formation = NULL;
7900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007901 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7902 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007903
7904 ip_addr[0] = '\0';
7905 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007906 int res;
7907
7908 res = os_snprintf(ip_addr, sizeof(ip_addr),
7909 " ip_addr=%u.%u.%u.%u "
7910 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7911 ip[0], ip[1], ip[2], ip[3],
7912 ip[4], ip[5], ip[6], ip[7],
7913 ip[8], ip[9], ip[10], ip[11]);
7914 if (os_snprintf_error(sizeof(ip_addr), res))
7915 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007916 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007917 }
7918
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007919 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7920 ssid->passphrase == NULL && ssid->psk_set ?
7921 ssid->psk : NULL,
7922 ssid->passphrase, go_dev_addr, persistent,
7923 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007924
7925 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007926 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7927 ssid, go_dev_addr);
7928
Hai Shalom5f92bc92019-04-18 11:54:11 -07007929 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007930}
7931
7932
7933int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7934 u32 interval1, u32 duration2, u32 interval2)
7935{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007936 int ret;
7937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007938 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7939 return -1;
7940
7941 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7942 wpa_s->current_ssid == NULL ||
7943 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7944 return -1;
7945
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007946 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7947 wpa_s->own_addr, wpa_s->assoc_freq,
7948 duration1, interval1, duration2, interval2);
7949 if (ret == 0)
7950 wpa_s->waiting_presence_resp = 1;
7951
7952 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007953}
7954
7955
7956int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7957 unsigned int interval)
7958{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007959 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7960 return -1;
7961
7962 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7963}
7964
7965
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007966static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7967{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007968 if (wpa_s->current_ssid == NULL) {
7969 /*
7970 * current_ssid can be cleared when P2P client interface gets
7971 * disconnected, so assume this interface was used as P2P
7972 * client.
7973 */
7974 return 1;
7975 }
7976 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007977 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7978}
7979
7980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007981static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7982{
7983 struct wpa_supplicant *wpa_s = eloop_ctx;
7984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007985 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007986 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7987 "disabled");
7988 return;
7989 }
7990
Dmitry Shmidt04949592012-07-19 12:16:46 -07007991 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7992 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007993 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007994}
7995
7996
7997static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7998{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007999 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008000
Dmitry Shmidt04949592012-07-19 12:16:46 -07008001 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8002 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008004 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8005 return;
8006
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008007 timeout = wpa_s->conf->p2p_group_idle;
8008 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8009 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8010 timeout = P2P_MAX_CLIENT_IDLE;
8011
8012 if (timeout == 0)
8013 return;
8014
Dmitry Shmidt04949592012-07-19 12:16:46 -07008015 if (timeout < 0) {
8016 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8017 timeout = 0; /* special client mode no-timeout */
8018 else
8019 return;
8020 }
8021
8022 if (wpa_s->p2p_in_provisioning) {
8023 /*
8024 * Use the normal group formation timeout during the
8025 * provisioning phase to avoid terminating this process too
8026 * early due to group idle timeout.
8027 */
8028 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8029 "during provisioning");
8030 return;
8031 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308032
Dmitry Shmidt04949592012-07-19 12:16:46 -07008033 if (wpa_s->show_group_started) {
8034 /*
8035 * Use the normal group formation timeout between the end of
8036 * the provisioning phase and completion of 4-way handshake to
8037 * avoid terminating this process too early due to group idle
8038 * timeout.
8039 */
8040 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8041 "while waiting for initial 4-way handshake to "
8042 "complete");
8043 return;
8044 }
8045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008046 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008047 timeout);
8048 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8049 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008050}
8051
8052
Jouni Malinen2b89da82012-08-31 22:04:41 +03008053/* Returns 1 if the interface was removed */
8054int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8055 u16 reason_code, const u8 *ie, size_t ie_len,
8056 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008057{
8058 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008059 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008060
Dmitry Shmidt04949592012-07-19 12:16:46 -07008061 if (!locally_generated)
8062 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8063 ie_len);
8064
8065 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8066 wpa_s->current_ssid &&
8067 wpa_s->current_ssid->p2p_group &&
8068 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8069 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8070 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008071 if (wpas_p2p_group_delete(wpa_s,
8072 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8073 > 0)
8074 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008075 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008076
8077 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008078}
8079
8080
8081void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008082 u16 reason_code, const u8 *ie, size_t ie_len,
8083 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008084{
8085 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8086 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008087
Dmitry Shmidt04949592012-07-19 12:16:46 -07008088 if (!locally_generated)
8089 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8090 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008091}
8092
8093
8094void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8095{
8096 struct p2p_data *p2p = wpa_s->global->p2p;
8097
8098 if (p2p == NULL)
8099 return;
8100
8101 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8102 return;
8103
8104 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8105 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8106
8107 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8108 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8109
8110 if (wpa_s->wps &&
8111 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8112 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8113
8114 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8115 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8116
8117 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8118 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8119 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8120 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8121 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8122 }
8123
8124 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8125 p2p_set_sec_dev_types(p2p,
8126 (void *) wpa_s->conf->sec_device_type,
8127 wpa_s->conf->num_sec_device_types);
8128
8129 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8130 int i;
8131 p2p_remove_wps_vendor_extensions(p2p);
8132 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8133 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8134 continue;
8135 p2p_add_wps_vendor_extension(
8136 p2p, wpa_s->conf->wps_vendor_ext[i]);
8137 }
8138 }
8139
8140 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8141 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8142 char country[3];
8143 country[0] = wpa_s->conf->country[0];
8144 country[1] = wpa_s->conf->country[1];
8145 country[2] = 0x04;
8146 p2p_set_country(p2p, country);
8147 }
8148
8149 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8150 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8151 wpa_s->conf->p2p_ssid_postfix ?
8152 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8153 0);
8154 }
8155
8156 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8157 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008158
8159 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8160 u8 reg_class, channel;
8161 int ret;
8162 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008163 u8 channel_forced;
8164
Jouni Malinen75ecf522011-06-27 15:19:46 -07008165 if (wpa_s->conf->p2p_listen_reg_class &&
8166 wpa_s->conf->p2p_listen_channel) {
8167 reg_class = wpa_s->conf->p2p_listen_reg_class;
8168 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008169 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008170 } else {
8171 reg_class = 81;
8172 /*
8173 * Pick one of the social channels randomly as the
8174 * listen channel.
8175 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008176 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8177 channel = 1;
8178 else
8179 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008180 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008181 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008182 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8183 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008184 if (ret)
8185 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8186 "failed: %d", ret);
8187 }
8188 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8189 u8 op_reg_class, op_channel, cfg_op_channel;
8190 int ret = 0;
8191 unsigned int r;
8192 if (wpa_s->conf->p2p_oper_reg_class &&
8193 wpa_s->conf->p2p_oper_channel) {
8194 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8195 op_channel = wpa_s->conf->p2p_oper_channel;
8196 cfg_op_channel = 1;
8197 } else {
8198 op_reg_class = 81;
8199 /*
8200 * Use random operation channel from (1, 6, 11)
8201 *if no other preference is indicated.
8202 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008203 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8204 op_channel = 1;
8205 else
8206 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008207 cfg_op_channel = 0;
8208 }
8209 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8210 cfg_op_channel);
8211 if (ret)
8212 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8213 "failed: %d", ret);
8214 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008215
8216 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8217 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8218 wpa_s->conf->p2p_pref_chan) < 0) {
8219 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8220 "update failed");
8221 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008222
8223 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8224 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8225 "update failed");
8226 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008227 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008228
8229 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8230 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008231}
8232
8233
8234int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8235 int duration)
8236{
8237 if (!wpa_s->ap_iface)
8238 return -1;
8239 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8240 duration);
8241}
8242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008243
8244int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8245{
8246 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8247 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008248
8249 wpa_s->global->cross_connection = enabled;
8250 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8251
8252 if (!enabled) {
8253 struct wpa_supplicant *iface;
8254
8255 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8256 {
8257 if (iface->cross_connect_enabled == 0)
8258 continue;
8259
8260 iface->cross_connect_enabled = 0;
8261 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008262 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008263 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8264 iface->ifname,
8265 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008266 }
8267 }
8268
8269 return 0;
8270}
8271
8272
8273static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8274{
8275 struct wpa_supplicant *iface;
8276
8277 if (!uplink->global->cross_connection)
8278 return;
8279
8280 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8281 if (!iface->cross_connect_enabled)
8282 continue;
8283 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8284 0)
8285 continue;
8286 if (iface->ap_iface == NULL)
8287 continue;
8288 if (iface->cross_connect_in_use)
8289 continue;
8290
8291 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008292 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008293 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8294 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008295 }
8296}
8297
8298
8299static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8300{
8301 struct wpa_supplicant *iface;
8302
8303 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8304 if (!iface->cross_connect_enabled)
8305 continue;
8306 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8307 0)
8308 continue;
8309 if (!iface->cross_connect_in_use)
8310 continue;
8311
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008312 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008313 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8314 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008315 iface->cross_connect_in_use = 0;
8316 }
8317}
8318
8319
8320void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8321{
8322 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8323 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8324 wpa_s->cross_connect_disallowed)
8325 wpas_p2p_disable_cross_connect(wpa_s);
8326 else
8327 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008328 if (!wpa_s->ap_iface &&
8329 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8330 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008331}
8332
8333
8334void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8335{
8336 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008337 if (!wpa_s->ap_iface &&
8338 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8339 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008340 wpas_p2p_set_group_idle_timeout(wpa_s);
8341}
8342
8343
8344static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8345{
8346 struct wpa_supplicant *iface;
8347
8348 if (!wpa_s->global->cross_connection)
8349 return;
8350
8351 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8352 if (iface == wpa_s)
8353 continue;
8354 if (iface->drv_flags &
8355 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8356 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008357 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8358 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008359 continue;
8360
8361 wpa_s->cross_connect_enabled = 1;
8362 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8363 sizeof(wpa_s->cross_connect_uplink));
8364 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8365 "%s to %s whenever uplink is available",
8366 wpa_s->ifname, wpa_s->cross_connect_uplink);
8367
8368 if (iface->ap_iface || iface->current_ssid == NULL ||
8369 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8370 iface->cross_connect_disallowed ||
8371 iface->wpa_state != WPA_COMPLETED)
8372 break;
8373
8374 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008375 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008376 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8377 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008378 break;
8379 }
8380}
8381
8382
8383int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8384{
8385 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8386 !wpa_s->p2p_in_provisioning)
8387 return 0; /* not P2P client operation */
8388
8389 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8390 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008391 if (wpa_s != wpa_s->p2pdev)
8392 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008393 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008394 return 1;
8395}
8396
8397
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008398void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8399{
8400 struct wpa_supplicant *wpa_s = eloop_ctx;
8401 wpas_p2p_notif_pbc_overlap(wpa_s);
8402}
8403
8404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008405void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8406 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008407{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008408 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008409 struct wpa_used_freq_data *freqs = NULL;
8410 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008411
8412 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8413 return;
8414
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008415 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8416 if (!freqs)
8417 return;
8418
8419 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008421 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008422 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008423 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8424 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008425 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8426 "channel list");
8427 return;
8428 }
8429
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008430 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008431
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008432 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008433
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008434 /*
8435 * The used frequencies map changed, so it is possible that a GO is
8436 * using a channel that is no longer valid for P2P use. It is also
8437 * possible that due to policy consideration, it would be preferable to
8438 * move it to a frequency already used by other station interfaces.
8439 */
8440 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8441
8442 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008443}
8444
8445
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008446static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8447 struct wpa_scan_results *scan_res)
8448{
8449 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8450}
8451
8452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008453int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8454{
8455 struct wpa_global *global = wpa_s->global;
8456 int found = 0;
8457 const u8 *peer;
8458
8459 if (global->p2p == NULL)
8460 return -1;
8461
8462 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8463
8464 if (wpa_s->pending_interface_name[0] &&
8465 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8466 found = 1;
8467
8468 peer = p2p_get_go_neg_peer(global->p2p);
8469 if (peer) {
8470 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8471 MACSTR, MAC2STR(peer));
8472 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008473 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008474 }
8475
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008476 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8477 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8478 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8479 found = 1;
8480 }
8481
8482 if (wpa_s->pending_pd_before_join) {
8483 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8484 wpa_s->pending_pd_before_join = 0;
8485 found = 1;
8486 }
8487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008488 wpas_p2p_stop_find(wpa_s);
8489
8490 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8491 if (wpa_s == global->p2p_group_formation &&
8492 (wpa_s->p2p_in_provisioning ||
8493 wpa_s->parent->pending_interface_type ==
8494 WPA_IF_P2P_CLIENT)) {
8495 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8496 "formation found - cancelling",
8497 wpa_s->ifname);
8498 found = 1;
8499 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008500 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008501 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008502 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008503 break;
8504 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008505 wpas_p2p_group_delete(wpa_s,
8506 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008507 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008508 } else if (wpa_s->p2p_in_invitation) {
8509 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8510 wpa_s->ifname);
8511 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008512 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008513 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008514 }
8515 }
8516
8517 if (!found) {
8518 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8519 return -1;
8520 }
8521
8522 return 0;
8523}
8524
8525
8526void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8527{
8528 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8529 return;
8530
8531 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8532 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008533 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008534}
8535
8536
8537void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8538 int freq_24, int freq_5, int freq_overall)
8539{
8540 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008541 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008542 return;
8543 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8544}
8545
8546
8547int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8548{
8549 u8 peer[ETH_ALEN];
8550 struct p2p_data *p2p = wpa_s->global->p2p;
8551
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008552 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008553 return -1;
8554
8555 if (hwaddr_aton(addr, peer))
8556 return -1;
8557
8558 return p2p_unauthorize(p2p, peer);
8559}
8560
8561
8562/**
8563 * wpas_p2p_disconnect - Disconnect from a P2P Group
8564 * @wpa_s: Pointer to wpa_supplicant data
8565 * Returns: 0 on success, -1 on failure
8566 *
8567 * This can be used to disconnect from a group in which the local end is a P2P
8568 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8569 * virtual network interface was created for this group, that interface will be
8570 * removed. Otherwise, only the configured P2P group network will be removed
8571 * from the interface.
8572 */
8573int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8574{
8575
8576 if (wpa_s == NULL)
8577 return -1;
8578
Jouni Malinen2b89da82012-08-31 22:04:41 +03008579 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8580 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008581}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008582
8583
8584int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8585{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008586 int ret;
8587
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008588 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8589 return 0;
8590
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008591 ret = p2p_in_progress(wpa_s->global->p2p);
8592 if (ret == 0) {
8593 /*
8594 * Check whether there is an ongoing WPS provisioning step (or
8595 * other parts of group formation) on another interface since
8596 * p2p_in_progress() does not report this to avoid issues for
8597 * scans during such provisioning step.
8598 */
8599 if (wpa_s->global->p2p_group_formation &&
8600 wpa_s->global->p2p_group_formation != wpa_s) {
8601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8602 "in group formation",
8603 wpa_s->global->p2p_group_formation->ifname);
8604 ret = 1;
8605 }
8606 }
8607
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008608 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008609 struct os_reltime now;
8610 os_get_reltime(&now);
8611 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8612 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008613 /* Wait for the first client has expired */
8614 wpa_s->global->p2p_go_wait_client.sec = 0;
8615 } else {
8616 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8617 ret = 1;
8618 }
8619 }
8620
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008621 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008622}
8623
8624
8625void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8626 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008627{
8628 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8629 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008630 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008631 /**
8632 * Remove the network by scheduling the group formation
8633 * timeout to happen immediately. The teardown code
8634 * needs to be scheduled to run asynch later so that we
8635 * don't delete data from under ourselves unexpectedly.
8636 * Calling wpas_p2p_group_formation_timeout directly
8637 * causes a series of crashes in WPS failure scenarios.
8638 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008639 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8640 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008641 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008642 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008643 }
8644}
8645
8646
8647struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008648 const u8 *addr, const u8 *ssid,
8649 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008650{
8651 struct wpa_ssid *s;
8652 size_t i;
8653
8654 for (s = wpa_s->conf->ssid; s; s = s->next) {
8655 if (s->disabled != 2)
8656 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008657 if (ssid &&
8658 (ssid_len != s->ssid_len ||
8659 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8660 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008661 if (addr == NULL) {
8662 if (s->mode == WPAS_MODE_P2P_GO)
8663 return s;
8664 continue;
8665 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008666 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8667 return s; /* peer is GO in the persistent group */
8668 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8669 continue;
8670 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008671 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008672 addr, ETH_ALEN) == 0)
8673 return s; /* peer is P2P client in persistent
8674 * group */
8675 }
8676 }
8677
8678 return NULL;
8679}
8680
8681
8682void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8683 const u8 *addr)
8684{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008685 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008686 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008687 /*
8688 * This can happen if WPS provisioning step is not terminated
8689 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8690 * peer was able to connect, there is no need to time out group
8691 * formation after this, though. In addition, this is used with
8692 * the initial connection wait on the GO as a separate formation
8693 * timeout and as such, expected to be hit after the initial WPS
8694 * provisioning step.
8695 */
8696 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008697
8698 if (!wpa_s->p2p_go_group_formation_completed &&
8699 !wpa_s->group_formation_reported) {
8700 /*
8701 * GO has not yet notified group formation success since
8702 * the WPS step was not completed cleanly. Do that
8703 * notification now since the P2P Client was able to
8704 * connect and as such, must have received the
8705 * credential from the WPS step.
8706 */
8707 if (wpa_s->global->p2p)
8708 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008709 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008710 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008711 }
8712 if (!wpa_s->p2p_go_group_formation_completed) {
8713 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8714 wpa_s->p2p_go_group_formation_completed = 1;
8715 wpa_s->global->p2p_group_formation = NULL;
8716 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008717 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008718 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008719 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008720 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008721 if (addr == NULL)
8722 return;
8723 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8724}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008725
Dmitry Shmidt04949592012-07-19 12:16:46 -07008726
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008727static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8728 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008729{
8730 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008731 int ret = 0;
8732
Dmitry Shmidt04949592012-07-19 12:16:46 -07008733 if (wpa_s->global->p2p_group_formation)
8734 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008735 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008736 offchannel_send_action_done(wpa_s);
8737 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008738 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008739 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8740 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8741 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8742 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008743 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008744 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008745 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008746 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008747 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008748 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008749 wpa_s->p2p_go_he,
8750 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008751 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008752 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008753}
8754
8755
8756int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8757{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008758 int res;
8759
Dmitry Shmidt04949592012-07-19 12:16:46 -07008760 if (!wpa_s->p2p_fallback_to_go_neg ||
8761 wpa_s->p2p_in_provisioning <= 5)
8762 return 0;
8763
8764 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8765 return 0; /* peer operating as a GO */
8766
8767 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8768 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008769 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008770 "reason=GO-not-found");
8771 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008772
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008773 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008774}
8775
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008776
8777unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8778{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008779 struct wpa_supplicant *ifs;
8780
8781 if (wpa_s->wpa_state > WPA_SCANNING) {
8782 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8783 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008784 wpa_s->conf->p2p_search_delay);
8785 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008786 }
8787
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008788 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8789 radio_list) {
8790 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008791 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8792 "delay due to concurrent operation on "
8793 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008794 wpa_s->conf->p2p_search_delay,
8795 ifs->ifname);
8796 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008797 }
8798 }
8799
8800 return 0;
8801}
8802
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008803
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008804static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8805 struct wpa_ssid *s, const u8 *addr,
8806 int iface_addr)
8807{
8808 struct psk_list_entry *psk, *tmp;
8809 int changed = 0;
8810
8811 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8812 list) {
8813 if ((iface_addr && !psk->p2p &&
8814 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8815 (!iface_addr && psk->p2p &&
8816 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8817 wpa_dbg(wpa_s, MSG_DEBUG,
8818 "P2P: Remove persistent group PSK list entry for "
8819 MACSTR " p2p=%u",
8820 MAC2STR(psk->addr), psk->p2p);
8821 dl_list_del(&psk->list);
8822 os_free(psk);
8823 changed++;
8824 }
8825 }
8826
8827 return changed;
8828}
8829
8830
8831void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8832 const u8 *p2p_dev_addr,
8833 const u8 *psk, size_t psk_len)
8834{
8835 struct wpa_ssid *ssid = wpa_s->current_ssid;
8836 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008837 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008838
8839 if (psk_len != sizeof(p->psk))
8840 return;
8841
8842 if (p2p_dev_addr) {
8843 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8844 " p2p_dev_addr=" MACSTR,
8845 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8846 if (is_zero_ether_addr(p2p_dev_addr))
8847 p2p_dev_addr = NULL;
8848 } else {
8849 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8850 MAC2STR(mac_addr));
8851 }
8852
8853 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8854 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8855 /* To be added to persistent group once created */
8856 if (wpa_s->global->add_psk == NULL) {
8857 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8858 if (wpa_s->global->add_psk == NULL)
8859 return;
8860 }
8861 p = wpa_s->global->add_psk;
8862 if (p2p_dev_addr) {
8863 p->p2p = 1;
8864 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8865 } else {
8866 p->p2p = 0;
8867 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8868 }
8869 os_memcpy(p->psk, psk, psk_len);
8870 return;
8871 }
8872
8873 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8874 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8875 return;
8876 }
8877
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008878 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008879 ssid->ssid_len);
8880 if (!persistent) {
8881 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8882 return;
8883 }
8884
8885 p = os_zalloc(sizeof(*p));
8886 if (p == NULL)
8887 return;
8888 if (p2p_dev_addr) {
8889 p->p2p = 1;
8890 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8891 } else {
8892 p->p2p = 0;
8893 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8894 }
8895 os_memcpy(p->psk, psk, psk_len);
8896
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008897 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8898 (last = dl_list_last(&persistent->psk_list,
8899 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008900 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8901 MACSTR " (p2p=%u) to make room for a new one",
8902 MAC2STR(last->addr), last->p2p);
8903 dl_list_del(&last->list);
8904 os_free(last);
8905 }
8906
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008907 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008908 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8909 p2p_dev_addr == NULL);
8910 if (p2p_dev_addr) {
8911 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8912 MACSTR, MAC2STR(p2p_dev_addr));
8913 } else {
8914 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8915 MAC2STR(mac_addr));
8916 }
8917 dl_list_add(&persistent->psk_list, &p->list);
8918
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008919 if (wpa_s->p2pdev->conf->update_config &&
8920 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008921 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008922}
8923
8924
8925static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8926 struct wpa_ssid *s, const u8 *addr,
8927 int iface_addr)
8928{
8929 int res;
8930
8931 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008932 if (res > 0 && wpa_s->conf->update_config &&
8933 wpa_config_write(wpa_s->confname, wpa_s->conf))
8934 wpa_dbg(wpa_s, MSG_DEBUG,
8935 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008936}
8937
8938
8939static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8940 const u8 *peer, int iface_addr)
8941{
8942 struct hostapd_data *hapd;
8943 struct hostapd_wpa_psk *psk, *prev, *rem;
8944 struct sta_info *sta;
8945
8946 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8947 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8948 return;
8949
8950 /* Remove per-station PSK entry */
8951 hapd = wpa_s->ap_iface->bss[0];
8952 prev = NULL;
8953 psk = hapd->conf->ssid.wpa_psk;
8954 while (psk) {
8955 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8956 (!iface_addr &&
8957 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8958 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8959 MACSTR " iface_addr=%d",
8960 MAC2STR(peer), iface_addr);
8961 if (prev)
8962 prev->next = psk->next;
8963 else
8964 hapd->conf->ssid.wpa_psk = psk->next;
8965 rem = psk;
8966 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07008967 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008968 } else {
8969 prev = psk;
8970 psk = psk->next;
8971 }
8972 }
8973
8974 /* Disconnect from group */
8975 if (iface_addr)
8976 sta = ap_get_sta(hapd, peer);
8977 else
8978 sta = ap_get_sta_p2p(hapd, peer);
8979 if (sta) {
8980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8981 " (iface_addr=%d) from group",
8982 MAC2STR(peer), iface_addr);
8983 hostapd_drv_sta_deauth(hapd, sta->addr,
8984 WLAN_REASON_DEAUTH_LEAVING);
8985 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8986 }
8987}
8988
8989
8990void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8991 int iface_addr)
8992{
8993 struct wpa_ssid *s;
8994 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008995 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008996
8997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8998
8999 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009000 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009001 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9002 continue;
9003 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009004 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9005 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009006 }
9007
9008 /* Remove from any operating group */
9009 for (w = wpa_s->global->ifaces; w; w = w->next)
9010 wpas_p2p_remove_client_go(w, peer, iface_addr);
9011}
9012
9013
9014static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9015{
9016 struct wpa_supplicant *wpa_s = eloop_ctx;
9017 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9018}
9019
9020
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009021static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9022{
9023 struct wpa_supplicant *wpa_s = eloop_ctx;
9024
9025 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9026 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9027}
9028
9029
9030int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9031 struct wpa_ssid *ssid)
9032{
9033 struct wpa_supplicant *iface;
9034
9035 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9036 if (!iface->current_ssid ||
9037 iface->current_ssid->frequency == freq ||
9038 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9039 !iface->current_ssid->p2p_group))
9040 continue;
9041
9042 /* Remove the connection with least priority */
9043 if (!wpas_is_p2p_prioritized(iface)) {
9044 /* STA connection has priority over existing
9045 * P2P connection, so remove the interface. */
9046 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9047 eloop_register_timeout(0, 0,
9048 wpas_p2p_group_freq_conflict,
9049 iface, NULL);
9050 /* If connection in progress is P2P connection, do not
9051 * proceed for the connection. */
9052 if (wpa_s == iface)
9053 return -1;
9054 else
9055 return 0;
9056 } else {
9057 /* P2P connection has priority, disable the STA network
9058 */
9059 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9060 ssid);
9061 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9062 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9063 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9064 ETH_ALEN);
9065 /* If P2P connection is in progress, continue
9066 * connecting...*/
9067 if (wpa_s == iface)
9068 return 0;
9069 else
9070 return -1;
9071 }
9072 }
9073
9074 return 0;
9075}
9076
9077
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009078int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9079{
9080 struct wpa_ssid *ssid = wpa_s->current_ssid;
9081
9082 if (ssid == NULL || !ssid->p2p_group)
9083 return 0;
9084
9085 if (wpa_s->p2p_last_4way_hs_fail &&
9086 wpa_s->p2p_last_4way_hs_fail == ssid) {
9087 u8 go_dev_addr[ETH_ALEN];
9088 struct wpa_ssid *persistent;
9089
9090 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9091 ssid->ssid,
9092 ssid->ssid_len) <= 0) {
9093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9094 goto disconnect;
9095 }
9096
9097 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9098 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009099 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009100 ssid->ssid,
9101 ssid->ssid_len);
9102 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9103 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9104 goto disconnect;
9105 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009106 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009107 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9108 persistent->id);
9109 disconnect:
9110 wpa_s->p2p_last_4way_hs_fail = NULL;
9111 /*
9112 * Remove the group from a timeout to avoid issues with caller
9113 * continuing to use the interface if this is on a P2P group
9114 * interface.
9115 */
9116 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9117 wpa_s, NULL);
9118 return 1;
9119 }
9120
9121 wpa_s->p2p_last_4way_hs_fail = ssid;
9122 return 0;
9123}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009124
9125
9126#ifdef CONFIG_WPS_NFC
9127
9128static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9129 struct wpabuf *p2p)
9130{
9131 struct wpabuf *ret;
9132 size_t wsc_len;
9133
9134 if (p2p == NULL) {
9135 wpabuf_free(wsc);
9136 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9137 return NULL;
9138 }
9139
9140 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9141 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9142 if (ret == NULL) {
9143 wpabuf_free(wsc);
9144 wpabuf_free(p2p);
9145 return NULL;
9146 }
9147
9148 wpabuf_put_be16(ret, wsc_len);
9149 if (wsc)
9150 wpabuf_put_buf(ret, wsc);
9151 wpabuf_put_be16(ret, wpabuf_len(p2p));
9152 wpabuf_put_buf(ret, p2p);
9153
9154 wpabuf_free(wsc);
9155 wpabuf_free(p2p);
9156 wpa_hexdump_buf(MSG_DEBUG,
9157 "P2P: Generated NFC connection handover message", ret);
9158
9159 if (ndef && ret) {
9160 struct wpabuf *tmp;
9161 tmp = ndef_build_p2p(ret);
9162 wpabuf_free(ret);
9163 if (tmp == NULL) {
9164 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9165 return NULL;
9166 }
9167 ret = tmp;
9168 }
9169
9170 return ret;
9171}
9172
9173
9174static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9175 struct wpa_ssid **ssid, u8 *go_dev_addr)
9176{
9177 struct wpa_supplicant *iface;
9178
9179 if (go_dev_addr)
9180 os_memset(go_dev_addr, 0, ETH_ALEN);
9181 if (ssid)
9182 *ssid = NULL;
9183 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9184 if (iface->wpa_state < WPA_ASSOCIATING ||
9185 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9186 !iface->current_ssid->p2p_group ||
9187 iface->current_ssid->mode != WPAS_MODE_INFRA)
9188 continue;
9189 if (ssid)
9190 *ssid = iface->current_ssid;
9191 if (go_dev_addr)
9192 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9193 return iface->assoc_freq;
9194 }
9195 return 0;
9196}
9197
9198
9199struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9200 int ndef)
9201{
9202 struct wpabuf *wsc, *p2p;
9203 struct wpa_ssid *ssid;
9204 u8 go_dev_addr[ETH_ALEN];
9205 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9206
9207 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9208 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9209 return NULL;
9210 }
9211
9212 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9213 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9214 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9215 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9216 return NULL;
9217 }
9218
9219 if (cli_freq == 0) {
9220 wsc = wps_build_nfc_handover_req_p2p(
9221 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9222 } else
9223 wsc = NULL;
9224 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9225 go_dev_addr, ssid ? ssid->ssid : NULL,
9226 ssid ? ssid->ssid_len : 0);
9227
9228 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9229}
9230
9231
9232struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9233 int ndef, int tag)
9234{
9235 struct wpabuf *wsc, *p2p;
9236 struct wpa_ssid *ssid;
9237 u8 go_dev_addr[ETH_ALEN];
9238 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9239
9240 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9241 return NULL;
9242
9243 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9244 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9245 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9246 return NULL;
9247
9248 if (cli_freq == 0) {
9249 wsc = wps_build_nfc_handover_sel_p2p(
9250 wpa_s->parent->wps,
9251 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9252 DEV_PW_NFC_CONNECTION_HANDOVER,
9253 wpa_s->conf->wps_nfc_dh_pubkey,
9254 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9255 } else
9256 wsc = NULL;
9257 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9258 go_dev_addr, ssid ? ssid->ssid : NULL,
9259 ssid ? ssid->ssid_len : 0);
9260
9261 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9262}
9263
9264
9265static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9266 struct p2p_nfc_params *params)
9267{
9268 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9269 "connection handover (freq=%d)",
9270 params->go_freq);
9271
9272 if (params->go_freq && params->go_ssid_len) {
9273 wpa_s->p2p_wps_method = WPS_NFC;
9274 wpa_s->pending_join_wps_method = WPS_NFC;
9275 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9276 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9277 ETH_ALEN);
9278 return wpas_p2p_join_start(wpa_s, params->go_freq,
9279 params->go_ssid,
9280 params->go_ssid_len);
9281 }
9282
9283 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9284 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009285 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009286 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009287 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009288 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009289 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009290}
9291
9292
9293static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9294 struct p2p_nfc_params *params, int tag)
9295{
9296 int res, persistent;
9297 struct wpa_ssid *ssid;
9298
9299 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9300 "connection handover");
9301 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9302 ssid = wpa_s->current_ssid;
9303 if (ssid == NULL)
9304 continue;
9305 if (ssid->mode != WPAS_MODE_P2P_GO)
9306 continue;
9307 if (wpa_s->ap_iface == NULL)
9308 continue;
9309 break;
9310 }
9311 if (wpa_s == NULL) {
9312 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9313 return -1;
9314 }
9315
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009316 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009317 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009318 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009319 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9320 return -1;
9321 }
9322 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009323 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9324 wpa_s->p2pdev->p2p_oob_dev_pw,
9325 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9326 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009327 if (res)
9328 return res;
9329
9330 if (!tag) {
9331 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9332 return 0;
9333 }
9334
9335 if (!params->peer ||
9336 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9337 return 0;
9338
9339 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9340 " to join", MAC2STR(params->peer->p2p_device_addr));
9341
9342 wpa_s->global->p2p_invite_group = wpa_s;
9343 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009344 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009345 params->peer->p2p_device_addr,
9346 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009347 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009348
9349 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9350 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9351 ssid->ssid, ssid->ssid_len, ssid->frequency,
9352 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009353 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009354}
9355
9356
9357static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9358 struct p2p_nfc_params *params,
9359 int forced_freq)
9360{
9361 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9362 "connection handover");
9363 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9364 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009365 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009366 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009367 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009368 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009369}
9370
9371
9372static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9373 struct p2p_nfc_params *params,
9374 int forced_freq)
9375{
9376 int res;
9377
9378 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9379 "connection handover");
9380 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9381 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009382 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009383 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009384 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009385 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009386 if (res)
9387 return res;
9388
9389 res = wpas_p2p_listen(wpa_s, 60);
9390 if (res) {
9391 p2p_unauthorize(wpa_s->global->p2p,
9392 params->peer->p2p_device_addr);
9393 }
9394
9395 return res;
9396}
9397
9398
9399static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9400 const struct wpabuf *data,
9401 int sel, int tag, int forced_freq)
9402{
9403 const u8 *pos, *end;
9404 u16 len, id;
9405 struct p2p_nfc_params params;
9406 int res;
9407
9408 os_memset(&params, 0, sizeof(params));
9409 params.sel = sel;
9410
9411 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9412
9413 pos = wpabuf_head(data);
9414 end = pos + wpabuf_len(data);
9415
9416 if (end - pos < 2) {
9417 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9418 "attributes");
9419 return -1;
9420 }
9421 len = WPA_GET_BE16(pos);
9422 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009423 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009424 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9425 "attributes");
9426 return -1;
9427 }
9428 params.wsc_attr = pos;
9429 params.wsc_len = len;
9430 pos += len;
9431
9432 if (end - pos < 2) {
9433 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9434 "attributes");
9435 return -1;
9436 }
9437 len = WPA_GET_BE16(pos);
9438 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009439 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009440 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9441 "attributes");
9442 return -1;
9443 }
9444 params.p2p_attr = pos;
9445 params.p2p_len = len;
9446 pos += len;
9447
9448 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9449 params.wsc_attr, params.wsc_len);
9450 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9451 params.p2p_attr, params.p2p_len);
9452 if (pos < end) {
9453 wpa_hexdump(MSG_DEBUG,
9454 "P2P: Ignored extra data after P2P attributes",
9455 pos, end - pos);
9456 }
9457
9458 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9459 if (res)
9460 return res;
9461
9462 if (params.next_step == NO_ACTION)
9463 return 0;
9464
9465 if (params.next_step == BOTH_GO) {
9466 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9467 MAC2STR(params.peer->p2p_device_addr));
9468 return 0;
9469 }
9470
9471 if (params.next_step == PEER_CLIENT) {
9472 if (!is_zero_ether_addr(params.go_dev_addr)) {
9473 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9474 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9475 " ssid=\"%s\"",
9476 MAC2STR(params.peer->p2p_device_addr),
9477 params.go_freq,
9478 MAC2STR(params.go_dev_addr),
9479 wpa_ssid_txt(params.go_ssid,
9480 params.go_ssid_len));
9481 } else {
9482 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9483 "peer=" MACSTR " freq=%d",
9484 MAC2STR(params.peer->p2p_device_addr),
9485 params.go_freq);
9486 }
9487 return 0;
9488 }
9489
9490 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9491 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9492 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9493 return 0;
9494 }
9495
9496 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9497 wpa_s->p2p_oob_dev_pw = NULL;
9498
9499 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9500 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9501 "received");
9502 return -1;
9503 }
9504
9505 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9506 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9507 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9508 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9509 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9510 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9511 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9512
9513 if (tag) {
9514 if (id < 0x10) {
9515 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9516 "peer OOB Device Password Id %u", id);
9517 return -1;
9518 }
9519 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9520 "Device Password Id %u", id);
9521 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9522 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9523 params.oob_dev_pw_len -
9524 WPS_OOB_PUBKEY_HASH_LEN - 2);
9525 wpa_s->p2p_oob_dev_pw_id = id;
9526 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9527 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9528 params.oob_dev_pw_len -
9529 WPS_OOB_PUBKEY_HASH_LEN - 2);
9530 if (wpa_s->p2p_oob_dev_pw == NULL)
9531 return -1;
9532
9533 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9534 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9535 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9536 return -1;
9537 } else {
9538 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9539 "without Device Password");
9540 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9541 }
9542
9543 switch (params.next_step) {
9544 case NO_ACTION:
9545 case BOTH_GO:
9546 case PEER_CLIENT:
9547 /* already covered above */
9548 return 0;
9549 case JOIN_GROUP:
9550 return wpas_p2p_nfc_join_group(wpa_s, &params);
9551 case AUTH_JOIN:
9552 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9553 case INIT_GO_NEG:
9554 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9555 case RESP_GO_NEG:
9556 /* TODO: use own OOB Dev Pw */
9557 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9558 }
9559
9560 return -1;
9561}
9562
9563
9564int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9565 const struct wpabuf *data, int forced_freq)
9566{
9567 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9568 return -1;
9569
9570 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9571}
9572
9573
9574int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9575 const struct wpabuf *req,
9576 const struct wpabuf *sel, int forced_freq)
9577{
9578 struct wpabuf *tmp;
9579 int ret;
9580
9581 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9582 return -1;
9583
9584 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9585
9586 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9587 wpabuf_head(req), wpabuf_len(req));
9588 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9589 wpabuf_head(sel), wpabuf_len(sel));
9590 if (forced_freq)
9591 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9592 tmp = ndef_parse_p2p(init ? sel : req);
9593 if (tmp == NULL) {
9594 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9595 return -1;
9596 }
9597
9598 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9599 forced_freq);
9600 wpabuf_free(tmp);
9601
9602 return ret;
9603}
9604
9605
9606int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9607{
9608 const u8 *if_addr;
9609 int go_intent = wpa_s->conf->p2p_go_intent;
9610 struct wpa_supplicant *iface;
9611
9612 if (wpa_s->global->p2p == NULL)
9613 return -1;
9614
9615 if (!enabled) {
9616 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9617 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9618 {
9619 if (!iface->ap_iface)
9620 continue;
9621 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9622 }
9623 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9624 0, NULL);
9625 if (wpa_s->p2p_nfc_tag_enabled)
9626 wpas_p2p_remove_pending_group_interface(wpa_s);
9627 wpa_s->p2p_nfc_tag_enabled = 0;
9628 return 0;
9629 }
9630
9631 if (wpa_s->global->p2p_disabled)
9632 return -1;
9633
9634 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9635 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9636 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9637 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9638 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9639 "to allow static handover cases");
9640 return -1;
9641 }
9642
9643 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9644
9645 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9646 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9647 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9648 if (wpa_s->p2p_oob_dev_pw == NULL)
9649 return -1;
9650 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9651
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009652 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9653 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9654 /*
9655 * P2P Group Interface present and the command came on group
9656 * interface, so enable the token for the current interface.
9657 */
9658 wpa_s->create_p2p_iface = 0;
9659 } else {
9660 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9661 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009662
9663 if (wpa_s->create_p2p_iface) {
9664 enum wpa_driver_if_type iftype;
9665 /* Prepare to add a new interface for the group */
9666 iftype = WPA_IF_P2P_GROUP;
9667 if (go_intent == 15)
9668 iftype = WPA_IF_P2P_GO;
9669 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9670 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9671 "interface for the group");
9672 return -1;
9673 }
9674
9675 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009676 } else if (wpa_s->p2p_mgmt)
9677 if_addr = wpa_s->parent->own_addr;
9678 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009679 if_addr = wpa_s->own_addr;
9680
9681 wpa_s->p2p_nfc_tag_enabled = enabled;
9682
9683 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9684 struct hostapd_data *hapd;
9685 if (iface->ap_iface == NULL)
9686 continue;
9687 hapd = iface->ap_iface->bss[0];
9688 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9689 hapd->conf->wps_nfc_dh_pubkey =
9690 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9691 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9692 hapd->conf->wps_nfc_dh_privkey =
9693 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9694 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9695 hapd->conf->wps_nfc_dev_pw =
9696 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9697 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9698
9699 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9700 wpa_dbg(iface, MSG_DEBUG,
9701 "P2P: Failed to enable NFC Tag for GO");
9702 }
9703 }
9704 p2p_set_authorized_oob_dev_pw_id(
9705 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9706 if_addr);
9707
9708 return 0;
9709}
9710
9711#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009712
9713
9714static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9715 struct wpa_used_freq_data *freqs,
9716 unsigned int num)
9717{
9718 u8 curr_chan, cand, chan;
9719 unsigned int i;
9720
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009721 /*
9722 * If possible, optimize the Listen channel to be a channel that is
9723 * already used by one of the other interfaces.
9724 */
9725 if (!wpa_s->conf->p2p_optimize_listen_chan)
9726 return;
9727
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009728 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9729 for (i = 0, cand = 0; i < num; i++) {
9730 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9731 if (curr_chan == chan) {
9732 cand = 0;
9733 break;
9734 }
9735
9736 if (chan == 1 || chan == 6 || chan == 11)
9737 cand = chan;
9738 }
9739
9740 if (cand) {
9741 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009742 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009743 cand);
9744 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9745 }
9746}
9747
9748
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009749static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009750{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009751 struct hostapd_config *conf;
9752 struct p2p_go_neg_results params;
9753 struct csa_settings csa_settings;
9754 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9755 int old_freq = current_ssid->frequency;
9756 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009757
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009758 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9759 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9760 return -1;
9761 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009762
9763 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009764 * TODO: This function may not always work correctly. For example,
9765 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009766 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009767 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9768 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009769 wpa_dbg(wpa_s, MSG_DEBUG,
9770 "P2P CSA: Failed to select new frequency for GO");
9771 return -1;
9772 }
9773
9774 if (current_ssid->frequency == params.freq) {
9775 wpa_dbg(wpa_s, MSG_DEBUG,
9776 "P2P CSA: Selected same frequency - not moving GO");
9777 return 0;
9778 }
9779
9780 conf = hostapd_config_defaults();
9781 if (!conf) {
9782 wpa_dbg(wpa_s, MSG_DEBUG,
9783 "P2P CSA: Failed to allocate default config");
9784 return -1;
9785 }
9786
9787 current_ssid->frequency = params.freq;
9788 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9789 wpa_dbg(wpa_s, MSG_DEBUG,
9790 "P2P CSA: Failed to create new GO config");
9791 ret = -1;
9792 goto out;
9793 }
9794
Sunil4a3f9f52022-07-19 22:04:39 +00009795 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9796 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009797 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009798 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9799 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009800 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9801 wpa_s->ap_iface->current_mode->mode,
9802 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9803 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009804 ret = -1;
9805 goto out;
9806 }
9807
9808 os_memset(&csa_settings, 0, sizeof(csa_settings));
9809 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9810 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9811 csa_settings.freq_params.freq = params.freq;
9812 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9813 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9814 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9815
9816 if (conf->ieee80211ac) {
9817 int freq1 = 0, freq2 = 0;
9818 u8 chan, opclass;
9819
9820 if (ieee80211_freq_to_channel_ext(params.freq,
9821 conf->secondary_channel,
9822 conf->vht_oper_chwidth,
9823 &opclass, &chan) ==
9824 NUM_HOSTAPD_MODES) {
9825 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9826 ret = -1;
9827 goto out;
9828 }
9829
9830 if (conf->vht_oper_centr_freq_seg0_idx)
9831 freq1 = ieee80211_chan_to_freq(
9832 NULL, opclass,
9833 conf->vht_oper_centr_freq_seg0_idx);
9834
9835 if (conf->vht_oper_centr_freq_seg1_idx)
9836 freq2 = ieee80211_chan_to_freq(
9837 NULL, opclass,
9838 conf->vht_oper_centr_freq_seg1_idx);
9839
9840 if (freq1 < 0 || freq2 < 0) {
9841 wpa_dbg(wpa_s, MSG_DEBUG,
9842 "P2P CSA: Selected invalid VHT center freqs");
9843 ret = -1;
9844 goto out;
9845 }
9846
9847 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9848 csa_settings.freq_params.center_freq1 = freq1;
9849 csa_settings.freq_params.center_freq2 = freq2;
9850
9851 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009852 case CONF_OPER_CHWIDTH_80MHZ:
9853 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009854 csa_settings.freq_params.bandwidth = 80;
9855 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009856 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009857 csa_settings.freq_params.bandwidth = 160;
9858 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009859 default:
9860 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009861 }
9862 }
9863
9864 ret = ap_switch_channel(wpa_s, &csa_settings);
9865out:
9866 current_ssid->frequency = old_freq;
9867 hostapd_config_free(conf);
9868 return ret;
9869}
9870
9871
9872static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9873{
9874 struct p2p_go_neg_results params;
9875 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009876 void (*ap_configured_cb)(void *ctx, void *data);
9877 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009878
9879 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9880
9881 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9882 current_ssid->frequency);
9883
9884 /* Stop the AP functionality */
9885 /* TODO: Should do this in a way that does not indicated to possible
9886 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009887 /* If this action occurs before a group is started, the callback should
9888 * be preserved, or GROUP-STARTED event would be lost. If this action
9889 * occurs after a group is started, these pointers are all NULL and
9890 * harmless. */
9891 ap_configured_cb = wpa_s->ap_configured_cb;
9892 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9893 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009894 wpa_supplicant_ap_deinit(wpa_s);
9895
9896 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009897 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9898 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009899 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9900 wpas_p2p_group_delete(wpa_s,
9901 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9902 return;
9903 }
9904 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9905 params.freq);
9906
9907 if (params.freq &&
9908 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9909 wpa_printf(MSG_DEBUG,
9910 "P2P: Selected freq (%u MHz) is not valid for P2P",
9911 params.freq);
9912 wpas_p2p_group_delete(wpa_s,
9913 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9914 return;
9915 }
9916
Hai Shalom899fcc72020-10-19 14:38:18 -07009917 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009918 wpa_s->ap_configured_cb = ap_configured_cb;
9919 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9920 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009921
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009922 /* Update the frequency */
9923 current_ssid->frequency = params.freq;
9924 wpa_s->connect_without_scan = current_ssid;
9925 wpa_s->reassociate = 1;
9926 wpa_s->disconnected = 0;
9927 wpa_supplicant_req_scan(wpa_s, 0, 0);
9928}
9929
9930
9931static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9932{
9933 struct wpa_supplicant *wpa_s = eloop_ctx;
9934
9935 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009936 return;
9937
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009938 wpas_p2p_go_update_common_freqs(wpa_s);
9939
9940 /* Do not move GO in the middle of a CSA */
9941 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9942 wpa_printf(MSG_DEBUG,
9943 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009944 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009945 }
9946
9947 /*
9948 * First, try a channel switch flow. If it is not supported or fails,
9949 * take down the GO and bring it up again.
9950 */
9951 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9952 wpas_p2p_move_go_no_csa(wpa_s);
9953}
9954
9955
9956static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9957{
9958 struct wpa_supplicant *wpa_s = eloop_ctx;
9959 struct wpa_used_freq_data *freqs = NULL;
9960 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009961
9962 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9963 if (!freqs)
9964 return;
9965
9966 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9967
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009968 /* Previous attempt to move a GO was not possible -- try again. */
9969 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9970 WPAS_P2P_CHANNEL_UPDATE_ANY);
9971
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009972 os_free(freqs);
9973}
9974
9975
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009976/*
9977 * Consider moving a GO from its currently used frequency:
9978 * 1. It is possible that due to regulatory consideration the frequency
9979 * can no longer be used and there is a need to evacuate the GO.
9980 * 2. It is possible that due to MCC considerations, it would be preferable
9981 * to move the GO to a channel that is currently used by some other
9982 * station interface.
9983 *
9984 * In case a frequency that became invalid is once again valid, cancel a
9985 * previously initiated GO frequency change.
9986 */
9987static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9988 struct wpa_used_freq_data *freqs,
9989 unsigned int num)
9990{
9991 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9992 unsigned int timeout;
9993 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009994 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009995
9996 wpas_p2p_go_update_common_freqs(wpa_s);
9997
9998 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009999 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010000 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010001 for (i = 0, invalid_freq = 0; i < num; i++) {
10002 if (freqs[i].freq == freq) {
10003 flags = freqs[i].flags;
10004
10005 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010006 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10007 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010008 wpa_dbg(wpa_s, MSG_DEBUG,
10009 "P2P: Freq=%d MHz no longer valid for GO",
10010 freq);
10011 invalid_freq = 1;
10012 }
10013 } else if (freqs[i].flags == 0) {
10014 /* Freq is not used by any other station interface */
10015 continue;
10016 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010017 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010018 /* Freq is not valid for P2P use cases */
10019 continue;
10020 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10021 P2P_GO_FREQ_MOVE_SCM) {
10022 policy_move = 1;
10023 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10024 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10025 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10026 policy_move = 1;
10027 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10028 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10029 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10030 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10031 policy_move = 1;
10032 } else if ((wpa_s->drv_flags &
10033 WPA_DRIVER_FLAGS_AP_CSA) &&
10034 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10035 u8 chan;
10036
10037 /*
10038 * We do not support CSA between bands, so move
10039 * GO only within the same band.
10040 */
10041 if (wpa_s->ap_iface->current_mode->mode ==
10042 ieee80211_freq_to_chan(freqs[i].freq,
10043 &chan))
10044 policy_move = 1;
10045 }
10046 }
10047 }
10048
10049 wpa_dbg(wpa_s, MSG_DEBUG,
10050 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10051 invalid_freq, policy_move, flags);
10052
10053 /*
10054 * The channel is valid, or we are going to have a policy move, so
10055 * cancel timeout.
10056 */
10057 if (!invalid_freq || policy_move) {
10058 wpa_dbg(wpa_s, MSG_DEBUG,
10059 "P2P: Cancel a GO move from freq=%d MHz", freq);
10060 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10061
10062 if (wpas_p2p_in_progress(wpa_s)) {
10063 wpa_dbg(wpa_s, MSG_DEBUG,
10064 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10065 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10066 wpa_s, NULL);
10067 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10068 wpas_p2p_reconsider_moving_go,
10069 wpa_s, NULL);
10070 return;
10071 }
10072 }
10073
10074 if (!invalid_freq && (!policy_move || flags != 0)) {
10075 wpa_dbg(wpa_s, MSG_DEBUG,
10076 "P2P: Not initiating a GO frequency change");
10077 return;
10078 }
10079
10080 /*
10081 * Do not consider moving GO if it is in the middle of a CSA. When the
10082 * CSA is finished this flow should be retriggered.
10083 */
10084 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10085 wpa_dbg(wpa_s, MSG_DEBUG,
10086 "P2P: Not initiating a GO frequency change - CSA is in progress");
10087 return;
10088 }
10089
10090 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10091 timeout = P2P_GO_FREQ_CHANGE_TIME;
10092 else
10093 timeout = 0;
10094
10095 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10096 freq, timeout);
10097 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10098 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10099}
10100
10101
10102static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10103 struct wpa_used_freq_data *freqs,
10104 unsigned int num,
10105 enum wpas_p2p_channel_update_trig trig)
10106{
10107 struct wpa_supplicant *ifs;
10108
10109 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10110 NULL);
10111
10112 /*
10113 * Travers all the radio interfaces, and for each GO interface, check
10114 * if there is a need to move the GO from the frequency it is using,
10115 * or in case the frequency is valid again, cancel the evacuation flow.
10116 */
10117 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10118 radio_list) {
10119 if (ifs->current_ssid == NULL ||
10120 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10121 continue;
10122
10123 /*
10124 * The GO was just started or completed channel switch, no need
10125 * to move it.
10126 */
10127 if (wpa_s == ifs &&
10128 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10129 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10130 wpa_dbg(wpa_s, MSG_DEBUG,
10131 "P2P: GO move - schedule re-consideration");
10132 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10133 wpas_p2p_reconsider_moving_go,
10134 wpa_s, NULL);
10135 continue;
10136 }
10137
10138 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10139 }
10140}
10141
10142
10143void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10144{
10145 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10146 return;
10147
10148 wpas_p2p_update_channel_list(wpa_s,
10149 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10150}
10151
10152
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010153void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10154{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010155 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10156 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10157 "the management interface is being removed");
10158 wpas_p2p_deinit_global(wpa_s->global);
10159 }
10160}
10161
10162
10163void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10164{
10165 if (wpa_s->ap_iface->bss)
10166 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10167 wpas_p2p_group_deinit(wpa_s);
10168}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010169
10170
10171int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10172 unsigned int period, unsigned int interval,
10173 unsigned int count)
10174{
10175 struct p2p_data *p2p = wpa_s->global->p2p;
10176 u8 *device_types;
10177 size_t dev_types_len;
10178 struct wpabuf *buf;
10179 int ret;
10180
10181 if (wpa_s->p2p_lo_started) {
10182 wpa_dbg(wpa_s, MSG_DEBUG,
10183 "P2P Listen offload is already started");
10184 return 0;
10185 }
10186
10187 if (wpa_s->global->p2p == NULL ||
10188 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10189 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10190 return -1;
10191 }
10192
10193 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10194 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10195 freq);
10196 return -1;
10197 }
10198
10199 /* Get device type */
10200 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10201 WPS_DEV_TYPE_LEN;
10202 device_types = os_malloc(dev_types_len);
10203 if (!device_types)
10204 return -1;
10205 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10206 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10207 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10208
10209 /* Get Probe Response IE(s) */
10210 buf = p2p_build_probe_resp_template(p2p, freq);
10211 if (!buf) {
10212 os_free(device_types);
10213 return -1;
10214 }
10215
10216 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10217 device_types, dev_types_len,
10218 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10219 if (ret < 0)
10220 wpa_dbg(wpa_s, MSG_DEBUG,
10221 "P2P: Failed to start P2P listen offload");
10222
10223 os_free(device_types);
10224 wpabuf_free(buf);
10225
10226 if (ret == 0) {
10227 wpa_s->p2p_lo_started = 1;
10228
10229 /* Stop current P2P listen if any */
10230 wpas_stop_listen(wpa_s);
10231 }
10232
10233 return ret;
10234}
10235
10236
10237int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10238{
10239 int ret;
10240
10241 if (!wpa_s->p2p_lo_started)
10242 return 0;
10243
10244 ret = wpa_drv_p2p_lo_stop(wpa_s);
10245 if (ret < 0)
10246 wpa_dbg(wpa_s, MSG_DEBUG,
10247 "P2P: Failed to stop P2P listen offload");
10248
10249 wpa_s->p2p_lo_started = 0;
10250 return ret;
10251}