blob: c8f2e5ca3d1131b819702b4a8032903c575c7ff2 [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,
132 unsigned int *pref_freq_list,
133 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;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800708 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800709
710 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
711
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800712 if (force_freq)
713 *force_freq = 0;
714 if (pref_freq)
715 *pref_freq = 0;
716
717 size = P2P_MAX_PREF_CHANNELS;
718 if (force_freq && pref_freq &&
719 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
720 (int *) pref_freq, 0, pref_freq_list, &size))
721 wpas_p2p_set_own_freq_preference(wpa_s,
722 *force_freq ? *force_freq :
723 *pref_freq);
724
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800725 /*
726 * For non-concurrent capable devices:
727 * If persistent_go, then no new.
728 * If GO, then no client.
729 * If client, then no GO.
730 */
731 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800732 if (go_wpa_s)
733 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800734 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800735 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
736 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800737
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800738 wpa_printf(MSG_DEBUG,
739 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
740 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800741
742 /* If not concurrent, restrict our choices */
743 if (p2p_no_group_iface) {
744 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
745
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800746 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800747 return P2PS_SETUP_NONE;
748
749 if (go_wpa_s) {
750 if (role == P2PS_SETUP_CLIENT ||
751 incoming == P2PS_SETUP_GROUP_OWNER ||
752 p2p_client_limit_reached(go_wpa_s->p2p_group))
753 return P2PS_SETUP_NONE;
754
755 return P2PS_SETUP_GROUP_OWNER;
756 }
757
758 if (persistent_go) {
759 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
760 if (!incoming)
761 return P2PS_SETUP_GROUP_OWNER |
762 P2PS_SETUP_CLIENT;
763 if (incoming == P2PS_SETUP_NEW) {
764 u8 r;
765
766 if (os_get_random(&r, sizeof(r)) < 0 ||
767 (r & 1))
768 return P2PS_SETUP_CLIENT;
769 return P2PS_SETUP_GROUP_OWNER;
770 }
771 }
772 }
773 }
774
775 /* If a required role has been specified, handle it here */
776 if (role && role != P2PS_SETUP_NEW) {
777 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800778 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
779 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
780 /*
781 * Peer has an active GO, so if the role allows it and
782 * we do not have any active roles, become client.
783 */
784 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
785 !cli_wpa_s)
786 return P2PS_SETUP_CLIENT;
787
788 /* fall through */
789
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800790 case P2PS_SETUP_NONE:
791 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800792 conncap = role;
793 goto grp_owner;
794
795 case P2PS_SETUP_GROUP_OWNER:
796 /*
797 * Must be a complimentary role - cannot be a client to
798 * more than one peer.
799 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800800 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800801 return P2PS_SETUP_NONE;
802
803 return P2PS_SETUP_CLIENT;
804
805 case P2PS_SETUP_CLIENT:
806 /* Must be a complimentary role */
807 if (incoming != role) {
808 conncap = P2PS_SETUP_GROUP_OWNER;
809 goto grp_owner;
810 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700811 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800812
813 default:
814 return P2PS_SETUP_NONE;
815 }
816 }
817
818 /*
819 * For now, we only will support ownership of one group, and being a
820 * client of one group. Therefore, if we have either an existing GO
821 * group, or an existing client group, we will not do a new GO
822 * negotiation, but rather try to re-use the existing groups.
823 */
824 switch (incoming) {
825 case P2PS_SETUP_NONE:
826 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800827 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800828 conncap = P2PS_SETUP_GROUP_OWNER;
829 else if (!owned_members)
830 conncap = P2PS_SETUP_NEW;
831 else if (incoming == P2PS_SETUP_NONE)
832 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
833 else
834 conncap = P2PS_SETUP_CLIENT;
835 break;
836
837 case P2PS_SETUP_CLIENT:
838 conncap = P2PS_SETUP_GROUP_OWNER;
839 break;
840
841 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800842 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800843 conncap = P2PS_SETUP_CLIENT;
844 break;
845
846 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
847 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800848 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800849 conncap = P2PS_SETUP_GROUP_OWNER;
850 else {
851 u8 r;
852
853 if (os_get_random(&r, sizeof(r)) < 0 ||
854 (r & 1))
855 conncap = P2PS_SETUP_CLIENT;
856 else
857 conncap = P2PS_SETUP_GROUP_OWNER;
858 }
859 break;
860
861 default:
862 return P2PS_SETUP_NONE;
863 }
864
865grp_owner:
866 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
867 (!incoming && (conncap & P2PS_SETUP_NEW))) {
868 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
869 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800870
871 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800872 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800873 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800874 wpa_s->p2p_mgmt ?
875 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800876 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800877 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800878 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800879 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800880 wpa_printf(MSG_ERROR,
881 "P2P: Failed to allocate a new interface for the group");
882 return P2PS_SETUP_NONE;
883 }
884 wpa_s->global->pending_group_iface_for_p2ps = 1;
885 p2p_set_intended_addr(wpa_s->global->p2p,
886 wpa_s->pending_interface_addr);
887 }
888 }
889
890 return conncap;
891}
892
893
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700894static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
895 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896{
897 struct wpa_ssid *ssid;
898 char *gtype;
899 const char *reason;
900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 ssid = wpa_s->current_ssid;
902 if (ssid == NULL) {
903 /*
904 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700905 * pending P2P group interface waiting for provisioning or a
906 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 */
908 ssid = wpa_s->conf->ssid;
909 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700910 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 break;
912 ssid = ssid->next;
913 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300914 if (ssid == NULL &&
915 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
916 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700917 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
918 "not found");
919 return -1;
920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 }
922 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
923 gtype = "GO";
924 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
925 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
926 wpa_s->reassociate = 0;
927 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 gtype = "client";
929 } else
930 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700931
932 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
933 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
934
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800935 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700936 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800937 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
938 wpa_s, NULL)) {
939 wpa_printf(MSG_DEBUG,
940 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
941 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
942 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
943 wpa_s, NULL);
944 }
945 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (wpa_s->cross_connect_in_use) {
948 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800949 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700950 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
951 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700953 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 case P2P_GROUP_REMOVAL_REQUESTED:
955 reason = " reason=REQUESTED";
956 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700957 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
958 reason = " reason=FORMATION_FAILED";
959 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
961 reason = " reason=IDLE";
962 break;
963 case P2P_GROUP_REMOVAL_UNAVAILABLE:
964 reason = " reason=UNAVAILABLE";
965 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700966 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
967 reason = " reason=GO_ENDING_SESSION";
968 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700969 case P2P_GROUP_REMOVAL_PSK_FAILURE:
970 reason = " reason=PSK_FAILURE";
971 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800972 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
973 reason = " reason=FREQ_CONFLICT";
974 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 default:
976 reason = "";
977 break;
978 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700979 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800980 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700981 P2P_EVENT_GROUP_REMOVED "%s %s%s",
982 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800985 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
986 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700987 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
988 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800989 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800990 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800991 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
992 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700993 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800994 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700995 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700996
Dmitry Shmidt15907092014-03-25 10:42:57 -0700997 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800998 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
999 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001000
Dmitry Shmidt96571392013-10-14 12:54:46 -07001001 /*
1002 * Make sure wait for the first client does not remain active after the
1003 * group has been removed.
1004 */
1005 wpa_s->global->p2p_go_wait_client.sec = 0;
1006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1008 struct wpa_global *global;
1009 char *ifname;
1010 enum wpa_driver_if_type type;
1011 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1012 wpa_s->ifname);
1013 global = wpa_s->global;
1014 ifname = os_strdup(wpa_s->ifname);
1015 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001016 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001017 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 wpa_s = global->ifaces;
1019 if (wpa_s && ifname)
1020 wpa_drv_if_remove(wpa_s, type, ifname);
1021 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001022 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 }
1024
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001025 /*
1026 * The primary interface was used for P2P group operations, so
1027 * need to reset its p2pdev.
1028 */
1029 wpa_s->p2pdev = wpa_s->parent;
1030
Dmitry Shmidt96571392013-10-14 12:54:46 -07001031 if (!wpa_s->p2p_go_group_formation_completed) {
1032 wpa_s->global->p2p_group_formation = NULL;
1033 wpa_s->p2p_in_provisioning = 0;
1034 }
1035
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001036 wpa_s->show_group_started = 0;
1037 os_free(wpa_s->go_params);
1038 wpa_s->go_params = NULL;
1039
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040 os_free(wpa_s->p2p_group_common_freqs);
1041 wpa_s->p2p_group_common_freqs = NULL;
1042 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001043 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001044 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001045
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001046 wpa_s->waiting_presence_resp = 0;
1047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1049 if (ssid && (ssid->p2p_group ||
1050 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1051 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1052 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001053 if (ssid == wpa_s->current_ssid) {
1054 wpa_sm_set_config(wpa_s->wpa, NULL);
1055 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001057 }
1058 /*
1059 * Networks objects created during any P2P activities are not
1060 * exposed out as they might/will confuse certain non-P2P aware
1061 * applications since these network objects won't behave like
1062 * regular ones.
1063 *
1064 * Likewise, we don't send out network removed signals for such
1065 * network objects.
1066 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067 wpa_config_remove_network(wpa_s->conf, id);
1068 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001069 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 } else {
1071 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1072 "found");
1073 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001074 if (wpa_s->ap_iface)
1075 wpa_supplicant_ap_deinit(wpa_s);
1076 else
1077 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001078
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001079 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1080
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001081 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082}
1083
1084
1085static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1086 u8 *go_dev_addr,
1087 const u8 *ssid, size_t ssid_len)
1088{
1089 struct wpa_bss *bss;
1090 const u8 *bssid;
1091 struct wpabuf *p2p;
1092 u8 group_capab;
1093 const u8 *addr;
1094
1095 if (wpa_s->go_params)
1096 bssid = wpa_s->go_params->peer_interface_addr;
1097 else
1098 bssid = wpa_s->bssid;
1099
1100 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001101 if (bss == NULL && wpa_s->go_params &&
1102 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1103 bss = wpa_bss_get_p2p_dev_addr(
1104 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 if (bss == NULL) {
1106 u8 iface_addr[ETH_ALEN];
1107 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1108 iface_addr) == 0)
1109 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1110 }
1111 if (bss == NULL) {
1112 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1113 "group is persistent - BSS " MACSTR " not found",
1114 MAC2STR(bssid));
1115 return 0;
1116 }
1117
1118 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001119 if (p2p == NULL)
1120 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1121 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 if (p2p == NULL) {
1123 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1124 "group is persistent - BSS " MACSTR
1125 " did not include P2P IE", MAC2STR(bssid));
1126 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001127 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 wpa_hexdump(MSG_DEBUG, "P2P: Beacon 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 bss->beacon_ie_len);
1131 return 0;
1132 }
1133
1134 group_capab = p2p_get_group_capab(p2p);
1135 addr = p2p_get_go_dev_addr(p2p);
1136 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1137 "group_capab=0x%x", group_capab);
1138 if (addr) {
1139 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1140 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1141 MAC2STR(addr));
1142 } else
1143 os_memset(go_dev_addr, 0, ETH_ALEN);
1144 wpabuf_free(p2p);
1145
1146 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1147 "go_dev_addr=" MACSTR,
1148 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1149
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001150 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151}
1152
1153
Jouni Malinen75ecf522011-06-27 15:19:46 -07001154static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1155 struct wpa_ssid *ssid,
1156 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157{
1158 struct wpa_ssid *s;
1159 int changed = 0;
1160
1161 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1162 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1163 for (s = wpa_s->conf->ssid; s; s = s->next) {
1164 if (s->disabled == 2 &&
1165 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1166 s->ssid_len == ssid->ssid_len &&
1167 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1168 break;
1169 }
1170
1171 if (s) {
1172 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1173 "entry");
1174 if (ssid->passphrase && !s->passphrase)
1175 changed = 1;
1176 else if (ssid->passphrase && s->passphrase &&
1177 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1178 changed = 1;
1179 } else {
1180 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1181 "entry");
1182 changed = 1;
1183 s = wpa_config_add_network(wpa_s->conf);
1184 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001185 return -1;
1186
1187 /*
1188 * Instead of network_added we emit persistent_group_added
1189 * notification. Also to keep the defense checks in
1190 * persistent_group obj registration method, we set the
1191 * relevant flags in s to designate it as a persistent group.
1192 */
1193 s->p2p_group = 1;
1194 s->p2p_persistent_group = 1;
1195 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 wpa_config_set_network_defaults(s);
1197 }
1198
1199 s->p2p_group = 1;
1200 s->p2p_persistent_group = 1;
1201 s->disabled = 2;
1202 s->bssid_set = 1;
1203 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1204 s->mode = ssid->mode;
1205 s->auth_alg = WPA_AUTH_ALG_OPEN;
1206 s->key_mgmt = WPA_KEY_MGMT_PSK;
1207 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001208 s->pbss = ssid->pbss;
1209 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 s->export_keys = 1;
1211 if (ssid->passphrase) {
1212 os_free(s->passphrase);
1213 s->passphrase = os_strdup(ssid->passphrase);
1214 }
1215 if (ssid->psk_set) {
1216 s->psk_set = 1;
1217 os_memcpy(s->psk, ssid->psk, 32);
1218 }
1219 if (s->passphrase && !s->psk_set)
1220 wpa_config_update_psk(s);
1221 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1222 os_free(s->ssid);
1223 s->ssid = os_malloc(ssid->ssid_len);
1224 }
1225 if (s->ssid) {
1226 s->ssid_len = ssid->ssid_len;
1227 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1228 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001229 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1230 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1231 wpa_s->global->add_psk = NULL;
1232 changed = 1;
1233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 if (changed && wpa_s->conf->update_config &&
1236 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1237 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1238 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001239
1240 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241}
1242
1243
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001244static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1245 const u8 *addr)
1246{
1247 struct wpa_ssid *ssid, *s;
1248 u8 *n;
1249 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001250 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001251 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252
1253 ssid = wpa_s->current_ssid;
1254 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1255 !ssid->p2p_persistent_group)
1256 return;
1257
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001258 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1260 continue;
1261
1262 if (s->ssid_len == ssid->ssid_len &&
1263 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1264 break;
1265 }
1266
1267 if (s == NULL)
1268 return;
1269
1270 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001271 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001272 ETH_ALEN) != 0)
1273 continue;
1274
1275 if (i == s->num_p2p_clients - 1)
1276 return; /* already the most recent entry */
1277
1278 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001279 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1280 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1281 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001282 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001283 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1284 ETH_ALEN);
1285 os_memset(s->p2p_client_list +
1286 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1287 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001288 found = 1;
1289 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001290 }
1291
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001292 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1293 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001294 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001295 if (n == NULL)
1296 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001297 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1298 ETH_ALEN);
1299 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1300 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001301 s->p2p_client_list = n;
1302 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001303 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001304 /* Not enough room for an additional entry - drop the oldest
1305 * entry */
1306 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001307 s->p2p_client_list + 2 * ETH_ALEN,
1308 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001309 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001310 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001311 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001312 os_memset(s->p2p_client_list +
1313 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1314 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001315 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001316
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001317 if (p2p_wpa_s->conf->update_config &&
1318 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001319 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001320}
1321
1322
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001323static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1324 int go, struct wpa_ssid *ssid, int freq,
1325 const u8 *psk, const char *passphrase,
1326 const u8 *go_dev_addr, int persistent,
1327 const char *extra)
1328{
1329 const char *ssid_txt;
1330 char psk_txt[65];
1331
1332 if (psk)
1333 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1334 else
1335 psk_txt[0] = '\0';
1336
1337 if (ssid)
1338 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1339 else
1340 ssid_txt = "";
1341
1342 if (passphrase && passphrase[0] == '\0')
1343 passphrase = NULL;
1344
1345 /*
1346 * Include PSK/passphrase only in the control interface message and
1347 * leave it out from the debug log entry.
1348 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001349 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001350 P2P_EVENT_GROUP_STARTED
1351 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1352 MACSTR "%s%s",
1353 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1354 psk ? " psk=" : "", psk_txt,
1355 passphrase ? " passphrase=\"" : "",
1356 passphrase ? passphrase : "",
1357 passphrase ? "\"" : "",
1358 MAC2STR(go_dev_addr),
1359 persistent ? " [PERSISTENT]" : "", extra);
1360 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1361 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1362 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1363 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1364 extra);
1365}
1366
1367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001369 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370{
1371 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372 int client;
1373 int persistent;
1374 u8 go_dev_addr[ETH_ALEN];
1375
1376 /*
1377 * This callback is likely called for the main interface. Update wpa_s
1378 * to use the group interface if a new interface was created for the
1379 * group.
1380 */
1381 if (wpa_s->global->p2p_group_formation)
1382 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001383 if (wpa_s->p2p_go_group_formation_completed) {
1384 wpa_s->global->p2p_group_formation = NULL;
1385 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001386 } else if (wpa_s->p2p_in_provisioning && !success) {
1387 wpa_msg(wpa_s, MSG_DEBUG,
1388 "P2P: Stop provisioning state due to failure");
1389 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001390 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001391 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001392 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393
1394 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001395 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001396 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001397 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001398 if (already_deleted)
1399 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001400 wpas_p2p_group_delete(wpa_s,
1401 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 return;
1403 }
1404
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001405 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001406 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407
1408 ssid = wpa_s->current_ssid;
1409 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1410 ssid->mode = WPAS_MODE_P2P_GO;
1411 p2p_group_notif_formation_done(wpa_s->p2p_group);
1412 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1413 }
1414
1415 persistent = 0;
1416 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 client = ssid->mode == WPAS_MODE_INFRA;
1418 if (ssid->mode == WPAS_MODE_P2P_GO) {
1419 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001420 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1421 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001422 } else {
1423 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 persistent = wpas_p2p_persistent_group(wpa_s,
1425 go_dev_addr,
1426 ssid->ssid,
1427 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001428 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 client = wpa_s->p2p_group_interface ==
1431 P2P_GROUP_INTERFACE_CLIENT;
1432 os_memset(go_dev_addr, 0, ETH_ALEN);
1433 }
1434
1435 wpa_s->show_group_started = 0;
1436 if (client) {
1437 /*
1438 * Indicate event only after successfully completed 4-way
1439 * handshake, i.e., when the interface is ready for data
1440 * packets.
1441 */
1442 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001444 wpas_p2p_group_started(wpa_s, 1, ssid,
1445 ssid ? ssid->frequency : 0,
1446 ssid && ssid->passphrase == NULL &&
1447 ssid->psk_set ? ssid->psk : NULL,
1448 ssid ? ssid->passphrase : NULL,
1449 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 wpas_p2p_cross_connect_setup(wpa_s);
1451 wpas_p2p_set_group_idle_timeout(wpa_s);
1452 }
1453
1454 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001455 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1456 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001457 else {
1458 os_free(wpa_s->global->add_psk);
1459 wpa_s->global->add_psk = NULL;
1460 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001461
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001462 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001463 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001464 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466}
1467
1468
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001469struct send_action_work {
1470 unsigned int freq;
1471 u8 dst[ETH_ALEN];
1472 u8 src[ETH_ALEN];
1473 u8 bssid[ETH_ALEN];
1474 size_t len;
1475 unsigned int wait_time;
1476 u8 buf[0];
1477};
1478
1479
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001480static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1481{
1482 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1483
1484 wpa_printf(MSG_DEBUG,
1485 "P2P: Free Action frame radio work @%p (freq=%u dst="
1486 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1487 wpa_s->p2p_send_action_work, awork->freq,
1488 MAC2STR(awork->dst), MAC2STR(awork->src),
1489 MAC2STR(awork->bssid), awork->wait_time);
1490 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1491 awork->buf, awork->len);
1492 os_free(awork);
1493 wpa_s->p2p_send_action_work->ctx = NULL;
1494 radio_work_done(wpa_s->p2p_send_action_work);
1495 wpa_s->p2p_send_action_work = NULL;
1496}
1497
1498
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001499static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1500 void *timeout_ctx)
1501{
1502 struct wpa_supplicant *wpa_s = eloop_ctx;
1503
1504 if (!wpa_s->p2p_send_action_work)
1505 return;
1506
1507 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001508 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001509}
1510
1511
Dmitry Shmidt03658832014-08-13 11:03:49 -07001512static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001514 if (wpa_s->p2p_send_action_work) {
1515 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001516
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001517 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001518 wpa_printf(MSG_DEBUG,
1519 "P2P: Clear Action TX work @%p (wait_time=%u)",
1520 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001521 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001522 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001523 } else {
1524 /*
1525 * In theory, this should not be needed, but number of
1526 * places in the P2P code is still using non-zero wait
1527 * time for the last Action frame in the sequence and
1528 * some of these do not call send_action_done().
1529 */
1530 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1531 wpa_s, NULL);
1532 eloop_register_timeout(
1533 0, awork->wait_time * 1000,
1534 wpas_p2p_send_action_work_timeout,
1535 wpa_s, NULL);
1536 }
1537 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001538}
1539
1540
1541static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1542 unsigned int freq,
1543 const u8 *dst, const u8 *src,
1544 const u8 *bssid,
1545 const u8 *data, size_t data_len,
1546 enum offchannel_send_action_result
1547 result)
1548{
1549 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1550
1551 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1554 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001556 switch (result) {
1557 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1558 res = P2P_SEND_ACTION_SUCCESS;
1559 break;
1560 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1561 res = P2P_SEND_ACTION_NO_ACK;
1562 break;
1563 case OFFCHANNEL_SEND_ACTION_FAILED:
1564 res = P2P_SEND_ACTION_FAILED;
1565 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 }
1567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001568 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001570 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1571 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001573 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1574 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001576 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1577 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001578 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001579 P2P_EVENT_FALLBACK_TO_GO_NEG
1580 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001581 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1582 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 }
1584}
1585
1586
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001587static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1588{
1589 struct wpa_supplicant *wpa_s = work->wpa_s;
1590 struct send_action_work *awork = work->ctx;
1591
1592 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001593 if (work->started) {
1594 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1595 wpa_s, NULL);
1596 wpa_s->p2p_send_action_work = NULL;
1597 offchannel_send_action_done(wpa_s);
1598 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001599 os_free(awork);
1600 return;
1601 }
1602
1603 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1604 awork->bssid, awork->buf, awork->len,
1605 awork->wait_time,
1606 wpas_p2p_send_action_tx_status, 1) < 0) {
1607 os_free(awork);
1608 radio_work_done(work);
1609 return;
1610 }
1611 wpa_s->p2p_send_action_work = work;
1612}
1613
1614
1615static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1616 unsigned int freq, const u8 *dst,
1617 const u8 *src, const u8 *bssid, const u8 *buf,
1618 size_t len, unsigned int wait_time)
1619{
1620 struct send_action_work *awork;
1621
Hai Shalom81f62d82019-07-22 12:10:00 -07001622 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001623 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1624 return -1;
1625 }
1626
1627 awork = os_zalloc(sizeof(*awork) + len);
1628 if (awork == NULL)
1629 return -1;
1630
1631 awork->freq = freq;
1632 os_memcpy(awork->dst, dst, ETH_ALEN);
1633 os_memcpy(awork->src, src, ETH_ALEN);
1634 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1635 awork->len = len;
1636 awork->wait_time = wait_time;
1637 os_memcpy(awork->buf, buf, len);
1638
Hai Shalom81f62d82019-07-22 12:10:00 -07001639 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001640 wpas_send_action_cb, awork) < 0) {
1641 os_free(awork);
1642 return -1;
1643 }
1644
1645 return 0;
1646}
1647
1648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1650 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001651 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652{
1653 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001654 int listen_freq = -1, send_freq = -1;
1655
Hai Shalom021b0b52019-04-10 11:17:58 -07001656 if (scheduled)
1657 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001658 if (wpa_s->p2p_listen_work)
1659 listen_freq = wpa_s->p2p_listen_work->freq;
1660 if (wpa_s->p2p_send_action_work)
1661 send_freq = wpa_s->p2p_send_action_work->freq;
1662 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001663 int res;
1664
1665 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1666 listen_freq, send_freq, freq);
1667 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1668 len, wait_time);
1669 if (res == 0 && scheduled)
1670 *scheduled = 1;
1671 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001672 }
1673
1674 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001675 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1676 wait_time,
1677 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678}
1679
1680
1681static void wpas_send_action_done(void *ctx)
1682{
1683 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001684
1685 if (wpa_s->p2p_send_action_work) {
1686 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1687 wpa_s, NULL);
1688 os_free(wpa_s->p2p_send_action_work->ctx);
1689 radio_work_done(wpa_s->p2p_send_action_work);
1690 wpa_s->p2p_send_action_work = NULL;
1691 }
1692
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001693 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694}
1695
1696
1697static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1698 struct p2p_go_neg_results *params)
1699{
1700 if (wpa_s->go_params == NULL) {
1701 wpa_s->go_params = os_malloc(sizeof(*params));
1702 if (wpa_s->go_params == NULL)
1703 return -1;
1704 }
1705 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1706 return 0;
1707}
1708
1709
1710static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1711 struct p2p_go_neg_results *res)
1712{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001713 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001714 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1715 " dev_addr " MACSTR " wps_method %d",
1716 MAC2STR(res->peer_interface_addr),
1717 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1719 res->ssid, res->ssid_len);
1720 wpa_supplicant_ap_deinit(wpa_s);
1721 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001722 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001723 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001724#ifdef CONFIG_WPS_NFC
1725 } else if (res->wps_method == WPS_NFC) {
1726 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1727 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001728 wpa_s->p2pdev->p2p_oob_dev_pw,
1729 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1730 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001731 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001732 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001733 NULL,
1734 NULL, 0, 0);
1735#endif /* CONFIG_WPS_NFC */
1736 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001738 if (wpa_s->p2p_wps_method == WPS_P2PS)
1739 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1741 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1742 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1743 wpa_s->p2p_pin, 1, dev_pw_id);
1744 }
1745}
1746
1747
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001748static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1749 struct wpa_ssid *ssid)
1750{
1751 struct wpa_ssid *persistent;
1752 struct psk_list_entry *psk;
1753 struct hostapd_data *hapd;
1754
1755 if (!wpa_s->ap_iface)
1756 return;
1757
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001758 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001759 ssid->ssid_len);
1760 if (persistent == NULL)
1761 return;
1762
1763 hapd = wpa_s->ap_iface->bss[0];
1764
1765 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1766 list) {
1767 struct hostapd_wpa_psk *hpsk;
1768
1769 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1770 MACSTR " psk=%d",
1771 MAC2STR(psk->addr), psk->p2p);
1772 hpsk = os_zalloc(sizeof(*hpsk));
1773 if (hpsk == NULL)
1774 break;
1775 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1776 if (psk->p2p)
1777 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1778 else
1779 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1780 hpsk->next = hapd->conf->ssid.wpa_psk;
1781 hapd->conf->ssid.wpa_psk = hpsk;
1782 }
1783}
1784
1785
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001786static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1787{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001788 char buf[20 + P2P_MAX_CHANNELS * 6];
1789 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001790 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001791 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001792
Roshan Pius3a1667e2018-07-03 15:17:14 -07001793 pos = buf;
1794 end = pos + sizeof(buf);
1795 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1796 res = os_snprintf(pos, end - pos, " %d",
1797 wpa_s->p2p_group_common_freqs[i]);
1798 if (os_snprintf_error(end - pos, res))
1799 break;
1800 pos += res;
1801 }
1802 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001803
Roshan Pius3a1667e2018-07-03 15:17:14 -07001804 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805}
1806
1807
1808static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1809 struct p2p_go_neg_results *params)
1810{
1811 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1812
1813 wpa_s->p2p_group_common_freqs_num = 0;
1814 os_free(wpa_s->p2p_group_common_freqs);
1815 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1816 if (!wpa_s->p2p_group_common_freqs)
1817 return;
1818
1819 for (i = 0; i < len; i++) {
1820 if (!wpa_s->go_params->freq_list[i])
1821 break;
1822 wpa_s->p2p_group_common_freqs[i] =
1823 wpa_s->go_params->freq_list[i];
1824 }
1825 wpa_s->p2p_group_common_freqs_num = i;
1826}
1827
1828
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001829static void p2p_config_write(struct wpa_supplicant *wpa_s)
1830{
1831#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001832 if (wpa_s->p2pdev->conf->update_config &&
1833 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001834 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1835#endif /* CONFIG_NO_CONFIG_WRITE */
1836}
1837
1838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839static void p2p_go_configured(void *ctx, void *data)
1840{
1841 struct wpa_supplicant *wpa_s = ctx;
1842 struct p2p_go_neg_results *params = data;
1843 struct wpa_ssid *ssid;
1844
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001845 wpa_s->ap_configured_cb = NULL;
1846 wpa_s->ap_configured_cb_ctx = NULL;
1847 wpa_s->ap_configured_cb_data = NULL;
1848 if (!wpa_s->go_params) {
1849 wpa_printf(MSG_ERROR,
1850 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1851 return;
1852 }
1853
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001854 p2p_go_save_group_common_freqs(wpa_s, params);
1855 p2p_go_dump_common_freqs(wpa_s);
1856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 ssid = wpa_s->current_ssid;
1858 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1859 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1860 if (wpa_s->global->p2p_group_formation == wpa_s)
1861 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001862 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1863 params->passphrase[0] == '\0' ?
1864 params->psk : NULL,
1865 params->passphrase,
1866 wpa_s->global->p2p_dev_addr,
1867 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001868 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001869
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001870 if (wpa_s->p2pdev->p2ps_method_config_any) {
1871 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001872 wpa_dbg(wpa_s, MSG_DEBUG,
1873 "P2PS: Setting default PIN for ANY");
1874 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1875 "12345670", NULL, 0,
1876 0);
1877 } else {
1878 wpa_dbg(wpa_s, MSG_DEBUG,
1879 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001880 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001881 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001882 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001883 "12345670", NULL, 0, 0);
1884 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001885 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001886 }
1887
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001888 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001889 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001890 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001891 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001892 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001893 wpas_p2p_add_psk_list(wpa_s, ssid);
1894 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001895
1896 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001897 params->persistent_group, 0,
1898 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 wpas_p2p_cross_connect_setup(wpa_s);
1900 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001901
1902 if (wpa_s->p2p_first_connection_timeout) {
1903 wpa_dbg(wpa_s, MSG_DEBUG,
1904 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1905 wpa_s->p2p_first_connection_timeout);
1906 wpa_s->p2p_go_group_formation_completed = 0;
1907 wpa_s->global->p2p_group_formation = wpa_s;
1908 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001909 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001910 eloop_register_timeout(
1911 wpa_s->p2p_first_connection_timeout, 0,
1912 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001913 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001914 }
1915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 return;
1917 }
1918
1919 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1920 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1921 params->peer_interface_addr)) {
1922 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1923 "filtering");
1924 return;
1925 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001926 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001928 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001929#ifdef CONFIG_WPS_NFC
1930 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001931 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001932 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001933 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001934 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1935 return;
1936 }
1937 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001938 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1939 wpa_s->p2pdev->p2p_oob_dev_pw,
1940 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1941 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001942#endif /* CONFIG_WPS_NFC */
1943 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001945 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 os_free(wpa_s->go_params);
1947 wpa_s->go_params = NULL;
1948}
1949
1950
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001951/**
1952 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1953 * @freq: Frequency (MHz) to convert
1954 * @op_class: Buffer for returning operating class
1955 * @op_edmg_channel: Buffer for returning channel number
1956 * Returns: 0 on success, -1 on failure
1957 *
1958 * This can be used to find the highest channel bonding which includes the
1959 * specified frequency.
1960 */
1961static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1962 unsigned int freq,
1963 u8 *op_class, u8 *op_edmg_channel)
1964{
1965 struct hostapd_hw_modes *hwmode;
1966 struct ieee80211_edmg_config edmg;
1967 unsigned int i;
1968 enum chan_width chanwidth[] = {
1969 CHAN_WIDTH_8640,
1970 CHAN_WIDTH_6480,
1971 CHAN_WIDTH_4320,
1972 };
1973
1974 if (!wpa_s->hw.modes)
1975 return -1;
1976
1977 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08001978 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001979 if (!hwmode) {
1980 wpa_printf(MSG_ERROR,
1981 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1982 return -1;
1983 }
1984
1985 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1986 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1987 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1988 op_class,
1989 op_edmg_channel) < 0)
1990 continue;
1991
1992 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1993 if (edmg.channels &&
1994 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1995 wpa_printf(MSG_DEBUG,
1996 "Freq %u to EDMG channel %u at opclass %u",
1997 freq, *op_edmg_channel, *op_class);
1998 return 0;
1999 }
2000 }
2001
2002 return -1;
2003}
2004
2005
2006int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2007 struct p2p_go_neg_results *params)
2008{
2009 u8 op_channel, op_class;
2010 int freq;
2011
2012 /* Try social channel as primary channel frequency */
2013 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2014
2015 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2016 &op_channel) == 0) {
2017 wpa_printf(MSG_DEBUG,
2018 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2019 freq, op_channel, op_class);
2020 params->freq = freq;
2021 return 0;
2022 }
2023
2024 return -1;
2025}
2026
2027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2029 struct p2p_go_neg_results *params,
2030 int group_formation)
2031{
2032 struct wpa_ssid *ssid;
2033
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002034 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2035 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2036 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2037 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040
2041 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002042 if (ssid == NULL) {
2043 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002047 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002048 wpa_s->p2p_go_group_formation_completed = 0;
2049 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002050 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052 wpa_config_set_network_defaults(ssid);
2053 ssid->temporary = 1;
2054 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002055 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2057 WPAS_MODE_P2P_GO;
2058 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002059 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002060 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002061 ssid->max_oper_chwidth = params->max_oper_chwidth;
2062 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002063 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002064 if (params->edmg) {
2065 u8 op_channel, op_class;
2066
2067 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2068 &op_class, &op_channel)) {
2069 ssid->edmg_channel = op_channel;
2070 ssid->enable_edmg = params->edmg;
2071 } else {
2072 wpa_dbg(wpa_s, MSG_DEBUG,
2073 "P2P: Could not match EDMG channel, freq %d, for GO",
2074 params->freq);
2075 }
2076 }
2077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 ssid->ssid = os_zalloc(params->ssid_len + 1);
2079 if (ssid->ssid) {
2080 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2081 ssid->ssid_len = params->ssid_len;
2082 }
2083 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2084 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002085 if (is_6ghz_freq(ssid->frequency) &&
2086 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2087 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2088 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2089 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2090 ssid->sae_pwe = 1;
2091 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2092 } else {
2093 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2094 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 ssid->proto = WPA_PROTO_RSN;
2096 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002097 ssid->group_cipher = WPA_CIPHER_CCMP;
2098 if (params->freq > 56160) {
2099 /*
2100 * Enable GCMP instead of CCMP as pairwise_cipher and
2101 * group_cipher in 60 GHz.
2102 */
2103 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2104 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002105 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2106 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002107 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002108 if (os_strlen(params->passphrase) > 0) {
2109 ssid->passphrase = os_strdup(params->passphrase);
2110 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002111 wpa_msg_global(wpa_s, MSG_ERROR,
2112 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002113 wpa_config_remove_network(wpa_s->conf, ssid->id);
2114 return;
2115 }
2116 } else
2117 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002118 ssid->psk_set = params->psk_set;
2119 if (ssid->psk_set)
2120 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002121 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002122 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002123 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124
2125 wpa_s->ap_configured_cb = p2p_go_configured;
2126 wpa_s->ap_configured_cb_ctx = wpa_s;
2127 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002128 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002129 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 wpa_s->reassociate = 1;
2131 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002132 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2133 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 wpa_supplicant_req_scan(wpa_s, 0, 0);
2135}
2136
2137
2138static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2139 const struct wpa_supplicant *src)
2140{
2141 struct wpa_config *d;
2142 const struct wpa_config *s;
2143
2144 d = dst->conf;
2145 s = src->conf;
2146
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002147#define C(n) \
2148do { \
2149 if (s->n && !d->n) \
2150 d->n = os_strdup(s->n); \
2151} while (0)
2152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 C(device_name);
2154 C(manufacturer);
2155 C(model_name);
2156 C(model_number);
2157 C(serial_number);
2158 C(config_methods);
2159#undef C
2160
2161 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2162 os_memcpy(d->sec_device_type, s->sec_device_type,
2163 sizeof(d->sec_device_type));
2164 d->num_sec_device_types = s->num_sec_device_types;
2165
2166 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002167 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002169 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002170 d->max_num_sta = s->max_num_sta;
2171 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002172 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002173 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002174 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002175 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002176 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002177 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002178 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002179 d->pmf = s->pmf;
2180 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002181
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002182 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2183 !d->wps_nfc_pw_from_config) {
2184 wpabuf_free(d->wps_nfc_dh_privkey);
2185 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002186 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2187 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2188 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002189 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002190 d->go_interworking = s->go_interworking;
2191 d->go_access_network_type = s->go_access_network_type;
2192 d->go_internet = s->go_internet;
2193 d->go_venue_group = s->go_venue_group;
2194 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002195 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196}
2197
2198
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002199static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2200 char *ifname, size_t len)
2201{
2202 char *ifname_ptr = wpa_s->ifname;
2203
2204 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2205 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2206 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2207 }
2208
2209 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2210 if (os_strlen(ifname) >= IFNAMSIZ &&
2211 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002212 int res;
2213
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002214 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002215 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2216 if (os_snprintf_error(len, res) && len)
2217 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002218 }
2219}
2220
2221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2223 enum wpa_driver_if_type type)
2224{
2225 char ifname[120], force_ifname[120];
2226
2227 if (wpa_s->pending_interface_name[0]) {
2228 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2229 "- skip creation of a new one");
2230 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2231 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2232 "unknown?! ifname='%s'",
2233 wpa_s->pending_interface_name);
2234 return -1;
2235 }
2236 return 0;
2237 }
2238
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002239 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240 force_ifname[0] = '\0';
2241
2242 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2243 ifname);
2244 wpa_s->p2p_group_idx++;
2245
2246 wpa_s->pending_interface_type = type;
2247 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2248 wpa_s->pending_interface_addr, NULL) < 0) {
2249 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2250 "interface");
2251 return -1;
2252 }
2253
Jimmy Chen36c21992018-11-29 16:46:43 +08002254 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002255 random_mac_addr(wpa_s->pending_interface_addr);
2256 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2257 " for the group",
2258 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002259 }
2260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 if (force_ifname[0]) {
2262 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2263 force_ifname);
2264 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2265 sizeof(wpa_s->pending_interface_name));
2266 } else
2267 os_strlcpy(wpa_s->pending_interface_name, ifname,
2268 sizeof(wpa_s->pending_interface_name));
2269 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2270 MACSTR, wpa_s->pending_interface_name,
2271 MAC2STR(wpa_s->pending_interface_addr));
2272
2273 return 0;
2274}
2275
2276
2277static void wpas_p2p_remove_pending_group_interface(
2278 struct wpa_supplicant *wpa_s)
2279{
2280 if (!wpa_s->pending_interface_name[0] ||
2281 is_zero_ether_addr(wpa_s->pending_interface_addr))
2282 return; /* No pending virtual interface */
2283
2284 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2285 wpa_s->pending_interface_name);
2286 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2287 wpa_s->pending_interface_name);
2288 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2289 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002290 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291}
2292
2293
2294static struct wpa_supplicant *
2295wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2296{
2297 struct wpa_interface iface;
2298 struct wpa_supplicant *group_wpa_s;
2299
2300 if (!wpa_s->pending_interface_name[0]) {
2301 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2302 if (!wpas_p2p_create_iface(wpa_s))
2303 return NULL;
2304 /*
2305 * Something has forced us to remove the pending interface; try
2306 * to create a new one and hope for the best that we will get
2307 * the same local address.
2308 */
2309 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2310 WPA_IF_P2P_CLIENT) < 0)
2311 return NULL;
2312 }
2313
2314 os_memset(&iface, 0, sizeof(iface));
2315 iface.ifname = wpa_s->pending_interface_name;
2316 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002317 if (wpa_s->conf->ctrl_interface == NULL &&
2318 wpa_s->parent != wpa_s &&
2319 wpa_s->p2p_mgmt &&
2320 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2321 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2322 else
2323 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002325 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326 if (group_wpa_s == NULL) {
2327 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2328 "wpa_supplicant interface");
2329 return NULL;
2330 }
2331 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002332 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2333 P2P_GROUP_INTERFACE_CLIENT;
2334 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002335 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336
2337 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2338
Jimmy Chen36c21992018-11-29 16:46:43 +08002339 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002340 if (wpa_drv_set_mac_addr(group_wpa_s,
2341 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002342 wpa_msg(group_wpa_s, MSG_INFO,
2343 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002344 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2345 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002346 return NULL;
2347 }
2348
2349 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2350 wpa_msg(group_wpa_s, MSG_INFO,
2351 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002352 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2353 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002354 return NULL;
2355 }
2356
Hai Shalom74f70d42019-02-11 14:42:39 -08002357 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2358 " for the group",
2359 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002360 }
2361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 return group_wpa_s;
2363}
2364
2365
2366static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2367 void *timeout_ctx)
2368{
2369 struct wpa_supplicant *wpa_s = eloop_ctx;
2370 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002371 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002372}
2373
2374
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002375static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2376 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002377{
2378 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002379 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 if (wpa_s->global->p2p)
2381 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002382 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383}
2384
2385
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002386static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2387{
2388 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2389 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002390 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002391 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002392 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002393 wpa_s->global->p2p_fail_on_wps_complete = 0;
2394}
2395
2396
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002397void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2398{
2399 if (wpa_s->global->p2p_group_formation != wpa_s)
2400 return;
2401 /* Speed up group formation timeout since this cannot succeed */
2402 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002403 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002404 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002405 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002406}
2407
2408
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002409static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410{
2411 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002412 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413
2414 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2415 wpa_drv_cancel_remain_on_channel(wpa_s);
2416 wpa_s->off_channel_freq = 0;
2417 wpa_s->roc_waiting_drv_freq = 0;
2418 }
2419
2420 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002421 wpa_msg_global(wpa_s, MSG_INFO,
2422 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2423 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002424 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 wpas_p2p_remove_pending_group_interface(wpa_s);
2426 return;
2427 }
2428
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002429 if (!res->role_go) {
2430 /* Inform driver of the operating channel of GO. */
2431 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2432 }
2433
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002434 if (wpa_s->p2p_go_ht40)
2435 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002436 if (wpa_s->p2p_go_vht)
2437 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002438 if (wpa_s->p2p_go_he)
2439 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002440 if (wpa_s->p2p_go_edmg)
2441 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002442 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2443 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002444
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002445 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2446 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2447 " wps_method=%s",
2448 res->role_go ? "GO" : "client", res->freq, res->ht40,
2449 MAC2STR(res->peer_device_addr),
2450 MAC2STR(res->peer_interface_addr),
2451 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002452 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453
Dmitry Shmidt04949592012-07-19 12:16:46 -07002454 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2455 struct wpa_ssid *ssid;
2456 ssid = wpa_config_get_network(wpa_s->conf,
2457 wpa_s->p2p_persistent_id);
2458 if (ssid && ssid->disabled == 2 &&
2459 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2460 size_t len = os_strlen(ssid->passphrase);
2461 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2462 "on requested persistent group");
2463 os_memcpy(res->passphrase, ssid->passphrase, len);
2464 res->passphrase[len] = '\0';
2465 }
2466 }
2467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002469 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2471 if (group_wpa_s == NULL) {
2472 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002473 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2474 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002475 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476 return;
2477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2479 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002481 group_wpa_s = wpa_s->parent;
2482 wpa_s->global->p2p_group_formation = group_wpa_s;
2483 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002484 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002485 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002487 group_wpa_s->p2p_in_provisioning = 1;
2488 group_wpa_s->p2pdev = wpa_s;
2489 if (group_wpa_s != wpa_s) {
2490 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2491 sizeof(group_wpa_s->p2p_pin));
2492 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2493 }
2494 if (res->role_go) {
2495 wpas_start_wps_go(group_wpa_s, res, 1);
2496 } else {
2497 os_get_reltime(&group_wpa_s->scan_min_time);
2498 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 }
2500
Hai Shalomfdcde762020-04-02 11:19:20 -07002501 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2503
2504 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2505 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2506 (res->peer_config_timeout % 100) * 10000,
2507 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2508}
2509
2510
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002511static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2512 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513{
2514 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002515 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002516 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2517 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002519 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520}
2521
2522
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002523static void wpas_dev_found(void *ctx, const u8 *addr,
2524 const struct p2p_peer_info *info,
2525 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002527 u8 *wfd_dev_info = NULL;
2528 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002529 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002530 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002531#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 struct wpa_supplicant *wpa_s = ctx;
2533 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002534 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002535 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002536
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002537#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002538 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2539 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002540 if (wfd_dev_info_hex) {
2541 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2542 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2543 // Only used for notification, so not handling error.
2544 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2545 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002546
2547 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2548 WFD_SUBELEM_R2_DEVICE_INFO);
2549 if (wfd_r2_dev_info_hex) {
2550 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2551 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2552 // Only used for notification, so not handling error.
2553 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2554 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002555#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002556
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002557 if (info->p2ps_instance) {
2558 char str[256];
2559 const u8 *buf = wpabuf_head(info->p2ps_instance);
2560 size_t len = wpabuf_len(info->p2ps_instance);
2561
2562 while (len) {
2563 u32 id;
2564 u16 methods;
2565 u8 str_len;
2566
2567 if (len < 4 + 2 + 1)
2568 break;
2569 id = WPA_GET_LE32(buf);
2570 buf += sizeof(u32);
2571 methods = WPA_GET_BE16(buf);
2572 buf += sizeof(u16);
2573 str_len = *buf++;
2574 if (str_len > len - 4 - 2 - 1)
2575 break;
2576 os_memcpy(str, buf, str_len);
2577 str[str_len] = '\0';
2578 buf += str_len;
2579 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2580
2581 wpa_msg_global(wpa_s, MSG_INFO,
2582 P2P_EVENT_DEVICE_FOUND MACSTR
2583 " p2p_dev_addr=" MACSTR
2584 " pri_dev_type=%s name='%s'"
2585 " config_methods=0x%x"
2586 " dev_capab=0x%x"
2587 " group_capab=0x%x"
2588 " adv_id=%x asp_svc=%s%s",
2589 MAC2STR(addr),
2590 MAC2STR(info->p2p_device_addr),
2591 wps_dev_type_bin2str(
2592 info->pri_dev_type,
2593 devtype, sizeof(devtype)),
2594 info->device_name, methods,
2595 info->dev_capab, info->group_capab,
2596 id, str,
2597 info->vendor_elems ?
2598 " vendor_elems=1" : "");
2599 }
2600 goto done;
2601 }
2602
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002603 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2604 " p2p_dev_addr=" MACSTR
2605 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002606 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002607 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2608 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2609 sizeof(devtype)),
2610 info->device_name, info->config_methods,
2611 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002612 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002613 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002614 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2615 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002616 info->vendor_elems ? " vendor_elems=1" : "",
2617 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002619done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002620 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002621 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002622#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002623
Roshan Piusfd2fd662017-01-23 13:41:57 -08002624 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002625 wfd_dev_info_len, wfd_r2_dev_info,
2626 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002627 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628}
2629
2630
2631static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2632{
2633 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002634
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002635 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2636 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2639}
2640
2641
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002642static void wpas_find_stopped(void *ctx)
2643{
2644 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002645
2646 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2647 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2648
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002649 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002650 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002651}
2652
2653
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002654struct wpas_p2p_listen_work {
2655 unsigned int freq;
2656 unsigned int duration;
2657 struct wpabuf *probe_resp_ie;
2658};
2659
2660
2661static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2662{
2663 if (lwork == NULL)
2664 return;
2665 wpabuf_free(lwork->probe_resp_ie);
2666 os_free(lwork);
2667}
2668
2669
2670static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2671{
2672 struct wpas_p2p_listen_work *lwork;
2673
2674 if (!wpa_s->p2p_listen_work)
2675 return;
2676
2677 lwork = wpa_s->p2p_listen_work->ctx;
2678 wpas_p2p_listen_work_free(lwork);
2679 radio_work_done(wpa_s->p2p_listen_work);
2680 wpa_s->p2p_listen_work = NULL;
2681}
2682
2683
2684static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2685{
2686 struct wpa_supplicant *wpa_s = work->wpa_s;
2687 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002688 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002689
2690 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002691 if (work->started) {
2692 wpa_s->p2p_listen_work = NULL;
2693 wpas_stop_listen(wpa_s);
2694 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002695 wpas_p2p_listen_work_free(lwork);
2696 return;
2697 }
2698
2699 wpa_s->p2p_listen_work = work;
2700
2701 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2702
2703 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2704 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2705 "report received Probe Request frames");
2706 wpas_p2p_listen_work_done(wpa_s);
2707 return;
2708 }
2709
2710 wpa_s->pending_listen_freq = lwork->freq;
2711 wpa_s->pending_listen_duration = lwork->duration;
2712
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002713 duration = lwork->duration;
2714#ifdef CONFIG_TESTING_OPTIONS
2715 if (wpa_s->extra_roc_dur) {
2716 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2717 duration, duration + wpa_s->extra_roc_dur);
2718 duration += wpa_s->extra_roc_dur;
2719 }
2720#endif /* CONFIG_TESTING_OPTIONS */
2721
2722 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002723 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2724 "to remain on channel (%u MHz) for Listen "
2725 "state", lwork->freq);
2726 wpas_p2p_listen_work_done(wpa_s);
2727 wpa_s->pending_listen_freq = 0;
2728 return;
2729 }
2730 wpa_s->off_channel_freq = 0;
2731 wpa_s->roc_waiting_drv_freq = lwork->freq;
2732}
2733
2734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735static int wpas_start_listen(void *ctx, unsigned int freq,
2736 unsigned int duration,
2737 const struct wpabuf *probe_resp_ie)
2738{
2739 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002740 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002742 if (wpa_s->p2p_listen_work) {
2743 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744 return -1;
2745 }
2746
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002747 lwork = os_zalloc(sizeof(*lwork));
2748 if (lwork == NULL)
2749 return -1;
2750 lwork->freq = freq;
2751 lwork->duration = duration;
2752 if (probe_resp_ie) {
2753 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2754 if (lwork->probe_resp_ie == NULL) {
2755 wpas_p2p_listen_work_free(lwork);
2756 return -1;
2757 }
2758 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002760 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2761 lwork) < 0) {
2762 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 return -1;
2764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765
2766 return 0;
2767}
2768
2769
2770static void wpas_stop_listen(void *ctx)
2771{
2772 struct wpa_supplicant *wpa_s = ctx;
2773 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2774 wpa_drv_cancel_remain_on_channel(wpa_s);
2775 wpa_s->off_channel_freq = 0;
2776 wpa_s->roc_waiting_drv_freq = 0;
2777 }
2778 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002779
2780 /*
2781 * Don't cancel Probe Request RX reporting for a connected P2P Client
2782 * handling Probe Request frames.
2783 */
2784 if (!wpa_s->p2p_cli_probe)
2785 wpa_drv_probe_req_report(wpa_s, 0);
2786
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002787 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788}
2789
2790
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002791static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2792 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793{
2794 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002795 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002796 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002797}
2798
2799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2801 const u8 *peer, const char *params,
2802 unsigned int generated_pin)
2803{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002804 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2805 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806}
2807
2808
2809static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2810 const u8 *peer, const char *params)
2811{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002812 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2813 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814}
2815
2816
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002817static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2818 const u8 *dev_addr, const u8 *pri_dev_type,
2819 const char *dev_name, u16 supp_config_methods,
2820 u8 dev_capab, u8 group_capab, const u8 *group_id,
2821 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822{
2823 struct wpa_supplicant *wpa_s = ctx;
2824 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002825 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826 u8 empty_dev_type[8];
2827 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002828 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002829 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002830
2831 if (group_id) {
2832 for (group = wpa_s->global->ifaces; group; group = group->next)
2833 {
2834 struct wpa_ssid *s = group->current_ssid;
2835 if (s != NULL &&
2836 s->mode == WPAS_MODE_P2P_GO &&
2837 group_id_len - ETH_ALEN == s->ssid_len &&
2838 os_memcmp(group_id + ETH_ALEN, s->ssid,
2839 s->ssid_len) == 0)
2840 break;
2841 }
2842 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843
2844 if (pri_dev_type == NULL) {
2845 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2846 pri_dev_type = empty_dev_type;
2847 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002848 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2849 " pri_dev_type=%s name='%s' config_methods=0x%x "
2850 "dev_capab=0x%x group_capab=0x%x%s%s",
2851 MAC2STR(dev_addr),
2852 wps_dev_type_bin2str(pri_dev_type, devtype,
2853 sizeof(devtype)),
2854 dev_name, supp_config_methods, dev_capab, group_capab,
2855 group ? " group=" : "",
2856 group ? group->ifname : "");
2857 if (os_snprintf_error(sizeof(params), res))
2858 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002862 if (wps_generate_pin(&generated_pin) < 0) {
2863 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2864 wpas_notify_p2p_provision_discovery(
2865 wpa_s, peer, 0 /* response */,
2866 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2867 return;
2868 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 wpas_prov_disc_local_display(wpa_s, peer, params,
2870 generated_pin);
2871 } else if (config_methods & WPS_CONFIG_KEYPAD)
2872 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2873 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002874 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2875 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002876
2877 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2878 P2P_PROV_DISC_SUCCESS,
2879 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880}
2881
2882
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002883static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884{
2885 struct wpa_supplicant *wpa_s = ctx;
2886 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002887 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002889 if (wpa_s->pending_pd_before_join &&
2890 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2891 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2892 wpa_s->pending_pd_before_join = 0;
2893 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2894 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002895 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002896 return;
2897 }
2898
Dmitry Shmidt04949592012-07-19 12:16:46 -07002899 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002900 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2901 int res;
2902
2903 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2904 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2905 if (os_snprintf_error(sizeof(params), res))
2906 params[sizeof(params) - 1] = '\0';
2907 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002908 params[0] = '\0';
2909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002911 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002913 if (wps_generate_pin(&generated_pin) < 0) {
2914 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2915 wpas_notify_p2p_provision_discovery(
2916 wpa_s, peer, 0 /* response */,
2917 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2918 return;
2919 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002920 wpas_prov_disc_local_display(wpa_s, peer, params,
2921 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002923 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2924 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925
Jouni Malinen75ecf522011-06-27 15:19:46 -07002926 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2927 P2P_PROV_DISC_SUCCESS,
2928 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929}
2930
2931
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002932static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002933 enum p2p_prov_disc_status status,
2934 u32 adv_id, const u8 *adv_mac,
2935 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002936{
2937 struct wpa_supplicant *wpa_s = ctx;
2938
Dmitry Shmidt04949592012-07-19 12:16:46 -07002939 if (wpa_s->p2p_fallback_to_go_neg) {
2940 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2941 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002942 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002943 P2P_EVENT_FALLBACK_TO_GO_NEG
2944 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002945 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2946 return;
2947 }
2948
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002949 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002950 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002951 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2952 "join-existing-group operation (no ACK for PD "
2953 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002954 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002955 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002956 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002957
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002958 if (adv_id && adv_mac && deferred_session_resp) {
2959 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2960 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2961 " deferred_session_resp='%s'",
2962 MAC2STR(peer), status, adv_id,
2963 deferred_session_resp);
2964 } else if (adv_id && adv_mac) {
2965 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2966 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2967 MAC2STR(peer), status, adv_id);
2968 } else {
2969 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2970 " p2p_dev_addr=" MACSTR " status=%d",
2971 MAC2STR(peer), status);
2972 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002973
Jouni Malinen75ecf522011-06-27 15:19:46 -07002974 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2975 status, 0, 0);
2976}
2977
2978
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002979static int freq_included(struct wpa_supplicant *wpa_s,
2980 const struct p2p_channels *channels,
2981 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002982{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002983 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2984 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2985 return 1;
2986 return 0;
2987}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002988
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002989
2990static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2991{
2992 unsigned int num = P2P_MAX_CHANNELS;
2993 int *common_freqs;
2994 int ret;
2995
2996 p2p_go_dump_common_freqs(wpa_s);
2997 common_freqs = os_calloc(num, sizeof(int));
2998 if (!common_freqs)
2999 return;
3000
3001 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3002 if (ret < 0) {
3003 wpa_dbg(wpa_s, MSG_DEBUG,
3004 "P2P: Failed to get group common freqs");
3005 os_free(common_freqs);
3006 return;
3007 }
3008
3009 os_free(wpa_s->p2p_group_common_freqs);
3010 wpa_s->p2p_group_common_freqs = common_freqs;
3011 wpa_s->p2p_group_common_freqs_num = num;
3012 p2p_go_dump_common_freqs(wpa_s);
3013}
3014
3015
3016/*
3017 * Check if the given frequency is one of the possible operating frequencies
3018 * set after the completion of the GO Negotiation.
3019 */
3020static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3021{
3022 unsigned int i;
3023
3024 p2p_go_dump_common_freqs(wpa_s);
3025
3026 /* assume no restrictions */
3027 if (!wpa_s->p2p_group_common_freqs_num)
3028 return 1;
3029
3030 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3031 if (wpa_s->p2p_group_common_freqs[i] == freq)
3032 return 1;
3033 }
3034 return 0;
3035}
3036
3037
3038static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3039 struct sta_info *sta, void *ctx)
3040{
3041 int *ecsa_support = ctx;
3042
3043 *ecsa_support &= sta->ecsa_supported;
3044
3045 return 0;
3046}
3047
3048
3049/* Check if all the peers support eCSA */
3050static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3051{
3052 int ecsa_support = 1;
3053
3054 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3055 &ecsa_support);
3056
3057 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003058}
3059
3060
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003061/**
3062 * Pick the best frequency to use from all the currently used frequencies.
3063 */
3064static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3065 struct wpa_used_freq_data *freqs,
3066 unsigned int num)
3067{
3068 unsigned int i, c;
3069
3070 /* find a candidate freq that is supported by P2P */
3071 for (c = 0; c < num; c++)
3072 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3073 break;
3074
3075 if (c == num)
3076 return 0;
3077
3078 /* once we have a candidate, try to find a 'better' one */
3079 for (i = c + 1; i < num; i++) {
3080 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3081 continue;
3082
3083 /*
3084 * 1. Infrastructure station interfaces have higher preference.
3085 * 2. P2P Clients have higher preference.
3086 * 3. All others.
3087 */
3088 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3089 c = i;
3090 break;
3091 }
3092
3093 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3094 c = i;
3095 }
3096 return freqs[c].freq;
3097}
3098
3099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3101 const u8 *go_dev_addr, const u8 *ssid,
3102 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003103 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003104 const struct p2p_channels *channels,
3105 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106{
3107 struct wpa_supplicant *wpa_s = ctx;
3108 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003109 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003111 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112
3113 if (!persistent_group) {
3114 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003115 " to join an active group (SSID: %s)",
3116 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3118 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3119 == 0 ||
3120 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3121 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3122 "authorized invitation");
3123 goto accept_inv;
3124 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003125
3126#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003127 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3128 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003129 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003130 wpa_s->p2p_wps_method = WPS_NFC;
3131 wpa_s->pending_join_wps_method = WPS_NFC;
3132 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003133 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003134 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003135 bssid, ETH_ALEN);
3136 goto accept_inv;
3137 }
3138#endif /* CONFIG_WPS_NFC */
3139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 /*
3141 * Do not accept the invitation automatically; notify user and
3142 * request approval.
3143 */
3144 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3145 }
3146
3147 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3148 if (grp) {
3149 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3150 "running persistent group");
3151 if (*go)
3152 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3153 goto accept_inv;
3154 }
3155
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003156 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3157 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3158 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3159 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003160 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003161 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3163
3164 for (s = wpa_s->conf->ssid; s; s = s->next) {
3165 if (s->disabled == 2 &&
3166 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3167 s->ssid_len == ssid_len &&
3168 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3169 break;
3170 }
3171
3172 if (!s) {
3173 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3174 " requested reinvocation of an unknown group",
3175 MAC2STR(sa));
3176 return P2P_SC_FAIL_UNKNOWN_GROUP;
3177 }
3178
3179 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3180 *go = 1;
3181 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3182 wpa_printf(MSG_DEBUG, "P2P: The only available "
3183 "interface is already in use - reject "
3184 "invitation");
3185 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3186 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003187 if (wpa_s->p2p_mgmt)
3188 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3189 ETH_ALEN);
3190 else
3191 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 } else if (s->mode == WPAS_MODE_P2P_GO) {
3193 *go = 1;
3194 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3195 {
3196 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3197 "interface address for the group");
3198 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3199 }
3200 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3201 ETH_ALEN);
3202 }
3203
3204accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003205 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003207 best_freq = 0;
3208 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3209 sizeof(struct wpa_used_freq_data));
3210 if (freqs) {
3211 int num_channels = wpa_s->num_multichan_concurrent;
3212 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3213 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3214 os_free(freqs);
3215 }
3216
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003217 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003218 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003219 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003220 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003221
3222 if (wpa_s->num_multichan_concurrent < 2 ||
3223 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3224 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 -07003225 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003226 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227 }
3228
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003229 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3230 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003231 if (*go == 0) {
3232 /* We are the client */
3233 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3234 "running a GO but we are capable of MCC, "
3235 "figure out the best channel to use");
3236 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003237 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003238 /* We are the GO, and *force_freq is not in the
3239 * intersection */
3240 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3241 "in intersection but we are capable of MCC, "
3242 "figure out the best channel to use",
3243 *force_freq);
3244 *force_freq = 0;
3245 }
3246 }
3247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 return P2P_SC_SUCCESS;
3249}
3250
3251
3252static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3253 const u8 *ssid, size_t ssid_len,
3254 const u8 *go_dev_addr, u8 status,
3255 int op_freq)
3256{
3257 struct wpa_supplicant *wpa_s = ctx;
3258 struct wpa_ssid *s;
3259
3260 for (s = wpa_s->conf->ssid; s; s = s->next) {
3261 if (s->disabled == 2 &&
3262 s->ssid_len == ssid_len &&
3263 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3264 break;
3265 }
3266
3267 if (status == P2P_SC_SUCCESS) {
3268 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003269 " was accepted; op_freq=%d MHz, SSID=%s",
3270 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003272 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003273 if (go) {
3274 wpa_msg_global(wpa_s, MSG_INFO,
3275 P2P_EVENT_INVITATION_ACCEPTED
3276 "sa=" MACSTR
3277 " persistent=%d freq=%d",
3278 MAC2STR(sa), s->id, op_freq);
3279 } else {
3280 wpa_msg_global(wpa_s, MSG_INFO,
3281 P2P_EVENT_INVITATION_ACCEPTED
3282 "sa=" MACSTR
3283 " persistent=%d",
3284 MAC2STR(sa), s->id);
3285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003287 wpa_s, s, go, 0, op_freq, 0,
3288 wpa_s->conf->p2p_go_ht40,
3289 wpa_s->conf->p2p_go_vht,
3290 0,
3291 wpa_s->conf->p2p_go_he,
3292 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003293 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Hai Shalomc1a21442022-02-04 13:43:00 -08003294 1, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003296 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003297 wpa_msg_global(wpa_s, MSG_INFO,
3298 P2P_EVENT_INVITATION_ACCEPTED
3299 "sa=" MACSTR " go_dev_addr=" MACSTR
3300 " bssid=" MACSTR " unknown-network",
3301 MAC2STR(sa), MAC2STR(go_dev_addr),
3302 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003304 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003305 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 }
3307 return;
3308 }
3309
3310 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3311 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3312 " was rejected (status %u)", MAC2STR(sa), status);
3313 return;
3314 }
3315
3316 if (!s) {
3317 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003318 wpa_msg_global(wpa_s, MSG_INFO,
3319 P2P_EVENT_INVITATION_RECEIVED
3320 "sa=" MACSTR " go_dev_addr=" MACSTR
3321 " bssid=" MACSTR " unknown-network",
3322 MAC2STR(sa), MAC2STR(go_dev_addr),
3323 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003325 wpa_msg_global(wpa_s, MSG_INFO,
3326 P2P_EVENT_INVITATION_RECEIVED
3327 "sa=" MACSTR " go_dev_addr=" MACSTR
3328 " unknown-network",
3329 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003331 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3332 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333 return;
3334 }
3335
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003336 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003337 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3338 "sa=" MACSTR " persistent=%d freq=%d",
3339 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003340 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003341 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3342 "sa=" MACSTR " persistent=%d",
3343 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003344 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003345 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3346 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347}
3348
3349
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003350static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3351 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003352 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003353{
3354 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003355 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003356
3357 if (ssid == NULL)
3358 return;
3359
3360 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003361 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003362 ETH_ALEN) == 0)
3363 break;
3364 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003365 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003366 if (ssid->mode != WPAS_MODE_P2P_GO &&
3367 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3368 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3369 "due to invitation result", ssid->id);
3370 wpas_notify_network_removed(wpa_s, ssid);
3371 wpa_config_remove_network(wpa_s->conf, ssid->id);
3372 return;
3373 }
3374 return; /* Peer not found in client list */
3375 }
3376
3377 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003378 "group %d client list%s",
3379 MAC2STR(peer), ssid->id,
3380 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003381 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3382 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3383 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003384 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003385 if (p2p_wpa_s->conf->update_config &&
3386 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003387 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003388}
3389
3390
3391static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3392 const u8 *peer)
3393{
3394 struct wpa_ssid *ssid;
3395
3396 wpa_s = wpa_s->global->p2p_invite_group;
3397 if (wpa_s == NULL)
3398 return; /* No known invitation group */
3399 ssid = wpa_s->current_ssid;
3400 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3401 !ssid->p2p_persistent_group)
3402 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003403 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003404 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003405 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003406}
3407
3408
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003409static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003410 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003411 const u8 *peer, int neg_freq,
3412 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413{
3414 struct wpa_supplicant *wpa_s = ctx;
3415 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003416 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003417
3418 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003419 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3420 "status=%d " MACSTR,
3421 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003423 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3424 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425 }
3426 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3427
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003428 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3429 status, MAC2STR(peer));
3430 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003431 struct wpa_supplicant *group_if =
3432 wpa_s->global->p2p_invite_group;
3433
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003434 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3435 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003436
3437 /*
3438 * Invitation to an active group. If this is successful and we
3439 * are the GO, set the client wait to postpone some concurrent
3440 * operations and to allow provisioning and connection to happen
3441 * more quickly.
3442 */
3443 if (status == P2P_SC_SUCCESS &&
3444 group_if && group_if->current_ssid &&
3445 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3446 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3447#ifdef CONFIG_TESTING_OPTIONS
3448 if (group_if->p2p_go_csa_on_inv) {
3449 wpa_printf(MSG_DEBUG,
3450 "Testing: force P2P GO CSA after invitation");
3451 eloop_cancel_timeout(
3452 wpas_p2p_reconsider_moving_go,
3453 wpa_s, NULL);
3454 eloop_register_timeout(
3455 0, 50000,
3456 wpas_p2p_reconsider_moving_go,
3457 wpa_s, NULL);
3458 }
3459#endif /* CONFIG_TESTING_OPTIONS */
3460 }
3461 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003464 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3465 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3466 "invitation exchange to indicate readiness for "
3467 "re-invocation");
3468 }
3469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003470 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003471 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3472 ssid = wpa_config_get_network(
3473 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003474 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003475 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003476 wpas_p2p_remove_pending_group_interface(wpa_s);
3477 return;
3478 }
3479
3480 ssid = wpa_config_get_network(wpa_s->conf,
3481 wpa_s->pending_invite_ssid_id);
3482 if (ssid == NULL) {
3483 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3484 "data matching with invitation");
3485 return;
3486 }
3487
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003488 /*
3489 * The peer could have missed our ctrl::ack frame for Invitation
3490 * Response and continue retransmitting the frame. To reduce the
3491 * likelihood of the peer not getting successful TX status for the
3492 * Invitation Response frame, wait a short time here before starting
3493 * the persistent group so that we will remain on the current channel to
3494 * acknowledge any possible retransmission from the peer.
3495 */
3496 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3497 "starting persistent group");
3498 os_sleep(0, 50000);
3499
Dmitry Shmidt15907092014-03-25 10:42:57 -07003500 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003501 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003502 freq = neg_freq;
3503 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003504 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003505 freq = peer_oper_freq;
3506 else
3507 freq = 0;
3508
3509 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3510 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003512 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003513 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003514 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003515 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003516 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003517 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003518 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003519 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003520 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003521 ssid->mode == WPAS_MODE_P2P_GO ?
3522 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003523 0, 1,
3524 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525}
3526
3527
Dmitry Shmidt04949592012-07-19 12:16:46 -07003528static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3529 unsigned int freq)
3530{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003531 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3532 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003533 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003534}
3535
3536
3537static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3538{
3539 reg->channel[reg->channels] = chan;
3540 reg->channels++;
3541}
3542
3543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003545 struct p2p_channels *chan,
3546 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547{
3548 int i, cla = 0;
3549
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003550 wpa_s->global->p2p_24ghz_social_channels = 1;
3551
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003552 os_memset(cli_chan, 0, sizeof(*cli_chan));
3553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3555 "band");
3556
3557 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3558 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003559 chan->reg_class[cla].channels = 0;
3560 for (i = 0; i < 11; i++) {
3561 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3562 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3563 }
3564 if (chan->reg_class[cla].channels)
3565 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566
3567 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3568 "band");
3569
3570 /* Operating class 115 - 5 GHz, channels 36-48 */
3571 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003572 chan->reg_class[cla].channels = 0;
3573 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3574 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3575 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3576 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3577 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3578 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3579 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3580 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3581 if (chan->reg_class[cla].channels)
3582 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583
3584 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3585 "band");
3586
3587 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3588 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003589 chan->reg_class[cla].channels = 0;
3590 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3591 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3592 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3593 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3594 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3595 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3596 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3597 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3598 if (chan->reg_class[cla].channels)
3599 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600
3601 chan->reg_classes = cla;
3602 return 0;
3603}
3604
3605
Hai Shalomc1a21442022-02-04 13:43:00 -08003606static enum chan_allowed has_channel(struct wpa_global *global,
3607 struct hostapd_hw_modes *mode, u8 op_class,
3608 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609{
3610 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611 unsigned int freq;
3612
Hai Shalomc1a21442022-02-04 13:43:00 -08003613 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003614 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003615 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616
3617 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003618 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 if (flags)
3620 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003621 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003622 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003623 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3624 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003625 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3626 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003627 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 }
3629 }
3630
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003631 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632}
3633
3634
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003635static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3636 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003637 u8 channel, const u8 *center_channels,
3638 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003639{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003640 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003641
3642 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3643 return 0;
3644
Hai Shalomc1a21442022-02-04 13:43:00 -08003645 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003646 /*
3647 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3648 * so the center channel is 6 channels away from the start/end.
3649 */
3650 if (channel >= center_channels[i] - 6 &&
3651 channel <= center_channels[i] + 6)
3652 return center_channels[i];
3653
3654 return 0;
3655}
3656
3657
Hai Shalomc1a21442022-02-04 13:43:00 -08003658static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3659 155, 171 };
3660static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3661 119, 135, 151, 167, 183, 199,
3662 215 };
3663
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003664static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3665 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003666 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003667{
3668 u8 center_chan;
3669 int i, flags;
3670 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003671 const u8 *chans;
3672 size_t num_chans;
3673 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003674
Hai Shalomc1a21442022-02-04 13:43:00 -08003675 if (is_6ghz) {
3676 chans = center_channels_6ghz_80mhz;
3677 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3678 } else {
3679 chans = center_channels_5ghz_80mhz;
3680 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3681 }
3682 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3683 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003684 if (!center_chan)
3685 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003686 if (!wpa_s->p2p_go_allow_dfs &&
3687 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003688 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3689
3690 /* check all the channels are available */
3691 for (i = 0; i < 4; i++) {
3692 int adj_chan = center_chan - 6 + i * 4;
3693
Hai Shalomc1a21442022-02-04 13:43:00 -08003694 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3695 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003696 if (res == NOT_ALLOWED)
3697 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003698 if (res == RADAR)
3699 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003700 if (res == NO_IR)
3701 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003702 if (!is_6ghz) {
3703 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3704 return NOT_ALLOWED;
3705 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3706 return NOT_ALLOWED;
3707 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3708 return NOT_ALLOWED;
3709 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3710 return NOT_ALLOWED;
3711 } else if (is_6ghz &&
3712 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3713 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003714 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003715 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003716 }
3717
3718 return ret;
3719}
3720
3721
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003722static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3723 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003724 u8 channel, const u8 *center_channels,
3725 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003726{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003727 unsigned int i;
3728
3729 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3730 return 0;
3731
Hai Shalomc1a21442022-02-04 13:43:00 -08003732 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003733 /*
3734 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3735 * so the center channel is 14 channels away from the start/end.
3736 */
3737 if (channel >= center_channels[i] - 14 &&
3738 channel <= center_channels[i] + 14)
3739 return center_channels[i];
3740
3741 return 0;
3742}
3743
3744
Hai Shalomc1a21442022-02-04 13:43:00 -08003745static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3746static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3747 207 };
3748
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003749static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3750 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003751 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003752{
3753 u8 center_chan;
3754 int i, flags;
3755 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003756 const u8 *chans;
3757 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003758
Hai Shalomc1a21442022-02-04 13:43:00 -08003759 if (is_6ghz_op_class(op_class)) {
3760 chans = center_channels_6ghz_160mhz;
3761 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3762 } else {
3763 chans = center_channels_5ghz_160mhz;
3764 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3765 }
3766 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3767 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003768 if (!center_chan)
3769 return NOT_ALLOWED;
3770 /* VHT 160 MHz uses DFS channels in most countries. */
3771
3772 /* Check all the channels are available */
3773 for (i = 0; i < 8; i++) {
3774 int adj_chan = center_chan - 14 + i * 4;
3775
Hai Shalomc1a21442022-02-04 13:43:00 -08003776 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3777 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003778 if (res == NOT_ALLOWED)
3779 return NOT_ALLOWED;
3780
Hai Shalomc1a21442022-02-04 13:43:00 -08003781 if (res == RADAR)
3782 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003783 if (res == NO_IR)
3784 ret = NO_IR;
3785
Hai Shalomc1a21442022-02-04 13:43:00 -08003786 if (!is_6ghz_op_class(op_class)) {
3787 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3788 return NOT_ALLOWED;
3789 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3790 return NOT_ALLOWED;
3791 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3792 return NOT_ALLOWED;
3793 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3794 return NOT_ALLOWED;
3795 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3796 return NOT_ALLOWED;
3797 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3798 return NOT_ALLOWED;
3799 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3800 return NOT_ALLOWED;
3801 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3802 return NOT_ALLOWED;
3803 } else if (is_6ghz_op_class(op_class) &&
3804 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3805 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003806 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003807 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003808 }
3809
3810 return ret;
3811}
3812
3813
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003814static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3815 struct hostapd_hw_modes *mode,
3816 u8 channel)
3817{
3818 struct ieee80211_edmg_config edmg;
3819
3820 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3821 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3822 return ALLOWED;
3823
3824 return NOT_ALLOWED;
3825}
3826
3827
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003828static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3829 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003830 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003831{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003832 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003833 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003834
Hai Shalomc1a21442022-02-04 13:43:00 -08003835 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003836 if (bw == BW40MINUS) {
3837 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3838 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003839 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3840 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003841 } else if (bw == BW40PLUS) {
3842 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3843 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003844 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3845 NULL);
3846 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3847 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3848 return NOT_ALLOWED;
3849 if (get_6ghz_sec_channel(channel) < 0)
3850 res2 = has_channel(wpa_s->global, mode, op_class,
3851 channel - 4, NULL);
3852 else
3853 res2 = has_channel(wpa_s->global, mode, op_class,
3854 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003855 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003856 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3857 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003858 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003859 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3860 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003861 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3862 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003863 }
3864
3865 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3866 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003867 if (res == NO_IR || res2 == NO_IR)
3868 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003869 if (res == RADAR || res2 == RADAR)
3870 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003871 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003872}
3873
3874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003876 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003877 struct p2p_channels *cli_chan,
3878 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003880 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003881 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3885 "of all supported channels; assume dualband "
3886 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003887 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 }
3889
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003890 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003892 for (op = 0; global_op_class[op].op_class; op++) {
3893 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003894 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003895 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896
Hai Shalom899fcc72020-10-19 14:38:18 -07003897 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003898 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003899 continue;
3900
Hai Shalomfdcde762020-04-02 11:19:20 -07003901 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3902 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903 if (mode == NULL)
3904 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003905 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3906 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003908 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003909
3910 /* Check for non-continuous jump in channel index
3911 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003912 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003913 ch < 149 && ch + o->inc > 149)
3914 ch = 149;
3915
Hai Shalomc1a21442022-02-04 13:43:00 -08003916 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
3917 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003918 if (res == ALLOWED) {
3919 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003920 if (cla == P2P_MAX_REG_CLASSES)
3921 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003922 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3923 o->op_class);
3924 reg = &chan->reg_class[cla];
3925 cla++;
3926 reg->reg_class = o->op_class;
3927 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003928 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3929 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003930 reg->channel[reg->channels] = ch;
3931 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003932 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003933 wpa_s->conf->p2p_add_cli_chan) {
3934 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003935 if (cli_cla == P2P_MAX_REG_CLASSES)
3936 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003937 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3938 o->op_class);
3939 cli_reg = &cli_chan->reg_class[cli_cla];
3940 cli_cla++;
3941 cli_reg->reg_class = o->op_class;
3942 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003943 if (cli_reg->channels ==
3944 P2P_MAX_REG_CLASS_CHANNELS)
3945 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003946 cli_reg->channel[cli_reg->channels] = ch;
3947 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949 }
3950 if (reg) {
3951 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3952 reg->channel, reg->channels);
3953 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003954 if (cli_reg) {
3955 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3956 cli_reg->channel, cli_reg->channels);
3957 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 }
3959
3960 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003961 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 return 0;
3964}
3965
3966
Hai Shalomc1a21442022-02-04 13:43:00 -08003967int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
3968 struct hostapd_hw_modes *mode,
3969 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003970{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003971 int op;
3972 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003973
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003974 for (op = 0; global_op_class[op].op_class; op++) {
3975 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003976 u16 ch;
3977 int chan = channel;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003978
Hai Shalomc1a21442022-02-04 13:43:00 -08003979 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
3980 * driver offloaded DFS. */
3981 if ((o->p2p == NO_P2P_SUPP &&
3982 (!is_dfs_global_op_class(o->op_class) ||
3983 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07003984 (is_6ghz_op_class(o->op_class) &&
3985 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003986 continue;
3987
Hai Shalomc1a21442022-02-04 13:43:00 -08003988 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
3989 get_6ghz_sec_channel(channel) < 0)
3990 chan = channel - 4;
3991
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003992 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3993 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08003994 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
3995 o->bw != BW40) ||
3996 ch != chan)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003997 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08003998 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
3999 ch, o->bw);
4000 if (ret == ALLOWED) {
4001 if (is_6ghz_op_class(o->op_class) &&
4002 o->bw == BW40)
4003 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004004 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004005 }
4006 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4007 /* Allow RADAR channels used for driver
4008 * offloaded DFS */
4009 return (o->bw == BW40MINUS) ? -1 : 1;
4010 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004011 }
4012 }
4013 return 0;
4014}
4015
4016
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004017int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004018 struct hostapd_hw_modes *mode, u8 channel,
4019 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004020{
Hai Shalomc1a21442022-02-04 13:43:00 -08004021 const u8 *chans;
4022 size_t num_chans;
4023 enum chan_allowed ret;
4024
4025 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4026 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004027 return 0;
4028
Hai Shalomc1a21442022-02-04 13:43:00 -08004029 if (is_6ghz_op_class(op_class)) {
4030 chans = center_channels_6ghz_80mhz;
4031 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4032 } else {
4033 chans = center_channels_5ghz_80mhz;
4034 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4035 }
4036 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4037 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004038}
4039
4040
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004041int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004042 struct hostapd_hw_modes *mode, u8 channel,
4043 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004044{
Hai Shalomc1a21442022-02-04 13:43:00 -08004045 const u8 *chans;
4046 size_t num_chans;
4047 enum chan_allowed ret;
4048
4049 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4050 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004051 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004052 if (is_6ghz_op_class(op_class)) {
4053 chans = center_channels_6ghz_160mhz;
4054 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4055 } else {
4056 chans = center_channels_5ghz_160mhz;
4057 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4058 }
4059 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4060 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004061}
4062
4063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4065 size_t buf_len)
4066{
4067 struct wpa_supplicant *wpa_s = ctx;
4068
4069 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4070 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4071 break;
4072 }
4073 if (wpa_s == NULL)
4074 return -1;
4075
4076 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4077}
4078
4079
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004080struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4081 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004082{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004083 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4084 struct wpa_ssid *s = wpa_s->current_ssid;
4085 if (s == NULL)
4086 continue;
4087 if (s->mode != WPAS_MODE_P2P_GO &&
4088 s->mode != WPAS_MODE_AP &&
4089 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4090 continue;
4091 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004092 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004093 continue;
4094 return wpa_s;
4095 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004096
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004097 return NULL;
4098
4099}
4100
4101
4102struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4103 const u8 *peer_dev_addr)
4104{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004105 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4106 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004107 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004108 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004109 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4110 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004111 }
4112
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004113 return NULL;
4114}
4115
4116
4117static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4118{
4119 struct wpa_supplicant *wpa_s = ctx;
4120
4121 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004122}
4123
4124
Dmitry Shmidt18463232014-01-24 12:29:41 -08004125static int wpas_is_concurrent_session_active(void *ctx)
4126{
4127 struct wpa_supplicant *wpa_s = ctx;
4128 struct wpa_supplicant *ifs;
4129
4130 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4131 if (ifs == wpa_s)
4132 continue;
4133 if (ifs->wpa_state > WPA_ASSOCIATED)
4134 return 1;
4135 }
4136 return 0;
4137}
4138
4139
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004140static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4141{
4142 struct wpa_supplicant *wpa_s = ctx;
4143 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4144}
4145
4146
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004147int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4148 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004149{
4150 struct wpa_interface iface;
4151 struct wpa_supplicant *p2pdev_wpa_s;
4152 char ifname[100];
4153 char force_name[100];
4154 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004155 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004156
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004157 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4158 wpa_s->ifname);
4159 if (os_snprintf_error(sizeof(ifname), ret))
4160 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004161 /* Cut length at the maximum size. Note that we don't need to ensure
4162 * collision free names here as the created interface is not a netdev.
4163 */
4164 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004165 force_name[0] = '\0';
4166 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004167
4168 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4169 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4170 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4171
4172 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004173 force_name, wpa_s->pending_interface_addr, NULL);
4174 if (ret < 0) {
4175 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4176 return ret;
4177 }
4178 os_strlcpy(wpa_s->pending_interface_name, ifname,
4179 sizeof(wpa_s->pending_interface_name));
4180
4181 os_memset(&iface, 0, sizeof(iface));
4182 iface.p2p_mgmt = 1;
4183 iface.ifname = wpa_s->pending_interface_name;
4184 iface.driver = wpa_s->driver->name;
4185 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004186
4187 /*
4188 * If a P2P Device configuration file was given, use it as the interface
4189 * configuration file (instead of using parent's configuration file.
4190 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004191 if (conf_p2p_dev) {
4192 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004193 iface.ctrl_interface = NULL;
4194 } else {
4195 iface.confname = wpa_s->confname;
4196 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4197 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004198
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004199 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004200 if (!p2pdev_wpa_s) {
4201 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4202 return -1;
4203 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004204
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004205 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004206 wpa_s->pending_interface_name[0] = '\0';
4207 return 0;
4208}
4209
4210
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004211static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4212 const u8 *noa, size_t noa_len)
4213{
4214 struct wpa_supplicant *wpa_s, *intf = ctx;
4215 char hex[100];
4216
4217 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4218 if (wpa_s->waiting_presence_resp)
4219 break;
4220 }
4221 if (!wpa_s) {
4222 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4223 return;
4224 }
4225 wpa_s->waiting_presence_resp = 0;
4226
4227 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4228 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4229 " status=%u noa=%s", MAC2STR(src), status, hex);
4230}
4231
4232
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004233static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4234 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004235 u8 *ret_ssid, size_t *ret_ssid_len,
4236 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004237{
4238 struct wpa_supplicant *wpa_s = ctx;
4239 struct wpa_ssid *s;
4240
4241 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4242 if (s) {
4243 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4244 *ret_ssid_len = s->ssid_len;
4245 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004246
4247 if (s->mode != WPAS_MODE_P2P_GO) {
4248 os_memset(intended_iface_addr, 0, ETH_ALEN);
4249 } else if (wpas_p2p_create_iface(wpa_s)) {
4250 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4251 return 0;
4252
4253 os_memcpy(intended_iface_addr,
4254 wpa_s->pending_interface_addr, ETH_ALEN);
4255 } else {
4256 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4257 ETH_ALEN);
4258 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004259 return 1;
4260 }
4261
4262 return 0;
4263}
4264
4265
4266static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004267 u8 *ssid, size_t *ssid_len, int *group_iface,
4268 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004269{
4270 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004271 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004272 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004273
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004274 /*
4275 * group_iface will be set to 1 only if a dedicated interface for P2P
4276 * role is required. First, we try to reuse an active GO. However,
4277 * if it is not present, we will try to reactivate an existing
4278 * persistent group and set group_iface to 1, so the caller will know
4279 * that the pending interface should be used.
4280 */
4281 *group_iface = 0;
4282
4283 if (freq)
4284 *freq = 0;
4285
4286 go = wpas_p2p_get_go_group(wpa_s);
4287 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004288 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004289 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004290 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004291 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4292 else
4293 return 0;
4294 } else {
4295 s = go->current_ssid;
4296 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4297 if (freq)
4298 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004299 }
4300
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004301 os_memcpy(ssid, s->ssid, s->ssid_len);
4302 *ssid_len = s->ssid_len;
4303
4304 return 1;
4305}
4306
4307
4308static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4309 const u8 *ssid, size_t ssid_len)
4310{
4311 struct wpa_supplicant *wpa_s = ctx;
4312 struct wpa_ssid *s;
4313 int save_config = 0;
4314 size_t i;
4315
4316 /* Start with our first choice of Persistent Groups */
4317 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4318 if (go && ssid && ssid_len &&
4319 s->ssid_len == ssid_len &&
4320 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4321 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4322 break;
4323
4324 /* Remove stale persistent group */
4325 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004326 wpa_dbg(wpa_s, MSG_DEBUG,
4327 "P2P: Remove stale persistent group id=%d",
4328 s->id);
4329 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004330 wpa_config_remove_network(wpa_s->conf, s->id);
4331 save_config = 1;
4332 continue;
4333 }
4334
4335 for (i = 0; i < s->num_p2p_clients; i++) {
4336 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4337 peer, ETH_ALEN) != 0)
4338 continue;
4339
4340 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4341 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4342 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4343 break;
4344 }
4345 s->num_p2p_clients--;
4346 save_config = 1;
4347 }
4348
4349 if (save_config)
4350 p2p_config_write(wpa_s);
4351
4352 /* Return TRUE if valid SSID remains */
4353 return s != NULL;
4354}
4355
4356
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004357static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4358 const u8 *feat_cap, size_t feat_cap_len)
4359{
4360 static const char pref[] = " feature_cap=";
4361 int ret;
4362
4363 buf[0] = '\0';
4364
4365 /*
4366 * We expect a feature capability to contain at least one byte to be
4367 * reported. The string buffer provided by the caller function is
4368 * expected to be big enough to contain all bytes of the attribute for
4369 * known specifications. This function truncates the reported bytes if
4370 * the feature capability data exceeds the string buffer size.
4371 */
4372 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4373 return;
4374
4375 os_memcpy(buf, pref, sizeof(pref));
4376 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4377 buf_len - sizeof(pref) + 1,
4378 feat_cap, feat_cap_len);
4379
4380 if (ret != (2 * (int) feat_cap_len))
4381 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4382}
4383
4384
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004385static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4386 const u8 *adv_mac, const u8 *ses_mac,
4387 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4388 u8 conncap, int passwd_id,
4389 const u8 *persist_ssid,
4390 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004391 int prov_start, const char *session_info,
4392 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004393 unsigned int freq,
4394 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004395{
4396 struct wpa_supplicant *wpa_s = ctx;
4397 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004398 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004399 int save_config = 0;
4400 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004401 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004402
4403 if (!dev)
4404 return;
4405
4406 os_memset(mac, 0, ETH_ALEN);
4407 if (!adv_mac)
4408 adv_mac = mac;
4409 if (!ses_mac)
4410 ses_mac = mac;
4411 if (!grp_mac)
4412 grp_mac = mac;
4413
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004414 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4415 feat_cap, feat_cap_len);
4416
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004417 if (prov_start) {
4418 if (session_info == NULL) {
4419 wpa_msg_global(wpa_s, MSG_INFO,
4420 P2P_EVENT_P2PS_PROVISION_START MACSTR
4421 " adv_id=%x conncap=%x"
4422 " adv_mac=" MACSTR
4423 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004424 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004425 MAC2STR(dev), adv_id, conncap,
4426 MAC2STR(adv_mac),
4427 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004428 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004429 } else {
4430 wpa_msg_global(wpa_s, MSG_INFO,
4431 P2P_EVENT_P2PS_PROVISION_START MACSTR
4432 " adv_id=%x conncap=%x"
4433 " adv_mac=" MACSTR
4434 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004435 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004436 MAC2STR(dev), adv_id, conncap,
4437 MAC2STR(adv_mac),
4438 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004439 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004440 }
4441 return;
4442 }
4443
4444 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4445 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4446
4447 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4448 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4449 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4450
4451 if (persistent_go && !persistent_go->num_p2p_clients) {
4452 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004453 wpa_dbg(wpa_s, MSG_DEBUG,
4454 "P2P: Remove empty persistent group id=%d",
4455 persistent_go->id);
4456 wpas_notify_persistent_group_removed(wpa_s,
4457 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004458 wpa_config_remove_network(wpa_s->conf,
4459 persistent_go->id);
4460 }
4461
4462 wpa_msg_global(wpa_s, MSG_INFO,
4463 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4464 " status=%d"
4465 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004466 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004467 MAC2STR(dev), status,
4468 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004469 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004470 return;
4471 }
4472
4473 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004474 if (persist_ssid && persist_ssid_size)
4475 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4476 persist_ssid_size);
4477
4478 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4479 is_zero_ether_addr(grp_mac)) {
4480 wpa_dbg(wpa_s, MSG_ERROR,
4481 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4482 return;
4483 }
4484
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004485 for (;;) {
4486 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4487 if (!stale)
4488 break;
4489
4490 if (s && s->ssid_len == stale->ssid_len &&
4491 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4492 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4493 break;
4494
4495 /* Remove stale persistent group */
4496 if (stale->mode != WPAS_MODE_P2P_GO ||
4497 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004498 wpa_dbg(wpa_s, MSG_DEBUG,
4499 "P2P: Remove stale persistent group id=%d",
4500 stale->id);
4501 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004502 wpa_config_remove_network(wpa_s->conf, stale->id);
4503 } else {
4504 size_t i;
4505
4506 for (i = 0; i < stale->num_p2p_clients; i++) {
4507 if (os_memcmp(stale->p2p_client_list +
4508 i * ETH_ALEN,
4509 dev, ETH_ALEN) == 0) {
4510 os_memmove(stale->p2p_client_list +
4511 i * ETH_ALEN,
4512 stale->p2p_client_list +
4513 (i + 1) * ETH_ALEN,
4514 (stale->num_p2p_clients -
4515 i - 1) * ETH_ALEN);
4516 break;
4517 }
4518 }
4519 stale->num_p2p_clients--;
4520 }
4521 save_config = 1;
4522 }
4523
4524 if (save_config)
4525 p2p_config_write(wpa_s);
4526
4527 if (s) {
4528 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4529 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4530
4531 if (persistent_go && s != persistent_go &&
4532 !persistent_go->num_p2p_clients) {
4533 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004534 wpa_dbg(wpa_s, MSG_DEBUG,
4535 "P2P: Remove empty persistent group id=%d",
4536 persistent_go->id);
4537 wpas_notify_persistent_group_removed(wpa_s,
4538 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004539 wpa_config_remove_network(wpa_s->conf,
4540 persistent_go->id);
4541 /* Save config */
4542 }
4543
4544 wpa_msg_global(wpa_s, MSG_INFO,
4545 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4546 " status=%d"
4547 " adv_id=%x adv_mac=" MACSTR
4548 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004549 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004550 MAC2STR(dev), status,
4551 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004552 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004553 return;
4554 }
4555
Hai Shalom5f92bc92019-04-18 11:54:11 -07004556 wpa_s->global->pending_p2ps_group = 0;
4557 wpa_s->global->pending_p2ps_group_freq = 0;
4558
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004559 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004560 /*
4561 * We need to copy the interface name. Simply saving a
4562 * pointer isn't enough, since if we use pending_interface_name
4563 * it will be overwritten when the group is added.
4564 */
4565 char go_ifname[100];
4566
4567 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004568 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004569 if (!response_done) {
4570 wpa_s->global->pending_p2ps_group = 1;
4571 wpa_s->global->pending_p2ps_group_freq = freq;
4572 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004573
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004574 if (!wpas_p2p_create_iface(wpa_s))
4575 os_memcpy(go_ifname, wpa_s->ifname,
4576 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004577 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004578 os_memcpy(go_ifname,
4579 wpa_s->pending_interface_name,
4580 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004581
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004582 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004583 wpas_p2ps_prov_complete(
4584 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4585 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004586 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004587 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4588 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004589 return;
4590 }
4591
4592 /* If PD Resp complete, start up the GO */
4593 if (response_done && persistent_go) {
4594 wpas_p2p_group_add_persistent(
4595 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004596 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004597 persistent_go->mode ==
4598 WPAS_MODE_P2P_GO ?
4599 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08004600 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004601 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004602 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004603 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004604 }
4605
4606 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004607 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4608 ETH_ALEN);
4609 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004610 }
4611 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004612 os_memcpy(go_ifname, go_wpa_s->ifname,
4613 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004614
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004615 if (is_zero_ether_addr(grp_mac)) {
4616 wpa_dbg(go_wpa_s, MSG_DEBUG,
4617 "P2P: Setting PIN-1 for ANY");
4618 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4619 "12345670", NULL, 0,
4620 0);
4621 } else {
4622 wpa_dbg(go_wpa_s, MSG_DEBUG,
4623 "P2P: Setting PIN-1 for " MACSTR,
4624 MAC2STR(grp_mac));
4625 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4626 "12345670", NULL, 0,
4627 0);
4628 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004629
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004630 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4631 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004632 }
4633
4634 wpa_msg_global(wpa_s, MSG_INFO,
4635 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4636 " status=%d conncap=%x"
4637 " adv_id=%x adv_mac=" MACSTR
4638 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004639 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004640 MAC2STR(dev), status, conncap,
4641 adv_id, MAC2STR(adv_mac),
4642 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004643 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004644 return;
4645 }
4646
4647 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4648 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4649
4650 if (persistent_go && !persistent_go->num_p2p_clients) {
4651 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004652 wpa_dbg(wpa_s, MSG_DEBUG,
4653 "P2P: Remove empty persistent group id=%d",
4654 persistent_go->id);
4655 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004656 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4657 }
4658
4659 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004660 char ssid_hex[32 * 2 + 1];
4661
4662 if (group_ssid)
4663 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4664 group_ssid, group_ssid_len);
4665 else
4666 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004667 wpa_msg_global(wpa_s, MSG_INFO,
4668 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4669 " status=%d conncap=%x"
4670 " adv_id=%x adv_mac=" MACSTR
4671 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004672 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004673 MAC2STR(dev), status, conncap,
4674 adv_id, MAC2STR(adv_mac),
4675 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004676 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4677 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004678 } else {
4679 wpa_msg_global(wpa_s, MSG_INFO,
4680 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4681 " status=%d conncap=%x"
4682 " adv_id=%x adv_mac=" MACSTR
4683 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004684 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004685 MAC2STR(dev), status, conncap,
4686 adv_id, MAC2STR(adv_mac),
4687 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004688 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004689 }
4690}
4691
4692
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004693static int _wpas_p2p_in_progress(void *ctx)
4694{
4695 struct wpa_supplicant *wpa_s = ctx;
4696 return wpas_p2p_in_progress(wpa_s);
4697}
4698
4699
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004700static int wpas_prov_disc_resp_cb(void *ctx)
4701{
4702 struct wpa_supplicant *wpa_s = ctx;
4703 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004704 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004705
4706 if (!wpa_s->global->pending_p2ps_group)
4707 return 0;
4708
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004709 freq = wpa_s->global->pending_p2ps_group_freq;
4710 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004711 wpa_s->global->pending_p2ps_group = 0;
4712
4713 if (wpas_p2p_get_go_group(wpa_s))
4714 return 0;
4715 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4716
4717 if (persistent_go) {
4718 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004719 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4720 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004721 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004722 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
4723 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004724 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004725 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4726 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004727 }
4728
4729 return 1;
4730}
4731
4732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004733static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4734 unsigned int *len,
4735 unsigned int *freq_list)
4736{
4737 struct wpa_supplicant *wpa_s = ctx;
4738
4739 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4740 WPA_IF_P2P_CLIENT, len, freq_list);
4741}
4742
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004743int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4744{
4745 u8 addr[ETH_ALEN] = {0};
4746
4747 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4748 return 0;
4749
Hai Shalom60840252021-02-19 19:02:11 -08004750 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4751 if (is_zero_ether_addr(
4752 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4753 !is_zero_ether_addr(wpa_s->own_addr)) {
4754 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4755 wpa_s->own_addr, ETH_ALEN);
4756 }
4757 return 0;
4758 }
4759
4760 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004761 if (random_mac_addr(addr) < 0) {
4762 wpa_msg(wpa_s, MSG_INFO,
4763 "Failed to generate random MAC address");
4764 return -EINVAL;
4765 }
4766
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004767 /* Store generated MAC address. */
4768 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4769 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004770 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004771 /* If there are existing saved groups, restore last MAC address.
4772 * if there is no last used MAC address, the last one is
4773 * factory MAC. */
4774 if (is_zero_ether_addr(
4775 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004776 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004777 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4778 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004779 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4780 }
4781
4782 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4783 wpa_msg(wpa_s, MSG_INFO,
4784 "Failed to set random MAC address");
4785 return -EINVAL;
4786 }
4787
4788 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4789 wpa_msg(wpa_s, MSG_INFO,
4790 "Could not update MAC address information");
4791 return -EINVAL;
4792 }
4793
4794 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4795 MAC2STR(addr));
4796
4797 return 0;
4798}
4799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800/**
4801 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4802 * @global: Pointer to global data from wpa_supplicant_init()
4803 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4804 * Returns: 0 on success, -1 on failure
4805 */
4806int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4807{
4808 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809 int i;
4810
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004811 if (wpa_s->conf->p2p_disabled)
4812 return 0;
4813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4815 return 0;
4816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004817 if (global->p2p)
4818 return 0;
4819
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004820 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4821 wpa_msg(wpa_s, MSG_ERROR,
4822 "Failed to initialize P2P random MAC address.");
4823 return -1;
4824 }
4825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004828 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829 p2p.p2p_scan = wpas_p2p_scan;
4830 p2p.send_action = wpas_send_action;
4831 p2p.send_action_done = wpas_send_action_done;
4832 p2p.go_neg_completed = wpas_go_neg_completed;
4833 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4834 p2p.dev_found = wpas_dev_found;
4835 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004836 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 p2p.start_listen = wpas_start_listen;
4838 p2p.stop_listen = wpas_stop_listen;
4839 p2p.send_probe_resp = wpas_send_probe_resp;
4840 p2p.sd_request = wpas_sd_request;
4841 p2p.sd_response = wpas_sd_response;
4842 p2p.prov_disc_req = wpas_prov_disc_req;
4843 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004844 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 p2p.invitation_process = wpas_invitation_process;
4846 p2p.invitation_received = wpas_invitation_received;
4847 p2p.invitation_result = wpas_invitation_result;
4848 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004849 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004850 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004851 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004852 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004853 p2p.get_persistent_group = wpas_get_persistent_group;
4854 p2p.get_go_info = wpas_get_go_info;
4855 p2p.remove_stale_groups = wpas_remove_stale_groups;
4856 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4857 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4858 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004859 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004860 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004861
4862 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004863 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 p2p.dev_name = wpa_s->conf->device_name;
4865 p2p.manufacturer = wpa_s->conf->manufacturer;
4866 p2p.model_name = wpa_s->conf->model_name;
4867 p2p.model_number = wpa_s->conf->model_number;
4868 p2p.serial_number = wpa_s->conf->serial_number;
4869 if (wpa_s->wps) {
4870 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4871 p2p.config_methods = wpa_s->wps->config_methods;
4872 }
4873
Hai Shalom60840252021-02-19 19:02:11 -08004874 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4875 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004876 wpa_printf(MSG_ERROR,
4877 "P2P: Failed to configure supported channel list");
4878 return -1;
4879 }
4880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 if (wpa_s->conf->p2p_listen_reg_class &&
4882 wpa_s->conf->p2p_listen_channel) {
4883 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4884 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004885 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004886 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 /*
4888 * Pick one of the social channels randomly as the listen
4889 * channel.
4890 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004891 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004892 &p2p.channel,
4893 &global->p2p_go_avoid_freq,
4894 &global->p2p_disallow_freq) !=
4895 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004896 wpa_printf(MSG_INFO,
4897 "P2P: No social channels supported by the driver - do not enable P2P");
4898 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004899 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004900 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004902 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4903 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904
4905 if (wpa_s->conf->p2p_oper_reg_class &&
4906 wpa_s->conf->p2p_oper_channel) {
4907 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4908 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4909 p2p.cfg_op_channel = 1;
4910 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4911 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4912
4913 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004915 * Use random operation channel from 2.4 GHz band social
4916 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4917 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004919 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004920 &p2p.op_channel, NULL,
4921 NULL) != 0) {
4922 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004923 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004924 p2p.op_reg_class = 0;
4925 p2p.op_channel = 0;
4926 /* This will be overridden during group setup in
4927 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929 p2p.cfg_op_channel = 0;
4930 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4931 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4932 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004933
4934 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4935 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4936 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4937 }
4938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4940 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4941 p2p.country[2] = 0x04;
4942 } else
4943 os_memcpy(p2p.country, "XX\x04", 3);
4944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4946 WPS_DEV_TYPE_LEN);
4947
4948 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4949 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4950 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4951
4952 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4953 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4954
4955 p2p.max_peers = 100;
4956
4957 if (wpa_s->conf->p2p_ssid_postfix) {
4958 p2p.ssid_postfix_len =
4959 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4960 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4961 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4962 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4963 p2p.ssid_postfix_len);
4964 }
4965
4966 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4967
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004968 p2p.max_listen = wpa_s->max_remain_on_chan;
4969
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004970 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4971 wpa_s->conf->p2p_passphrase_len <= 63)
4972 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4973 else
4974 p2p.passphrase_len = 8;
4975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976 global->p2p = p2p_init(&p2p);
4977 if (global->p2p == NULL)
4978 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004979 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980
4981 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4982 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4983 continue;
4984 p2p_add_wps_vendor_extension(
4985 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4986 }
4987
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004988 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 return 0;
4991}
4992
4993
4994/**
4995 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4996 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4997 *
4998 * This function deinitialize per-interface P2P data.
4999 */
5000void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5001{
5002 if (wpa_s->driver && wpa_s->drv_priv)
5003 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005004
5005 if (wpa_s->go_params) {
5006 /* Clear any stored provisioning info */
5007 p2p_clear_provisioning_info(
5008 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005009 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005010 }
5011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005012 os_free(wpa_s->go_params);
5013 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005014 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5016 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005017 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5019 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5020 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005021 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005022 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005023 wpas_p2p_listen_work_done(wpa_s);
5024 if (wpa_s->p2p_send_action_work) {
5025 os_free(wpa_s->p2p_send_action_work->ctx);
5026 radio_work_done(wpa_s->p2p_send_action_work);
5027 wpa_s->p2p_send_action_work = NULL;
5028 }
5029 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005031 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5032 wpa_s->p2p_oob_dev_pw = NULL;
5033
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005034 os_free(wpa_s->p2p_group_common_freqs);
5035 wpa_s->p2p_group_common_freqs = NULL;
5036 wpa_s->p2p_group_common_freqs_num = 0;
5037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 /* TODO: remove group interface from the driver if this wpa_s instance
5039 * is on top of a P2P group interface */
5040}
5041
5042
5043/**
5044 * wpas_p2p_deinit_global - Deinitialize global P2P module
5045 * @global: Pointer to global data from wpa_supplicant_init()
5046 *
5047 * This function deinitializes the global (per device) P2P module.
5048 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005049static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005050{
5051 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005052
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005053 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005054
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005055 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056
5057 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5059 wpa_s = wpa_s->next;
5060 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 tmp = global->ifaces;
5062 while (tmp &&
5063 (tmp == wpa_s ||
5064 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5065 tmp = tmp->next;
5066 }
5067 if (tmp == NULL)
5068 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005069 /* Disconnect from the P2P group and deinit the interface */
5070 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071 }
5072
5073 /*
5074 * Deinit GO data on any possibly remaining interface (if main
5075 * interface is used as GO).
5076 */
5077 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5078 if (wpa_s->ap_iface)
5079 wpas_p2p_group_deinit(wpa_s);
5080 }
5081
5082 p2p_deinit(global->p2p);
5083 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005084 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085}
5086
5087
5088static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5089{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005090 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005091 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 if (wpa_s->drv_flags &
5093 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5094 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5095 return 1; /* P2P group requires a new interface in every case
5096 */
5097 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5098 return 0; /* driver does not support concurrent operations */
5099 if (wpa_s->global->ifaces->next)
5100 return 1; /* more that one interface already in use */
5101 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5102 return 1; /* this interface is already in use */
5103 return 0;
5104}
5105
5106
5107static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5108 const u8 *peer_addr,
5109 enum p2p_wps_method wps_method,
5110 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005111 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005112 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005113{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005114 if (persistent_group && wpa_s->conf->persistent_reconnect)
5115 persistent_group = 2;
5116
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005117 /*
5118 * Increase GO config timeout if HT40 is used since it takes some time
5119 * to scan channels for coex purposes before the BSS can be started.
5120 */
5121 p2p_set_config_timeout(wpa_s->global->p2p,
5122 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005124 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5125 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005126 persistent_group, ssid ? ssid->ssid : NULL,
5127 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005128 wpa_s->p2p_pd_before_go_neg, pref_freq,
5129 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5130 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131}
5132
5133
5134static int wpas_p2p_auth_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 Shmidt8d520ff2011-05-09 14:06:53 -07005144 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5145 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005146 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005147 ssid ? ssid->ssid_len : 0, pref_freq,
5148 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5149 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150}
5151
5152
5153static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5154{
5155 wpa_s->p2p_join_scan_count++;
5156 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5157 wpa_s->p2p_join_scan_count);
5158 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5159 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5160 " for join operationg - stop join attempt",
5161 MAC2STR(wpa_s->pending_join_iface_addr));
5162 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005163 if (wpa_s->p2p_auto_pd) {
5164 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005165 wpa_msg_global(wpa_s, MSG_INFO,
5166 P2P_EVENT_PROV_DISC_FAILURE
5167 " p2p_dev_addr=" MACSTR " status=N/A",
5168 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005169 return;
5170 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005171 if (wpa_s->p2p_fallback_to_go_neg) {
5172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5173 "failed - fall back to GO Negotiation");
5174 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5175 P2P_EVENT_FALLBACK_TO_GO_NEG
5176 "reason=join-failed");
5177 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5178 return;
5179 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005180 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005181 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005182 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 }
5184}
5185
5186
Dmitry Shmidt04949592012-07-19 12:16:46 -07005187static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5188{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005189 int res;
5190 unsigned int num, i;
5191 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005192
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005193 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5194 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005195 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005196 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005197
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005198 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5199 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005200 if (!freqs)
5201 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005202
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005203 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5204 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005205
5206 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005207 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005208 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5209 freq);
5210 res = 0;
5211 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005212 }
5213 }
5214
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005215 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005216 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005217
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005218exit_free:
5219 os_free(freqs);
5220 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005221}
5222
5223
5224static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5225 const u8 *peer_dev_addr)
5226{
5227 struct wpa_bss *bss;
5228 int updated;
5229
5230 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5231 if (bss == NULL)
5232 return -1;
5233 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5234 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5235 "last scan");
5236 return 0;
5237 }
5238
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005239 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5240 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005241 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5242 "%ld.%06ld (%supdated in last scan)",
5243 bss->last_update.sec, bss->last_update.usec,
5244 updated ? "": "not ");
5245
5246 return updated;
5247}
5248
5249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005250static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5251 struct wpa_scan_results *scan_res)
5252{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005253 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005254 int freq;
5255 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5258
5259 if (wpa_s->global->p2p_disabled)
5260 return;
5261
Dmitry Shmidt04949592012-07-19 12:16:46 -07005262 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5263 scan_res ? (int) scan_res->num : -1,
5264 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265
5266 if (scan_res)
5267 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5268
Dmitry Shmidt04949592012-07-19 12:16:46 -07005269 if (wpa_s->p2p_auto_pd) {
5270 int join = wpas_p2p_peer_go(wpa_s,
5271 wpa_s->pending_join_dev_addr);
5272 if (join == 0 &&
5273 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5274 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005275 bss = wpa_bss_get_bssid_latest(
5276 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005277 if (bss) {
5278 freq = bss->freq;
5279 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5280 "the peer " MACSTR " at %d MHz",
5281 wpa_s->auto_pd_scan_retry,
5282 MAC2STR(wpa_s->
5283 pending_join_dev_addr),
5284 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005285 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005286 return;
5287 }
5288 }
5289
5290 if (join < 0)
5291 join = 0;
5292
5293 wpa_s->p2p_auto_pd = 0;
5294 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5295 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5296 MAC2STR(wpa_s->pending_join_dev_addr), join);
5297 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005298 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005299 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005300 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005301 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005302 wpa_msg_global(wpa_s, MSG_INFO,
5303 P2P_EVENT_PROV_DISC_FAILURE
5304 " p2p_dev_addr=" MACSTR " status=N/A",
5305 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005306 }
5307 return;
5308 }
5309
5310 if (wpa_s->p2p_auto_join) {
5311 int join = wpas_p2p_peer_go(wpa_s,
5312 wpa_s->pending_join_dev_addr);
5313 if (join < 0) {
5314 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5315 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005316 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005317 P2P_EVENT_FALLBACK_TO_GO_NEG
5318 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5320 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5321 wpa_s->p2p_persistent_group, 0, 0, 0,
5322 wpa_s->p2p_go_intent,
5323 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005324 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005325 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005326 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005327 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005328 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005329 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005330 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005331 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005332 NULL, 0,
5333 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334 return;
5335 }
5336
5337 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5338 "try to join the group", join ? "" :
5339 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005340 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005341 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005342 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005343 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005344 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005345 }
5346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5348 wpa_s->pending_join_iface_addr);
5349 if (freq < 0 &&
5350 p2p_get_interface_addr(wpa_s->global->p2p,
5351 wpa_s->pending_join_dev_addr,
5352 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005353 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5354 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5356 "address for join from " MACSTR " to " MACSTR
5357 " based on newly discovered P2P peer entry",
5358 MAC2STR(wpa_s->pending_join_iface_addr),
5359 MAC2STR(iface_addr));
5360 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5361 ETH_ALEN);
5362
5363 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5364 wpa_s->pending_join_iface_addr);
5365 }
5366 if (freq >= 0) {
5367 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5368 "from P2P peer table: %d MHz", freq);
5369 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005370 if (wpa_s->p2p_join_ssid_len) {
5371 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5372 MACSTR " and SSID %s",
5373 MAC2STR(wpa_s->pending_join_iface_addr),
5374 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5375 wpa_s->p2p_join_ssid_len));
5376 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5377 wpa_s->p2p_join_ssid,
5378 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005379 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005380 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5381 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5382 bss = wpa_bss_get_bssid_latest(wpa_s,
5383 wpa_s->pending_join_iface_addr);
5384 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005386 u8 dev_addr[ETH_ALEN];
5387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388 freq = bss->freq;
5389 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005390 "from BSS table: %d MHz (SSID %s)", freq,
5391 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005392 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005393 dev_addr) == 0 &&
5394 os_memcmp(wpa_s->pending_join_dev_addr,
5395 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5396 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5397 ETH_ALEN) != 0) {
5398 wpa_printf(MSG_DEBUG,
5399 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5400 MAC2STR(dev_addr),
5401 MAC2STR(wpa_s->pending_join_dev_addr));
5402 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5403 ETH_ALEN);
5404 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005405 }
5406 if (freq > 0) {
5407 u16 method;
5408
Dmitry Shmidt04949592012-07-19 12:16:46 -07005409 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005410 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005411 P2P_EVENT_GROUP_FORMATION_FAILURE
5412 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005413 wpas_notify_p2p_group_formation_failure(
5414 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005415 return;
5416 }
5417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005418 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5419 "prior to joining an existing group (GO " MACSTR
5420 " freq=%u MHz)",
5421 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5422 wpa_s->pending_pd_before_join = 1;
5423
5424 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425 case WPS_PIN_DISPLAY:
5426 method = WPS_CONFIG_KEYPAD;
5427 break;
5428 case WPS_PIN_KEYPAD:
5429 method = WPS_CONFIG_DISPLAY;
5430 break;
5431 case WPS_PBC:
5432 method = WPS_CONFIG_PUSHBUTTON;
5433 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005434 case WPS_P2PS:
5435 method = WPS_CONFIG_P2PS;
5436 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437 default:
5438 method = 0;
5439 break;
5440 }
5441
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005442 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5443 wpa_s->pending_join_dev_addr) ==
5444 method)) {
5445 /*
5446 * We have already performed provision discovery for
5447 * joining the group. Proceed directly to join
5448 * operation without duplicated provision discovery. */
5449 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5450 "with " MACSTR " already done - proceed to "
5451 "join",
5452 MAC2STR(wpa_s->pending_join_dev_addr));
5453 wpa_s->pending_pd_before_join = 0;
5454 goto start;
5455 }
5456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005457 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005458 wpa_s->pending_join_dev_addr,
5459 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005460 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5462 "Discovery Request before joining an "
5463 "existing group");
5464 wpa_s->pending_pd_before_join = 0;
5465 goto start;
5466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 return;
5468 }
5469
5470 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5471 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5472 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5473 wpas_p2p_check_join_scan_limit(wpa_s);
5474 return;
5475
5476start:
5477 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005478 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5479 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480}
5481
5482
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005483static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5484 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486 int ret;
5487 struct wpa_driver_scan_params params;
5488 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005489 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005490 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005491 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492
5493 os_memset(&params, 0, sizeof(params));
5494
5495 /* P2P Wildcard SSID */
5496 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005497 if (ssid && ssid_len) {
5498 params.ssids[0].ssid = ssid;
5499 params.ssids[0].ssid_len = ssid_len;
5500 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5501 wpa_s->p2p_join_ssid_len = ssid_len;
5502 } else {
5503 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5504 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5505 wpa_s->p2p_join_ssid_len = 0;
5506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507
5508 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005509 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5510 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5511 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512 if (wps_ie == NULL) {
5513 wpas_p2p_scan_res_join(wpa_s, NULL);
5514 return;
5515 }
5516
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005517 if (!freq) {
5518 int oper_freq;
5519 /*
5520 * If freq is not provided, check the operating freq of the GO
5521 * and use a single channel scan on if possible.
5522 */
5523 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5524 wpa_s->pending_join_iface_addr);
5525 if (oper_freq > 0)
5526 freq = oper_freq;
5527 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005528 if (freq > 0) {
5529 freqs[0] = freq;
5530 params.freqs = freqs;
Hai Shalomc1a21442022-02-04 13:43:00 -08005531 } else if (wpa_s->conf->p2p_6ghz_disable ||
5532 !is_p2p_allow_6ghz(wpa_s->global->p2p)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07005533 wpa_printf(MSG_DEBUG,
5534 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005535 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5536 0);
5537 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5538 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005539 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005540
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005541 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5542 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5543 if (ies == NULL) {
5544 wpabuf_free(wps_ie);
5545 wpas_p2p_scan_res_join(wpa_s, NULL);
5546 return;
5547 }
5548 wpabuf_put_buf(ies, wps_ie);
5549 wpabuf_free(wps_ie);
5550
5551 bands = wpas_get_bands(wpa_s, freqs);
5552 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5553
5554 params.p2p_probe = 1;
5555 params.extra_ies = wpabuf_head(ies);
5556 params.extra_ies_len = wpabuf_len(ies);
5557
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005558 if (wpa_s->clear_driver_scan_cache) {
5559 wpa_printf(MSG_DEBUG,
5560 "Request driver to clear scan cache due to local BSS flush");
5561 params.only_new_results = 1;
5562 }
5563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564 /*
5565 * Run a scan to update BSS table and start Provision Discovery once
5566 * the new scan results become available.
5567 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005568 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005569 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005570 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005571 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005572 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005573 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005574 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005575 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005576 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577
5578 wpabuf_free(ies);
5579
5580 if (ret) {
5581 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5582 "try again later");
5583 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5584 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5585 wpas_p2p_check_join_scan_limit(wpa_s);
5586 }
5587}
5588
5589
Dmitry Shmidt04949592012-07-19 12:16:46 -07005590static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5591{
5592 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005593 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005594}
5595
5596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005597static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005598 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005599 int auto_join, int op_freq,
5600 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601{
5602 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005603 MACSTR " dev " MACSTR " op_freq=%d)%s",
5604 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005605 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005606 if (ssid && ssid_len) {
5607 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5608 wpa_ssid_txt(ssid, ssid_len));
5609 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610
Dmitry Shmidt04949592012-07-19 12:16:46 -07005611 wpa_s->p2p_auto_pd = 0;
5612 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5614 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5615 wpa_s->pending_join_wps_method = wps_method;
5616
5617 /* Make sure we are not running find during connection establishment */
5618 wpas_p2p_stop_find(wpa_s);
5619
5620 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005621 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 return 0;
5623}
5624
5625
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005626static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5627 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628{
5629 struct wpa_supplicant *group;
5630 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005631 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632
5633 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5634 if (group == NULL)
5635 return -1;
5636 if (group != wpa_s) {
5637 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5638 sizeof(group->p2p_pin));
5639 group->p2p_wps_method = wpa_s->p2p_wps_method;
5640 }
5641
Hai Shalom74f70d42019-02-11 14:42:39 -08005642 /*
5643 * Need to mark the current interface for p2p_group_formation
5644 * when a separate group interface is not used. This is needed
5645 * to allow p2p_cancel stop a pending p2p_connect-join.
5646 * wpas_p2p_init_group_interface() addresses this for the case
5647 * where a separate group interface is used.
5648 */
5649 if (group == wpa_s->parent)
5650 wpa_s->global->p2p_group_formation = group;
5651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005653 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654
5655 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005656 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005657 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5658 ETH_ALEN);
5659 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005660 if (freq && ssid && ssid_len) {
5661 res.freq = freq;
5662 res.ssid_len = ssid_len;
5663 os_memcpy(res.ssid, ssid, ssid_len);
5664 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005665 if (ssid && ssid_len) {
5666 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5667 ssid, ssid_len);
5668 } else {
5669 bss = wpa_bss_get_bssid_latest(
5670 wpa_s, wpa_s->pending_join_iface_addr);
5671 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005672 if (bss) {
5673 res.freq = bss->freq;
5674 res.ssid_len = bss->ssid_len;
5675 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5676 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5677 bss->freq,
5678 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005679 } else if (ssid && ssid_len) {
5680 res.ssid_len = ssid_len;
5681 os_memcpy(res.ssid, ssid, ssid_len);
5682 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5683 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005684 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005685 }
5686
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005687 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5688 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5689 "starting client");
5690 wpa_drv_cancel_remain_on_channel(wpa_s);
5691 wpa_s->off_channel_freq = 0;
5692 wpa_s->roc_waiting_drv_freq = 0;
5693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694 wpas_start_wps_enrollee(group, &res);
5695
5696 /*
5697 * Allow a longer timeout for join-a-running-group than normal 15
5698 * second group formation timeout since the GO may not have authorized
5699 * our connection yet.
5700 */
5701 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5702 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5703 wpa_s, NULL);
5704
5705 return 0;
5706}
5707
5708
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005709static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005710 int *force_freq, int *pref_freq, int go,
5711 unsigned int *pref_freq_list,
5712 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005713{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005714 struct wpa_used_freq_data *freqs;
5715 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005716 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305717 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005718
5719 max_pref_freq = *num_pref_freq;
5720 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005721
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005722 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5723 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005724 if (!freqs)
5725 return -1;
5726
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005727 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5728 wpa_s->num_multichan_concurrent);
5729
5730 /*
5731 * It is possible that the total number of used frequencies is bigger
5732 * than the number of frequencies used for P2P, so get the system wide
5733 * number of unused frequencies.
5734 */
5735 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5736
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005737 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005738 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5739 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005740
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005741 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005742 int ret;
5743 if (go)
5744 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5745 else
5746 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5747 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005748 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005749 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5750 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005751 /*
5752 * If freq is a DFS channel and DFS is offloaded
5753 * to the driver, allow P2P GO to use it.
5754 */
5755 wpa_printf(MSG_DEBUG,
5756 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5757 freq);
5758 } else {
5759 wpa_printf(MSG_DEBUG,
5760 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5761 freq);
5762 res = -3;
5763 goto exit_free;
5764 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005765 }
5766
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005767 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005768 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005769 freq_in_use = 1;
5770 }
5771
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005772 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005773 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5774 freq);
5775 res = -2;
5776 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005777 }
5778 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5779 "requested channel (%u MHz)", freq);
5780 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005781 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005782 }
5783
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005784 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005785
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305786 if (*pref_freq == 0) {
5787 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005788 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305789 while (i < wpa_s->conf->num_p2p_pref_chan) {
5790 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5791 wpa_s->conf->p2p_pref_chan[i].op_class,
5792 wpa_s->conf->p2p_pref_chan[i].chan);
5793
5794 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5795 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5796 best_freq = p2p_pref_freq;
5797 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5798 "from P2P preferred channel list", best_freq);
5799 break;
5800 } else {
5801 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5802 "frequency (%u MHz) ", p2p_pref_freq);
5803 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005804 i++;
5805 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305806 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305807 enum wpa_driver_if_type iface_type;
5808
5809 if (go)
5810 iface_type = WPA_IF_P2P_GO;
5811 else
5812 iface_type = WPA_IF_P2P_CLIENT;
5813
5814 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5815 best_freq, go);
5816
5817 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5818 &max_pref_freq,
5819 pref_freq_list);
5820 if (!res && max_pref_freq > 0) {
5821 *num_pref_freq = max_pref_freq;
5822 i = 0;
5823 while (i < *num_pref_freq &&
5824 (!p2p_supported_freq(wpa_s->global->p2p,
5825 pref_freq_list[i]) ||
5826 wpas_p2p_disallowed_freq(wpa_s->global,
5827 pref_freq_list[i]))) {
5828 wpa_printf(MSG_DEBUG,
5829 "P2P: preferred_freq_list[%d]=%d is disallowed",
5830 i, pref_freq_list[i]);
5831 i++;
5832 }
5833 if (i != *num_pref_freq) {
5834 best_freq = pref_freq_list[i];
5835 wpa_printf(MSG_DEBUG,
5836 "P2P: Using preferred_freq_list[%d]=%d",
5837 i, best_freq);
5838 } else {
5839 wpa_printf(MSG_DEBUG,
5840 "P2P: All driver preferred frequencies are "
5841 "disallowed for P2P use");
5842 *num_pref_freq = 0;
5843 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005844 } else {
5845 wpa_printf(MSG_DEBUG,
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305846 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005847 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005848 }
5849 }
5850
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005851 /* We have a candidate frequency to use */
5852 if (best_freq > 0) {
5853 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005854 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005855 best_freq);
5856 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005857 } else {
5858 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 -07005859 best_freq);
5860 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005861 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005862 } else if (num_unused > 0) {
5863 wpa_printf(MSG_DEBUG,
5864 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5865 *force_freq = 0;
5866 } else {
5867 wpa_printf(MSG_DEBUG,
5868 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5869 res = -2;
5870 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005871 }
5872
5873exit_ok:
5874 res = 0;
5875exit_free:
5876 os_free(freqs);
5877 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005878}
5879
5880
Hai Shalomc1a21442022-02-04 13:43:00 -08005881static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5882 const u8 *peer_addr)
5883{
5884 if (wpa_s->conf->p2p_6ghz_disable ||
5885 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5886 HOSTAPD_MODE_IEEE80211A, true))
5887 return false;
5888
5889 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5890 return false;
5891 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5892 return false;
5893
5894 return true;
5895}
5896
5897
5898static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5899 const u8 *peer_addr, bool allow_6ghz, int freq)
5900{
5901 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5902 wpa_printf(MSG_DEBUG,
5903 "P2P: Allow connection on 6 GHz channels");
5904 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5905 } else {
5906 if (is_6ghz_freq(freq))
5907 return -2;
5908 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5909 }
5910
5911 return 0;
5912}
5913
5914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915/**
5916 * wpas_p2p_connect - Request P2P Group Formation to be started
5917 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5918 * @peer_addr: Address of the peer P2P Device
5919 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5920 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005921 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005922 * @join: Whether to join an existing group (as a client) instead of starting
5923 * Group Owner negotiation; @peer_addr is BSSID in that case
5924 * @auth: Whether to only authorize the connection instead of doing that and
5925 * initiating Group Owner negotiation
5926 * @go_intent: GO Intent or -1 to use default
5927 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005928 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005929 * @persistent_id: Persistent group credentials to use for forcing GO
5930 * parameters or -1 to generate new values (SSID/passphrase)
5931 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5932 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005933 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005934 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005935 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005936 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005937 * @group_ssid: Specific Group SSID for join or %NULL if not set
5938 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08005939 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005940 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5941 * failure, -2 on failure due to channel not currently available,
5942 * -3 if forced channel is not supported
5943 */
5944int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5945 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005946 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005947 int go_intent, int freq, unsigned int vht_center_freq2,
5948 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005949 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005950 const u8 *group_ssid, size_t group_ssid_len,
5951 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005952{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005953 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005954 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005955 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005956 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005957 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005958 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005959
5960 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5961 return -1;
5962
Dmitry Shmidt04949592012-07-19 12:16:46 -07005963 if (persistent_id >= 0) {
5964 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5965 if (ssid == NULL || ssid->disabled != 2 ||
5966 ssid->mode != WPAS_MODE_P2P_GO)
5967 return -1;
5968 }
5969
Hai Shalomc1a21442022-02-04 13:43:00 -08005970 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07005971 return -2;
5972
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005973 os_free(wpa_s->global->add_psk);
5974 wpa_s->global->add_psk = NULL;
5975
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005976 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005977 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005978 wpa_s->global->pending_p2ps_group_freq = 0;
5979 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981 if (go_intent < 0)
5982 go_intent = wpa_s->conf->p2p_go_intent;
5983
5984 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005985 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986
5987 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005988 wpa_s->p2p_persistent_group = !!persistent_group;
5989 wpa_s->p2p_persistent_id = persistent_id;
5990 wpa_s->p2p_go_intent = go_intent;
5991 wpa_s->p2p_connect_freq = freq;
5992 wpa_s->p2p_fallback_to_go_neg = 0;
5993 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005994 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005995 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005996 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5997 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005998 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005999 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000
6001 if (pin)
6002 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6003 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006004 if (wps_generate_pin((unsigned int *) &ret) < 0)
6005 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006006 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6007 "%08d", ret);
6008 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6009 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6011 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006012 } else if (wps_method == WPS_P2PS) {
6013 /* Force the P2Ps default PIN to be used */
6014 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015 } else
6016 wpa_s->p2p_pin[0] = '\0';
6017
Dmitry Shmidt04949592012-07-19 12:16:46 -07006018 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006019 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6020 if (auth) {
6021 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6022 "connect a running group from " MACSTR,
6023 MAC2STR(peer_addr));
6024 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6025 return ret;
6026 }
6027 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6028 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6029 iface_addr) < 0) {
6030 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6031 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6032 dev_addr);
6033 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006034 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006035 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006036 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6037 "%ld.%06ld",
6038 wpa_s->p2p_auto_started.sec,
6039 wpa_s->p2p_auto_started.usec);
6040 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006041 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006042 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006043 auto_join, freq,
6044 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006045 return -1;
6046 return ret;
6047 }
6048
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006049 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006050 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006051 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006052 if (res)
6053 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006054 wpas_p2p_set_own_freq_preference(wpa_s,
6055 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006057 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006059 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006061 if (wpa_s->create_p2p_iface) {
6062 /* Prepare to add a new interface for the group */
6063 iftype = WPA_IF_P2P_GROUP;
6064 if (go_intent == 15)
6065 iftype = WPA_IF_P2P_GO;
6066 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6067 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6068 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006071
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006072 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006073 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006074 if (wpa_s->p2p_mgmt)
6075 if_addr = wpa_s->parent->own_addr;
6076 else
6077 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006078 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006080
6081 if (auth) {
6082 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006083 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006084 force_freq, persistent_group, ssid,
6085 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086 return -1;
6087 return ret;
6088 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006089
6090 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6091 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006092 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006093 if (wpa_s->create_p2p_iface)
6094 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006095 return -1;
6096 }
6097 return ret;
6098}
6099
6100
6101/**
6102 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6103 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6104 * @freq: Frequency of the channel in MHz
6105 * @duration: Duration of the stay on the channel in milliseconds
6106 *
6107 * This callback is called when the driver indicates that it has started the
6108 * requested remain-on-channel duration.
6109 */
6110void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6111 unsigned int freq, unsigned int duration)
6112{
6113 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6114 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006115 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)",
6116 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6117 wpa_s->roc_waiting_drv_freq, freq, duration);
6118 if (wpa_s->off_channel_freq &&
6119 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6121 wpa_s->pending_listen_duration);
6122 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006123 } else {
6124 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6125 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6126 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006127 }
6128}
6129
6130
Jithu Jance57cea1a2014-08-20 10:22:04 -07006131int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006132{
6133 /* Limit maximum Listen state time based on driver limitation. */
6134 if (timeout > wpa_s->max_remain_on_chan)
6135 timeout = wpa_s->max_remain_on_chan;
6136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006137 return p2p_listen(wpa_s->global->p2p, timeout);
6138}
6139
6140
6141/**
6142 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6143 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6144 * @freq: Frequency of the channel in MHz
6145 *
6146 * This callback is called when the driver indicates that a remain-on-channel
6147 * operation has been completed, i.e., the duration on the requested channel
6148 * has timed out.
6149 */
6150void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6151 unsigned int freq)
6152{
6153 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6154 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006155 wpa_s->global->p2p_long_listen,
6156 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006157 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6159 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006160 if (wpa_s->global->p2p_long_listen > 0)
6161 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6163 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006164 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006165 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006166 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006167 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006168 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006169 } else {
6170 /*
6171 * When listen duration is over, stop listen & update p2p_state
6172 * to IDLE.
6173 */
6174 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006175 }
6176}
6177
6178
6179/**
6180 * wpas_p2p_group_remove - Remove a P2P group
6181 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6182 * @ifname: Network interface name of the group interface or "*" to remove all
6183 * groups
6184 * Returns: 0 on success, -1 on failure
6185 *
6186 * This function is used to remove a P2P group. This can be used to disconnect
6187 * from a group in which the local end is a P2P Client or to end a P2P Group in
6188 * case the local end is the Group Owner. If a virtual network interface was
6189 * created for this group, that interface will be removed. Otherwise, only the
6190 * configured P2P group network will be removed from the interface.
6191 */
6192int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6193{
6194 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006195 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006196
6197 if (os_strcmp(ifname, "*") == 0) {
6198 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006199 bool calling_wpa_s_group_removed = false;
6200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201 wpa_s = global->ifaces;
6202 while (wpa_s) {
6203 prev = wpa_s;
6204 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006205 if (prev->p2p_group_interface !=
6206 NOT_P2P_GROUP_INTERFACE ||
6207 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006208 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006209 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006210 if (prev == calling_wpa_s)
6211 calling_wpa_s_group_removed = true;
6212 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006213 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006214
6215 if (!calling_wpa_s_group_removed &&
6216 (calling_wpa_s->p2p_group_interface !=
6217 NOT_P2P_GROUP_INTERFACE ||
6218 (calling_wpa_s->current_ssid &&
6219 calling_wpa_s->current_ssid->p2p_group))) {
6220 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6221 wpas_p2p_disconnect_safely(calling_wpa_s,
6222 calling_wpa_s);
6223 }
6224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225 return 0;
6226 }
6227
6228 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6229 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6230 break;
6231 }
6232
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006233 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006234}
6235
6236
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006237static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6238{
6239 unsigned int r;
6240
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006241 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6242 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
6243 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
6244 int res;
6245
6246 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6247 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006248 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6249 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6250
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006251 if (!res && size > 0) {
6252 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006253 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006254 (!p2p_supported_freq(wpa_s->global->p2p,
6255 pref_freq_list[i]) ||
6256 wpas_p2p_disallowed_freq(wpa_s->global,
6257 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006258 wpa_printf(MSG_DEBUG,
6259 "P2P: preferred_freq_list[%d]=%d is disallowed",
6260 i, pref_freq_list[i]);
6261 i++;
6262 }
6263 if (i != size) {
6264 freq = pref_freq_list[i];
6265 wpa_printf(MSG_DEBUG,
6266 "P2P: Using preferred_freq_list[%d]=%d",
6267 i, freq);
6268 } else {
6269 wpa_printf(MSG_DEBUG,
6270 "P2P: All driver preferred frequencies are disallowed for P2P use");
6271 }
6272 } else {
6273 wpa_printf(MSG_DEBUG,
6274 "P2P: No preferred frequency list available");
6275 }
6276 }
6277
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006278 if (freq == 2) {
6279 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6280 "band");
6281 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006282 p2p_supported_freq_go(wpa_s->global->p2p,
6283 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006284 freq = wpa_s->best_24_freq;
6285 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6286 "channel: %d MHz", freq);
6287 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006288 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6289 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006290 int possible_2g_freqs[] = {
6291 /* operating class 81 */
6292 2412, 2437, 2462,
6293 };
6294 int possible_2g_freqs_num =
6295 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6296 int i;
6297 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6298 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6299 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6300 break;
6301 }
6302 }
6303
6304 if (i >= possible_2g_freqs_num) {
6305 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6306 "2.4 GHz channel for P2P group");
6307 return -1;
6308 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006309 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6310 "channel: %d MHz", freq);
6311 }
6312 }
6313
6314 if (freq == 5) {
6315 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6316 "band");
6317 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006318 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006319 wpa_s->best_5_freq)) {
6320 freq = wpa_s->best_5_freq;
6321 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6322 "channel: %d MHz", freq);
6323 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006324 const int freqs[] = {
6325 /* operating class 115 */
6326 5180, 5200, 5220, 5240,
6327 /* operating class 124 */
6328 5745, 5765, 5785, 5805,
6329 };
6330 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6331
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006332 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6333 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006334
6335 /*
6336 * most of 5G channels are DFS, only operating class 115 and 124
6337 * are available possibly, randomly pick a start to check them.
6338 */
6339 int possible_5g_freqs[] = {
6340 /* operating class 115 */
6341 5180, 5200, 5220, 5240,
6342 /* operating class 124 */
6343 5745, 5765, 5785, 5805,
6344 };
6345 int possible_5g_freqs_num =
6346 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6347
Jimmy Chen64b90632020-09-29 17:27:34 +08006348 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6349 if (p2p_supported_freq_go(
6350 wpa_s->global->p2p,
6351 possible_5g_freqs[r % possible_5g_freqs_num])) {
6352 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6353 break;
6354 }
6355 }
6356
6357 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006358 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6359 "5 GHz channel for P2P group");
6360 return -1;
6361 }
6362 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6363 "channel: %d MHz", freq);
6364 }
6365 }
6366
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006367 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006368 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006369 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6370 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006371 /*
6372 * If freq is a DFS channel and DFS is offloaded to the
6373 * driver, allow P2P GO to use it.
6374 */
6375 wpa_printf(MSG_DEBUG, "P2P: "
6376 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6377 __func__, freq);
6378 return freq;
6379 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006380 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6381 "(%u MHz) is not supported for P2P uses",
6382 freq);
6383 return -1;
6384 }
6385
6386 return freq;
6387}
6388
6389
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006390static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6391 const struct p2p_channels *channels,
6392 int freq)
6393{
6394 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6395 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6396 freq_included(wpa_s, channels, freq))
6397 return 1;
6398 return 0;
6399}
6400
6401
6402static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6403 struct p2p_go_neg_results *params,
6404 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006405{
6406 unsigned int i, r;
6407
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006408 /* try all channels in operating class 115 */
6409 for (i = 0; i < 4; i++) {
6410 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006411 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006412 goto out;
6413 }
6414
6415 /* try all channels in operating class 124 */
6416 for (i = 0; i < 4; i++) {
6417 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006418 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006419 goto out;
6420 }
6421
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006422 /* try social channel class 180 channel 2 */
6423 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006424 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006425 goto out;
6426
6427 /* try all channels in reg. class 180 */
6428 for (i = 0; i < 4; i++) {
6429 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006430 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006431 goto out;
6432 }
6433
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006434 /* try some random selection of the social channels */
6435 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6436 return;
6437
6438 for (i = 0; i < 3; i++) {
6439 params->freq = 2412 + ((r + i) % 3) * 25;
6440 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6441 goto out;
6442 }
6443
6444 /* try all other channels in operating class 81 */
6445 for (i = 0; i < 11; i++) {
6446 params->freq = 2412 + i * 5;
6447
6448 /* skip social channels; covered in the previous loop */
6449 if (params->freq == 2412 ||
6450 params->freq == 2437 ||
6451 params->freq == 2462)
6452 continue;
6453
6454 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6455 goto out;
6456 }
6457
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006458 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006459 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006460 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006461out:
6462 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6463 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006464}
6465
6466
Roshan Pius3a1667e2018-07-03 15:17:14 -07006467static int wpas_same_band(int freq1, int freq2)
6468{
6469 enum hostapd_hw_mode mode1, mode2;
6470 u8 chan1, chan2;
6471
6472 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6473 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6474 if (mode1 == NUM_HOSTAPD_MODES)
6475 return 0;
6476 return mode1 == mode2;
6477}
6478
6479
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006480static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6481 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006482 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006483 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006484 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006485 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006486{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006487 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006488 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006489 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006490 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006491 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006492
6493 os_memset(params, 0, sizeof(*params));
6494 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006495 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006496 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006497 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006498 params->max_oper_chwidth = max_oper_chwidth;
6499 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006500 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006501
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006502 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6503 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006504 if (!freqs)
6505 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006506
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006507 num = get_shared_radio_freqs_data(wpa_s, freqs,
6508 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006509
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006510 if (wpa_s->current_ssid &&
6511 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6512 wpa_s->wpa_state == WPA_COMPLETED) {
6513 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6514 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006515
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006516 /*
6517 * If the frequency selection is done for an active P2P GO that
6518 * is not sharing a frequency, allow to select a new frequency
6519 * even if there are no unused frequencies as we are about to
6520 * move the P2P GO so its frequency can be re-used.
6521 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006522 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006523 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6524 freqs[i].flags == 0) {
6525 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006526 break;
6527 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006528 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006529 }
6530
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006531 /* Try to use EDMG channel */
6532 if (params->edmg) {
6533 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6534 goto success;
6535 params->edmg = 0;
6536 }
6537
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006538 /* try using the forced freq */
6539 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006540 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6541 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006542 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006543 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006544 freq);
6545 goto fail;
6546 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006547 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6548 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006549 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6550 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006551 /*
6552 * If freq is a DFS channel and DFS is offloaded
6553 * to the driver, allow P2P GO to use it.
6554 */
6555 wpa_printf(MSG_DEBUG,
6556 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6557 __func__, freq);
6558 } else {
6559 wpa_printf(MSG_DEBUG,
6560 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6561 freq);
6562 goto fail;
6563 }
6564 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006565
6566 for (i = 0; i < num; i++) {
6567 if (freqs[i].freq == freq) {
6568 wpa_printf(MSG_DEBUG,
6569 "P2P: forced freq (%d MHz) is also shared",
6570 freq);
6571 params->freq = freq;
6572 goto success;
6573 }
6574 }
6575
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006576 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006577 wpa_printf(MSG_DEBUG,
6578 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6579 freq);
6580 goto fail;
6581 }
6582
6583 wpa_printf(MSG_DEBUG,
6584 "P2P: force GO freq (%d MHz) on a free channel",
6585 freq);
6586 params->freq = freq;
6587 goto success;
6588 }
6589
6590 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006591 if (num &&
6592 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006593 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6594 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6595 wpa_printf(MSG_DEBUG,
6596 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006597 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006598 params->freq = cand;
6599 goto success;
6600 }
6601
6602 /* try using one of the shared freqs */
6603 for (i = 0; i < num; i++) {
6604 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6605 freqs[i].freq)) {
6606 wpa_printf(MSG_DEBUG,
6607 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006608 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006609 params->freq = freqs[i].freq;
6610 goto success;
6611 }
6612 }
6613 }
6614
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006615 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006616 wpa_printf(MSG_DEBUG,
6617 "P2P: Cannot force GO on any of the channels we are already using");
6618 goto fail;
6619 }
6620
6621 /* try using the setting from the configuration file */
6622 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6623 wpa_s->conf->p2p_oper_channel >= 1 &&
6624 wpa_s->conf->p2p_oper_channel <= 11 &&
6625 wpas_p2p_supported_freq_go(
6626 wpa_s, channels,
6627 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6628 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6629 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6630 "frequency %d MHz", params->freq);
6631 goto success;
6632 }
6633
6634 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6635 wpa_s->conf->p2p_oper_reg_class == 116 ||
6636 wpa_s->conf->p2p_oper_reg_class == 117 ||
6637 wpa_s->conf->p2p_oper_reg_class == 124 ||
6638 wpa_s->conf->p2p_oper_reg_class == 125 ||
6639 wpa_s->conf->p2p_oper_reg_class == 126 ||
6640 wpa_s->conf->p2p_oper_reg_class == 127) &&
6641 wpas_p2p_supported_freq_go(wpa_s, channels,
6642 5000 +
6643 5 * wpa_s->conf->p2p_oper_channel)) {
6644 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6645 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6646 "frequency %d MHz", params->freq);
6647 goto success;
6648 }
6649
6650 /* Try using best channels */
6651 if (wpa_s->conf->p2p_oper_channel == 0 &&
6652 wpa_s->best_overall_freq > 0 &&
6653 wpas_p2p_supported_freq_go(wpa_s, channels,
6654 wpa_s->best_overall_freq)) {
6655 params->freq = wpa_s->best_overall_freq;
6656 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6657 "channel %d MHz", params->freq);
6658 goto success;
6659 }
6660
6661 if (wpa_s->conf->p2p_oper_channel == 0 &&
6662 wpa_s->best_24_freq > 0 &&
6663 wpas_p2p_supported_freq_go(wpa_s, channels,
6664 wpa_s->best_24_freq)) {
6665 params->freq = wpa_s->best_24_freq;
6666 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6667 "channel %d MHz", params->freq);
6668 goto success;
6669 }
6670
6671 if (wpa_s->conf->p2p_oper_channel == 0 &&
6672 wpa_s->best_5_freq > 0 &&
6673 wpas_p2p_supported_freq_go(wpa_s, channels,
6674 wpa_s->best_5_freq)) {
6675 params->freq = wpa_s->best_5_freq;
6676 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6677 "channel %d MHz", params->freq);
6678 goto success;
6679 }
6680
6681 /* try using preferred channels */
6682 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6683 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6684 params->freq = cand;
6685 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6686 "channels", params->freq);
6687 goto success;
6688 }
6689
Roshan Pius3a1667e2018-07-03 15:17:14 -07006690 /* Try using a channel that allows VHT to be used with 80 MHz */
6691 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6692 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6693 enum hostapd_hw_mode mode;
6694 struct hostapd_hw_modes *hwmode;
6695 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006696 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006697
6698 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006699 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006700 mode = ieee80211_freq_to_chan(cand, &chan);
6701 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006702 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006703 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006704 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6705 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006706 continue;
6707 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6708 params->freq = cand;
6709 wpa_printf(MSG_DEBUG,
6710 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6711 params->freq);
6712 goto success;
6713 }
6714 }
6715 }
6716
6717 /* Try using a channel that allows HT to be used with 40 MHz on the same
6718 * band so that CSA can be used */
6719 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6720 wpa_s->p2p_group_common_freqs) {
6721 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6722 enum hostapd_hw_mode mode;
6723 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006724 u8 chan, op_class;
6725 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006726
Hai Shalomc1a21442022-02-04 13:43:00 -08006727 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006728 cand = wpa_s->p2p_group_common_freqs[i];
6729 mode = ieee80211_freq_to_chan(cand, &chan);
6730 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006731 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006732 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6733 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006734 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006735 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006736 if (is_6ghz &&
6737 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6738 BW40) == ALLOWED)
6739 supported = true;
6740
6741 if (!is_6ghz &&
6742 ieee80211_freq_to_channel_ext(
6743 cand, -1, CHANWIDTH_USE_HT, &op_class,
6744 &chan) != NUM_HOSTAPD_MODES &&
6745 wpas_p2p_verify_channel(
6746 wpa_s, hwmode, op_class, chan,
6747 BW40MINUS) == ALLOWED)
6748 supported = true;
6749
6750 if (!supported && !is_6ghz &&
6751 ieee80211_freq_to_channel_ext(
6752 cand, 1, CHANWIDTH_USE_HT, &op_class,
6753 &chan) != NUM_HOSTAPD_MODES &&
6754 wpas_p2p_verify_channel(
6755 wpa_s, hwmode, op_class, chan,
6756 BW40PLUS) == ALLOWED)
6757 supported = true;
6758
6759 if (!supported)
6760 continue;
6761
Roshan Pius3a1667e2018-07-03 15:17:14 -07006762 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6763 params->freq = cand;
6764 wpa_printf(MSG_DEBUG,
6765 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6766 params->freq);
6767 goto success;
6768 }
6769 }
6770 }
6771
6772 /* Try using one of the group common freqs on the same band so that CSA
6773 * can be used */
6774 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6775 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6776 cand = wpa_s->p2p_group_common_freqs[i];
6777 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6778 cand))
6779 continue;
6780 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6781 params->freq = cand;
6782 wpa_printf(MSG_DEBUG,
6783 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6784 params->freq);
6785 goto success;
6786 }
6787 }
6788 }
6789
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006790 /* Try using one of the group common freqs */
6791 if (wpa_s->p2p_group_common_freqs) {
6792 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6793 cand = wpa_s->p2p_group_common_freqs[i];
6794 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6795 params->freq = cand;
6796 wpa_printf(MSG_DEBUG,
6797 "P2P: Use freq %d MHz common with the peer",
6798 params->freq);
6799 goto success;
6800 }
6801 }
6802 }
6803
6804 /* no preference, select some channel */
6805 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6806
6807 if (params->freq == 0) {
6808 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6809 goto fail;
6810 }
6811
6812success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006813 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006814 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006815fail:
6816 os_free(freqs);
6817 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006818}
6819
6820
6821static struct wpa_supplicant *
6822wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6823 int go)
6824{
6825 struct wpa_supplicant *group_wpa_s;
6826
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006827 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006828 if (wpa_s->p2p_mgmt) {
6829 /*
6830 * We may be called on the p2p_dev interface which
6831 * cannot be used for group operations, so always use
6832 * the primary interface.
6833 */
6834 wpa_s->parent->p2pdev = wpa_s;
6835 wpa_s = wpa_s->parent;
6836 }
6837 wpa_dbg(wpa_s, MSG_DEBUG,
6838 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006839 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006840 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006841 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006842 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844
6845 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006846 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006847 wpa_msg_global(wpa_s, MSG_ERROR,
6848 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006849 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006850 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006851 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6852 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006853 wpa_msg_global(wpa_s, MSG_ERROR,
6854 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855 wpas_p2p_remove_pending_group_interface(wpa_s);
6856 return NULL;
6857 }
6858
Roshan Pius3a1667e2018-07-03 15:17:14 -07006859 if (go && wpa_s->p2p_go_do_acs) {
6860 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6861 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6862 wpa_s->p2p_go_do_acs = 0;
6863 }
6864
Hai Shalomc1a21442022-02-04 13:43:00 -08006865 if (go && wpa_s->p2p_go_allow_dfs) {
6866 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6867 wpa_s->p2p_go_allow_dfs = 0;
6868 }
6869
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006870 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6871 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006872 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006873 return group_wpa_s;
6874}
6875
6876
6877/**
6878 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6879 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6880 * @persistent_group: Whether to create a persistent group
6881 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006882 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006883 * @ht40: Start GO with 40 MHz channel width
6884 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006885 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006886 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006887 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006888 * Returns: 0 on success, -1 on failure
6889 *
6890 * This function creates a new P2P group with the local end as the Group Owner,
6891 * i.e., without using Group Owner Negotiation.
6892 */
6893int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006894 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006895 int max_oper_chwidth, int he, int edmg,
6896 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006897{
6898 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006899
6900 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6901 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006902 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6903 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006904
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006905 os_free(wpa_s->global->add_psk);
6906 wpa_s->global->add_psk = NULL;
6907
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006908 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006909 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006910 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006911
Roshan Pius3a1667e2018-07-03 15:17:14 -07006912 if (!wpa_s->p2p_go_do_acs) {
6913 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6914 if (freq < 0)
6915 return -1;
6916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006918 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006919 ht40, vht, max_oper_chwidth, he, edmg,
6920 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006921 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006923 p2p_go_params(wpa_s->global->p2p, &params);
6924 params.persistent_group = persistent_group;
6925
6926 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6927 if (wpa_s == NULL)
6928 return -1;
6929 wpas_start_wps_go(wpa_s, &params, 0);
6930
6931 return 0;
6932}
6933
6934
6935static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006936 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006937 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938{
6939 struct wpa_ssid *ssid;
6940
6941 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6942 if (wpa_s == NULL)
6943 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006944 if (force_scan)
6945 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006946 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006947
6948 wpa_supplicant_ap_deinit(wpa_s);
6949
6950 ssid = wpa_config_add_network(wpa_s->conf);
6951 if (ssid == NULL)
6952 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006953 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006954 wpa_config_set_network_defaults(ssid);
6955 ssid->temporary = 1;
6956 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006957 ssid->pbss = params->pbss;
6958 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6959 WPA_CIPHER_CCMP;
6960 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6962 ssid->ssid = os_malloc(params->ssid_len);
6963 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006964 wpa_config_remove_network(wpa_s->conf, ssid->id);
6965 return -1;
6966 }
6967 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6968 ssid->ssid_len = params->ssid_len;
6969 ssid->p2p_group = 1;
6970 ssid->export_keys = 1;
6971 if (params->psk_set) {
6972 os_memcpy(ssid->psk, params->psk, 32);
6973 ssid->psk_set = 1;
6974 }
6975 if (params->passphrase)
6976 ssid->passphrase = os_strdup(params->passphrase);
6977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006978 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006979 wpa_s->p2p_in_invitation = 1;
6980 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006981 wpa_s->p2p_go_group_formation_completed = 0;
6982 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006984 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006985 NULL);
6986 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6987 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006988 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006989 wpa_supplicant_select_network(wpa_s, ssid);
6990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006991 return 0;
6992}
6993
6994
6995int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6996 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006997 int force_freq, int neg_freq,
6998 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006999 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007000 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007001 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007002 int connection_timeout, int force_scan,
7003 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004{
7005 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007006 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007007
7008 if (ssid->disabled != 2 || ssid->ssid == NULL)
7009 return -1;
7010
7011 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7012 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7013 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7014 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007015 if (go == 0 &&
7016 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007017 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007018 /*
7019 * This can happen if Invitation Response frame was lost
7020 * and the peer (GO of a persistent group) tries to
7021 * invite us again. Reschedule the timeout to avoid
7022 * terminating the wait for the connection too early
7023 * since we now know that the peer is still trying to
7024 * invite us instead of having already started the GO.
7025 */
7026 wpa_printf(MSG_DEBUG,
7027 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7028 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7029 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007030 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007032 return 0;
7033 }
7034
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007035 os_free(wpa_s->global->add_psk);
7036 wpa_s->global->add_psk = NULL;
7037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007039 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007040
Dmitry Shmidt04949592012-07-19 12:16:46 -07007041 wpa_s->p2p_fallback_to_go_neg = 0;
7042
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007043 if (ssid->mode == WPAS_MODE_P2P_GO) {
7044 if (force_freq > 0) {
7045 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7046 if (freq < 0)
7047 return -1;
7048 } else {
7049 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7050 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007051 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007052 freq = 0;
7053 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007054 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007055 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007056 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007057 struct os_reltime now;
7058 struct wpa_bss *bss =
7059 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007060
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007061 os_get_reltime(&now);
7062 if (bss &&
7063 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007064 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007065 freq = bss->freq;
7066 else
7067 freq = 0;
7068 }
7069
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007070 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
7071 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007072 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007073 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007074 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007075
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007076 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007077 ht40, vht, max_oper_chwidth, he, edmg,
7078 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007079 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080
7081 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007082 params.psk_set = ssid->psk_set;
7083 if (params.psk_set)
7084 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007085 if (ssid->passphrase) {
7086 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7087 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7088 "persistent group");
7089 return -1;
7090 }
7091 os_strlcpy(params.passphrase, ssid->passphrase,
7092 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7095 params.ssid_len = ssid->ssid_len;
7096 params.persistent_group = 1;
7097
7098 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7099 if (wpa_s == NULL)
7100 return -1;
7101
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007102 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7103
Dmitry Shmidt56052862013-10-04 10:23:25 -07007104 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007105 wpas_start_wps_go(wpa_s, &params, 0);
7106
7107 return 0;
7108}
7109
7110
7111static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7112 struct wpabuf *proberesp_ies)
7113{
7114 struct wpa_supplicant *wpa_s = ctx;
7115 if (wpa_s->ap_iface) {
7116 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007117 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7118 wpabuf_free(beacon_ies);
7119 wpabuf_free(proberesp_ies);
7120 return;
7121 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007122 if (beacon_ies) {
7123 wpabuf_free(hapd->p2p_beacon_ie);
7124 hapd->p2p_beacon_ie = beacon_ies;
7125 }
7126 wpabuf_free(hapd->p2p_probe_resp_ie);
7127 hapd->p2p_probe_resp_ie = proberesp_ies;
7128 } else {
7129 wpabuf_free(beacon_ies);
7130 wpabuf_free(proberesp_ies);
7131 }
7132 wpa_supplicant_ap_update_beacon(wpa_s);
7133}
7134
7135
7136static void wpas_p2p_idle_update(void *ctx, int idle)
7137{
7138 struct wpa_supplicant *wpa_s = ctx;
7139 if (!wpa_s->ap_iface)
7140 return;
7141 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007142 if (idle) {
7143 if (wpa_s->global->p2p_fail_on_wps_complete &&
7144 wpa_s->p2p_in_provisioning) {
7145 wpas_p2p_grpform_fail_after_wps(wpa_s);
7146 return;
7147 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007149 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007150 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7151}
7152
7153
7154struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007155 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007156{
7157 struct p2p_group *group;
7158 struct p2p_group_config *cfg;
7159
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007160 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7161 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007162 return NULL;
7163
7164 cfg = os_zalloc(sizeof(*cfg));
7165 if (cfg == NULL)
7166 return NULL;
7167
Dmitry Shmidt04949592012-07-19 12:16:46 -07007168 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007169 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007170 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007171 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007172 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7173 if (wpa_s->max_stations &&
7174 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7175 cfg->max_clients = wpa_s->max_stations;
7176 else
7177 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007178 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7179 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007180 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007181 cfg->cb_ctx = wpa_s;
7182 cfg->ie_update = wpas_p2p_ie_update;
7183 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007184 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7185 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007186
7187 group = p2p_group_init(wpa_s->global->p2p, cfg);
7188 if (group == NULL)
7189 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007190 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007191 p2p_group_notif_formation_done(group);
7192 wpa_s->p2p_group = group;
7193 return group;
7194}
7195
7196
7197void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7198 int registrar)
7199{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007200 struct wpa_ssid *ssid = wpa_s->current_ssid;
7201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007202 if (!wpa_s->p2p_in_provisioning) {
7203 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7204 "provisioning not in progress");
7205 return;
7206 }
7207
Dmitry Shmidt04949592012-07-19 12:16:46 -07007208 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7209 u8 go_dev_addr[ETH_ALEN];
7210 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7211 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7212 ssid->ssid_len);
7213 /* Clear any stored provisioning info */
7214 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7215 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007216
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007217 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007218 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007219 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007220 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7221 /*
7222 * Use a separate timeout for initial data connection to
7223 * complete to allow the group to be removed automatically if
7224 * something goes wrong in this step before the P2P group idle
7225 * timeout mechanism is taken into use.
7226 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007227 wpa_dbg(wpa_s, MSG_DEBUG,
7228 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7229 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007230 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7231 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007232 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007233 /* Complete group formation on successful data connection. */
7234 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007235 } else if (ssid) {
7236 /*
7237 * Use a separate timeout for initial data connection to
7238 * complete to allow the group to be removed automatically if
7239 * the client does not complete data connection successfully.
7240 */
7241 wpa_dbg(wpa_s, MSG_DEBUG,
7242 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7243 P2P_MAX_INITIAL_CONN_WAIT_GO);
7244 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7245 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007246 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007247 /*
7248 * Complete group formation on first successful data connection
7249 */
7250 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007251 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007252 if (wpa_s->global->p2p)
7253 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007254 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007255}
7256
7257
Jouni Malinen75ecf522011-06-27 15:19:46 -07007258void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7259 struct wps_event_fail *fail)
7260{
7261 if (!wpa_s->p2p_in_provisioning) {
7262 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7263 "provisioning not in progress");
7264 return;
7265 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007266
7267 if (wpa_s->go_params) {
7268 p2p_clear_provisioning_info(
7269 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007270 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007271 }
7272
Jouni Malinen75ecf522011-06-27 15:19:46 -07007273 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007274
7275 if (wpa_s == wpa_s->global->p2p_group_formation) {
7276 /*
7277 * Allow some time for the failed WPS negotiation exchange to
7278 * complete, but remove the group since group formation cannot
7279 * succeed after provisioning failure.
7280 */
7281 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7282 wpa_s->global->p2p_fail_on_wps_complete = 1;
7283 eloop_deplete_timeout(0, 50000,
7284 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007285 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007286 }
7287}
7288
7289
7290int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7291{
7292 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7293 !wpa_s->p2p_in_provisioning)
7294 return 0;
7295
7296 wpas_p2p_grpform_fail_after_wps(wpa_s);
7297
7298 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007299}
7300
7301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007302int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007303 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007304 enum wpas_p2p_prov_disc_use use,
7305 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007306{
7307 u16 config_methods;
7308
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007309 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007310 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007311 wpa_s->p2p_fallback_to_go_neg = 0;
7312 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007313 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7314 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007315 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7316 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7317
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007318 wpa_printf(MSG_DEBUG,
7319 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7320 __func__, p2ps_prov->conncap,
7321 p2ps_prov->adv_id, p2ps_prov->conncap,
7322 p2ps_prov->status, p2ps_prov->info);
7323
7324 config_methods = 0;
7325 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007326 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007327 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007328 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007329 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7330 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007331 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007332 else {
7333 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007334 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007335 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007337
Dmitry Shmidt04949592012-07-19 12:16:46 -07007338 if (use == WPAS_P2P_PD_AUTO) {
7339 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7340 wpa_s->pending_pd_config_methods = config_methods;
7341 wpa_s->p2p_auto_pd = 1;
7342 wpa_s->p2p_auto_join = 0;
7343 wpa_s->pending_pd_before_join = 0;
7344 wpa_s->auto_pd_scan_retry = 0;
7345 wpas_p2p_stop_find(wpa_s);
7346 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007347 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007348 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7349 wpa_s->p2p_auto_started.sec,
7350 wpa_s->p2p_auto_started.usec);
7351 wpas_p2p_join_scan(wpa_s, NULL);
7352 return 0;
7353 }
7354
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007355 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7356 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007357 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007360 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007361 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007362 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363}
7364
7365
7366int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7367 char *end)
7368{
7369 return p2p_scan_result_text(ies, ies_len, buf, end);
7370}
7371
7372
7373static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7374{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007375 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007376 return;
7377
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007378 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007379 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007380 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7381 wpa_s, NULL);
7382 offchannel_send_action_done(wpa_s);
7383 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7386 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007387 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007388}
7389
7390
7391int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7392 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007393 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007394 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007395 u8 seek_cnt, const char **seek_string, int freq,
7396 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007397{
7398 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007399 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007400
Dmitry Shmidt04949592012-07-19 12:16:46 -07007401 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007402 wpa_s->p2p_in_provisioning) {
7403 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7404 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7405 " (P2P disabled)" : "",
7406 wpa_s->p2p_in_provisioning ?
7407 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007410
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007411 wpa_supplicant_cancel_sched_scan(wpa_s);
7412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007413 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007414 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007415 search_delay, seek_cnt, seek_string, freq,
7416 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007417}
7418
7419
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007420static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7421 struct wpa_scan_results *scan_res)
7422{
7423 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7424
7425 if (wpa_s->p2p_scan_work) {
7426 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7427 wpa_s->p2p_scan_work = NULL;
7428 radio_work_done(work);
7429 }
7430
7431 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7432 return;
7433
7434 /*
7435 * Indicate that results have been processed so that the P2P module can
7436 * continue pending tasks.
7437 */
Hai Shalom60840252021-02-19 19:02:11 -08007438 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007439}
7440
7441
7442static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007443{
7444 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007445 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7447 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007448
7449 if (wpa_s->global->p2p)
7450 p2p_stop_find(wpa_s->global->p2p);
7451
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007452 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7453 wpa_printf(MSG_DEBUG,
7454 "P2P: Do not consider the scan results after stop_find");
7455 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7456 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007457}
7458
7459
7460void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7461{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007462 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007463 if (!wpa_s->global->pending_group_iface_for_p2ps)
7464 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007465}
7466
7467
7468static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7469{
7470 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007471 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007472}
7473
7474
7475int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7476{
7477 int res;
7478
7479 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7480 return -1;
7481
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007482 if (wpa_s->p2p_lo_started) {
7483 wpa_printf(MSG_DEBUG,
7484 "P2P: Cannot start P2P listen, it is offloaded");
7485 return -1;
7486 }
7487
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007488 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007489 wpas_p2p_clear_pending_action_tx(wpa_s);
7490
7491 if (timeout == 0) {
7492 /*
7493 * This is a request for unlimited Listen state. However, at
7494 * least for now, this is mapped to a Listen state for one
7495 * hour.
7496 */
7497 timeout = 3600;
7498 }
7499 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007500 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007501
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007502 /*
7503 * Stop previous find/listen operation to avoid trying to request a new
7504 * remain-on-channel operation while the driver is still running the
7505 * previous one.
7506 */
7507 if (wpa_s->global->p2p)
7508 p2p_stop_find(wpa_s->global->p2p);
7509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007510 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7511 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007512 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007513 eloop_register_timeout(timeout, 0,
7514 wpas_p2p_long_listen_timeout,
7515 wpa_s, NULL);
7516 }
7517
7518 return res;
7519}
7520
7521
7522int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7523 u8 *buf, size_t len, int p2p_group)
7524{
7525 struct wpabuf *p2p_ie;
7526 int ret;
7527
7528 if (wpa_s->global->p2p_disabled)
7529 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007530 /*
7531 * Advertize mandatory cross connection capability even on
7532 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7533 */
7534 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007535 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536 if (wpa_s->global->p2p == NULL)
7537 return -1;
7538 if (bss == NULL)
7539 return -1;
7540
7541 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7542 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7543 p2p_group, p2p_ie);
7544 wpabuf_free(p2p_ie);
7545
7546 return ret;
7547}
7548
7549
7550int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007551 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007552 const u8 *ie, size_t ie_len,
7553 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554{
7555 if (wpa_s->global->p2p_disabled)
7556 return 0;
7557 if (wpa_s->global->p2p == NULL)
7558 return 0;
7559
Dmitry Shmidt04949592012-07-19 12:16:46 -07007560 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007561 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007562 case P2P_PREQ_NOT_P2P:
7563 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7564 ssi_signal);
7565 /* fall through */
7566 case P2P_PREQ_MALFORMED:
7567 case P2P_PREQ_NOT_LISTEN:
7568 case P2P_PREQ_NOT_PROCESSED:
7569 default: /* make gcc happy */
7570 return 0;
7571 case P2P_PREQ_PROCESSED:
7572 return 1;
7573 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007574}
7575
7576
7577void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7578 const u8 *sa, const u8 *bssid,
7579 u8 category, const u8 *data, size_t len, int freq)
7580{
7581 if (wpa_s->global->p2p_disabled)
7582 return;
7583 if (wpa_s->global->p2p == NULL)
7584 return;
7585
7586 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7587 freq);
7588}
7589
7590
7591void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7592{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007593 unsigned int bands;
7594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595 if (wpa_s->global->p2p_disabled)
7596 return;
7597 if (wpa_s->global->p2p == NULL)
7598 return;
7599
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007600 bands = wpas_get_bands(wpa_s, NULL);
7601 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007602}
7603
7604
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007605static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606{
7607 p2p_group_deinit(wpa_s->p2p_group);
7608 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007609
7610 wpa_s->ap_configured_cb = NULL;
7611 wpa_s->ap_configured_cb_ctx = NULL;
7612 wpa_s->ap_configured_cb_data = NULL;
7613 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007614}
7615
7616
7617int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7618{
Hai Shalomfdcde762020-04-02 11:19:20 -07007619 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007621 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7622 return -1;
7623
7624 return p2p_reject(wpa_s->global->p2p, addr);
7625}
7626
7627
7628/* Invite to reinvoke a persistent group */
7629int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007630 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007631 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007632 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007633{
7634 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007635 u8 *bssid = NULL;
7636 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007637 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007638 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007639 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007640
Hai Shalomc1a21442022-02-04 13:43:00 -08007641 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7642 return -1;
7643
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007644 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007645 if (peer_addr)
7646 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7647 else
7648 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007649
Jouni Malinen31be0a42012-08-31 21:20:51 +03007650 wpa_s->p2p_persistent_go_freq = freq;
7651 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007652 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007653 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007654 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7655 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007656 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007657 if (ssid->mode == WPAS_MODE_P2P_GO) {
7658 role = P2P_INVITE_ROLE_GO;
7659 if (peer_addr == NULL) {
7660 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7661 "address in invitation command");
7662 return -1;
7663 }
7664 if (wpas_p2p_create_iface(wpa_s)) {
7665 if (wpas_p2p_add_group_interface(wpa_s,
7666 WPA_IF_P2P_GO) < 0) {
7667 wpa_printf(MSG_ERROR, "P2P: Failed to "
7668 "allocate a new interface for the "
7669 "group");
7670 return -1;
7671 }
7672 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007673 } else if (wpa_s->p2p_mgmt)
7674 bssid = wpa_s->parent->own_addr;
7675 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007676 bssid = wpa_s->own_addr;
7677 } else {
7678 role = P2P_INVITE_ROLE_CLIENT;
7679 peer_addr = ssid->bssid;
7680 }
7681 wpa_s->pending_invite_ssid_id = ssid->id;
7682
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007683 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007684 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007685 role == P2P_INVITE_ROLE_GO,
7686 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007687 if (res)
7688 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007690 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7691 return -1;
7692
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007693 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7694
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007695 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7696 no_pref_freq_given && pref_freq > 0 &&
7697 wpa_s->num_multichan_concurrent > 1 &&
7698 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7699 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7700 pref_freq);
7701 pref_freq = 0;
7702 }
7703
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007704 /*
7705 * Stop any find/listen operations before invitation and possibly
7706 * connection establishment.
7707 */
7708 wpas_p2p_stop_find_oper(wpa_s);
7709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007710 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007711 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007712 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007713}
7714
7715
7716/* Invite to join an active group */
7717int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007718 const u8 *peer_addr, const u8 *go_dev_addr,
7719 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007720{
7721 struct wpa_global *global = wpa_s->global;
7722 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007723 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007724 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007725 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007726 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007727 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007728 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007729
Jouni Malinen31be0a42012-08-31 21:20:51 +03007730 wpa_s->p2p_persistent_go_freq = 0;
7731 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007732 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007733 wpa_s->p2p_go_vht_center_freq2 = 0;
7734 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007735 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007737 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7738 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7739 break;
7740 }
7741 if (wpa_s == NULL) {
7742 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7743 return -1;
7744 }
7745
7746 ssid = wpa_s->current_ssid;
7747 if (ssid == NULL) {
7748 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7749 "invitation");
7750 return -1;
7751 }
7752
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007753 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007754 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007755 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007756 ssid->ssid, ssid->ssid_len);
7757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007758 if (ssid->mode == WPAS_MODE_P2P_GO) {
7759 role = P2P_INVITE_ROLE_ACTIVE_GO;
7760 bssid = wpa_s->own_addr;
7761 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007762 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007763 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007764 } else {
7765 role = P2P_INVITE_ROLE_CLIENT;
7766 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7767 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7768 "invite to current group");
7769 return -1;
7770 }
7771 bssid = wpa_s->bssid;
7772 if (go_dev_addr == NULL &&
7773 !is_zero_ether_addr(wpa_s->go_dev_addr))
7774 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007775 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7776 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007777 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007778 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007780 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7781 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007782 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7783 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007784
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007785 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007786 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007787 role == P2P_INVITE_ROLE_ACTIVE_GO,
7788 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007789 if (res)
7790 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007791 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007794 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007795 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007796}
7797
7798
7799void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7800{
7801 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007802 u8 go_dev_addr[ETH_ALEN];
7803 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007804 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007805 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007806 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007807
Dmitry Shmidt04949592012-07-19 12:16:46 -07007808 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7809 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007810 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007811 }
7812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007813 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007814 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007815
7816 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007817 if (!wpa_s->p2p_go_group_formation_completed &&
7818 wpa_s->global->p2p_group_formation == wpa_s) {
7819 wpa_dbg(wpa_s, MSG_DEBUG,
7820 "P2P: Marking group formation completed on client on data connection");
7821 wpa_s->p2p_go_group_formation_completed = 1;
7822 wpa_s->global->p2p_group_formation = NULL;
7823 wpa_s->p2p_in_provisioning = 0;
7824 wpa_s->p2p_in_invitation = 0;
7825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007827 os_memset(go_dev_addr, 0, ETH_ALEN);
7828 if (ssid->bssid_set)
7829 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7830 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7831 ssid->ssid_len);
7832 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7833
7834 if (wpa_s->global->p2p_group_formation == wpa_s)
7835 wpa_s->global->p2p_group_formation = NULL;
7836
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007837 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7838 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007839
7840 ip_addr[0] = '\0';
7841 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007842 int res;
7843
7844 res = os_snprintf(ip_addr, sizeof(ip_addr),
7845 " ip_addr=%u.%u.%u.%u "
7846 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7847 ip[0], ip[1], ip[2], ip[3],
7848 ip[4], ip[5], ip[6], ip[7],
7849 ip[8], ip[9], ip[10], ip[11]);
7850 if (os_snprintf_error(sizeof(ip_addr), res))
7851 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007852 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007853 }
7854
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007855 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7856 ssid->passphrase == NULL && ssid->psk_set ?
7857 ssid->psk : NULL,
7858 ssid->passphrase, go_dev_addr, persistent,
7859 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007860
7861 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007862 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7863 ssid, go_dev_addr);
7864
Hai Shalom5f92bc92019-04-18 11:54:11 -07007865 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007866}
7867
7868
7869int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7870 u32 interval1, u32 duration2, u32 interval2)
7871{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007872 int ret;
7873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007874 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7875 return -1;
7876
7877 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7878 wpa_s->current_ssid == NULL ||
7879 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7880 return -1;
7881
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007882 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7883 wpa_s->own_addr, wpa_s->assoc_freq,
7884 duration1, interval1, duration2, interval2);
7885 if (ret == 0)
7886 wpa_s->waiting_presence_resp = 1;
7887
7888 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889}
7890
7891
7892int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7893 unsigned int interval)
7894{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007895 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7896 return -1;
7897
7898 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7899}
7900
7901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007902static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7903{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007904 if (wpa_s->current_ssid == NULL) {
7905 /*
7906 * current_ssid can be cleared when P2P client interface gets
7907 * disconnected, so assume this interface was used as P2P
7908 * client.
7909 */
7910 return 1;
7911 }
7912 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007913 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7914}
7915
7916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007917static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7918{
7919 struct wpa_supplicant *wpa_s = eloop_ctx;
7920
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007921 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007922 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7923 "disabled");
7924 return;
7925 }
7926
Dmitry Shmidt04949592012-07-19 12:16:46 -07007927 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7928 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007929 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007930}
7931
7932
7933static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7934{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007935 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007936
Dmitry Shmidt04949592012-07-19 12:16:46 -07007937 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7938 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007940 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7941 return;
7942
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007943 timeout = wpa_s->conf->p2p_group_idle;
7944 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7945 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7946 timeout = P2P_MAX_CLIENT_IDLE;
7947
7948 if (timeout == 0)
7949 return;
7950
Dmitry Shmidt04949592012-07-19 12:16:46 -07007951 if (timeout < 0) {
7952 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7953 timeout = 0; /* special client mode no-timeout */
7954 else
7955 return;
7956 }
7957
7958 if (wpa_s->p2p_in_provisioning) {
7959 /*
7960 * Use the normal group formation timeout during the
7961 * provisioning phase to avoid terminating this process too
7962 * early due to group idle timeout.
7963 */
7964 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7965 "during provisioning");
7966 return;
7967 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307968
Dmitry Shmidt04949592012-07-19 12:16:46 -07007969 if (wpa_s->show_group_started) {
7970 /*
7971 * Use the normal group formation timeout between the end of
7972 * the provisioning phase and completion of 4-way handshake to
7973 * avoid terminating this process too early due to group idle
7974 * timeout.
7975 */
7976 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7977 "while waiting for initial 4-way handshake to "
7978 "complete");
7979 return;
7980 }
7981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007982 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007983 timeout);
7984 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7985 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007986}
7987
7988
Jouni Malinen2b89da82012-08-31 22:04:41 +03007989/* Returns 1 if the interface was removed */
7990int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7991 u16 reason_code, const u8 *ie, size_t ie_len,
7992 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007993{
7994 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007995 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007996
Dmitry Shmidt04949592012-07-19 12:16:46 -07007997 if (!locally_generated)
7998 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7999 ie_len);
8000
8001 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8002 wpa_s->current_ssid &&
8003 wpa_s->current_ssid->p2p_group &&
8004 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8005 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8006 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008007 if (wpas_p2p_group_delete(wpa_s,
8008 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8009 > 0)
8010 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008011 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008012
8013 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008014}
8015
8016
8017void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008018 u16 reason_code, const u8 *ie, size_t ie_len,
8019 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008020{
8021 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8022 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008023
Dmitry Shmidt04949592012-07-19 12:16:46 -07008024 if (!locally_generated)
8025 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8026 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008027}
8028
8029
8030void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8031{
8032 struct p2p_data *p2p = wpa_s->global->p2p;
8033
8034 if (p2p == NULL)
8035 return;
8036
8037 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8038 return;
8039
8040 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8041 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8042
8043 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8044 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8045
8046 if (wpa_s->wps &&
8047 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8048 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8049
8050 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8051 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8052
8053 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8054 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8055 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8056 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8057 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8058 }
8059
8060 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8061 p2p_set_sec_dev_types(p2p,
8062 (void *) wpa_s->conf->sec_device_type,
8063 wpa_s->conf->num_sec_device_types);
8064
8065 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8066 int i;
8067 p2p_remove_wps_vendor_extensions(p2p);
8068 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8069 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8070 continue;
8071 p2p_add_wps_vendor_extension(
8072 p2p, wpa_s->conf->wps_vendor_ext[i]);
8073 }
8074 }
8075
8076 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8077 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8078 char country[3];
8079 country[0] = wpa_s->conf->country[0];
8080 country[1] = wpa_s->conf->country[1];
8081 country[2] = 0x04;
8082 p2p_set_country(p2p, country);
8083 }
8084
8085 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8086 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8087 wpa_s->conf->p2p_ssid_postfix ?
8088 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8089 0);
8090 }
8091
8092 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8093 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008094
8095 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8096 u8 reg_class, channel;
8097 int ret;
8098 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008099 u8 channel_forced;
8100
Jouni Malinen75ecf522011-06-27 15:19:46 -07008101 if (wpa_s->conf->p2p_listen_reg_class &&
8102 wpa_s->conf->p2p_listen_channel) {
8103 reg_class = wpa_s->conf->p2p_listen_reg_class;
8104 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008105 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008106 } else {
8107 reg_class = 81;
8108 /*
8109 * Pick one of the social channels randomly as the
8110 * listen channel.
8111 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008112 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8113 channel = 1;
8114 else
8115 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008116 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008117 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008118 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8119 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008120 if (ret)
8121 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8122 "failed: %d", ret);
8123 }
8124 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8125 u8 op_reg_class, op_channel, cfg_op_channel;
8126 int ret = 0;
8127 unsigned int r;
8128 if (wpa_s->conf->p2p_oper_reg_class &&
8129 wpa_s->conf->p2p_oper_channel) {
8130 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8131 op_channel = wpa_s->conf->p2p_oper_channel;
8132 cfg_op_channel = 1;
8133 } else {
8134 op_reg_class = 81;
8135 /*
8136 * Use random operation channel from (1, 6, 11)
8137 *if no other preference is indicated.
8138 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008139 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8140 op_channel = 1;
8141 else
8142 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008143 cfg_op_channel = 0;
8144 }
8145 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8146 cfg_op_channel);
8147 if (ret)
8148 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8149 "failed: %d", ret);
8150 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008151
8152 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8153 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8154 wpa_s->conf->p2p_pref_chan) < 0) {
8155 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8156 "update failed");
8157 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008158
8159 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8160 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8161 "update failed");
8162 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008163 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008164
8165 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8166 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008167}
8168
8169
8170int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8171 int duration)
8172{
8173 if (!wpa_s->ap_iface)
8174 return -1;
8175 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8176 duration);
8177}
8178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008179
8180int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8181{
8182 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8183 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008184
8185 wpa_s->global->cross_connection = enabled;
8186 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8187
8188 if (!enabled) {
8189 struct wpa_supplicant *iface;
8190
8191 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8192 {
8193 if (iface->cross_connect_enabled == 0)
8194 continue;
8195
8196 iface->cross_connect_enabled = 0;
8197 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008198 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008199 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8200 iface->ifname,
8201 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008202 }
8203 }
8204
8205 return 0;
8206}
8207
8208
8209static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8210{
8211 struct wpa_supplicant *iface;
8212
8213 if (!uplink->global->cross_connection)
8214 return;
8215
8216 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8217 if (!iface->cross_connect_enabled)
8218 continue;
8219 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8220 0)
8221 continue;
8222 if (iface->ap_iface == NULL)
8223 continue;
8224 if (iface->cross_connect_in_use)
8225 continue;
8226
8227 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008228 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008229 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8230 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008231 }
8232}
8233
8234
8235static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8236{
8237 struct wpa_supplicant *iface;
8238
8239 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8240 if (!iface->cross_connect_enabled)
8241 continue;
8242 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8243 0)
8244 continue;
8245 if (!iface->cross_connect_in_use)
8246 continue;
8247
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008248 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008249 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8250 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251 iface->cross_connect_in_use = 0;
8252 }
8253}
8254
8255
8256void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8257{
8258 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8259 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8260 wpa_s->cross_connect_disallowed)
8261 wpas_p2p_disable_cross_connect(wpa_s);
8262 else
8263 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008264 if (!wpa_s->ap_iface &&
8265 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8266 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008267}
8268
8269
8270void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8271{
8272 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008273 if (!wpa_s->ap_iface &&
8274 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8275 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008276 wpas_p2p_set_group_idle_timeout(wpa_s);
8277}
8278
8279
8280static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8281{
8282 struct wpa_supplicant *iface;
8283
8284 if (!wpa_s->global->cross_connection)
8285 return;
8286
8287 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8288 if (iface == wpa_s)
8289 continue;
8290 if (iface->drv_flags &
8291 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8292 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008293 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8294 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008295 continue;
8296
8297 wpa_s->cross_connect_enabled = 1;
8298 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8299 sizeof(wpa_s->cross_connect_uplink));
8300 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8301 "%s to %s whenever uplink is available",
8302 wpa_s->ifname, wpa_s->cross_connect_uplink);
8303
8304 if (iface->ap_iface || iface->current_ssid == NULL ||
8305 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8306 iface->cross_connect_disallowed ||
8307 iface->wpa_state != WPA_COMPLETED)
8308 break;
8309
8310 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008311 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008312 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8313 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008314 break;
8315 }
8316}
8317
8318
8319int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8320{
8321 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8322 !wpa_s->p2p_in_provisioning)
8323 return 0; /* not P2P client operation */
8324
8325 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8326 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008327 if (wpa_s != wpa_s->p2pdev)
8328 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008329 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008330 return 1;
8331}
8332
8333
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008334void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8335{
8336 struct wpa_supplicant *wpa_s = eloop_ctx;
8337 wpas_p2p_notif_pbc_overlap(wpa_s);
8338}
8339
8340
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008341void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8342 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008343{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008344 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008345 struct wpa_used_freq_data *freqs = NULL;
8346 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008347
8348 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8349 return;
8350
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008351 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8352 if (!freqs)
8353 return;
8354
8355 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008357 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008358 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008359 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8360 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008361 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8362 "channel list");
8363 return;
8364 }
8365
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008366 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008367
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008368 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008369
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008370 /*
8371 * The used frequencies map changed, so it is possible that a GO is
8372 * using a channel that is no longer valid for P2P use. It is also
8373 * possible that due to policy consideration, it would be preferable to
8374 * move it to a frequency already used by other station interfaces.
8375 */
8376 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8377
8378 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008379}
8380
8381
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008382static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8383 struct wpa_scan_results *scan_res)
8384{
8385 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8386}
8387
8388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008389int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8390{
8391 struct wpa_global *global = wpa_s->global;
8392 int found = 0;
8393 const u8 *peer;
8394
8395 if (global->p2p == NULL)
8396 return -1;
8397
8398 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8399
8400 if (wpa_s->pending_interface_name[0] &&
8401 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8402 found = 1;
8403
8404 peer = p2p_get_go_neg_peer(global->p2p);
8405 if (peer) {
8406 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8407 MACSTR, MAC2STR(peer));
8408 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008409 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008410 }
8411
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008412 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8413 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8414 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8415 found = 1;
8416 }
8417
8418 if (wpa_s->pending_pd_before_join) {
8419 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8420 wpa_s->pending_pd_before_join = 0;
8421 found = 1;
8422 }
8423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008424 wpas_p2p_stop_find(wpa_s);
8425
8426 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8427 if (wpa_s == global->p2p_group_formation &&
8428 (wpa_s->p2p_in_provisioning ||
8429 wpa_s->parent->pending_interface_type ==
8430 WPA_IF_P2P_CLIENT)) {
8431 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8432 "formation found - cancelling",
8433 wpa_s->ifname);
8434 found = 1;
8435 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008436 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008437 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008438 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008439 break;
8440 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008441 wpas_p2p_group_delete(wpa_s,
8442 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008443 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008444 } else if (wpa_s->p2p_in_invitation) {
8445 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8446 wpa_s->ifname);
8447 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008448 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008449 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008450 }
8451 }
8452
8453 if (!found) {
8454 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8455 return -1;
8456 }
8457
8458 return 0;
8459}
8460
8461
8462void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8463{
8464 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8465 return;
8466
8467 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8468 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008469 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008470}
8471
8472
8473void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8474 int freq_24, int freq_5, int freq_overall)
8475{
8476 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008477 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008478 return;
8479 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8480}
8481
8482
8483int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8484{
8485 u8 peer[ETH_ALEN];
8486 struct p2p_data *p2p = wpa_s->global->p2p;
8487
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008488 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008489 return -1;
8490
8491 if (hwaddr_aton(addr, peer))
8492 return -1;
8493
8494 return p2p_unauthorize(p2p, peer);
8495}
8496
8497
8498/**
8499 * wpas_p2p_disconnect - Disconnect from a P2P Group
8500 * @wpa_s: Pointer to wpa_supplicant data
8501 * Returns: 0 on success, -1 on failure
8502 *
8503 * This can be used to disconnect from a group in which the local end is a P2P
8504 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8505 * virtual network interface was created for this group, that interface will be
8506 * removed. Otherwise, only the configured P2P group network will be removed
8507 * from the interface.
8508 */
8509int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8510{
8511
8512 if (wpa_s == NULL)
8513 return -1;
8514
Jouni Malinen2b89da82012-08-31 22:04:41 +03008515 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8516 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008517}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008518
8519
8520int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8521{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008522 int ret;
8523
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008524 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8525 return 0;
8526
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008527 ret = p2p_in_progress(wpa_s->global->p2p);
8528 if (ret == 0) {
8529 /*
8530 * Check whether there is an ongoing WPS provisioning step (or
8531 * other parts of group formation) on another interface since
8532 * p2p_in_progress() does not report this to avoid issues for
8533 * scans during such provisioning step.
8534 */
8535 if (wpa_s->global->p2p_group_formation &&
8536 wpa_s->global->p2p_group_formation != wpa_s) {
8537 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8538 "in group formation",
8539 wpa_s->global->p2p_group_formation->ifname);
8540 ret = 1;
8541 }
8542 }
8543
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008544 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008545 struct os_reltime now;
8546 os_get_reltime(&now);
8547 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8548 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008549 /* Wait for the first client has expired */
8550 wpa_s->global->p2p_go_wait_client.sec = 0;
8551 } else {
8552 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8553 ret = 1;
8554 }
8555 }
8556
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008557 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008558}
8559
8560
8561void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8562 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008563{
8564 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8565 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008566 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008567 /**
8568 * Remove the network by scheduling the group formation
8569 * timeout to happen immediately. The teardown code
8570 * needs to be scheduled to run asynch later so that we
8571 * don't delete data from under ourselves unexpectedly.
8572 * Calling wpas_p2p_group_formation_timeout directly
8573 * causes a series of crashes in WPS failure scenarios.
8574 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008575 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8576 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008577 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008578 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008579 }
8580}
8581
8582
8583struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008584 const u8 *addr, const u8 *ssid,
8585 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008586{
8587 struct wpa_ssid *s;
8588 size_t i;
8589
8590 for (s = wpa_s->conf->ssid; s; s = s->next) {
8591 if (s->disabled != 2)
8592 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008593 if (ssid &&
8594 (ssid_len != s->ssid_len ||
8595 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8596 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008597 if (addr == NULL) {
8598 if (s->mode == WPAS_MODE_P2P_GO)
8599 return s;
8600 continue;
8601 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008602 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8603 return s; /* peer is GO in the persistent group */
8604 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8605 continue;
8606 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008607 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008608 addr, ETH_ALEN) == 0)
8609 return s; /* peer is P2P client in persistent
8610 * group */
8611 }
8612 }
8613
8614 return NULL;
8615}
8616
8617
8618void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8619 const u8 *addr)
8620{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008621 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008622 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008623 /*
8624 * This can happen if WPS provisioning step is not terminated
8625 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8626 * peer was able to connect, there is no need to time out group
8627 * formation after this, though. In addition, this is used with
8628 * the initial connection wait on the GO as a separate formation
8629 * timeout and as such, expected to be hit after the initial WPS
8630 * provisioning step.
8631 */
8632 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008633
8634 if (!wpa_s->p2p_go_group_formation_completed &&
8635 !wpa_s->group_formation_reported) {
8636 /*
8637 * GO has not yet notified group formation success since
8638 * the WPS step was not completed cleanly. Do that
8639 * notification now since the P2P Client was able to
8640 * connect and as such, must have received the
8641 * credential from the WPS step.
8642 */
8643 if (wpa_s->global->p2p)
8644 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008645 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008646 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008647 }
8648 if (!wpa_s->p2p_go_group_formation_completed) {
8649 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8650 wpa_s->p2p_go_group_formation_completed = 1;
8651 wpa_s->global->p2p_group_formation = NULL;
8652 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008653 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008654 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008655 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008656 if (addr == NULL)
8657 return;
8658 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8659}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008660
Dmitry Shmidt04949592012-07-19 12:16:46 -07008661
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008662static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8663 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008664{
8665 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008666 int ret = 0;
8667
Dmitry Shmidt04949592012-07-19 12:16:46 -07008668 if (wpa_s->global->p2p_group_formation)
8669 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008670 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008671 offchannel_send_action_done(wpa_s);
8672 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008673 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008674 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8675 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8676 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8677 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008678 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008679 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008680 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008681 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008682 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008683 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008684 wpa_s->p2p_go_he,
8685 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008686 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008687 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008688}
8689
8690
8691int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8692{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008693 int res;
8694
Dmitry Shmidt04949592012-07-19 12:16:46 -07008695 if (!wpa_s->p2p_fallback_to_go_neg ||
8696 wpa_s->p2p_in_provisioning <= 5)
8697 return 0;
8698
8699 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8700 return 0; /* peer operating as a GO */
8701
8702 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8703 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008704 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008705 "reason=GO-not-found");
8706 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008707
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008708 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008709}
8710
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008711
8712unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8713{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008714 struct wpa_supplicant *ifs;
8715
8716 if (wpa_s->wpa_state > WPA_SCANNING) {
8717 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8718 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008719 wpa_s->conf->p2p_search_delay);
8720 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008721 }
8722
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008723 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8724 radio_list) {
8725 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008726 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8727 "delay due to concurrent operation on "
8728 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008729 wpa_s->conf->p2p_search_delay,
8730 ifs->ifname);
8731 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008732 }
8733 }
8734
8735 return 0;
8736}
8737
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008738
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008739static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8740 struct wpa_ssid *s, const u8 *addr,
8741 int iface_addr)
8742{
8743 struct psk_list_entry *psk, *tmp;
8744 int changed = 0;
8745
8746 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8747 list) {
8748 if ((iface_addr && !psk->p2p &&
8749 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8750 (!iface_addr && psk->p2p &&
8751 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8752 wpa_dbg(wpa_s, MSG_DEBUG,
8753 "P2P: Remove persistent group PSK list entry for "
8754 MACSTR " p2p=%u",
8755 MAC2STR(psk->addr), psk->p2p);
8756 dl_list_del(&psk->list);
8757 os_free(psk);
8758 changed++;
8759 }
8760 }
8761
8762 return changed;
8763}
8764
8765
8766void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8767 const u8 *p2p_dev_addr,
8768 const u8 *psk, size_t psk_len)
8769{
8770 struct wpa_ssid *ssid = wpa_s->current_ssid;
8771 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008772 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008773
8774 if (psk_len != sizeof(p->psk))
8775 return;
8776
8777 if (p2p_dev_addr) {
8778 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8779 " p2p_dev_addr=" MACSTR,
8780 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8781 if (is_zero_ether_addr(p2p_dev_addr))
8782 p2p_dev_addr = NULL;
8783 } else {
8784 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8785 MAC2STR(mac_addr));
8786 }
8787
8788 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8789 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8790 /* To be added to persistent group once created */
8791 if (wpa_s->global->add_psk == NULL) {
8792 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8793 if (wpa_s->global->add_psk == NULL)
8794 return;
8795 }
8796 p = wpa_s->global->add_psk;
8797 if (p2p_dev_addr) {
8798 p->p2p = 1;
8799 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8800 } else {
8801 p->p2p = 0;
8802 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8803 }
8804 os_memcpy(p->psk, psk, psk_len);
8805 return;
8806 }
8807
8808 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8809 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8810 return;
8811 }
8812
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008813 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008814 ssid->ssid_len);
8815 if (!persistent) {
8816 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8817 return;
8818 }
8819
8820 p = os_zalloc(sizeof(*p));
8821 if (p == NULL)
8822 return;
8823 if (p2p_dev_addr) {
8824 p->p2p = 1;
8825 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8826 } else {
8827 p->p2p = 0;
8828 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8829 }
8830 os_memcpy(p->psk, psk, psk_len);
8831
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008832 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8833 (last = dl_list_last(&persistent->psk_list,
8834 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008835 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8836 MACSTR " (p2p=%u) to make room for a new one",
8837 MAC2STR(last->addr), last->p2p);
8838 dl_list_del(&last->list);
8839 os_free(last);
8840 }
8841
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008842 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008843 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8844 p2p_dev_addr == NULL);
8845 if (p2p_dev_addr) {
8846 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8847 MACSTR, MAC2STR(p2p_dev_addr));
8848 } else {
8849 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8850 MAC2STR(mac_addr));
8851 }
8852 dl_list_add(&persistent->psk_list, &p->list);
8853
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008854 if (wpa_s->p2pdev->conf->update_config &&
8855 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008856 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008857}
8858
8859
8860static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8861 struct wpa_ssid *s, const u8 *addr,
8862 int iface_addr)
8863{
8864 int res;
8865
8866 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008867 if (res > 0 && wpa_s->conf->update_config &&
8868 wpa_config_write(wpa_s->confname, wpa_s->conf))
8869 wpa_dbg(wpa_s, MSG_DEBUG,
8870 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008871}
8872
8873
8874static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8875 const u8 *peer, int iface_addr)
8876{
8877 struct hostapd_data *hapd;
8878 struct hostapd_wpa_psk *psk, *prev, *rem;
8879 struct sta_info *sta;
8880
8881 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8882 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8883 return;
8884
8885 /* Remove per-station PSK entry */
8886 hapd = wpa_s->ap_iface->bss[0];
8887 prev = NULL;
8888 psk = hapd->conf->ssid.wpa_psk;
8889 while (psk) {
8890 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8891 (!iface_addr &&
8892 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8893 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8894 MACSTR " iface_addr=%d",
8895 MAC2STR(peer), iface_addr);
8896 if (prev)
8897 prev->next = psk->next;
8898 else
8899 hapd->conf->ssid.wpa_psk = psk->next;
8900 rem = psk;
8901 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07008902 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008903 } else {
8904 prev = psk;
8905 psk = psk->next;
8906 }
8907 }
8908
8909 /* Disconnect from group */
8910 if (iface_addr)
8911 sta = ap_get_sta(hapd, peer);
8912 else
8913 sta = ap_get_sta_p2p(hapd, peer);
8914 if (sta) {
8915 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8916 " (iface_addr=%d) from group",
8917 MAC2STR(peer), iface_addr);
8918 hostapd_drv_sta_deauth(hapd, sta->addr,
8919 WLAN_REASON_DEAUTH_LEAVING);
8920 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8921 }
8922}
8923
8924
8925void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8926 int iface_addr)
8927{
8928 struct wpa_ssid *s;
8929 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008930 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008931
8932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8933
8934 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008935 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008936 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8937 continue;
8938 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008939 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8940 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008941 }
8942
8943 /* Remove from any operating group */
8944 for (w = wpa_s->global->ifaces; w; w = w->next)
8945 wpas_p2p_remove_client_go(w, peer, iface_addr);
8946}
8947
8948
8949static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8950{
8951 struct wpa_supplicant *wpa_s = eloop_ctx;
8952 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8953}
8954
8955
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008956static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8957{
8958 struct wpa_supplicant *wpa_s = eloop_ctx;
8959
8960 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8961 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8962}
8963
8964
8965int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8966 struct wpa_ssid *ssid)
8967{
8968 struct wpa_supplicant *iface;
8969
8970 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8971 if (!iface->current_ssid ||
8972 iface->current_ssid->frequency == freq ||
8973 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8974 !iface->current_ssid->p2p_group))
8975 continue;
8976
8977 /* Remove the connection with least priority */
8978 if (!wpas_is_p2p_prioritized(iface)) {
8979 /* STA connection has priority over existing
8980 * P2P connection, so remove the interface. */
8981 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8982 eloop_register_timeout(0, 0,
8983 wpas_p2p_group_freq_conflict,
8984 iface, NULL);
8985 /* If connection in progress is P2P connection, do not
8986 * proceed for the connection. */
8987 if (wpa_s == iface)
8988 return -1;
8989 else
8990 return 0;
8991 } else {
8992 /* P2P connection has priority, disable the STA network
8993 */
8994 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8995 ssid);
8996 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8997 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8998 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8999 ETH_ALEN);
9000 /* If P2P connection is in progress, continue
9001 * connecting...*/
9002 if (wpa_s == iface)
9003 return 0;
9004 else
9005 return -1;
9006 }
9007 }
9008
9009 return 0;
9010}
9011
9012
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009013int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9014{
9015 struct wpa_ssid *ssid = wpa_s->current_ssid;
9016
9017 if (ssid == NULL || !ssid->p2p_group)
9018 return 0;
9019
9020 if (wpa_s->p2p_last_4way_hs_fail &&
9021 wpa_s->p2p_last_4way_hs_fail == ssid) {
9022 u8 go_dev_addr[ETH_ALEN];
9023 struct wpa_ssid *persistent;
9024
9025 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9026 ssid->ssid,
9027 ssid->ssid_len) <= 0) {
9028 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9029 goto disconnect;
9030 }
9031
9032 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9033 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009034 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009035 ssid->ssid,
9036 ssid->ssid_len);
9037 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9038 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9039 goto disconnect;
9040 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009041 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009042 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9043 persistent->id);
9044 disconnect:
9045 wpa_s->p2p_last_4way_hs_fail = NULL;
9046 /*
9047 * Remove the group from a timeout to avoid issues with caller
9048 * continuing to use the interface if this is on a P2P group
9049 * interface.
9050 */
9051 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9052 wpa_s, NULL);
9053 return 1;
9054 }
9055
9056 wpa_s->p2p_last_4way_hs_fail = ssid;
9057 return 0;
9058}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009059
9060
9061#ifdef CONFIG_WPS_NFC
9062
9063static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9064 struct wpabuf *p2p)
9065{
9066 struct wpabuf *ret;
9067 size_t wsc_len;
9068
9069 if (p2p == NULL) {
9070 wpabuf_free(wsc);
9071 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9072 return NULL;
9073 }
9074
9075 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9076 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9077 if (ret == NULL) {
9078 wpabuf_free(wsc);
9079 wpabuf_free(p2p);
9080 return NULL;
9081 }
9082
9083 wpabuf_put_be16(ret, wsc_len);
9084 if (wsc)
9085 wpabuf_put_buf(ret, wsc);
9086 wpabuf_put_be16(ret, wpabuf_len(p2p));
9087 wpabuf_put_buf(ret, p2p);
9088
9089 wpabuf_free(wsc);
9090 wpabuf_free(p2p);
9091 wpa_hexdump_buf(MSG_DEBUG,
9092 "P2P: Generated NFC connection handover message", ret);
9093
9094 if (ndef && ret) {
9095 struct wpabuf *tmp;
9096 tmp = ndef_build_p2p(ret);
9097 wpabuf_free(ret);
9098 if (tmp == NULL) {
9099 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9100 return NULL;
9101 }
9102 ret = tmp;
9103 }
9104
9105 return ret;
9106}
9107
9108
9109static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9110 struct wpa_ssid **ssid, u8 *go_dev_addr)
9111{
9112 struct wpa_supplicant *iface;
9113
9114 if (go_dev_addr)
9115 os_memset(go_dev_addr, 0, ETH_ALEN);
9116 if (ssid)
9117 *ssid = NULL;
9118 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9119 if (iface->wpa_state < WPA_ASSOCIATING ||
9120 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9121 !iface->current_ssid->p2p_group ||
9122 iface->current_ssid->mode != WPAS_MODE_INFRA)
9123 continue;
9124 if (ssid)
9125 *ssid = iface->current_ssid;
9126 if (go_dev_addr)
9127 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9128 return iface->assoc_freq;
9129 }
9130 return 0;
9131}
9132
9133
9134struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9135 int ndef)
9136{
9137 struct wpabuf *wsc, *p2p;
9138 struct wpa_ssid *ssid;
9139 u8 go_dev_addr[ETH_ALEN];
9140 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9141
9142 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9143 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9144 return NULL;
9145 }
9146
9147 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9148 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9149 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9150 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9151 return NULL;
9152 }
9153
9154 if (cli_freq == 0) {
9155 wsc = wps_build_nfc_handover_req_p2p(
9156 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9157 } else
9158 wsc = NULL;
9159 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9160 go_dev_addr, ssid ? ssid->ssid : NULL,
9161 ssid ? ssid->ssid_len : 0);
9162
9163 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9164}
9165
9166
9167struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9168 int ndef, int tag)
9169{
9170 struct wpabuf *wsc, *p2p;
9171 struct wpa_ssid *ssid;
9172 u8 go_dev_addr[ETH_ALEN];
9173 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9174
9175 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9176 return NULL;
9177
9178 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9179 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9180 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9181 return NULL;
9182
9183 if (cli_freq == 0) {
9184 wsc = wps_build_nfc_handover_sel_p2p(
9185 wpa_s->parent->wps,
9186 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9187 DEV_PW_NFC_CONNECTION_HANDOVER,
9188 wpa_s->conf->wps_nfc_dh_pubkey,
9189 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9190 } else
9191 wsc = NULL;
9192 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9193 go_dev_addr, ssid ? ssid->ssid : NULL,
9194 ssid ? ssid->ssid_len : 0);
9195
9196 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9197}
9198
9199
9200static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9201 struct p2p_nfc_params *params)
9202{
9203 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9204 "connection handover (freq=%d)",
9205 params->go_freq);
9206
9207 if (params->go_freq && params->go_ssid_len) {
9208 wpa_s->p2p_wps_method = WPS_NFC;
9209 wpa_s->pending_join_wps_method = WPS_NFC;
9210 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9211 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9212 ETH_ALEN);
9213 return wpas_p2p_join_start(wpa_s, params->go_freq,
9214 params->go_ssid,
9215 params->go_ssid_len);
9216 }
9217
9218 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9219 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009220 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009221 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009222 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009223 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009224 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009225}
9226
9227
9228static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9229 struct p2p_nfc_params *params, int tag)
9230{
9231 int res, persistent;
9232 struct wpa_ssid *ssid;
9233
9234 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9235 "connection handover");
9236 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9237 ssid = wpa_s->current_ssid;
9238 if (ssid == NULL)
9239 continue;
9240 if (ssid->mode != WPAS_MODE_P2P_GO)
9241 continue;
9242 if (wpa_s->ap_iface == NULL)
9243 continue;
9244 break;
9245 }
9246 if (wpa_s == NULL) {
9247 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9248 return -1;
9249 }
9250
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009251 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009252 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009253 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009254 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9255 return -1;
9256 }
9257 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009258 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9259 wpa_s->p2pdev->p2p_oob_dev_pw,
9260 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9261 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009262 if (res)
9263 return res;
9264
9265 if (!tag) {
9266 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9267 return 0;
9268 }
9269
9270 if (!params->peer ||
9271 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9272 return 0;
9273
9274 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9275 " to join", MAC2STR(params->peer->p2p_device_addr));
9276
9277 wpa_s->global->p2p_invite_group = wpa_s;
9278 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009279 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009280 params->peer->p2p_device_addr,
9281 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009282 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009283
9284 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9285 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9286 ssid->ssid, ssid->ssid_len, ssid->frequency,
9287 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009288 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009289}
9290
9291
9292static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9293 struct p2p_nfc_params *params,
9294 int forced_freq)
9295{
9296 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9297 "connection handover");
9298 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9299 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009300 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009301 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009302 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009303 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009304}
9305
9306
9307static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9308 struct p2p_nfc_params *params,
9309 int forced_freq)
9310{
9311 int res;
9312
9313 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9314 "connection handover");
9315 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9316 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009317 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009318 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009319 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009320 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009321 if (res)
9322 return res;
9323
9324 res = wpas_p2p_listen(wpa_s, 60);
9325 if (res) {
9326 p2p_unauthorize(wpa_s->global->p2p,
9327 params->peer->p2p_device_addr);
9328 }
9329
9330 return res;
9331}
9332
9333
9334static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9335 const struct wpabuf *data,
9336 int sel, int tag, int forced_freq)
9337{
9338 const u8 *pos, *end;
9339 u16 len, id;
9340 struct p2p_nfc_params params;
9341 int res;
9342
9343 os_memset(&params, 0, sizeof(params));
9344 params.sel = sel;
9345
9346 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9347
9348 pos = wpabuf_head(data);
9349 end = pos + wpabuf_len(data);
9350
9351 if (end - pos < 2) {
9352 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9353 "attributes");
9354 return -1;
9355 }
9356 len = WPA_GET_BE16(pos);
9357 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009358 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009359 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9360 "attributes");
9361 return -1;
9362 }
9363 params.wsc_attr = pos;
9364 params.wsc_len = len;
9365 pos += len;
9366
9367 if (end - pos < 2) {
9368 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9369 "attributes");
9370 return -1;
9371 }
9372 len = WPA_GET_BE16(pos);
9373 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009374 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009375 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9376 "attributes");
9377 return -1;
9378 }
9379 params.p2p_attr = pos;
9380 params.p2p_len = len;
9381 pos += len;
9382
9383 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9384 params.wsc_attr, params.wsc_len);
9385 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9386 params.p2p_attr, params.p2p_len);
9387 if (pos < end) {
9388 wpa_hexdump(MSG_DEBUG,
9389 "P2P: Ignored extra data after P2P attributes",
9390 pos, end - pos);
9391 }
9392
9393 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9394 if (res)
9395 return res;
9396
9397 if (params.next_step == NO_ACTION)
9398 return 0;
9399
9400 if (params.next_step == BOTH_GO) {
9401 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9402 MAC2STR(params.peer->p2p_device_addr));
9403 return 0;
9404 }
9405
9406 if (params.next_step == PEER_CLIENT) {
9407 if (!is_zero_ether_addr(params.go_dev_addr)) {
9408 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9409 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9410 " ssid=\"%s\"",
9411 MAC2STR(params.peer->p2p_device_addr),
9412 params.go_freq,
9413 MAC2STR(params.go_dev_addr),
9414 wpa_ssid_txt(params.go_ssid,
9415 params.go_ssid_len));
9416 } else {
9417 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9418 "peer=" MACSTR " freq=%d",
9419 MAC2STR(params.peer->p2p_device_addr),
9420 params.go_freq);
9421 }
9422 return 0;
9423 }
9424
9425 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9426 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9427 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9428 return 0;
9429 }
9430
9431 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9432 wpa_s->p2p_oob_dev_pw = NULL;
9433
9434 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9435 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9436 "received");
9437 return -1;
9438 }
9439
9440 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9441 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9442 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9443 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9444 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9445 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9446 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9447
9448 if (tag) {
9449 if (id < 0x10) {
9450 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9451 "peer OOB Device Password Id %u", id);
9452 return -1;
9453 }
9454 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9455 "Device Password Id %u", id);
9456 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9457 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9458 params.oob_dev_pw_len -
9459 WPS_OOB_PUBKEY_HASH_LEN - 2);
9460 wpa_s->p2p_oob_dev_pw_id = id;
9461 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9462 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9463 params.oob_dev_pw_len -
9464 WPS_OOB_PUBKEY_HASH_LEN - 2);
9465 if (wpa_s->p2p_oob_dev_pw == NULL)
9466 return -1;
9467
9468 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9469 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9470 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9471 return -1;
9472 } else {
9473 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9474 "without Device Password");
9475 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9476 }
9477
9478 switch (params.next_step) {
9479 case NO_ACTION:
9480 case BOTH_GO:
9481 case PEER_CLIENT:
9482 /* already covered above */
9483 return 0;
9484 case JOIN_GROUP:
9485 return wpas_p2p_nfc_join_group(wpa_s, &params);
9486 case AUTH_JOIN:
9487 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9488 case INIT_GO_NEG:
9489 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9490 case RESP_GO_NEG:
9491 /* TODO: use own OOB Dev Pw */
9492 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9493 }
9494
9495 return -1;
9496}
9497
9498
9499int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9500 const struct wpabuf *data, int forced_freq)
9501{
9502 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9503 return -1;
9504
9505 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9506}
9507
9508
9509int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9510 const struct wpabuf *req,
9511 const struct wpabuf *sel, int forced_freq)
9512{
9513 struct wpabuf *tmp;
9514 int ret;
9515
9516 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9517 return -1;
9518
9519 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9520
9521 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9522 wpabuf_head(req), wpabuf_len(req));
9523 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9524 wpabuf_head(sel), wpabuf_len(sel));
9525 if (forced_freq)
9526 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9527 tmp = ndef_parse_p2p(init ? sel : req);
9528 if (tmp == NULL) {
9529 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9530 return -1;
9531 }
9532
9533 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9534 forced_freq);
9535 wpabuf_free(tmp);
9536
9537 return ret;
9538}
9539
9540
9541int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9542{
9543 const u8 *if_addr;
9544 int go_intent = wpa_s->conf->p2p_go_intent;
9545 struct wpa_supplicant *iface;
9546
9547 if (wpa_s->global->p2p == NULL)
9548 return -1;
9549
9550 if (!enabled) {
9551 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9552 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9553 {
9554 if (!iface->ap_iface)
9555 continue;
9556 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9557 }
9558 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9559 0, NULL);
9560 if (wpa_s->p2p_nfc_tag_enabled)
9561 wpas_p2p_remove_pending_group_interface(wpa_s);
9562 wpa_s->p2p_nfc_tag_enabled = 0;
9563 return 0;
9564 }
9565
9566 if (wpa_s->global->p2p_disabled)
9567 return -1;
9568
9569 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9570 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9571 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9572 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9573 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9574 "to allow static handover cases");
9575 return -1;
9576 }
9577
9578 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9579
9580 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9581 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9582 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9583 if (wpa_s->p2p_oob_dev_pw == NULL)
9584 return -1;
9585 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9586
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009587 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9588 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9589 /*
9590 * P2P Group Interface present and the command came on group
9591 * interface, so enable the token for the current interface.
9592 */
9593 wpa_s->create_p2p_iface = 0;
9594 } else {
9595 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9596 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009597
9598 if (wpa_s->create_p2p_iface) {
9599 enum wpa_driver_if_type iftype;
9600 /* Prepare to add a new interface for the group */
9601 iftype = WPA_IF_P2P_GROUP;
9602 if (go_intent == 15)
9603 iftype = WPA_IF_P2P_GO;
9604 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9605 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9606 "interface for the group");
9607 return -1;
9608 }
9609
9610 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009611 } else if (wpa_s->p2p_mgmt)
9612 if_addr = wpa_s->parent->own_addr;
9613 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009614 if_addr = wpa_s->own_addr;
9615
9616 wpa_s->p2p_nfc_tag_enabled = enabled;
9617
9618 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9619 struct hostapd_data *hapd;
9620 if (iface->ap_iface == NULL)
9621 continue;
9622 hapd = iface->ap_iface->bss[0];
9623 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9624 hapd->conf->wps_nfc_dh_pubkey =
9625 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9626 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9627 hapd->conf->wps_nfc_dh_privkey =
9628 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9629 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9630 hapd->conf->wps_nfc_dev_pw =
9631 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9632 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9633
9634 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9635 wpa_dbg(iface, MSG_DEBUG,
9636 "P2P: Failed to enable NFC Tag for GO");
9637 }
9638 }
9639 p2p_set_authorized_oob_dev_pw_id(
9640 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9641 if_addr);
9642
9643 return 0;
9644}
9645
9646#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009647
9648
9649static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9650 struct wpa_used_freq_data *freqs,
9651 unsigned int num)
9652{
9653 u8 curr_chan, cand, chan;
9654 unsigned int i;
9655
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009656 /*
9657 * If possible, optimize the Listen channel to be a channel that is
9658 * already used by one of the other interfaces.
9659 */
9660 if (!wpa_s->conf->p2p_optimize_listen_chan)
9661 return;
9662
9663 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9664 return;
9665
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009666 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9667 for (i = 0, cand = 0; i < num; i++) {
9668 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9669 if (curr_chan == chan) {
9670 cand = 0;
9671 break;
9672 }
9673
9674 if (chan == 1 || chan == 6 || chan == 11)
9675 cand = chan;
9676 }
9677
9678 if (cand) {
9679 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009680 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009681 cand);
9682 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9683 }
9684}
9685
9686
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009687static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009688{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009689 struct hostapd_config *conf;
9690 struct p2p_go_neg_results params;
9691 struct csa_settings csa_settings;
9692 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9693 int old_freq = current_ssid->frequency;
9694 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009695
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009696 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9697 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9698 return -1;
9699 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009700
9701 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009702 * TODO: This function may not always work correctly. For example,
9703 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009704 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009705 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9706 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009707 wpa_dbg(wpa_s, MSG_DEBUG,
9708 "P2P CSA: Failed to select new frequency for GO");
9709 return -1;
9710 }
9711
9712 if (current_ssid->frequency == params.freq) {
9713 wpa_dbg(wpa_s, MSG_DEBUG,
9714 "P2P CSA: Selected same frequency - not moving GO");
9715 return 0;
9716 }
9717
9718 conf = hostapd_config_defaults();
9719 if (!conf) {
9720 wpa_dbg(wpa_s, MSG_DEBUG,
9721 "P2P CSA: Failed to allocate default config");
9722 return -1;
9723 }
9724
9725 current_ssid->frequency = params.freq;
9726 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9727 wpa_dbg(wpa_s, MSG_DEBUG,
9728 "P2P CSA: Failed to create new GO config");
9729 ret = -1;
9730 goto out;
9731 }
9732
9733 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9734 wpa_dbg(wpa_s, MSG_DEBUG,
9735 "P2P CSA: CSA to a different band is not supported");
9736 ret = -1;
9737 goto out;
9738 }
9739
9740 os_memset(&csa_settings, 0, sizeof(csa_settings));
9741 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9742 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9743 csa_settings.freq_params.freq = params.freq;
9744 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9745 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9746 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9747
9748 if (conf->ieee80211ac) {
9749 int freq1 = 0, freq2 = 0;
9750 u8 chan, opclass;
9751
9752 if (ieee80211_freq_to_channel_ext(params.freq,
9753 conf->secondary_channel,
9754 conf->vht_oper_chwidth,
9755 &opclass, &chan) ==
9756 NUM_HOSTAPD_MODES) {
9757 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9758 ret = -1;
9759 goto out;
9760 }
9761
9762 if (conf->vht_oper_centr_freq_seg0_idx)
9763 freq1 = ieee80211_chan_to_freq(
9764 NULL, opclass,
9765 conf->vht_oper_centr_freq_seg0_idx);
9766
9767 if (conf->vht_oper_centr_freq_seg1_idx)
9768 freq2 = ieee80211_chan_to_freq(
9769 NULL, opclass,
9770 conf->vht_oper_centr_freq_seg1_idx);
9771
9772 if (freq1 < 0 || freq2 < 0) {
9773 wpa_dbg(wpa_s, MSG_DEBUG,
9774 "P2P CSA: Selected invalid VHT center freqs");
9775 ret = -1;
9776 goto out;
9777 }
9778
9779 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9780 csa_settings.freq_params.center_freq1 = freq1;
9781 csa_settings.freq_params.center_freq2 = freq2;
9782
9783 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009784 case CHANWIDTH_80MHZ:
9785 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009786 csa_settings.freq_params.bandwidth = 80;
9787 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009788 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009789 csa_settings.freq_params.bandwidth = 160;
9790 break;
9791 }
9792 }
9793
9794 ret = ap_switch_channel(wpa_s, &csa_settings);
9795out:
9796 current_ssid->frequency = old_freq;
9797 hostapd_config_free(conf);
9798 return ret;
9799}
9800
9801
9802static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9803{
9804 struct p2p_go_neg_results params;
9805 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009806 void (*ap_configured_cb)(void *ctx, void *data);
9807 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009808
9809 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9810
9811 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9812 current_ssid->frequency);
9813
9814 /* Stop the AP functionality */
9815 /* TODO: Should do this in a way that does not indicated to possible
9816 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009817 /* If this action occurs before a group is started, the callback should
9818 * be preserved, or GROUP-STARTED event would be lost. If this action
9819 * occurs after a group is started, these pointers are all NULL and
9820 * harmless. */
9821 ap_configured_cb = wpa_s->ap_configured_cb;
9822 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9823 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009824 wpa_supplicant_ap_deinit(wpa_s);
9825
9826 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009827 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9828 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009829 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9830 wpas_p2p_group_delete(wpa_s,
9831 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9832 return;
9833 }
9834 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9835 params.freq);
9836
9837 if (params.freq &&
9838 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9839 wpa_printf(MSG_DEBUG,
9840 "P2P: Selected freq (%u MHz) is not valid for P2P",
9841 params.freq);
9842 wpas_p2p_group_delete(wpa_s,
9843 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9844 return;
9845 }
9846
Hai Shalom899fcc72020-10-19 14:38:18 -07009847 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009848 wpa_s->ap_configured_cb = ap_configured_cb;
9849 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9850 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009851
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009852 /* Update the frequency */
9853 current_ssid->frequency = params.freq;
9854 wpa_s->connect_without_scan = current_ssid;
9855 wpa_s->reassociate = 1;
9856 wpa_s->disconnected = 0;
9857 wpa_supplicant_req_scan(wpa_s, 0, 0);
9858}
9859
9860
9861static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9862{
9863 struct wpa_supplicant *wpa_s = eloop_ctx;
9864
9865 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009866 return;
9867
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009868 wpas_p2p_go_update_common_freqs(wpa_s);
9869
9870 /* Do not move GO in the middle of a CSA */
9871 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9872 wpa_printf(MSG_DEBUG,
9873 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009874 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009875 }
9876
9877 /*
9878 * First, try a channel switch flow. If it is not supported or fails,
9879 * take down the GO and bring it up again.
9880 */
9881 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9882 wpas_p2p_move_go_no_csa(wpa_s);
9883}
9884
9885
9886static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9887{
9888 struct wpa_supplicant *wpa_s = eloop_ctx;
9889 struct wpa_used_freq_data *freqs = NULL;
9890 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009891
9892 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9893 if (!freqs)
9894 return;
9895
9896 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9897
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009898 /* Previous attempt to move a GO was not possible -- try again. */
9899 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9900 WPAS_P2P_CHANNEL_UPDATE_ANY);
9901
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009902 os_free(freqs);
9903}
9904
9905
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009906/*
9907 * Consider moving a GO from its currently used frequency:
9908 * 1. It is possible that due to regulatory consideration the frequency
9909 * can no longer be used and there is a need to evacuate the GO.
9910 * 2. It is possible that due to MCC considerations, it would be preferable
9911 * to move the GO to a channel that is currently used by some other
9912 * station interface.
9913 *
9914 * In case a frequency that became invalid is once again valid, cancel a
9915 * previously initiated GO frequency change.
9916 */
9917static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9918 struct wpa_used_freq_data *freqs,
9919 unsigned int num)
9920{
9921 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9922 unsigned int timeout;
9923 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009924 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009925
9926 wpas_p2p_go_update_common_freqs(wpa_s);
9927
9928 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009929 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009930 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009931 for (i = 0, invalid_freq = 0; i < num; i++) {
9932 if (freqs[i].freq == freq) {
9933 flags = freqs[i].flags;
9934
9935 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009936 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9937 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009938 wpa_dbg(wpa_s, MSG_DEBUG,
9939 "P2P: Freq=%d MHz no longer valid for GO",
9940 freq);
9941 invalid_freq = 1;
9942 }
9943 } else if (freqs[i].flags == 0) {
9944 /* Freq is not used by any other station interface */
9945 continue;
9946 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009947 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009948 /* Freq is not valid for P2P use cases */
9949 continue;
9950 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9951 P2P_GO_FREQ_MOVE_SCM) {
9952 policy_move = 1;
9953 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9954 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9955 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9956 policy_move = 1;
9957 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9958 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9959 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9960 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9961 policy_move = 1;
9962 } else if ((wpa_s->drv_flags &
9963 WPA_DRIVER_FLAGS_AP_CSA) &&
9964 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9965 u8 chan;
9966
9967 /*
9968 * We do not support CSA between bands, so move
9969 * GO only within the same band.
9970 */
9971 if (wpa_s->ap_iface->current_mode->mode ==
9972 ieee80211_freq_to_chan(freqs[i].freq,
9973 &chan))
9974 policy_move = 1;
9975 }
9976 }
9977 }
9978
9979 wpa_dbg(wpa_s, MSG_DEBUG,
9980 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9981 invalid_freq, policy_move, flags);
9982
9983 /*
9984 * The channel is valid, or we are going to have a policy move, so
9985 * cancel timeout.
9986 */
9987 if (!invalid_freq || policy_move) {
9988 wpa_dbg(wpa_s, MSG_DEBUG,
9989 "P2P: Cancel a GO move from freq=%d MHz", freq);
9990 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9991
9992 if (wpas_p2p_in_progress(wpa_s)) {
9993 wpa_dbg(wpa_s, MSG_DEBUG,
9994 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9995 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9996 wpa_s, NULL);
9997 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9998 wpas_p2p_reconsider_moving_go,
9999 wpa_s, NULL);
10000 return;
10001 }
10002 }
10003
10004 if (!invalid_freq && (!policy_move || flags != 0)) {
10005 wpa_dbg(wpa_s, MSG_DEBUG,
10006 "P2P: Not initiating a GO frequency change");
10007 return;
10008 }
10009
10010 /*
10011 * Do not consider moving GO if it is in the middle of a CSA. When the
10012 * CSA is finished this flow should be retriggered.
10013 */
10014 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10015 wpa_dbg(wpa_s, MSG_DEBUG,
10016 "P2P: Not initiating a GO frequency change - CSA is in progress");
10017 return;
10018 }
10019
10020 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10021 timeout = P2P_GO_FREQ_CHANGE_TIME;
10022 else
10023 timeout = 0;
10024
10025 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10026 freq, timeout);
10027 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10028 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10029}
10030
10031
10032static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10033 struct wpa_used_freq_data *freqs,
10034 unsigned int num,
10035 enum wpas_p2p_channel_update_trig trig)
10036{
10037 struct wpa_supplicant *ifs;
10038
10039 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10040 NULL);
10041
10042 /*
10043 * Travers all the radio interfaces, and for each GO interface, check
10044 * if there is a need to move the GO from the frequency it is using,
10045 * or in case the frequency is valid again, cancel the evacuation flow.
10046 */
10047 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10048 radio_list) {
10049 if (ifs->current_ssid == NULL ||
10050 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10051 continue;
10052
10053 /*
10054 * The GO was just started or completed channel switch, no need
10055 * to move it.
10056 */
10057 if (wpa_s == ifs &&
10058 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10059 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10060 wpa_dbg(wpa_s, MSG_DEBUG,
10061 "P2P: GO move - schedule re-consideration");
10062 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10063 wpas_p2p_reconsider_moving_go,
10064 wpa_s, NULL);
10065 continue;
10066 }
10067
10068 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10069 }
10070}
10071
10072
10073void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10074{
10075 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10076 return;
10077
10078 wpas_p2p_update_channel_list(wpa_s,
10079 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10080}
10081
10082
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010083void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10084{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010085 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10086 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10087 "the management interface is being removed");
10088 wpas_p2p_deinit_global(wpa_s->global);
10089 }
10090}
10091
10092
10093void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10094{
10095 if (wpa_s->ap_iface->bss)
10096 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10097 wpas_p2p_group_deinit(wpa_s);
10098}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010099
10100
10101int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10102 unsigned int period, unsigned int interval,
10103 unsigned int count)
10104{
10105 struct p2p_data *p2p = wpa_s->global->p2p;
10106 u8 *device_types;
10107 size_t dev_types_len;
10108 struct wpabuf *buf;
10109 int ret;
10110
10111 if (wpa_s->p2p_lo_started) {
10112 wpa_dbg(wpa_s, MSG_DEBUG,
10113 "P2P Listen offload is already started");
10114 return 0;
10115 }
10116
10117 if (wpa_s->global->p2p == NULL ||
10118 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10119 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10120 return -1;
10121 }
10122
10123 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10124 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10125 freq);
10126 return -1;
10127 }
10128
10129 /* Get device type */
10130 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10131 WPS_DEV_TYPE_LEN;
10132 device_types = os_malloc(dev_types_len);
10133 if (!device_types)
10134 return -1;
10135 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10136 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10137 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10138
10139 /* Get Probe Response IE(s) */
10140 buf = p2p_build_probe_resp_template(p2p, freq);
10141 if (!buf) {
10142 os_free(device_types);
10143 return -1;
10144 }
10145
10146 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10147 device_types, dev_types_len,
10148 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10149 if (ret < 0)
10150 wpa_dbg(wpa_s, MSG_DEBUG,
10151 "P2P: Failed to start P2P listen offload");
10152
10153 os_free(device_types);
10154 wpabuf_free(buf);
10155
10156 if (ret == 0) {
10157 wpa_s->p2p_lo_started = 1;
10158
10159 /* Stop current P2P listen if any */
10160 wpas_stop_listen(wpa_s);
10161 }
10162
10163 return ret;
10164}
10165
10166
10167int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10168{
10169 int ret;
10170
10171 if (!wpa_s->p2p_lo_started)
10172 return 0;
10173
10174 ret = wpa_drv_p2p_lo_stop(wpa_s);
10175 if (ret < 0)
10176 wpa_dbg(wpa_s, MSG_DEBUG,
10177 "P2P: Failed to stop P2P listen offload");
10178
10179 wpa_s->p2p_lo_started = 0;
10180 return ret;
10181}