blob: a8cab8dc7927ad28e7b4a9cedeb9bb07b56ab4db [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +0000132 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
169
Hai Shalomc1a21442022-02-04 13:43:00 -0800170static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
171{
172 int he_capab = 0;
173
174 if (mode)
175 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
176 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
177 return he_capab;
178}
179
180
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700181/*
182 * Get the number of concurrent channels that the HW can operate, but that are
183 * currently not in use by any of the wpa_supplicant interfaces.
184 */
185static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
186{
187 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800188 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700189
190 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
191 if (!freqs)
192 return -1;
193
194 num = get_shared_radio_freqs(wpa_s, freqs,
195 wpa_s->num_multichan_concurrent);
196 os_free(freqs);
197
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800198 unused = wpa_s->num_multichan_concurrent - num;
199 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
200 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700201}
202
203
204/*
205 * Get the frequencies that are currently in use by one or more of the virtual
206 * interfaces, and that are also valid for P2P operation.
207 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700208static unsigned int
209wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
210 struct wpa_used_freq_data *p2p_freqs,
211 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700212{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 unsigned int num, i, j;
215
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700216 freqs = os_calloc(wpa_s->num_multichan_concurrent,
217 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700218 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700220
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700221 num = get_shared_radio_freqs_data(wpa_s, freqs,
222 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700223
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700224 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700225
226 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700227 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228 p2p_freqs[j++] = freqs[i];
229 }
230
231 os_free(freqs);
232
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700233 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800234
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700235 return j;
236}
237
238
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
240 int freq)
241{
242 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
243 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700244
245 /* Use the wpa_s used to control the P2P Device operation */
246 wpa_s = wpa_s->global->p2p_init_wpa_s;
247
248 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800249 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
250 wpas_p2p_num_unused_channels(wpa_s) > 0) {
251 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
252 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700253 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800254 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700255 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
256}
257
258
Hai Shalom60840252021-02-19 19:02:11 -0800259static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
260{
261 unsigned int delay = wpas_p2p_search_delay(wpa_s);
262
263 /* In case of concurrent P2P and external scans, delay P2P search. */
264 if (external_scan_running(wpa_s->radio)) {
265 delay = wpa_s->conf->p2p_search_delay;
266 wpa_printf(MSG_DEBUG,
267 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
268 delay);
269 }
270
271 p2p_scan_res_handled(wpa_s->global->p2p, delay);
272}
273
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
276 struct wpa_scan_results *scan_res)
277{
278 size_t i;
279
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800280 if (wpa_s->p2p_scan_work) {
281 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
282 wpa_s->p2p_scan_work = NULL;
283 radio_work_done(work);
284 }
285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
287 return;
288
289 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
290 (int) scan_res->num);
291
292 for (i = 0; i < scan_res->num; i++) {
293 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700295 const u8 *ies;
296 size_t ies_len;
297
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800298 time_tmp_age.sec = bss->age / 1000;
299 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700301
302 ies = (const u8 *) (bss + 1);
303 ies_len = bss->ie_len;
304 if (bss->beacon_ie_len > 0 &&
305 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
306 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
307 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
308 MACSTR, MAC2STR(bss->bssid));
309 ies = ies + ies_len;
310 ies_len = bss->beacon_ie_len;
311 }
312
313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800315 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700316 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 break;
318 }
319
Hai Shalom60840252021-02-19 19:02:11 -0800320 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321}
322
323
Hai Shalom60840252021-02-19 19:02:11 -0800324static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700325{
Hai Shalom60840252021-02-19 19:02:11 -0800326 if (wpa_s->p2p_scan_work) {
327 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700328
Hai Shalom60840252021-02-19 19:02:11 -0800329 wpa_s->p2p_scan_work = NULL;
330 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700331 }
332
Hai Shalom60840252021-02-19 19:02:11 -0800333 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
334 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700335
Hai Shalom60840252021-02-19 19:02:11 -0800336 wpa_dbg(wpa_s, MSG_DEBUG,
337 "P2P: Failed to get scan results - try to continue");
338 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700339}
340
341
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800342static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
343{
344 struct wpa_supplicant *wpa_s = work->wpa_s;
345 struct wpa_driver_scan_params *params = work->ctx;
346 int ret;
347
348 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800349 if (!work->started) {
350 wpa_scan_free_params(params);
351 return;
352 }
353
354 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800355 return;
356 }
357
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800358 if (wpa_s->clear_driver_scan_cache) {
359 wpa_printf(MSG_DEBUG,
360 "Request driver to clear scan cache due to local BSS flush");
361 params->only_new_results = 1;
362 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700363
Hai Shalomc1a21442022-02-04 13:43:00 -0800364 if (!params->p2p_include_6ghz && !params->freqs) {
Hai Shalom899fcc72020-10-19 14:38:18 -0700365 wpa_printf(MSG_DEBUG,
Hai Shalomc1a21442022-02-04 13:43:00 -0800366 "P2P: Exclude 6 GHz channels - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -0800367 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
368 0);
369 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
370 0);
Hai Shalom899fcc72020-10-19 14:38:18 -0700371 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800373 if (ret == 0)
374 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 wpa_scan_free_params(params);
376 work->ctx = NULL;
377 if (ret) {
378 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800380 return;
381 }
382
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800383 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800384 os_get_reltime(&wpa_s->scan_trigger_time);
385 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800386 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800387 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800388 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800389 wpa_s->p2p_scan_work = work;
390}
391
392
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800393static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
394 int freq)
395{
396 if (wpa_s->global->p2p_24ghz_social_channels &&
397 (freq == 2412 || freq == 2437 || freq == 2462)) {
398 /*
399 * Search all social channels regardless of whether these have
400 * been disabled for P2P operating channel use to avoid missing
401 * peers.
402 */
403 return 1;
404 }
405 return p2p_supported_freq(wpa_s->global->p2p, freq);
406}
407
408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
410 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800411 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
412 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413{
414 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800415 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700417 unsigned int num_channels = 0;
418 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800419 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700420 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800421 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422
423 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
424 return -1;
425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800426 if (wpa_s->p2p_scan_work) {
427 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
428 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 }
430
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800431 params = os_zalloc(sizeof(*params));
432 if (params == NULL)
433 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434
435 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800436 params->num_ssids = 1;
437 n = os_malloc(P2P_WILDCARD_SSID_LEN);
438 if (n == NULL)
439 goto fail;
440 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
441 params->ssids[0].ssid = n;
442 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443
444 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
446 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 num_req_dev_types, req_dev_types);
448 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800449 goto fail;
Hai Shalomc1a21442022-02-04 13:43:00 -0800450 if (!wpa_s->conf->p2p_6ghz_disable)
451 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 switch (type) {
453 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700454 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
455 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800456 if (params->freqs == NULL)
457 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700458 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800459 if (wpas_p2p_search_social_channel(
460 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700461 params->freqs[num_channels++] =
462 social_channels_freq[i];
463 }
464 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 break;
466 case P2P_SCAN_FULL:
467 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800468 case P2P_SCAN_SPECIFIC:
469 params->freqs = os_calloc(2, sizeof(int));
470 if (params->freqs == NULL)
471 goto fail;
472 params->freqs[0] = freq;
473 params->freqs[1] = 0;
474 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
477 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800478 if (params->freqs == NULL)
479 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700480 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481 if (wpas_p2p_search_social_channel(
482 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700483 params->freqs[num_channels++] =
484 social_channels_freq[i];
485 }
486 if (p2p_supported_freq(wpa_s->global->p2p, freq))
487 params->freqs[num_channels++] = freq;
488 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489 break;
490 }
491
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800492 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
493 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
494 if (ies == NULL) {
495 wpabuf_free(wps_ie);
496 goto fail;
497 }
498 wpabuf_put_buf(ies, wps_ie);
499 wpabuf_free(wps_ie);
500
501 bands = wpas_get_bands(wpa_s, params->freqs);
502 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
503
504 params->p2p_probe = 1;
505 n = os_malloc(wpabuf_len(ies));
506 if (n == NULL) {
507 wpabuf_free(ies);
508 goto fail;
509 }
510 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
511 params->extra_ies = n;
512 params->extra_ies_len = wpabuf_len(ies);
513 wpabuf_free(ies);
514
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800515 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800516 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
517 params) < 0)
518 goto fail;
519 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800521fail:
522 wpa_scan_free_params(params);
523 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524}
525
526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
528{
529 switch (p2p_group_interface) {
530 case P2P_GROUP_INTERFACE_PENDING:
531 return WPA_IF_P2P_GROUP;
532 case P2P_GROUP_INTERFACE_GO:
533 return WPA_IF_P2P_GO;
534 case P2P_GROUP_INTERFACE_CLIENT:
535 return WPA_IF_P2P_CLIENT;
536 }
537
538 return WPA_IF_P2P_GROUP;
539}
540
541
542static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
543 const u8 *ssid,
544 size_t ssid_len, int *go)
545{
546 struct wpa_ssid *s;
547
548 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
549 for (s = wpa_s->conf->ssid; s; s = s->next) {
550 if (s->disabled != 0 || !s->p2p_group ||
551 s->ssid_len != ssid_len ||
552 os_memcmp(ssid, s->ssid, ssid_len) != 0)
553 continue;
554 if (s->mode == WPAS_MODE_P2P_GO &&
555 s != wpa_s->current_ssid)
556 continue;
557 if (go)
558 *go = s->mode == WPAS_MODE_P2P_GO;
559 return wpa_s;
560 }
561 }
562
563 return NULL;
564}
565
566
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800567static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
568{
569 struct wpa_supplicant *wpa_s = eloop_ctx;
570 wpa_printf(MSG_DEBUG,
571 "P2P: Complete previously requested removal of %s",
572 wpa_s->ifname);
573 wpas_p2p_disconnect(wpa_s);
574}
575
576
577static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
578 struct wpa_supplicant *calling_wpa_s)
579{
580 if (calling_wpa_s == wpa_s && wpa_s &&
581 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
582 /*
583 * The calling wpa_s instance is going to be removed. Do that
584 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700585 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800586 * control interface responses on the per-interface socket.
587 */
588 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
589 wpa_s, NULL) < 0)
590 return -1;
591 return 0;
592 }
593
594 return wpas_p2p_disconnect(wpa_s);
595}
596
597
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800598/* Determine total number of clients in active groups where we are the GO */
599static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
600{
601 unsigned int count = 0;
602 struct wpa_ssid *s;
603
604 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
605 for (s = wpa_s->conf->ssid; s; s = s->next) {
606 wpa_printf(MSG_DEBUG,
607 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
608 wpa_s, s, s->disabled, s->p2p_group,
609 s->mode);
610 if (!s->disabled && s->p2p_group &&
611 s->mode == WPAS_MODE_P2P_GO) {
612 count += p2p_get_group_num_members(
613 wpa_s->p2p_group);
614 }
615 }
616 }
617
618 return count;
619}
620
621
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800622static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
623{
624 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
625 !wpa_s->current_ssid->disabled &&
626 wpa_s->current_ssid->p2p_group &&
627 wpa_s->current_ssid->p2p_persistent_group;
628}
629
630
631static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
632{
633 return p2p_is_active_persistent_group(wpa_s) &&
634 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
635}
636
637
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800638/* Find an interface for a P2P group where we are the GO */
639static struct wpa_supplicant *
640wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
641{
642 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800643
644 if (!wpa_s)
645 return NULL;
646
647 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 if (!p2p_is_active_persistent_go(wpa_s))
649 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800650
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800651 /* Prefer a group with connected clients */
652 if (p2p_get_group_num_members(wpa_s->p2p_group))
653 return wpa_s;
654 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800655 }
656
657 /* No group with connected clients, so pick the one without (if any) */
658 return save;
659}
660
661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800662static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800663{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800664 return p2p_is_active_persistent_group(wpa_s) &&
665 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
666}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800667
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800668
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800669/* Find an interface for a P2P group where we are the P2P Client */
670static struct wpa_supplicant *
671wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
672{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800673 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800674 if (p2p_is_active_persistent_cli(wpa_s))
675 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800676 }
677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800679}
680
681
682/* Find a persistent group where we are the GO */
683static struct wpa_ssid *
684wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
685{
686 struct wpa_ssid *s;
687
688 for (s = wpa_s->conf->ssid; s; s = s->next) {
689 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
690 return s;
691 }
692
693 return NULL;
694}
695
696
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800697static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
698 unsigned int *force_freq,
699 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800700{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800701 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800702 struct wpa_ssid *s;
703 u8 conncap = P2PS_SETUP_NONE;
704 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800705 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800706 struct wpa_ssid *persistent_go;
707 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000708 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
709 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800710
711 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
712
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800713 if (force_freq)
714 *force_freq = 0;
715 if (pref_freq)
716 *pref_freq = 0;
717
718 size = P2P_MAX_PREF_CHANNELS;
719 if (force_freq && pref_freq &&
720 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
721 (int *) pref_freq, 0, pref_freq_list, &size))
722 wpas_p2p_set_own_freq_preference(wpa_s,
723 *force_freq ? *force_freq :
724 *pref_freq);
725
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800726 /*
727 * For non-concurrent capable devices:
728 * If persistent_go, then no new.
729 * If GO, then no client.
730 * If client, then no GO.
731 */
732 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800733 if (go_wpa_s)
734 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800735 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800736 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
737 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800738
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800739 wpa_printf(MSG_DEBUG,
740 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
741 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800742
743 /* If not concurrent, restrict our choices */
744 if (p2p_no_group_iface) {
745 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
746
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800747 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800748 return P2PS_SETUP_NONE;
749
750 if (go_wpa_s) {
751 if (role == P2PS_SETUP_CLIENT ||
752 incoming == P2PS_SETUP_GROUP_OWNER ||
753 p2p_client_limit_reached(go_wpa_s->p2p_group))
754 return P2PS_SETUP_NONE;
755
756 return P2PS_SETUP_GROUP_OWNER;
757 }
758
759 if (persistent_go) {
760 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
761 if (!incoming)
762 return P2PS_SETUP_GROUP_OWNER |
763 P2PS_SETUP_CLIENT;
764 if (incoming == P2PS_SETUP_NEW) {
765 u8 r;
766
767 if (os_get_random(&r, sizeof(r)) < 0 ||
768 (r & 1))
769 return P2PS_SETUP_CLIENT;
770 return P2PS_SETUP_GROUP_OWNER;
771 }
772 }
773 }
774 }
775
776 /* If a required role has been specified, handle it here */
777 if (role && role != P2PS_SETUP_NEW) {
778 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800779 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
780 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
781 /*
782 * Peer has an active GO, so if the role allows it and
783 * we do not have any active roles, become client.
784 */
785 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
786 !cli_wpa_s)
787 return P2PS_SETUP_CLIENT;
788
789 /* fall through */
790
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800791 case P2PS_SETUP_NONE:
792 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800793 conncap = role;
794 goto grp_owner;
795
796 case P2PS_SETUP_GROUP_OWNER:
797 /*
798 * Must be a complimentary role - cannot be a client to
799 * more than one peer.
800 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800802 return P2PS_SETUP_NONE;
803
804 return P2PS_SETUP_CLIENT;
805
806 case P2PS_SETUP_CLIENT:
807 /* Must be a complimentary role */
808 if (incoming != role) {
809 conncap = P2PS_SETUP_GROUP_OWNER;
810 goto grp_owner;
811 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700812 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813
814 default:
815 return P2PS_SETUP_NONE;
816 }
817 }
818
819 /*
820 * For now, we only will support ownership of one group, and being a
821 * client of one group. Therefore, if we have either an existing GO
822 * group, or an existing client group, we will not do a new GO
823 * negotiation, but rather try to re-use the existing groups.
824 */
825 switch (incoming) {
826 case P2PS_SETUP_NONE:
827 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800828 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800829 conncap = P2PS_SETUP_GROUP_OWNER;
830 else if (!owned_members)
831 conncap = P2PS_SETUP_NEW;
832 else if (incoming == P2PS_SETUP_NONE)
833 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
834 else
835 conncap = P2PS_SETUP_CLIENT;
836 break;
837
838 case P2PS_SETUP_CLIENT:
839 conncap = P2PS_SETUP_GROUP_OWNER;
840 break;
841
842 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800843 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800844 conncap = P2PS_SETUP_CLIENT;
845 break;
846
847 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
848 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800849 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800850 conncap = P2PS_SETUP_GROUP_OWNER;
851 else {
852 u8 r;
853
854 if (os_get_random(&r, sizeof(r)) < 0 ||
855 (r & 1))
856 conncap = P2PS_SETUP_CLIENT;
857 else
858 conncap = P2PS_SETUP_GROUP_OWNER;
859 }
860 break;
861
862 default:
863 return P2PS_SETUP_NONE;
864 }
865
866grp_owner:
867 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
868 (!incoming && (conncap & P2PS_SETUP_NEW))) {
869 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
870 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800871
872 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800873 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800874 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800875 wpa_s->p2p_mgmt ?
876 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800877 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800878 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800879 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800880 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800881 wpa_printf(MSG_ERROR,
882 "P2P: Failed to allocate a new interface for the group");
883 return P2PS_SETUP_NONE;
884 }
885 wpa_s->global->pending_group_iface_for_p2ps = 1;
886 p2p_set_intended_addr(wpa_s->global->p2p,
887 wpa_s->pending_interface_addr);
888 }
889 }
890
891 return conncap;
892}
893
894
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
896 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897{
898 struct wpa_ssid *ssid;
899 char *gtype;
900 const char *reason;
901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 ssid = wpa_s->current_ssid;
903 if (ssid == NULL) {
904 /*
905 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700906 * pending P2P group interface waiting for provisioning or a
907 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 */
909 ssid = wpa_s->conf->ssid;
910 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700911 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 break;
913 ssid = ssid->next;
914 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300915 if (ssid == NULL &&
916 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
917 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700918 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
919 "not found");
920 return -1;
921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 }
923 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
924 gtype = "GO";
925 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
926 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
927 wpa_s->reassociate = 0;
928 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 gtype = "client";
930 } else
931 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700932
933 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
934 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
935
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800936 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700937 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800938 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
939 wpa_s, NULL)) {
940 wpa_printf(MSG_DEBUG,
941 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
942 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
943 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
944 wpa_s, NULL);
945 }
946 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 if (wpa_s->cross_connect_in_use) {
949 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800950 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700951 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
952 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 case P2P_GROUP_REMOVAL_REQUESTED:
956 reason = " reason=REQUESTED";
957 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700958 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
959 reason = " reason=FORMATION_FAILED";
960 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
962 reason = " reason=IDLE";
963 break;
964 case P2P_GROUP_REMOVAL_UNAVAILABLE:
965 reason = " reason=UNAVAILABLE";
966 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
968 reason = " reason=GO_ENDING_SESSION";
969 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700970 case P2P_GROUP_REMOVAL_PSK_FAILURE:
971 reason = " reason=PSK_FAILURE";
972 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800973 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
974 reason = " reason=FREQ_CONFLICT";
975 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 default:
977 reason = "";
978 break;
979 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800981 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700982 P2P_EVENT_GROUP_REMOVED "%s %s%s",
983 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800986 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
987 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700988 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
989 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800990 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800991 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800992 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
993 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700994 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800995 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700996 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997
Dmitry Shmidt15907092014-03-25 10:42:57 -0700998 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800999 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1000 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001001
Dmitry Shmidt96571392013-10-14 12:54:46 -07001002 /*
1003 * Make sure wait for the first client does not remain active after the
1004 * group has been removed.
1005 */
1006 wpa_s->global->p2p_go_wait_client.sec = 0;
1007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1009 struct wpa_global *global;
1010 char *ifname;
1011 enum wpa_driver_if_type type;
1012 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1013 wpa_s->ifname);
1014 global = wpa_s->global;
1015 ifname = os_strdup(wpa_s->ifname);
1016 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001017 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001018 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 wpa_s = global->ifaces;
1020 if (wpa_s && ifname)
1021 wpa_drv_if_remove(wpa_s, type, ifname);
1022 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001023 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 }
1025
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001026 /*
1027 * The primary interface was used for P2P group operations, so
1028 * need to reset its p2pdev.
1029 */
1030 wpa_s->p2pdev = wpa_s->parent;
1031
Dmitry Shmidt96571392013-10-14 12:54:46 -07001032 if (!wpa_s->p2p_go_group_formation_completed) {
1033 wpa_s->global->p2p_group_formation = NULL;
1034 wpa_s->p2p_in_provisioning = 0;
1035 }
1036
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001037 wpa_s->show_group_started = 0;
1038 os_free(wpa_s->go_params);
1039 wpa_s->go_params = NULL;
1040
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001041 os_free(wpa_s->p2p_group_common_freqs);
1042 wpa_s->p2p_group_common_freqs = NULL;
1043 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001044 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001045 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001046
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001047 wpa_s->waiting_presence_resp = 0;
1048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1050 if (ssid && (ssid->p2p_group ||
1051 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1052 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1053 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001054 if (ssid == wpa_s->current_ssid) {
1055 wpa_sm_set_config(wpa_s->wpa, NULL);
1056 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001058 }
1059 /*
1060 * Networks objects created during any P2P activities are not
1061 * exposed out as they might/will confuse certain non-P2P aware
1062 * applications since these network objects won't behave like
1063 * regular ones.
1064 *
1065 * Likewise, we don't send out network removed signals for such
1066 * network objects.
1067 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 wpa_config_remove_network(wpa_s->conf, id);
1069 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001070 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 } else {
1072 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1073 "found");
1074 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001075 if (wpa_s->ap_iface)
1076 wpa_supplicant_ap_deinit(wpa_s);
1077 else
1078 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001079
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001080 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1081
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001082 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083}
1084
1085
1086static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1087 u8 *go_dev_addr,
1088 const u8 *ssid, size_t ssid_len)
1089{
1090 struct wpa_bss *bss;
1091 const u8 *bssid;
1092 struct wpabuf *p2p;
1093 u8 group_capab;
1094 const u8 *addr;
1095
1096 if (wpa_s->go_params)
1097 bssid = wpa_s->go_params->peer_interface_addr;
1098 else
1099 bssid = wpa_s->bssid;
1100
1101 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001102 if (bss == NULL && wpa_s->go_params &&
1103 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1104 bss = wpa_bss_get_p2p_dev_addr(
1105 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 if (bss == NULL) {
1107 u8 iface_addr[ETH_ALEN];
1108 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1109 iface_addr) == 0)
1110 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1111 }
1112 if (bss == NULL) {
1113 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1114 "group is persistent - BSS " MACSTR " not found",
1115 MAC2STR(bssid));
1116 return 0;
1117 }
1118
1119 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001120 if (p2p == NULL)
1121 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1122 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 if (p2p == NULL) {
1124 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1125 "group is persistent - BSS " MACSTR
1126 " did not include P2P IE", MAC2STR(bssid));
1127 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001128 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001130 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 bss->beacon_ie_len);
1132 return 0;
1133 }
1134
1135 group_capab = p2p_get_group_capab(p2p);
1136 addr = p2p_get_go_dev_addr(p2p);
1137 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1138 "group_capab=0x%x", group_capab);
1139 if (addr) {
1140 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1141 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1142 MAC2STR(addr));
1143 } else
1144 os_memset(go_dev_addr, 0, ETH_ALEN);
1145 wpabuf_free(p2p);
1146
1147 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1148 "go_dev_addr=" MACSTR,
1149 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1150
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001151 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152}
1153
1154
Jouni Malinen75ecf522011-06-27 15:19:46 -07001155static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1156 struct wpa_ssid *ssid,
1157 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158{
1159 struct wpa_ssid *s;
1160 int changed = 0;
1161
1162 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1163 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1164 for (s = wpa_s->conf->ssid; s; s = s->next) {
1165 if (s->disabled == 2 &&
1166 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1167 s->ssid_len == ssid->ssid_len &&
1168 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1169 break;
1170 }
1171
1172 if (s) {
1173 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1174 "entry");
1175 if (ssid->passphrase && !s->passphrase)
1176 changed = 1;
1177 else if (ssid->passphrase && s->passphrase &&
1178 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1179 changed = 1;
1180 } else {
1181 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1182 "entry");
1183 changed = 1;
1184 s = wpa_config_add_network(wpa_s->conf);
1185 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001186 return -1;
1187
1188 /*
1189 * Instead of network_added we emit persistent_group_added
1190 * notification. Also to keep the defense checks in
1191 * persistent_group obj registration method, we set the
1192 * relevant flags in s to designate it as a persistent group.
1193 */
1194 s->p2p_group = 1;
1195 s->p2p_persistent_group = 1;
1196 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 wpa_config_set_network_defaults(s);
1198 }
1199
1200 s->p2p_group = 1;
1201 s->p2p_persistent_group = 1;
1202 s->disabled = 2;
1203 s->bssid_set = 1;
1204 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1205 s->mode = ssid->mode;
1206 s->auth_alg = WPA_AUTH_ALG_OPEN;
1207 s->key_mgmt = WPA_KEY_MGMT_PSK;
1208 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001209 s->pbss = ssid->pbss;
1210 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 s->export_keys = 1;
1212 if (ssid->passphrase) {
1213 os_free(s->passphrase);
1214 s->passphrase = os_strdup(ssid->passphrase);
1215 }
1216 if (ssid->psk_set) {
1217 s->psk_set = 1;
1218 os_memcpy(s->psk, ssid->psk, 32);
1219 }
1220 if (s->passphrase && !s->psk_set)
1221 wpa_config_update_psk(s);
1222 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1223 os_free(s->ssid);
1224 s->ssid = os_malloc(ssid->ssid_len);
1225 }
1226 if (s->ssid) {
1227 s->ssid_len = ssid->ssid_len;
1228 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1229 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001230 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1231 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1232 wpa_s->global->add_psk = NULL;
1233 changed = 1;
1234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 if (changed && wpa_s->conf->update_config &&
1237 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1238 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1239 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001240
1241 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242}
1243
1244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1246 const u8 *addr)
1247{
1248 struct wpa_ssid *ssid, *s;
1249 u8 *n;
1250 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001251 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001252 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001253
1254 ssid = wpa_s->current_ssid;
1255 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1256 !ssid->p2p_persistent_group)
1257 return;
1258
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001259 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1261 continue;
1262
1263 if (s->ssid_len == ssid->ssid_len &&
1264 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1265 break;
1266 }
1267
1268 if (s == NULL)
1269 return;
1270
1271 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001272 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001273 ETH_ALEN) != 0)
1274 continue;
1275
1276 if (i == s->num_p2p_clients - 1)
1277 return; /* already the most recent entry */
1278
1279 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001280 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1281 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1282 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001283 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001284 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1285 ETH_ALEN);
1286 os_memset(s->p2p_client_list +
1287 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1288 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001289 found = 1;
1290 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 }
1292
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001293 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1294 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001295 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001296 if (n == NULL)
1297 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001298 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1299 ETH_ALEN);
1300 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1301 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001302 s->p2p_client_list = n;
1303 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001304 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001305 /* Not enough room for an additional entry - drop the oldest
1306 * entry */
1307 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001308 s->p2p_client_list + 2 * ETH_ALEN,
1309 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001310 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001311 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001312 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001313 os_memset(s->p2p_client_list +
1314 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1315 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001316 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001317
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001318 if (p2p_wpa_s->conf->update_config &&
1319 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001320 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001321}
1322
1323
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001324static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1325 int go, struct wpa_ssid *ssid, int freq,
1326 const u8 *psk, const char *passphrase,
1327 const u8 *go_dev_addr, int persistent,
1328 const char *extra)
1329{
1330 const char *ssid_txt;
1331 char psk_txt[65];
1332
1333 if (psk)
1334 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1335 else
1336 psk_txt[0] = '\0';
1337
1338 if (ssid)
1339 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1340 else
1341 ssid_txt = "";
1342
1343 if (passphrase && passphrase[0] == '\0')
1344 passphrase = NULL;
1345
1346 /*
1347 * Include PSK/passphrase only in the control interface message and
1348 * leave it out from the debug log entry.
1349 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001350 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001351 P2P_EVENT_GROUP_STARTED
1352 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1353 MACSTR "%s%s",
1354 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1355 psk ? " psk=" : "", psk_txt,
1356 passphrase ? " passphrase=\"" : "",
1357 passphrase ? passphrase : "",
1358 passphrase ? "\"" : "",
1359 MAC2STR(go_dev_addr),
1360 persistent ? " [PERSISTENT]" : "", extra);
1361 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1362 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1363 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1364 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1365 extra);
1366}
1367
1368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001370 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371{
1372 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 int client;
1374 int persistent;
1375 u8 go_dev_addr[ETH_ALEN];
1376
1377 /*
1378 * This callback is likely called for the main interface. Update wpa_s
1379 * to use the group interface if a new interface was created for the
1380 * group.
1381 */
1382 if (wpa_s->global->p2p_group_formation)
1383 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001384 if (wpa_s->p2p_go_group_formation_completed) {
1385 wpa_s->global->p2p_group_formation = NULL;
1386 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001387 } else if (wpa_s->p2p_in_provisioning && !success) {
1388 wpa_msg(wpa_s, MSG_DEBUG,
1389 "P2P: Stop provisioning state due to failure");
1390 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001391 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001392 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001393 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394
1395 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001396 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001397 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001398 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001399 if (already_deleted)
1400 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001401 wpas_p2p_group_delete(wpa_s,
1402 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 return;
1404 }
1405
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001406 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001407 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408
1409 ssid = wpa_s->current_ssid;
1410 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1411 ssid->mode = WPAS_MODE_P2P_GO;
1412 p2p_group_notif_formation_done(wpa_s->p2p_group);
1413 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1414 }
1415
1416 persistent = 0;
1417 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 client = ssid->mode == WPAS_MODE_INFRA;
1419 if (ssid->mode == WPAS_MODE_P2P_GO) {
1420 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001421 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1422 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001423 } else {
1424 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 persistent = wpas_p2p_persistent_group(wpa_s,
1426 go_dev_addr,
1427 ssid->ssid,
1428 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 client = wpa_s->p2p_group_interface ==
1432 P2P_GROUP_INTERFACE_CLIENT;
1433 os_memset(go_dev_addr, 0, ETH_ALEN);
1434 }
1435
1436 wpa_s->show_group_started = 0;
1437 if (client) {
1438 /*
1439 * Indicate event only after successfully completed 4-way
1440 * handshake, i.e., when the interface is ready for data
1441 * packets.
1442 */
1443 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001445 wpas_p2p_group_started(wpa_s, 1, ssid,
1446 ssid ? ssid->frequency : 0,
1447 ssid && ssid->passphrase == NULL &&
1448 ssid->psk_set ? ssid->psk : NULL,
1449 ssid ? ssid->passphrase : NULL,
1450 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 wpas_p2p_cross_connect_setup(wpa_s);
1452 wpas_p2p_set_group_idle_timeout(wpa_s);
1453 }
1454
1455 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001456 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1457 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001458 else {
1459 os_free(wpa_s->global->add_psk);
1460 wpa_s->global->add_psk = NULL;
1461 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001462
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001463 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001464 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001465 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467}
1468
1469
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001470struct send_action_work {
1471 unsigned int freq;
1472 u8 dst[ETH_ALEN];
1473 u8 src[ETH_ALEN];
1474 u8 bssid[ETH_ALEN];
1475 size_t len;
1476 unsigned int wait_time;
1477 u8 buf[0];
1478};
1479
1480
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001481static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1482{
1483 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1484
1485 wpa_printf(MSG_DEBUG,
1486 "P2P: Free Action frame radio work @%p (freq=%u dst="
1487 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1488 wpa_s->p2p_send_action_work, awork->freq,
1489 MAC2STR(awork->dst), MAC2STR(awork->src),
1490 MAC2STR(awork->bssid), awork->wait_time);
1491 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1492 awork->buf, awork->len);
1493 os_free(awork);
1494 wpa_s->p2p_send_action_work->ctx = NULL;
1495 radio_work_done(wpa_s->p2p_send_action_work);
1496 wpa_s->p2p_send_action_work = NULL;
1497}
1498
1499
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001500static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1501 void *timeout_ctx)
1502{
1503 struct wpa_supplicant *wpa_s = eloop_ctx;
1504
1505 if (!wpa_s->p2p_send_action_work)
1506 return;
1507
1508 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001509 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001510}
1511
1512
Dmitry Shmidt03658832014-08-13 11:03:49 -07001513static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001515 if (wpa_s->p2p_send_action_work) {
1516 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001517
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001518 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001519 wpa_printf(MSG_DEBUG,
1520 "P2P: Clear Action TX work @%p (wait_time=%u)",
1521 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001522 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001523 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524 } else {
1525 /*
1526 * In theory, this should not be needed, but number of
1527 * places in the P2P code is still using non-zero wait
1528 * time for the last Action frame in the sequence and
1529 * some of these do not call send_action_done().
1530 */
1531 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1532 wpa_s, NULL);
1533 eloop_register_timeout(
1534 0, awork->wait_time * 1000,
1535 wpas_p2p_send_action_work_timeout,
1536 wpa_s, NULL);
1537 }
1538 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001539}
1540
1541
1542static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1543 unsigned int freq,
1544 const u8 *dst, const u8 *src,
1545 const u8 *bssid,
1546 const u8 *data, size_t data_len,
1547 enum offchannel_send_action_result
1548 result)
1549{
1550 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1551
1552 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1555 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001557 switch (result) {
1558 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1559 res = P2P_SEND_ACTION_SUCCESS;
1560 break;
1561 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1562 res = P2P_SEND_ACTION_NO_ACK;
1563 break;
1564 case OFFCHANNEL_SEND_ACTION_FAILED:
1565 res = P2P_SEND_ACTION_FAILED;
1566 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 }
1568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001569 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001571 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1572 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001573 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001574 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1575 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001577 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1578 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001579 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001580 P2P_EVENT_FALLBACK_TO_GO_NEG
1581 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001582 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1583 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 }
1585}
1586
1587
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001588static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1589{
1590 struct wpa_supplicant *wpa_s = work->wpa_s;
1591 struct send_action_work *awork = work->ctx;
1592
1593 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001594 if (work->started) {
1595 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1596 wpa_s, NULL);
1597 wpa_s->p2p_send_action_work = NULL;
1598 offchannel_send_action_done(wpa_s);
1599 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001600 os_free(awork);
1601 return;
1602 }
1603
1604 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1605 awork->bssid, awork->buf, awork->len,
1606 awork->wait_time,
1607 wpas_p2p_send_action_tx_status, 1) < 0) {
1608 os_free(awork);
1609 radio_work_done(work);
1610 return;
1611 }
1612 wpa_s->p2p_send_action_work = work;
1613}
1614
1615
1616static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1617 unsigned int freq, const u8 *dst,
1618 const u8 *src, const u8 *bssid, const u8 *buf,
1619 size_t len, unsigned int wait_time)
1620{
1621 struct send_action_work *awork;
1622
Hai Shalom81f62d82019-07-22 12:10:00 -07001623 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001624 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1625 return -1;
1626 }
1627
1628 awork = os_zalloc(sizeof(*awork) + len);
1629 if (awork == NULL)
1630 return -1;
1631
1632 awork->freq = freq;
1633 os_memcpy(awork->dst, dst, ETH_ALEN);
1634 os_memcpy(awork->src, src, ETH_ALEN);
1635 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1636 awork->len = len;
1637 awork->wait_time = wait_time;
1638 os_memcpy(awork->buf, buf, len);
1639
Hai Shalom81f62d82019-07-22 12:10:00 -07001640 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001641 wpas_send_action_cb, awork) < 0) {
1642 os_free(awork);
1643 return -1;
1644 }
1645
1646 return 0;
1647}
1648
1649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1651 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001652 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653{
1654 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001655 int listen_freq = -1, send_freq = -1;
1656
Hai Shalom021b0b52019-04-10 11:17:58 -07001657 if (scheduled)
1658 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001659 if (wpa_s->p2p_listen_work)
1660 listen_freq = wpa_s->p2p_listen_work->freq;
1661 if (wpa_s->p2p_send_action_work)
1662 send_freq = wpa_s->p2p_send_action_work->freq;
1663 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001664 int res;
1665
1666 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1667 listen_freq, send_freq, freq);
1668 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1669 len, wait_time);
1670 if (res == 0 && scheduled)
1671 *scheduled = 1;
1672 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001673 }
1674
1675 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001676 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1677 wait_time,
1678 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679}
1680
1681
1682static void wpas_send_action_done(void *ctx)
1683{
1684 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001685
1686 if (wpa_s->p2p_send_action_work) {
1687 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1688 wpa_s, NULL);
1689 os_free(wpa_s->p2p_send_action_work->ctx);
1690 radio_work_done(wpa_s->p2p_send_action_work);
1691 wpa_s->p2p_send_action_work = NULL;
1692 }
1693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001694 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695}
1696
1697
1698static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1699 struct p2p_go_neg_results *params)
1700{
1701 if (wpa_s->go_params == NULL) {
1702 wpa_s->go_params = os_malloc(sizeof(*params));
1703 if (wpa_s->go_params == NULL)
1704 return -1;
1705 }
1706 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1707 return 0;
1708}
1709
1710
1711static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1712 struct p2p_go_neg_results *res)
1713{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001714 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001715 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1716 " dev_addr " MACSTR " wps_method %d",
1717 MAC2STR(res->peer_interface_addr),
1718 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1720 res->ssid, res->ssid_len);
1721 wpa_supplicant_ap_deinit(wpa_s);
1722 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001723 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001724 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001725#ifdef CONFIG_WPS_NFC
1726 } else if (res->wps_method == WPS_NFC) {
1727 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1728 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001729 wpa_s->p2pdev->p2p_oob_dev_pw,
1730 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1731 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001732 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001733 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001734 NULL,
1735 NULL, 0, 0);
1736#endif /* CONFIG_WPS_NFC */
1737 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001739 if (wpa_s->p2p_wps_method == WPS_P2PS)
1740 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1742 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1743 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1744 wpa_s->p2p_pin, 1, dev_pw_id);
1745 }
1746}
1747
1748
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001749static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1750 struct wpa_ssid *ssid)
1751{
1752 struct wpa_ssid *persistent;
1753 struct psk_list_entry *psk;
1754 struct hostapd_data *hapd;
1755
1756 if (!wpa_s->ap_iface)
1757 return;
1758
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001759 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001760 ssid->ssid_len);
1761 if (persistent == NULL)
1762 return;
1763
1764 hapd = wpa_s->ap_iface->bss[0];
1765
1766 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1767 list) {
1768 struct hostapd_wpa_psk *hpsk;
1769
1770 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1771 MACSTR " psk=%d",
1772 MAC2STR(psk->addr), psk->p2p);
1773 hpsk = os_zalloc(sizeof(*hpsk));
1774 if (hpsk == NULL)
1775 break;
1776 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1777 if (psk->p2p)
1778 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1779 else
1780 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1781 hpsk->next = hapd->conf->ssid.wpa_psk;
1782 hapd->conf->ssid.wpa_psk = hpsk;
1783 }
1784}
1785
1786
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001787static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1788{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001789 char buf[20 + P2P_MAX_CHANNELS * 6];
1790 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001791 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001792 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001793
Roshan Pius3a1667e2018-07-03 15:17:14 -07001794 pos = buf;
1795 end = pos + sizeof(buf);
1796 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1797 res = os_snprintf(pos, end - pos, " %d",
1798 wpa_s->p2p_group_common_freqs[i]);
1799 if (os_snprintf_error(end - pos, res))
1800 break;
1801 pos += res;
1802 }
1803 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001804
Roshan Pius3a1667e2018-07-03 15:17:14 -07001805 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001806}
1807
1808
1809static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1810 struct p2p_go_neg_results *params)
1811{
1812 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1813
1814 wpa_s->p2p_group_common_freqs_num = 0;
1815 os_free(wpa_s->p2p_group_common_freqs);
1816 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1817 if (!wpa_s->p2p_group_common_freqs)
1818 return;
1819
1820 for (i = 0; i < len; i++) {
1821 if (!wpa_s->go_params->freq_list[i])
1822 break;
1823 wpa_s->p2p_group_common_freqs[i] =
1824 wpa_s->go_params->freq_list[i];
1825 }
1826 wpa_s->p2p_group_common_freqs_num = i;
1827}
1828
1829
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001830static void p2p_config_write(struct wpa_supplicant *wpa_s)
1831{
1832#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001833 if (wpa_s->p2pdev->conf->update_config &&
1834 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001835 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1836#endif /* CONFIG_NO_CONFIG_WRITE */
1837}
1838
1839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840static void p2p_go_configured(void *ctx, void *data)
1841{
1842 struct wpa_supplicant *wpa_s = ctx;
1843 struct p2p_go_neg_results *params = data;
1844 struct wpa_ssid *ssid;
1845
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001846 wpa_s->ap_configured_cb = NULL;
1847 wpa_s->ap_configured_cb_ctx = NULL;
1848 wpa_s->ap_configured_cb_data = NULL;
1849 if (!wpa_s->go_params) {
1850 wpa_printf(MSG_ERROR,
1851 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1852 return;
1853 }
1854
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001855 p2p_go_save_group_common_freqs(wpa_s, params);
1856 p2p_go_dump_common_freqs(wpa_s);
1857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001858 ssid = wpa_s->current_ssid;
1859 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1860 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1861 if (wpa_s->global->p2p_group_formation == wpa_s)
1862 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001863 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1864 params->passphrase[0] == '\0' ?
1865 params->psk : NULL,
1866 params->passphrase,
1867 wpa_s->global->p2p_dev_addr,
1868 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001869 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001870
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001871 if (wpa_s->p2pdev->p2ps_method_config_any) {
1872 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001873 wpa_dbg(wpa_s, MSG_DEBUG,
1874 "P2PS: Setting default PIN for ANY");
1875 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1876 "12345670", NULL, 0,
1877 0);
1878 } else {
1879 wpa_dbg(wpa_s, MSG_DEBUG,
1880 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001881 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001883 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001884 "12345670", NULL, 0, 0);
1885 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001886 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001887 }
1888
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001889 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001890 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001891 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001892 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001893 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001894 wpas_p2p_add_psk_list(wpa_s, ssid);
1895 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001896
1897 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001898 params->persistent_group, 0,
1899 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 wpas_p2p_cross_connect_setup(wpa_s);
1901 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001902
1903 if (wpa_s->p2p_first_connection_timeout) {
1904 wpa_dbg(wpa_s, MSG_DEBUG,
1905 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1906 wpa_s->p2p_first_connection_timeout);
1907 wpa_s->p2p_go_group_formation_completed = 0;
1908 wpa_s->global->p2p_group_formation = wpa_s;
1909 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001910 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001911 eloop_register_timeout(
1912 wpa_s->p2p_first_connection_timeout, 0,
1913 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001914 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001915 }
1916
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917 return;
1918 }
1919
1920 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1921 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1922 params->peer_interface_addr)) {
1923 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1924 "filtering");
1925 return;
1926 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001927 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001929 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001930#ifdef CONFIG_WPS_NFC
1931 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001932 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001933 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001934 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001935 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1936 return;
1937 }
1938 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001939 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1940 wpa_s->p2pdev->p2p_oob_dev_pw,
1941 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1942 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001943#endif /* CONFIG_WPS_NFC */
1944 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001946 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 os_free(wpa_s->go_params);
1948 wpa_s->go_params = NULL;
1949}
1950
1951
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001952/**
1953 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1954 * @freq: Frequency (MHz) to convert
1955 * @op_class: Buffer for returning operating class
1956 * @op_edmg_channel: Buffer for returning channel number
1957 * Returns: 0 on success, -1 on failure
1958 *
1959 * This can be used to find the highest channel bonding which includes the
1960 * specified frequency.
1961 */
1962static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1963 unsigned int freq,
1964 u8 *op_class, u8 *op_edmg_channel)
1965{
1966 struct hostapd_hw_modes *hwmode;
1967 struct ieee80211_edmg_config edmg;
1968 unsigned int i;
1969 enum chan_width chanwidth[] = {
1970 CHAN_WIDTH_8640,
1971 CHAN_WIDTH_6480,
1972 CHAN_WIDTH_4320,
1973 };
1974
1975 if (!wpa_s->hw.modes)
1976 return -1;
1977
1978 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08001979 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001980 if (!hwmode) {
1981 wpa_printf(MSG_ERROR,
1982 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1983 return -1;
1984 }
1985
1986 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1987 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1988 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1989 op_class,
1990 op_edmg_channel) < 0)
1991 continue;
1992
1993 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1994 if (edmg.channels &&
1995 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1996 wpa_printf(MSG_DEBUG,
1997 "Freq %u to EDMG channel %u at opclass %u",
1998 freq, *op_edmg_channel, *op_class);
1999 return 0;
2000 }
2001 }
2002
2003 return -1;
2004}
2005
2006
2007int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2008 struct p2p_go_neg_results *params)
2009{
2010 u8 op_channel, op_class;
2011 int freq;
2012
2013 /* Try social channel as primary channel frequency */
2014 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2015
2016 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2017 &op_channel) == 0) {
2018 wpa_printf(MSG_DEBUG,
2019 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2020 freq, op_channel, op_class);
2021 params->freq = freq;
2022 return 0;
2023 }
2024
2025 return -1;
2026}
2027
2028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2030 struct p2p_go_neg_results *params,
2031 int group_formation)
2032{
2033 struct wpa_ssid *ssid;
2034
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002035 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2036 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2037 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2038 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041
2042 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002043 if (ssid == NULL) {
2044 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002048 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002049 wpa_s->p2p_go_group_formation_completed = 0;
2050 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002051 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053 wpa_config_set_network_defaults(ssid);
2054 ssid->temporary = 1;
2055 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002056 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2058 WPAS_MODE_P2P_GO;
2059 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002060 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002061 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002062 ssid->max_oper_chwidth = params->max_oper_chwidth;
2063 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002064 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002065 if (params->edmg) {
2066 u8 op_channel, op_class;
2067
2068 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2069 &op_class, &op_channel)) {
2070 ssid->edmg_channel = op_channel;
2071 ssid->enable_edmg = params->edmg;
2072 } else {
2073 wpa_dbg(wpa_s, MSG_DEBUG,
2074 "P2P: Could not match EDMG channel, freq %d, for GO",
2075 params->freq);
2076 }
2077 }
2078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 ssid->ssid = os_zalloc(params->ssid_len + 1);
2080 if (ssid->ssid) {
2081 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2082 ssid->ssid_len = params->ssid_len;
2083 }
2084 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2085 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002086 if (is_6ghz_freq(ssid->frequency) &&
2087 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2088 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2089 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2090 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2091 ssid->sae_pwe = 1;
2092 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2093 } else {
2094 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2095 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 ssid->proto = WPA_PROTO_RSN;
2097 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002098 ssid->group_cipher = WPA_CIPHER_CCMP;
2099 if (params->freq > 56160) {
2100 /*
2101 * Enable GCMP instead of CCMP as pairwise_cipher and
2102 * group_cipher in 60 GHz.
2103 */
2104 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2105 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002106 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2107 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002108 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002109 if (os_strlen(params->passphrase) > 0) {
2110 ssid->passphrase = os_strdup(params->passphrase);
2111 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002112 wpa_msg_global(wpa_s, MSG_ERROR,
2113 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002114 wpa_config_remove_network(wpa_s->conf, ssid->id);
2115 return;
2116 }
2117 } else
2118 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002119 ssid->psk_set = params->psk_set;
2120 if (ssid->psk_set)
2121 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002122 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002123 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002124 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
2126 wpa_s->ap_configured_cb = p2p_go_configured;
2127 wpa_s->ap_configured_cb_ctx = wpa_s;
2128 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002129 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002130 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 wpa_s->reassociate = 1;
2132 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002133 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2134 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 wpa_supplicant_req_scan(wpa_s, 0, 0);
2136}
2137
2138
2139static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2140 const struct wpa_supplicant *src)
2141{
2142 struct wpa_config *d;
2143 const struct wpa_config *s;
2144
2145 d = dst->conf;
2146 s = src->conf;
2147
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002148#define C(n) \
2149do { \
2150 if (s->n && !d->n) \
2151 d->n = os_strdup(s->n); \
2152} while (0)
2153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 C(device_name);
2155 C(manufacturer);
2156 C(model_name);
2157 C(model_number);
2158 C(serial_number);
2159 C(config_methods);
2160#undef C
2161
2162 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2163 os_memcpy(d->sec_device_type, s->sec_device_type,
2164 sizeof(d->sec_device_type));
2165 d->num_sec_device_types = s->num_sec_device_types;
2166
2167 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002168 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002170 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002171 d->max_num_sta = s->max_num_sta;
2172 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002173 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002174 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002175 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002176 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002177 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002178 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002179 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002180 d->pmf = s->pmf;
2181 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002182
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002183 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2184 !d->wps_nfc_pw_from_config) {
2185 wpabuf_free(d->wps_nfc_dh_privkey);
2186 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002187 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2188 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2189 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002190 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002191 d->go_interworking = s->go_interworking;
2192 d->go_access_network_type = s->go_access_network_type;
2193 d->go_internet = s->go_internet;
2194 d->go_venue_group = s->go_venue_group;
2195 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002196 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197}
2198
2199
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002200static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2201 char *ifname, size_t len)
2202{
2203 char *ifname_ptr = wpa_s->ifname;
2204
2205 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2206 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2207 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2208 }
2209
2210 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2211 if (os_strlen(ifname) >= IFNAMSIZ &&
2212 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002213 int res;
2214
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002215 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002216 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2217 if (os_snprintf_error(len, res) && len)
2218 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002219 }
2220}
2221
2222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2224 enum wpa_driver_if_type type)
2225{
2226 char ifname[120], force_ifname[120];
2227
2228 if (wpa_s->pending_interface_name[0]) {
2229 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2230 "- skip creation of a new one");
2231 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2232 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2233 "unknown?! ifname='%s'",
2234 wpa_s->pending_interface_name);
2235 return -1;
2236 }
2237 return 0;
2238 }
2239
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002240 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 force_ifname[0] = '\0';
2242
2243 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2244 ifname);
2245 wpa_s->p2p_group_idx++;
2246
2247 wpa_s->pending_interface_type = type;
2248 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2249 wpa_s->pending_interface_addr, NULL) < 0) {
2250 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2251 "interface");
2252 return -1;
2253 }
2254
Jimmy Chen36c21992018-11-29 16:46:43 +08002255 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002256 random_mac_addr(wpa_s->pending_interface_addr);
2257 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2258 " for the group",
2259 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002260 }
2261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 if (force_ifname[0]) {
2263 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2264 force_ifname);
2265 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2266 sizeof(wpa_s->pending_interface_name));
2267 } else
2268 os_strlcpy(wpa_s->pending_interface_name, ifname,
2269 sizeof(wpa_s->pending_interface_name));
2270 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2271 MACSTR, wpa_s->pending_interface_name,
2272 MAC2STR(wpa_s->pending_interface_addr));
2273
2274 return 0;
2275}
2276
2277
2278static void wpas_p2p_remove_pending_group_interface(
2279 struct wpa_supplicant *wpa_s)
2280{
2281 if (!wpa_s->pending_interface_name[0] ||
2282 is_zero_ether_addr(wpa_s->pending_interface_addr))
2283 return; /* No pending virtual interface */
2284
2285 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2286 wpa_s->pending_interface_name);
2287 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2288 wpa_s->pending_interface_name);
2289 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2290 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002291 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292}
2293
2294
2295static struct wpa_supplicant *
2296wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2297{
2298 struct wpa_interface iface;
2299 struct wpa_supplicant *group_wpa_s;
2300
2301 if (!wpa_s->pending_interface_name[0]) {
2302 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2303 if (!wpas_p2p_create_iface(wpa_s))
2304 return NULL;
2305 /*
2306 * Something has forced us to remove the pending interface; try
2307 * to create a new one and hope for the best that we will get
2308 * the same local address.
2309 */
2310 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2311 WPA_IF_P2P_CLIENT) < 0)
2312 return NULL;
2313 }
2314
2315 os_memset(&iface, 0, sizeof(iface));
2316 iface.ifname = wpa_s->pending_interface_name;
2317 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002318 if (wpa_s->conf->ctrl_interface == NULL &&
2319 wpa_s->parent != wpa_s &&
2320 wpa_s->p2p_mgmt &&
2321 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2322 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2323 else
2324 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002326 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327 if (group_wpa_s == NULL) {
2328 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2329 "wpa_supplicant interface");
2330 return NULL;
2331 }
2332 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2334 P2P_GROUP_INTERFACE_CLIENT;
2335 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002336 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337
2338 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2339
Jimmy Chen36c21992018-11-29 16:46:43 +08002340 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002341 if (wpa_drv_set_mac_addr(group_wpa_s,
2342 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002343 wpa_msg(group_wpa_s, MSG_INFO,
2344 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002345 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2346 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002347 return NULL;
2348 }
2349
2350 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2351 wpa_msg(group_wpa_s, MSG_INFO,
2352 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002353 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2354 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002355 return NULL;
2356 }
2357
Hai Shalom74f70d42019-02-11 14:42:39 -08002358 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2359 " for the group",
2360 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002361 }
2362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 return group_wpa_s;
2364}
2365
2366
2367static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2368 void *timeout_ctx)
2369{
2370 struct wpa_supplicant *wpa_s = eloop_ctx;
2371 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002372 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002373}
2374
2375
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002376static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2377 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002378{
2379 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002380 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 if (wpa_s->global->p2p)
2382 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002383 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384}
2385
2386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002387static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2388{
2389 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2390 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002391 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002392 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002393 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002394 wpa_s->global->p2p_fail_on_wps_complete = 0;
2395}
2396
2397
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002398void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2399{
2400 if (wpa_s->global->p2p_group_formation != wpa_s)
2401 return;
2402 /* Speed up group formation timeout since this cannot succeed */
2403 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002404 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002405 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002406 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002407}
2408
2409
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002410static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411{
2412 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002413 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414
2415 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2416 wpa_drv_cancel_remain_on_channel(wpa_s);
2417 wpa_s->off_channel_freq = 0;
2418 wpa_s->roc_waiting_drv_freq = 0;
2419 }
2420
2421 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002422 wpa_msg_global(wpa_s, MSG_INFO,
2423 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2424 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002425 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426 wpas_p2p_remove_pending_group_interface(wpa_s);
2427 return;
2428 }
2429
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002430 if (!res->role_go) {
2431 /* Inform driver of the operating channel of GO. */
2432 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2433 }
2434
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002435 if (wpa_s->p2p_go_ht40)
2436 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002437 if (wpa_s->p2p_go_vht)
2438 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002439 if (wpa_s->p2p_go_he)
2440 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002441 if (wpa_s->p2p_go_edmg)
2442 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002443 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2444 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002445
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002446 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2447 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2448 " wps_method=%s",
2449 res->role_go ? "GO" : "client", res->freq, res->ht40,
2450 MAC2STR(res->peer_device_addr),
2451 MAC2STR(res->peer_interface_addr),
2452 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002453 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454
Dmitry Shmidt04949592012-07-19 12:16:46 -07002455 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2456 struct wpa_ssid *ssid;
2457 ssid = wpa_config_get_network(wpa_s->conf,
2458 wpa_s->p2p_persistent_id);
2459 if (ssid && ssid->disabled == 2 &&
2460 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2461 size_t len = os_strlen(ssid->passphrase);
2462 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2463 "on requested persistent group");
2464 os_memcpy(res->passphrase, ssid->passphrase, len);
2465 res->passphrase[len] = '\0';
2466 }
2467 }
2468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002470 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2472 if (group_wpa_s == NULL) {
2473 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002474 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2475 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002476 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 return;
2478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2480 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002482 group_wpa_s = wpa_s->parent;
2483 wpa_s->global->p2p_group_formation = group_wpa_s;
2484 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002485 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002486 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002488 group_wpa_s->p2p_in_provisioning = 1;
2489 group_wpa_s->p2pdev = wpa_s;
2490 if (group_wpa_s != wpa_s) {
2491 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2492 sizeof(group_wpa_s->p2p_pin));
2493 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2494 }
2495 if (res->role_go) {
2496 wpas_start_wps_go(group_wpa_s, res, 1);
2497 } else {
2498 os_get_reltime(&group_wpa_s->scan_min_time);
2499 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 }
2501
Hai Shalomfdcde762020-04-02 11:19:20 -07002502 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2504
2505 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2506 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2507 (res->peer_config_timeout % 100) * 10000,
2508 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2509}
2510
2511
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002512static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2513 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514{
2515 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002516 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002517 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2518 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002520 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521}
2522
2523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002524static void wpas_dev_found(void *ctx, const u8 *addr,
2525 const struct p2p_peer_info *info,
2526 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002528 u8 *wfd_dev_info = NULL;
2529 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002530 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002531 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002532#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 struct wpa_supplicant *wpa_s = ctx;
2534 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002535 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002536 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002537
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002538#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002539 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2540 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002541 if (wfd_dev_info_hex) {
2542 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2543 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2544 // Only used for notification, so not handling error.
2545 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2546 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002547
2548 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2549 WFD_SUBELEM_R2_DEVICE_INFO);
2550 if (wfd_r2_dev_info_hex) {
2551 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2552 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2553 // Only used for notification, so not handling error.
2554 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2555 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002556#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002557
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002558 if (info->p2ps_instance) {
2559 char str[256];
2560 const u8 *buf = wpabuf_head(info->p2ps_instance);
2561 size_t len = wpabuf_len(info->p2ps_instance);
2562
2563 while (len) {
2564 u32 id;
2565 u16 methods;
2566 u8 str_len;
2567
2568 if (len < 4 + 2 + 1)
2569 break;
2570 id = WPA_GET_LE32(buf);
2571 buf += sizeof(u32);
2572 methods = WPA_GET_BE16(buf);
2573 buf += sizeof(u16);
2574 str_len = *buf++;
2575 if (str_len > len - 4 - 2 - 1)
2576 break;
2577 os_memcpy(str, buf, str_len);
2578 str[str_len] = '\0';
2579 buf += str_len;
2580 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2581
2582 wpa_msg_global(wpa_s, MSG_INFO,
2583 P2P_EVENT_DEVICE_FOUND MACSTR
2584 " p2p_dev_addr=" MACSTR
2585 " pri_dev_type=%s name='%s'"
2586 " config_methods=0x%x"
2587 " dev_capab=0x%x"
2588 " group_capab=0x%x"
2589 " adv_id=%x asp_svc=%s%s",
2590 MAC2STR(addr),
2591 MAC2STR(info->p2p_device_addr),
2592 wps_dev_type_bin2str(
2593 info->pri_dev_type,
2594 devtype, sizeof(devtype)),
2595 info->device_name, methods,
2596 info->dev_capab, info->group_capab,
2597 id, str,
2598 info->vendor_elems ?
2599 " vendor_elems=1" : "");
2600 }
2601 goto done;
2602 }
2603
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002604 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2605 " p2p_dev_addr=" MACSTR
2606 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002607 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002608 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2609 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2610 sizeof(devtype)),
2611 info->device_name, info->config_methods,
2612 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002613 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002614 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002615 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2616 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002617 info->vendor_elems ? " vendor_elems=1" : "",
2618 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002620done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002621 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002622 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002623#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002624
Roshan Piusfd2fd662017-01-23 13:41:57 -08002625 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002626 wfd_dev_info_len, wfd_r2_dev_info,
2627 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002628 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629}
2630
2631
2632static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2633{
2634 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002636 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2637 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2640}
2641
2642
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002643static void wpas_find_stopped(void *ctx)
2644{
2645 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002646
2647 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2648 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2649
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002650 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002651 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002652}
2653
2654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002655struct wpas_p2p_listen_work {
2656 unsigned int freq;
2657 unsigned int duration;
2658 struct wpabuf *probe_resp_ie;
2659};
2660
2661
2662static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2663{
2664 if (lwork == NULL)
2665 return;
2666 wpabuf_free(lwork->probe_resp_ie);
2667 os_free(lwork);
2668}
2669
2670
2671static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2672{
2673 struct wpas_p2p_listen_work *lwork;
2674
2675 if (!wpa_s->p2p_listen_work)
2676 return;
2677
2678 lwork = wpa_s->p2p_listen_work->ctx;
2679 wpas_p2p_listen_work_free(lwork);
2680 radio_work_done(wpa_s->p2p_listen_work);
2681 wpa_s->p2p_listen_work = NULL;
2682}
2683
2684
2685static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2686{
2687 struct wpa_supplicant *wpa_s = work->wpa_s;
2688 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002689 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002690
2691 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002692 if (work->started) {
2693 wpa_s->p2p_listen_work = NULL;
2694 wpas_stop_listen(wpa_s);
2695 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002696 wpas_p2p_listen_work_free(lwork);
2697 return;
2698 }
2699
2700 wpa_s->p2p_listen_work = work;
2701
2702 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2703
2704 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2705 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2706 "report received Probe Request frames");
2707 wpas_p2p_listen_work_done(wpa_s);
2708 return;
2709 }
2710
2711 wpa_s->pending_listen_freq = lwork->freq;
2712 wpa_s->pending_listen_duration = lwork->duration;
2713
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002714 duration = lwork->duration;
2715#ifdef CONFIG_TESTING_OPTIONS
2716 if (wpa_s->extra_roc_dur) {
2717 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2718 duration, duration + wpa_s->extra_roc_dur);
2719 duration += wpa_s->extra_roc_dur;
2720 }
2721#endif /* CONFIG_TESTING_OPTIONS */
2722
2723 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002724 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2725 "to remain on channel (%u MHz) for Listen "
2726 "state", lwork->freq);
2727 wpas_p2p_listen_work_done(wpa_s);
2728 wpa_s->pending_listen_freq = 0;
2729 return;
2730 }
2731 wpa_s->off_channel_freq = 0;
2732 wpa_s->roc_waiting_drv_freq = lwork->freq;
2733}
2734
2735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736static int wpas_start_listen(void *ctx, unsigned int freq,
2737 unsigned int duration,
2738 const struct wpabuf *probe_resp_ie)
2739{
2740 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002741 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002743 if (wpa_s->p2p_listen_work) {
2744 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002745 return -1;
2746 }
2747
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002748 lwork = os_zalloc(sizeof(*lwork));
2749 if (lwork == NULL)
2750 return -1;
2751 lwork->freq = freq;
2752 lwork->duration = duration;
2753 if (probe_resp_ie) {
2754 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2755 if (lwork->probe_resp_ie == NULL) {
2756 wpas_p2p_listen_work_free(lwork);
2757 return -1;
2758 }
2759 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002761 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2762 lwork) < 0) {
2763 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 return -1;
2765 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766
2767 return 0;
2768}
2769
2770
2771static void wpas_stop_listen(void *ctx)
2772{
2773 struct wpa_supplicant *wpa_s = ctx;
2774 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2775 wpa_drv_cancel_remain_on_channel(wpa_s);
2776 wpa_s->off_channel_freq = 0;
2777 wpa_s->roc_waiting_drv_freq = 0;
2778 }
2779 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002780
2781 /*
2782 * Don't cancel Probe Request RX reporting for a connected P2P Client
2783 * handling Probe Request frames.
2784 */
2785 if (!wpa_s->p2p_cli_probe)
2786 wpa_drv_probe_req_report(wpa_s, 0);
2787
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002788 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789}
2790
2791
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002792static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2793 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794{
2795 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002796 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002797 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798}
2799
2800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2802 const u8 *peer, const char *params,
2803 unsigned int generated_pin)
2804{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002805 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2806 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807}
2808
2809
2810static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2811 const u8 *peer, const char *params)
2812{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002813 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2814 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815}
2816
2817
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002818static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2819 const u8 *dev_addr, const u8 *pri_dev_type,
2820 const char *dev_name, u16 supp_config_methods,
2821 u8 dev_capab, u8 group_capab, const u8 *group_id,
2822 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823{
2824 struct wpa_supplicant *wpa_s = ctx;
2825 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002826 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 u8 empty_dev_type[8];
2828 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002829 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002830 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002831
2832 if (group_id) {
2833 for (group = wpa_s->global->ifaces; group; group = group->next)
2834 {
2835 struct wpa_ssid *s = group->current_ssid;
2836 if (s != NULL &&
2837 s->mode == WPAS_MODE_P2P_GO &&
2838 group_id_len - ETH_ALEN == s->ssid_len &&
2839 os_memcmp(group_id + ETH_ALEN, s->ssid,
2840 s->ssid_len) == 0)
2841 break;
2842 }
2843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844
2845 if (pri_dev_type == NULL) {
2846 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2847 pri_dev_type = empty_dev_type;
2848 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002849 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2850 " pri_dev_type=%s name='%s' config_methods=0x%x "
2851 "dev_capab=0x%x group_capab=0x%x%s%s",
2852 MAC2STR(dev_addr),
2853 wps_dev_type_bin2str(pri_dev_type, devtype,
2854 sizeof(devtype)),
2855 dev_name, supp_config_methods, dev_capab, group_capab,
2856 group ? " group=" : "",
2857 group ? group->ifname : "");
2858 if (os_snprintf_error(sizeof(params), res))
2859 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002863 if (wps_generate_pin(&generated_pin) < 0) {
2864 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2865 wpas_notify_p2p_provision_discovery(
2866 wpa_s, peer, 0 /* response */,
2867 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2868 return;
2869 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 wpas_prov_disc_local_display(wpa_s, peer, params,
2871 generated_pin);
2872 } else if (config_methods & WPS_CONFIG_KEYPAD)
2873 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2874 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002875 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2876 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002877
2878 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2879 P2P_PROV_DISC_SUCCESS,
2880 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881}
2882
2883
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002884static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885{
2886 struct wpa_supplicant *wpa_s = ctx;
2887 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002888 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002890 if (wpa_s->pending_pd_before_join &&
2891 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2892 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2893 wpa_s->pending_pd_before_join = 0;
2894 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2895 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002896 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002897 return;
2898 }
2899
Dmitry Shmidt04949592012-07-19 12:16:46 -07002900 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002901 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2902 int res;
2903
2904 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2905 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2906 if (os_snprintf_error(sizeof(params), res))
2907 params[sizeof(params) - 1] = '\0';
2908 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002909 params[0] = '\0';
2910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002912 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002914 if (wps_generate_pin(&generated_pin) < 0) {
2915 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2916 wpas_notify_p2p_provision_discovery(
2917 wpa_s, peer, 0 /* response */,
2918 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2919 return;
2920 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002921 wpas_prov_disc_local_display(wpa_s, peer, params,
2922 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002924 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2925 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926
Jouni Malinen75ecf522011-06-27 15:19:46 -07002927 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2928 P2P_PROV_DISC_SUCCESS,
2929 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930}
2931
2932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002933static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002934 enum p2p_prov_disc_status status,
2935 u32 adv_id, const u8 *adv_mac,
2936 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002937{
2938 struct wpa_supplicant *wpa_s = ctx;
2939
Dmitry Shmidt04949592012-07-19 12:16:46 -07002940 if (wpa_s->p2p_fallback_to_go_neg) {
2941 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2942 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002943 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002944 P2P_EVENT_FALLBACK_TO_GO_NEG
2945 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002946 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2947 return;
2948 }
2949
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002950 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002951 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002952 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2953 "join-existing-group operation (no ACK for PD "
2954 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002955 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002956 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002957 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002958
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002959 if (adv_id && adv_mac && deferred_session_resp) {
2960 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2961 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2962 " deferred_session_resp='%s'",
2963 MAC2STR(peer), status, adv_id,
2964 deferred_session_resp);
2965 } else if (adv_id && adv_mac) {
2966 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2967 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2968 MAC2STR(peer), status, adv_id);
2969 } else {
2970 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2971 " p2p_dev_addr=" MACSTR " status=%d",
2972 MAC2STR(peer), status);
2973 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002974
Jouni Malinen75ecf522011-06-27 15:19:46 -07002975 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2976 status, 0, 0);
2977}
2978
2979
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002980static int freq_included(struct wpa_supplicant *wpa_s,
2981 const struct p2p_channels *channels,
2982 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002983{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002984 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2985 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2986 return 1;
2987 return 0;
2988}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002989
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002990
2991static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2992{
2993 unsigned int num = P2P_MAX_CHANNELS;
2994 int *common_freqs;
2995 int ret;
2996
2997 p2p_go_dump_common_freqs(wpa_s);
2998 common_freqs = os_calloc(num, sizeof(int));
2999 if (!common_freqs)
3000 return;
3001
3002 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3003 if (ret < 0) {
3004 wpa_dbg(wpa_s, MSG_DEBUG,
3005 "P2P: Failed to get group common freqs");
3006 os_free(common_freqs);
3007 return;
3008 }
3009
3010 os_free(wpa_s->p2p_group_common_freqs);
3011 wpa_s->p2p_group_common_freqs = common_freqs;
3012 wpa_s->p2p_group_common_freqs_num = num;
3013 p2p_go_dump_common_freqs(wpa_s);
3014}
3015
3016
3017/*
3018 * Check if the given frequency is one of the possible operating frequencies
3019 * set after the completion of the GO Negotiation.
3020 */
3021static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3022{
3023 unsigned int i;
3024
3025 p2p_go_dump_common_freqs(wpa_s);
3026
3027 /* assume no restrictions */
3028 if (!wpa_s->p2p_group_common_freqs_num)
3029 return 1;
3030
3031 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3032 if (wpa_s->p2p_group_common_freqs[i] == freq)
3033 return 1;
3034 }
3035 return 0;
3036}
3037
3038
3039static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3040 struct sta_info *sta, void *ctx)
3041{
3042 int *ecsa_support = ctx;
3043
3044 *ecsa_support &= sta->ecsa_supported;
3045
3046 return 0;
3047}
3048
3049
3050/* Check if all the peers support eCSA */
3051static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3052{
3053 int ecsa_support = 1;
3054
3055 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3056 &ecsa_support);
3057
3058 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003059}
3060
3061
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003062/**
3063 * Pick the best frequency to use from all the currently used frequencies.
3064 */
3065static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3066 struct wpa_used_freq_data *freqs,
3067 unsigned int num)
3068{
3069 unsigned int i, c;
3070
3071 /* find a candidate freq that is supported by P2P */
3072 for (c = 0; c < num; c++)
3073 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3074 break;
3075
3076 if (c == num)
3077 return 0;
3078
3079 /* once we have a candidate, try to find a 'better' one */
3080 for (i = c + 1; i < num; i++) {
3081 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3082 continue;
3083
3084 /*
3085 * 1. Infrastructure station interfaces have higher preference.
3086 * 2. P2P Clients have higher preference.
3087 * 3. All others.
3088 */
3089 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3090 c = i;
3091 break;
3092 }
3093
3094 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3095 c = i;
3096 }
3097 return freqs[c].freq;
3098}
3099
3100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3102 const u8 *go_dev_addr, const u8 *ssid,
3103 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003104 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003105 const struct p2p_channels *channels,
3106 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107{
3108 struct wpa_supplicant *wpa_s = ctx;
3109 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003110 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003112 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113
3114 if (!persistent_group) {
3115 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003116 " to join an active group (SSID: %s)",
3117 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3119 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3120 == 0 ||
3121 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3122 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3123 "authorized invitation");
3124 goto accept_inv;
3125 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003126
3127#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003128 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3129 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003130 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003131 wpa_s->p2p_wps_method = WPS_NFC;
3132 wpa_s->pending_join_wps_method = WPS_NFC;
3133 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003134 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003135 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003136 bssid, ETH_ALEN);
3137 goto accept_inv;
3138 }
3139#endif /* CONFIG_WPS_NFC */
3140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 /*
3142 * Do not accept the invitation automatically; notify user and
3143 * request approval.
3144 */
3145 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3146 }
3147
3148 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3149 if (grp) {
3150 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3151 "running persistent group");
3152 if (*go)
3153 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3154 goto accept_inv;
3155 }
3156
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003157 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3158 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3159 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3160 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003161 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003162 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003163 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3164
3165 for (s = wpa_s->conf->ssid; s; s = s->next) {
3166 if (s->disabled == 2 &&
3167 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3168 s->ssid_len == ssid_len &&
3169 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3170 break;
3171 }
3172
3173 if (!s) {
3174 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3175 " requested reinvocation of an unknown group",
3176 MAC2STR(sa));
3177 return P2P_SC_FAIL_UNKNOWN_GROUP;
3178 }
3179
3180 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3181 *go = 1;
3182 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3183 wpa_printf(MSG_DEBUG, "P2P: The only available "
3184 "interface is already in use - reject "
3185 "invitation");
3186 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3187 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003188 if (wpa_s->p2p_mgmt)
3189 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3190 ETH_ALEN);
3191 else
3192 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003193 } else if (s->mode == WPAS_MODE_P2P_GO) {
3194 *go = 1;
3195 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3196 {
3197 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3198 "interface address for the group");
3199 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3200 }
3201 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3202 ETH_ALEN);
3203 }
3204
3205accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003206 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3207
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003208 best_freq = 0;
3209 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3210 sizeof(struct wpa_used_freq_data));
3211 if (freqs) {
3212 int num_channels = wpa_s->num_multichan_concurrent;
3213 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3214 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3215 os_free(freqs);
3216 }
3217
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003218 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003219 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003220 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003221 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003222
3223 if (wpa_s->num_multichan_concurrent < 2 ||
3224 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3225 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 -07003226 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 }
3229
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003230 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3231 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003232 if (*go == 0) {
3233 /* We are the client */
3234 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3235 "running a GO but we are capable of MCC, "
3236 "figure out the best channel to use");
3237 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003238 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003239 /* We are the GO, and *force_freq is not in the
3240 * intersection */
3241 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3242 "in intersection but we are capable of MCC, "
3243 "figure out the best channel to use",
3244 *force_freq);
3245 *force_freq = 0;
3246 }
3247 }
3248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 return P2P_SC_SUCCESS;
3250}
3251
3252
3253static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3254 const u8 *ssid, size_t ssid_len,
3255 const u8 *go_dev_addr, u8 status,
3256 int op_freq)
3257{
3258 struct wpa_supplicant *wpa_s = ctx;
3259 struct wpa_ssid *s;
3260
3261 for (s = wpa_s->conf->ssid; s; s = s->next) {
3262 if (s->disabled == 2 &&
3263 s->ssid_len == ssid_len &&
3264 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3265 break;
3266 }
3267
3268 if (status == P2P_SC_SUCCESS) {
3269 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003270 " was accepted; op_freq=%d MHz, SSID=%s",
3271 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003273 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003274 if (go) {
3275 wpa_msg_global(wpa_s, MSG_INFO,
3276 P2P_EVENT_INVITATION_ACCEPTED
3277 "sa=" MACSTR
3278 " persistent=%d freq=%d",
3279 MAC2STR(sa), s->id, op_freq);
3280 } else {
3281 wpa_msg_global(wpa_s, MSG_INFO,
3282 P2P_EVENT_INVITATION_ACCEPTED
3283 "sa=" MACSTR
3284 " persistent=%d",
3285 MAC2STR(sa), s->id);
3286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003288 wpa_s, s, go, 0, op_freq, 0,
3289 wpa_s->conf->p2p_go_ht40,
3290 wpa_s->conf->p2p_go_vht,
3291 0,
3292 wpa_s->conf->p2p_go_he,
3293 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003294 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Hai Shalomc1a21442022-02-04 13:43:00 -08003295 1, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003297 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003298 wpa_msg_global(wpa_s, MSG_INFO,
3299 P2P_EVENT_INVITATION_ACCEPTED
3300 "sa=" MACSTR " go_dev_addr=" MACSTR
3301 " bssid=" MACSTR " unknown-network",
3302 MAC2STR(sa), MAC2STR(go_dev_addr),
3303 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003305 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003306 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 }
3308 return;
3309 }
3310
3311 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3312 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3313 " was rejected (status %u)", MAC2STR(sa), status);
3314 return;
3315 }
3316
3317 if (!s) {
3318 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003319 wpa_msg_global(wpa_s, MSG_INFO,
3320 P2P_EVENT_INVITATION_RECEIVED
3321 "sa=" MACSTR " go_dev_addr=" MACSTR
3322 " bssid=" MACSTR " unknown-network",
3323 MAC2STR(sa), MAC2STR(go_dev_addr),
3324 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003326 wpa_msg_global(wpa_s, MSG_INFO,
3327 P2P_EVENT_INVITATION_RECEIVED
3328 "sa=" MACSTR " go_dev_addr=" MACSTR
3329 " unknown-network",
3330 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003332 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3333 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 return;
3335 }
3336
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003337 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003338 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3339 "sa=" MACSTR " persistent=%d freq=%d",
3340 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003341 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003342 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3343 "sa=" MACSTR " persistent=%d",
3344 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003345 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003346 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3347 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003348}
3349
3350
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003351static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3352 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003353 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003354{
3355 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003356 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003357
3358 if (ssid == NULL)
3359 return;
3360
3361 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003362 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003363 ETH_ALEN) == 0)
3364 break;
3365 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003366 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003367 if (ssid->mode != WPAS_MODE_P2P_GO &&
3368 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3369 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3370 "due to invitation result", ssid->id);
3371 wpas_notify_network_removed(wpa_s, ssid);
3372 wpa_config_remove_network(wpa_s->conf, ssid->id);
3373 return;
3374 }
3375 return; /* Peer not found in client list */
3376 }
3377
3378 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003379 "group %d client list%s",
3380 MAC2STR(peer), ssid->id,
3381 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003382 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3383 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3384 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003385 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003386 if (p2p_wpa_s->conf->update_config &&
3387 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003388 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003389}
3390
3391
3392static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3393 const u8 *peer)
3394{
3395 struct wpa_ssid *ssid;
3396
3397 wpa_s = wpa_s->global->p2p_invite_group;
3398 if (wpa_s == NULL)
3399 return; /* No known invitation group */
3400 ssid = wpa_s->current_ssid;
3401 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3402 !ssid->p2p_persistent_group)
3403 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003404 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003405 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003406 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003407}
3408
3409
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003410static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003411 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003412 const u8 *peer, int neg_freq,
3413 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414{
3415 struct wpa_supplicant *wpa_s = ctx;
3416 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003417 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418
3419 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003420 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3421 "status=%d " MACSTR,
3422 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003424 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3425 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 }
3427 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3428
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003429 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3430 status, MAC2STR(peer));
3431 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003432 struct wpa_supplicant *group_if =
3433 wpa_s->global->p2p_invite_group;
3434
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003435 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3436 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003437
3438 /*
3439 * Invitation to an active group. If this is successful and we
3440 * are the GO, set the client wait to postpone some concurrent
3441 * operations and to allow provisioning and connection to happen
3442 * more quickly.
3443 */
3444 if (status == P2P_SC_SUCCESS &&
3445 group_if && group_if->current_ssid &&
3446 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3447 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3448#ifdef CONFIG_TESTING_OPTIONS
3449 if (group_if->p2p_go_csa_on_inv) {
3450 wpa_printf(MSG_DEBUG,
3451 "Testing: force P2P GO CSA after invitation");
3452 eloop_cancel_timeout(
3453 wpas_p2p_reconsider_moving_go,
3454 wpa_s, NULL);
3455 eloop_register_timeout(
3456 0, 50000,
3457 wpas_p2p_reconsider_moving_go,
3458 wpa_s, NULL);
3459 }
3460#endif /* CONFIG_TESTING_OPTIONS */
3461 }
3462 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003465 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3466 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3467 "invitation exchange to indicate readiness for "
3468 "re-invocation");
3469 }
3470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003472 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3473 ssid = wpa_config_get_network(
3474 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003475 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477 wpas_p2p_remove_pending_group_interface(wpa_s);
3478 return;
3479 }
3480
3481 ssid = wpa_config_get_network(wpa_s->conf,
3482 wpa_s->pending_invite_ssid_id);
3483 if (ssid == NULL) {
3484 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3485 "data matching with invitation");
3486 return;
3487 }
3488
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003489 /*
3490 * The peer could have missed our ctrl::ack frame for Invitation
3491 * Response and continue retransmitting the frame. To reduce the
3492 * likelihood of the peer not getting successful TX status for the
3493 * Invitation Response frame, wait a short time here before starting
3494 * the persistent group so that we will remain on the current channel to
3495 * acknowledge any possible retransmission from the peer.
3496 */
3497 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3498 "starting persistent group");
3499 os_sleep(0, 50000);
3500
Dmitry Shmidt15907092014-03-25 10:42:57 -07003501 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003502 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003503 freq = neg_freq;
3504 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003505 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003506 freq = peer_oper_freq;
3507 else
3508 freq = 0;
3509
3510 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3511 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003513 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003514 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003515 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003516 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003517 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003518 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003519 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003520 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003521 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003522 ssid->mode == WPAS_MODE_P2P_GO ?
3523 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003524 0, 1,
3525 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526}
3527
3528
Dmitry Shmidt04949592012-07-19 12:16:46 -07003529static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3530 unsigned int freq)
3531{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003532 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3533 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003534 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003535}
3536
3537
3538static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3539{
3540 reg->channel[reg->channels] = chan;
3541 reg->channels++;
3542}
3543
3544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003546 struct p2p_channels *chan,
3547 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548{
3549 int i, cla = 0;
3550
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003551 wpa_s->global->p2p_24ghz_social_channels = 1;
3552
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003553 os_memset(cli_chan, 0, sizeof(*cli_chan));
3554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3556 "band");
3557
3558 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3559 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003560 chan->reg_class[cla].channels = 0;
3561 for (i = 0; i < 11; i++) {
3562 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3563 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3564 }
3565 if (chan->reg_class[cla].channels)
3566 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567
3568 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3569 "band");
3570
3571 /* Operating class 115 - 5 GHz, channels 36-48 */
3572 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003573 chan->reg_class[cla].channels = 0;
3574 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3575 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3576 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3577 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3578 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3579 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3580 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3581 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3582 if (chan->reg_class[cla].channels)
3583 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584
3585 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3586 "band");
3587
3588 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3589 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003590 chan->reg_class[cla].channels = 0;
3591 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3592 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3593 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3594 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3595 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3596 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3597 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3598 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3599 if (chan->reg_class[cla].channels)
3600 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601
3602 chan->reg_classes = cla;
3603 return 0;
3604}
3605
3606
Hai Shalomc1a21442022-02-04 13:43:00 -08003607static enum chan_allowed has_channel(struct wpa_global *global,
3608 struct hostapd_hw_modes *mode, u8 op_class,
3609 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003610{
3611 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003612 unsigned int freq;
3613
Hai Shalomc1a21442022-02-04 13:43:00 -08003614 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003615 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003616 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617
3618 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003619 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 if (flags)
3621 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003622 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003623 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003624 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3625 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003626 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3627 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003628 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 }
3630 }
3631
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003632 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633}
3634
3635
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003636static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3637 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003638 u8 channel, const u8 *center_channels,
3639 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003640{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003641 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003642
3643 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3644 return 0;
3645
Hai Shalomc1a21442022-02-04 13:43:00 -08003646 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003647 /*
3648 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3649 * so the center channel is 6 channels away from the start/end.
3650 */
3651 if (channel >= center_channels[i] - 6 &&
3652 channel <= center_channels[i] + 6)
3653 return center_channels[i];
3654
3655 return 0;
3656}
3657
3658
Hai Shalomc1a21442022-02-04 13:43:00 -08003659static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3660 155, 171 };
3661static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3662 119, 135, 151, 167, 183, 199,
3663 215 };
3664
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003665static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3666 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003667 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003668{
3669 u8 center_chan;
3670 int i, flags;
3671 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003672 const u8 *chans;
3673 size_t num_chans;
3674 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003675
Hai Shalomc1a21442022-02-04 13:43:00 -08003676 if (is_6ghz) {
3677 chans = center_channels_6ghz_80mhz;
3678 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3679 } else {
3680 chans = center_channels_5ghz_80mhz;
3681 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3682 }
3683 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3684 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003685 if (!center_chan)
3686 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003687 if (!wpa_s->p2p_go_allow_dfs &&
3688 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003689 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3690
3691 /* check all the channels are available */
3692 for (i = 0; i < 4; i++) {
3693 int adj_chan = center_chan - 6 + i * 4;
3694
Hai Shalomc1a21442022-02-04 13:43:00 -08003695 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3696 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003697 if (res == NOT_ALLOWED)
3698 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003699 if (res == RADAR)
3700 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003701 if (res == NO_IR)
3702 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003703 if (!is_6ghz) {
3704 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3705 return NOT_ALLOWED;
3706 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3707 return NOT_ALLOWED;
3708 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3709 return NOT_ALLOWED;
3710 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3711 return NOT_ALLOWED;
3712 } else if (is_6ghz &&
3713 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3714 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003715 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003716 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003717 }
3718
3719 return ret;
3720}
3721
3722
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003723static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3724 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003725 u8 channel, const u8 *center_channels,
3726 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003727{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003728 unsigned int i;
3729
3730 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3731 return 0;
3732
Hai Shalomc1a21442022-02-04 13:43:00 -08003733 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003734 /*
3735 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3736 * so the center channel is 14 channels away from the start/end.
3737 */
3738 if (channel >= center_channels[i] - 14 &&
3739 channel <= center_channels[i] + 14)
3740 return center_channels[i];
3741
3742 return 0;
3743}
3744
3745
Hai Shalomc1a21442022-02-04 13:43:00 -08003746static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3747static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3748 207 };
3749
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003750static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3751 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003752 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003753{
3754 u8 center_chan;
3755 int i, flags;
3756 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003757 const u8 *chans;
3758 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003759
Hai Shalomc1a21442022-02-04 13:43:00 -08003760 if (is_6ghz_op_class(op_class)) {
3761 chans = center_channels_6ghz_160mhz;
3762 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3763 } else {
3764 chans = center_channels_5ghz_160mhz;
3765 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3766 }
3767 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3768 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003769 if (!center_chan)
3770 return NOT_ALLOWED;
3771 /* VHT 160 MHz uses DFS channels in most countries. */
3772
3773 /* Check all the channels are available */
3774 for (i = 0; i < 8; i++) {
3775 int adj_chan = center_chan - 14 + i * 4;
3776
Hai Shalomc1a21442022-02-04 13:43:00 -08003777 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3778 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003779 if (res == NOT_ALLOWED)
3780 return NOT_ALLOWED;
3781
Hai Shalomc1a21442022-02-04 13:43:00 -08003782 if (res == RADAR)
3783 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003784 if (res == NO_IR)
3785 ret = NO_IR;
3786
Hai Shalomc1a21442022-02-04 13:43:00 -08003787 if (!is_6ghz_op_class(op_class)) {
3788 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3789 return NOT_ALLOWED;
3790 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3791 return NOT_ALLOWED;
3792 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3793 return NOT_ALLOWED;
3794 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3795 return NOT_ALLOWED;
3796 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3797 return NOT_ALLOWED;
3798 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3799 return NOT_ALLOWED;
3800 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3801 return NOT_ALLOWED;
3802 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3803 return NOT_ALLOWED;
3804 } else if (is_6ghz_op_class(op_class) &&
3805 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3806 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003807 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003808 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003809 }
3810
3811 return ret;
3812}
3813
3814
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003815static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3816 struct hostapd_hw_modes *mode,
3817 u8 channel)
3818{
3819 struct ieee80211_edmg_config edmg;
3820
3821 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3822 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3823 return ALLOWED;
3824
3825 return NOT_ALLOWED;
3826}
3827
3828
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003829static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3830 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003831 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003832{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003833 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003834 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003835
Hai Shalomc1a21442022-02-04 13:43:00 -08003836 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003837 if (bw == BW40MINUS) {
3838 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3839 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003840 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3841 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003842 } else if (bw == BW40PLUS) {
3843 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3844 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003845 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3846 NULL);
3847 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3848 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3849 return NOT_ALLOWED;
3850 if (get_6ghz_sec_channel(channel) < 0)
3851 res2 = has_channel(wpa_s->global, mode, op_class,
3852 channel - 4, NULL);
3853 else
3854 res2 = has_channel(wpa_s->global, mode, op_class,
3855 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003856 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003857 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3858 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003859 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003860 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3861 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003862 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3863 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003864 }
3865
3866 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3867 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003868 if (res == NO_IR || res2 == NO_IR)
3869 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003870 if (res == RADAR || res2 == RADAR)
3871 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003872 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003873}
3874
3875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003877 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003878 struct p2p_channels *cli_chan,
3879 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003881 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003882 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003884 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3886 "of all supported channels; assume dualband "
3887 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003888 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 }
3890
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003891 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003893 for (op = 0; global_op_class[op].op_class; op++) {
3894 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003895 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003896 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897
Hai Shalom899fcc72020-10-19 14:38:18 -07003898 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003899 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003900 continue;
3901
Hai Shalomfdcde762020-04-02 11:19:20 -07003902 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3903 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003904 if (mode == NULL)
3905 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003906 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3907 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003909 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003910
3911 /* Check for non-continuous jump in channel index
3912 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003913 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003914 ch < 149 && ch + o->inc > 149)
3915 ch = 149;
3916
Hai Shalomc1a21442022-02-04 13:43:00 -08003917 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
3918 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003919 if (res == ALLOWED) {
3920 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003921 if (cla == P2P_MAX_REG_CLASSES)
3922 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003923 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3924 o->op_class);
3925 reg = &chan->reg_class[cla];
3926 cla++;
3927 reg->reg_class = o->op_class;
3928 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003929 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3930 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003931 reg->channel[reg->channels] = ch;
3932 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003933 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003934 wpa_s->conf->p2p_add_cli_chan) {
3935 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003936 if (cli_cla == P2P_MAX_REG_CLASSES)
3937 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003938 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3939 o->op_class);
3940 cli_reg = &cli_chan->reg_class[cli_cla];
3941 cli_cla++;
3942 cli_reg->reg_class = o->op_class;
3943 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003944 if (cli_reg->channels ==
3945 P2P_MAX_REG_CLASS_CHANNELS)
3946 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003947 cli_reg->channel[cli_reg->channels] = ch;
3948 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950 }
3951 if (reg) {
3952 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3953 reg->channel, reg->channels);
3954 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003955 if (cli_reg) {
3956 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3957 cli_reg->channel, cli_reg->channels);
3958 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959 }
3960
3961 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003962 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 return 0;
3965}
3966
3967
Hai Shalomc1a21442022-02-04 13:43:00 -08003968int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
3969 struct hostapd_hw_modes *mode,
3970 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003971{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003972 int op;
3973 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003974
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003975 for (op = 0; global_op_class[op].op_class; op++) {
3976 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003977 u16 ch;
3978 int chan = channel;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003979
Hai Shalomc1a21442022-02-04 13:43:00 -08003980 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
3981 * driver offloaded DFS. */
3982 if ((o->p2p == NO_P2P_SUPP &&
3983 (!is_dfs_global_op_class(o->op_class) ||
3984 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07003985 (is_6ghz_op_class(o->op_class) &&
3986 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003987 continue;
3988
Hai Shalomc1a21442022-02-04 13:43:00 -08003989 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
3990 get_6ghz_sec_channel(channel) < 0)
3991 chan = channel - 4;
3992
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003993 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3994 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08003995 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
3996 o->bw != BW40) ||
3997 ch != chan)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003998 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08003999 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4000 ch, o->bw);
4001 if (ret == ALLOWED) {
4002 if (is_6ghz_op_class(o->op_class) &&
4003 o->bw == BW40)
4004 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004005 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004006 }
4007 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4008 /* Allow RADAR channels used for driver
4009 * offloaded DFS */
4010 return (o->bw == BW40MINUS) ? -1 : 1;
4011 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004012 }
4013 }
4014 return 0;
4015}
4016
4017
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004018int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004019 struct hostapd_hw_modes *mode, u8 channel,
4020 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004021{
Hai Shalomc1a21442022-02-04 13:43:00 -08004022 const u8 *chans;
4023 size_t num_chans;
4024 enum chan_allowed ret;
4025
4026 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4027 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004028 return 0;
4029
Hai Shalomc1a21442022-02-04 13:43:00 -08004030 if (is_6ghz_op_class(op_class)) {
4031 chans = center_channels_6ghz_80mhz;
4032 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4033 } else {
4034 chans = center_channels_5ghz_80mhz;
4035 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4036 }
4037 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4038 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004039}
4040
4041
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004042int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004043 struct hostapd_hw_modes *mode, u8 channel,
4044 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004045{
Hai Shalomc1a21442022-02-04 13:43:00 -08004046 const u8 *chans;
4047 size_t num_chans;
4048 enum chan_allowed ret;
4049
4050 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4051 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004052 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004053 if (is_6ghz_op_class(op_class)) {
4054 chans = center_channels_6ghz_160mhz;
4055 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4056 } else {
4057 chans = center_channels_5ghz_160mhz;
4058 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4059 }
4060 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4061 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004062}
4063
4064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4066 size_t buf_len)
4067{
4068 struct wpa_supplicant *wpa_s = ctx;
4069
4070 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4071 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4072 break;
4073 }
4074 if (wpa_s == NULL)
4075 return -1;
4076
4077 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4078}
4079
4080
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004081struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4082 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004083{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004084 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4085 struct wpa_ssid *s = wpa_s->current_ssid;
4086 if (s == NULL)
4087 continue;
4088 if (s->mode != WPAS_MODE_P2P_GO &&
4089 s->mode != WPAS_MODE_AP &&
4090 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4091 continue;
4092 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004093 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004094 continue;
4095 return wpa_s;
4096 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004097
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004098 return NULL;
4099
4100}
4101
4102
4103struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4104 const u8 *peer_dev_addr)
4105{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004106 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4107 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004108 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004109 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004110 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4111 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004112 }
4113
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004114 return NULL;
4115}
4116
4117
4118static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4119{
4120 struct wpa_supplicant *wpa_s = ctx;
4121
4122 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004123}
4124
4125
Dmitry Shmidt18463232014-01-24 12:29:41 -08004126static int wpas_is_concurrent_session_active(void *ctx)
4127{
4128 struct wpa_supplicant *wpa_s = ctx;
4129 struct wpa_supplicant *ifs;
4130
4131 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4132 if (ifs == wpa_s)
4133 continue;
4134 if (ifs->wpa_state > WPA_ASSOCIATED)
4135 return 1;
4136 }
4137 return 0;
4138}
4139
4140
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004141static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4142{
4143 struct wpa_supplicant *wpa_s = ctx;
4144 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4145}
4146
4147
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004148int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4149 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004150{
4151 struct wpa_interface iface;
4152 struct wpa_supplicant *p2pdev_wpa_s;
4153 char ifname[100];
4154 char force_name[100];
4155 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004156 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004157
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004158 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4159 wpa_s->ifname);
4160 if (os_snprintf_error(sizeof(ifname), ret))
4161 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004162 /* Cut length at the maximum size. Note that we don't need to ensure
4163 * collision free names here as the created interface is not a netdev.
4164 */
4165 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004166 force_name[0] = '\0';
4167 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004168
4169 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4170 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4171 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4172
4173 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004174 force_name, wpa_s->pending_interface_addr, NULL);
4175 if (ret < 0) {
4176 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4177 return ret;
4178 }
4179 os_strlcpy(wpa_s->pending_interface_name, ifname,
4180 sizeof(wpa_s->pending_interface_name));
4181
4182 os_memset(&iface, 0, sizeof(iface));
4183 iface.p2p_mgmt = 1;
4184 iface.ifname = wpa_s->pending_interface_name;
4185 iface.driver = wpa_s->driver->name;
4186 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004187
4188 /*
4189 * If a P2P Device configuration file was given, use it as the interface
4190 * configuration file (instead of using parent's configuration file.
4191 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004192 if (conf_p2p_dev) {
4193 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004194 iface.ctrl_interface = NULL;
4195 } else {
4196 iface.confname = wpa_s->confname;
4197 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4198 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004199
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004200 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004201 if (!p2pdev_wpa_s) {
4202 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4203 return -1;
4204 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004205
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004206 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004207 wpa_s->pending_interface_name[0] = '\0';
4208 return 0;
4209}
4210
4211
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004212static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4213 const u8 *noa, size_t noa_len)
4214{
4215 struct wpa_supplicant *wpa_s, *intf = ctx;
4216 char hex[100];
4217
4218 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4219 if (wpa_s->waiting_presence_resp)
4220 break;
4221 }
4222 if (!wpa_s) {
4223 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4224 return;
4225 }
4226 wpa_s->waiting_presence_resp = 0;
4227
4228 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4229 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4230 " status=%u noa=%s", MAC2STR(src), status, hex);
4231}
4232
4233
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004234static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4235 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004236 u8 *ret_ssid, size_t *ret_ssid_len,
4237 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004238{
4239 struct wpa_supplicant *wpa_s = ctx;
4240 struct wpa_ssid *s;
4241
4242 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4243 if (s) {
4244 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4245 *ret_ssid_len = s->ssid_len;
4246 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004247
4248 if (s->mode != WPAS_MODE_P2P_GO) {
4249 os_memset(intended_iface_addr, 0, ETH_ALEN);
4250 } else if (wpas_p2p_create_iface(wpa_s)) {
4251 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4252 return 0;
4253
4254 os_memcpy(intended_iface_addr,
4255 wpa_s->pending_interface_addr, ETH_ALEN);
4256 } else {
4257 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4258 ETH_ALEN);
4259 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004260 return 1;
4261 }
4262
4263 return 0;
4264}
4265
4266
4267static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004268 u8 *ssid, size_t *ssid_len, int *group_iface,
4269 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004270{
4271 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004272 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004273 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004275 /*
4276 * group_iface will be set to 1 only if a dedicated interface for P2P
4277 * role is required. First, we try to reuse an active GO. However,
4278 * if it is not present, we will try to reactivate an existing
4279 * persistent group and set group_iface to 1, so the caller will know
4280 * that the pending interface should be used.
4281 */
4282 *group_iface = 0;
4283
4284 if (freq)
4285 *freq = 0;
4286
4287 go = wpas_p2p_get_go_group(wpa_s);
4288 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004289 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004290 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004291 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004292 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4293 else
4294 return 0;
4295 } else {
4296 s = go->current_ssid;
4297 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4298 if (freq)
4299 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004300 }
4301
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004302 os_memcpy(ssid, s->ssid, s->ssid_len);
4303 *ssid_len = s->ssid_len;
4304
4305 return 1;
4306}
4307
4308
4309static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4310 const u8 *ssid, size_t ssid_len)
4311{
4312 struct wpa_supplicant *wpa_s = ctx;
4313 struct wpa_ssid *s;
4314 int save_config = 0;
4315 size_t i;
4316
4317 /* Start with our first choice of Persistent Groups */
4318 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4319 if (go && ssid && ssid_len &&
4320 s->ssid_len == ssid_len &&
4321 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4322 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4323 break;
4324
4325 /* Remove stale persistent group */
4326 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004327 wpa_dbg(wpa_s, MSG_DEBUG,
4328 "P2P: Remove stale persistent group id=%d",
4329 s->id);
4330 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004331 wpa_config_remove_network(wpa_s->conf, s->id);
4332 save_config = 1;
4333 continue;
4334 }
4335
4336 for (i = 0; i < s->num_p2p_clients; i++) {
4337 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4338 peer, ETH_ALEN) != 0)
4339 continue;
4340
4341 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4342 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4343 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4344 break;
4345 }
4346 s->num_p2p_clients--;
4347 save_config = 1;
4348 }
4349
4350 if (save_config)
4351 p2p_config_write(wpa_s);
4352
4353 /* Return TRUE if valid SSID remains */
4354 return s != NULL;
4355}
4356
4357
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004358static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4359 const u8 *feat_cap, size_t feat_cap_len)
4360{
4361 static const char pref[] = " feature_cap=";
4362 int ret;
4363
4364 buf[0] = '\0';
4365
4366 /*
4367 * We expect a feature capability to contain at least one byte to be
4368 * reported. The string buffer provided by the caller function is
4369 * expected to be big enough to contain all bytes of the attribute for
4370 * known specifications. This function truncates the reported bytes if
4371 * the feature capability data exceeds the string buffer size.
4372 */
4373 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4374 return;
4375
4376 os_memcpy(buf, pref, sizeof(pref));
4377 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4378 buf_len - sizeof(pref) + 1,
4379 feat_cap, feat_cap_len);
4380
4381 if (ret != (2 * (int) feat_cap_len))
4382 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4383}
4384
4385
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004386static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4387 const u8 *adv_mac, const u8 *ses_mac,
4388 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4389 u8 conncap, int passwd_id,
4390 const u8 *persist_ssid,
4391 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004392 int prov_start, const char *session_info,
4393 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004394 unsigned int freq,
4395 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004396{
4397 struct wpa_supplicant *wpa_s = ctx;
4398 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004399 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004400 int save_config = 0;
4401 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004402 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004403
4404 if (!dev)
4405 return;
4406
4407 os_memset(mac, 0, ETH_ALEN);
4408 if (!adv_mac)
4409 adv_mac = mac;
4410 if (!ses_mac)
4411 ses_mac = mac;
4412 if (!grp_mac)
4413 grp_mac = mac;
4414
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004415 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4416 feat_cap, feat_cap_len);
4417
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004418 if (prov_start) {
4419 if (session_info == NULL) {
4420 wpa_msg_global(wpa_s, MSG_INFO,
4421 P2P_EVENT_P2PS_PROVISION_START MACSTR
4422 " adv_id=%x conncap=%x"
4423 " adv_mac=" MACSTR
4424 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004425 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004426 MAC2STR(dev), adv_id, conncap,
4427 MAC2STR(adv_mac),
4428 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004429 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004430 } else {
4431 wpa_msg_global(wpa_s, MSG_INFO,
4432 P2P_EVENT_P2PS_PROVISION_START MACSTR
4433 " adv_id=%x conncap=%x"
4434 " adv_mac=" MACSTR
4435 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004436 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004437 MAC2STR(dev), adv_id, conncap,
4438 MAC2STR(adv_mac),
4439 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004440 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004441 }
4442 return;
4443 }
4444
4445 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4446 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4447
4448 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4449 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4450 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4451
4452 if (persistent_go && !persistent_go->num_p2p_clients) {
4453 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004454 wpa_dbg(wpa_s, MSG_DEBUG,
4455 "P2P: Remove empty persistent group id=%d",
4456 persistent_go->id);
4457 wpas_notify_persistent_group_removed(wpa_s,
4458 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004459 wpa_config_remove_network(wpa_s->conf,
4460 persistent_go->id);
4461 }
4462
4463 wpa_msg_global(wpa_s, MSG_INFO,
4464 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4465 " status=%d"
4466 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004467 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004468 MAC2STR(dev), status,
4469 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004470 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004471 return;
4472 }
4473
4474 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004475 if (persist_ssid && persist_ssid_size)
4476 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4477 persist_ssid_size);
4478
4479 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4480 is_zero_ether_addr(grp_mac)) {
4481 wpa_dbg(wpa_s, MSG_ERROR,
4482 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4483 return;
4484 }
4485
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004486 for (;;) {
4487 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4488 if (!stale)
4489 break;
4490
4491 if (s && s->ssid_len == stale->ssid_len &&
4492 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4493 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4494 break;
4495
4496 /* Remove stale persistent group */
4497 if (stale->mode != WPAS_MODE_P2P_GO ||
4498 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004499 wpa_dbg(wpa_s, MSG_DEBUG,
4500 "P2P: Remove stale persistent group id=%d",
4501 stale->id);
4502 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004503 wpa_config_remove_network(wpa_s->conf, stale->id);
4504 } else {
4505 size_t i;
4506
4507 for (i = 0; i < stale->num_p2p_clients; i++) {
4508 if (os_memcmp(stale->p2p_client_list +
4509 i * ETH_ALEN,
4510 dev, ETH_ALEN) == 0) {
4511 os_memmove(stale->p2p_client_list +
4512 i * ETH_ALEN,
4513 stale->p2p_client_list +
4514 (i + 1) * ETH_ALEN,
4515 (stale->num_p2p_clients -
4516 i - 1) * ETH_ALEN);
4517 break;
4518 }
4519 }
4520 stale->num_p2p_clients--;
4521 }
4522 save_config = 1;
4523 }
4524
4525 if (save_config)
4526 p2p_config_write(wpa_s);
4527
4528 if (s) {
4529 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4530 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4531
4532 if (persistent_go && s != persistent_go &&
4533 !persistent_go->num_p2p_clients) {
4534 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004535 wpa_dbg(wpa_s, MSG_DEBUG,
4536 "P2P: Remove empty persistent group id=%d",
4537 persistent_go->id);
4538 wpas_notify_persistent_group_removed(wpa_s,
4539 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004540 wpa_config_remove_network(wpa_s->conf,
4541 persistent_go->id);
4542 /* Save config */
4543 }
4544
4545 wpa_msg_global(wpa_s, MSG_INFO,
4546 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4547 " status=%d"
4548 " adv_id=%x adv_mac=" MACSTR
4549 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004550 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004551 MAC2STR(dev), status,
4552 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004553 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004554 return;
4555 }
4556
Hai Shalom5f92bc92019-04-18 11:54:11 -07004557 wpa_s->global->pending_p2ps_group = 0;
4558 wpa_s->global->pending_p2ps_group_freq = 0;
4559
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004560 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004561 /*
4562 * We need to copy the interface name. Simply saving a
4563 * pointer isn't enough, since if we use pending_interface_name
4564 * it will be overwritten when the group is added.
4565 */
4566 char go_ifname[100];
4567
4568 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004569 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004570 if (!response_done) {
4571 wpa_s->global->pending_p2ps_group = 1;
4572 wpa_s->global->pending_p2ps_group_freq = freq;
4573 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004574
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004575 if (!wpas_p2p_create_iface(wpa_s))
4576 os_memcpy(go_ifname, wpa_s->ifname,
4577 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004578 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004579 os_memcpy(go_ifname,
4580 wpa_s->pending_interface_name,
4581 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004582
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004583 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004584 wpas_p2ps_prov_complete(
4585 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4586 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004587 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004588 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4589 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004590 return;
4591 }
4592
4593 /* If PD Resp complete, start up the GO */
4594 if (response_done && persistent_go) {
4595 wpas_p2p_group_add_persistent(
4596 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004597 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004598 persistent_go->mode ==
4599 WPAS_MODE_P2P_GO ?
4600 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08004601 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004602 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004603 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004604 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004605 }
4606
4607 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004608 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4609 ETH_ALEN);
4610 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004611 }
4612 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004613 os_memcpy(go_ifname, go_wpa_s->ifname,
4614 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004615
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004616 if (is_zero_ether_addr(grp_mac)) {
4617 wpa_dbg(go_wpa_s, MSG_DEBUG,
4618 "P2P: Setting PIN-1 for ANY");
4619 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4620 "12345670", NULL, 0,
4621 0);
4622 } else {
4623 wpa_dbg(go_wpa_s, MSG_DEBUG,
4624 "P2P: Setting PIN-1 for " MACSTR,
4625 MAC2STR(grp_mac));
4626 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4627 "12345670", NULL, 0,
4628 0);
4629 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004630
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004631 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4632 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004633 }
4634
4635 wpa_msg_global(wpa_s, MSG_INFO,
4636 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4637 " status=%d conncap=%x"
4638 " adv_id=%x adv_mac=" MACSTR
4639 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004640 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004641 MAC2STR(dev), status, conncap,
4642 adv_id, MAC2STR(adv_mac),
4643 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004644 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004645 return;
4646 }
4647
4648 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4649 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4650
4651 if (persistent_go && !persistent_go->num_p2p_clients) {
4652 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004653 wpa_dbg(wpa_s, MSG_DEBUG,
4654 "P2P: Remove empty persistent group id=%d",
4655 persistent_go->id);
4656 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004657 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4658 }
4659
4660 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004661 char ssid_hex[32 * 2 + 1];
4662
4663 if (group_ssid)
4664 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4665 group_ssid, group_ssid_len);
4666 else
4667 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004668 wpa_msg_global(wpa_s, MSG_INFO,
4669 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4670 " status=%d conncap=%x"
4671 " adv_id=%x adv_mac=" MACSTR
4672 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004673 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004674 MAC2STR(dev), status, conncap,
4675 adv_id, MAC2STR(adv_mac),
4676 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004677 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4678 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004679 } else {
4680 wpa_msg_global(wpa_s, MSG_INFO,
4681 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4682 " status=%d conncap=%x"
4683 " adv_id=%x adv_mac=" MACSTR
4684 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004685 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004686 MAC2STR(dev), status, conncap,
4687 adv_id, MAC2STR(adv_mac),
4688 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004689 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004690 }
4691}
4692
4693
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004694static int _wpas_p2p_in_progress(void *ctx)
4695{
4696 struct wpa_supplicant *wpa_s = ctx;
4697 return wpas_p2p_in_progress(wpa_s);
4698}
4699
4700
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004701static int wpas_prov_disc_resp_cb(void *ctx)
4702{
4703 struct wpa_supplicant *wpa_s = ctx;
4704 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004705 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004706
4707 if (!wpa_s->global->pending_p2ps_group)
4708 return 0;
4709
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004710 freq = wpa_s->global->pending_p2ps_group_freq;
4711 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004712 wpa_s->global->pending_p2ps_group = 0;
4713
4714 if (wpas_p2p_get_go_group(wpa_s))
4715 return 0;
4716 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4717
4718 if (persistent_go) {
4719 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004720 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4721 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004722 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004723 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
4724 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004725 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004726 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4727 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004728 }
4729
4730 return 1;
4731}
4732
4733
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004734static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4735 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004736 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004737{
4738 struct wpa_supplicant *wpa_s = ctx;
4739
4740 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4741 WPA_IF_P2P_CLIENT, len, freq_list);
4742}
4743
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004744int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4745{
JaeMan Park9de97322022-07-11 15:36:43 +09004746 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004747 u8 addr[ETH_ALEN] = {0};
4748
4749 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4750 return 0;
4751
Hai Shalom60840252021-02-19 19:02:11 -08004752 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4753 if (is_zero_ether_addr(
4754 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4755 !is_zero_ether_addr(wpa_s->own_addr)) {
4756 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4757 wpa_s->own_addr, ETH_ALEN);
4758 }
4759 return 0;
4760 }
4761
4762 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004763 if (random_mac_addr(addr) < 0) {
4764 wpa_msg(wpa_s, MSG_INFO,
4765 "Failed to generate random MAC address");
4766 return -EINVAL;
4767 }
4768
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004769 /* Store generated MAC address. */
4770 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4771 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004772 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004773 /* If there are existing saved groups, restore last MAC address.
4774 * if there is no last used MAC address, the last one is
4775 * factory MAC. */
4776 if (is_zero_ether_addr(
4777 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004778 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004779 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4780 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004781 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4782 }
4783
JaeMan Park9de97322022-07-11 15:36:43 +09004784 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4785
4786 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004787 wpa_msg(wpa_s, MSG_INFO,
4788 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004789 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004790 }
4791
JaeMan Park9de97322022-07-11 15:36:43 +09004792 ret = wpa_supplicant_update_mac_addr(wpa_s);
4793
4794 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004795 wpa_msg(wpa_s, MSG_INFO,
4796 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004797 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004798 }
4799
4800 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4801 MAC2STR(addr));
4802
4803 return 0;
4804}
4805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004806/**
4807 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4808 * @global: Pointer to global data from wpa_supplicant_init()
4809 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4810 * Returns: 0 on success, -1 on failure
4811 */
4812int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4813{
4814 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815 int i;
4816
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004817 if (wpa_s->conf->p2p_disabled)
4818 return 0;
4819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4821 return 0;
4822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004823 if (global->p2p)
4824 return 0;
4825
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004826 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4827 wpa_msg(wpa_s, MSG_ERROR,
4828 "Failed to initialize P2P random MAC address.");
4829 return -1;
4830 }
4831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004834 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 p2p.p2p_scan = wpas_p2p_scan;
4836 p2p.send_action = wpas_send_action;
4837 p2p.send_action_done = wpas_send_action_done;
4838 p2p.go_neg_completed = wpas_go_neg_completed;
4839 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4840 p2p.dev_found = wpas_dev_found;
4841 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004842 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 p2p.start_listen = wpas_start_listen;
4844 p2p.stop_listen = wpas_stop_listen;
4845 p2p.send_probe_resp = wpas_send_probe_resp;
4846 p2p.sd_request = wpas_sd_request;
4847 p2p.sd_response = wpas_sd_response;
4848 p2p.prov_disc_req = wpas_prov_disc_req;
4849 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004850 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851 p2p.invitation_process = wpas_invitation_process;
4852 p2p.invitation_received = wpas_invitation_received;
4853 p2p.invitation_result = wpas_invitation_result;
4854 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004855 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004856 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004857 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004858 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004859 p2p.get_persistent_group = wpas_get_persistent_group;
4860 p2p.get_go_info = wpas_get_go_info;
4861 p2p.remove_stale_groups = wpas_remove_stale_groups;
4862 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4863 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4864 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004865 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004866 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867
4868 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004869 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 p2p.dev_name = wpa_s->conf->device_name;
4871 p2p.manufacturer = wpa_s->conf->manufacturer;
4872 p2p.model_name = wpa_s->conf->model_name;
4873 p2p.model_number = wpa_s->conf->model_number;
4874 p2p.serial_number = wpa_s->conf->serial_number;
4875 if (wpa_s->wps) {
4876 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4877 p2p.config_methods = wpa_s->wps->config_methods;
4878 }
4879
Hai Shalom60840252021-02-19 19:02:11 -08004880 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4881 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004882 wpa_printf(MSG_ERROR,
4883 "P2P: Failed to configure supported channel list");
4884 return -1;
4885 }
4886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 if (wpa_s->conf->p2p_listen_reg_class &&
4888 wpa_s->conf->p2p_listen_channel) {
4889 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4890 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004891 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 /*
4894 * Pick one of the social channels randomly as the listen
4895 * channel.
4896 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004897 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004898 &p2p.channel,
4899 &global->p2p_go_avoid_freq,
4900 &global->p2p_disallow_freq) !=
4901 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004902 wpa_printf(MSG_INFO,
4903 "P2P: No social channels supported by the driver - do not enable P2P");
4904 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004905 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004906 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004908 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4909 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910
4911 if (wpa_s->conf->p2p_oper_reg_class &&
4912 wpa_s->conf->p2p_oper_channel) {
4913 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4914 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4915 p2p.cfg_op_channel = 1;
4916 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4917 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4918
4919 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004921 * Use random operation channel from 2.4 GHz band social
4922 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4923 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004925 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004926 &p2p.op_channel, NULL,
4927 NULL) != 0) {
4928 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004929 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004930 p2p.op_reg_class = 0;
4931 p2p.op_channel = 0;
4932 /* This will be overridden during group setup in
4933 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 p2p.cfg_op_channel = 0;
4936 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4937 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4938 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004939
4940 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4941 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4942 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4943 }
4944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4946 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4947 p2p.country[2] = 0x04;
4948 } else
4949 os_memcpy(p2p.country, "XX\x04", 3);
4950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4952 WPS_DEV_TYPE_LEN);
4953
4954 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4955 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4956 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4957
4958 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4959 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4960
4961 p2p.max_peers = 100;
4962
4963 if (wpa_s->conf->p2p_ssid_postfix) {
4964 p2p.ssid_postfix_len =
4965 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4966 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4967 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4968 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4969 p2p.ssid_postfix_len);
4970 }
4971
4972 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4973
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004974 p2p.max_listen = wpa_s->max_remain_on_chan;
4975
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004976 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4977 wpa_s->conf->p2p_passphrase_len <= 63)
4978 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4979 else
4980 p2p.passphrase_len = 8;
4981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982 global->p2p = p2p_init(&p2p);
4983 if (global->p2p == NULL)
4984 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004985 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986
4987 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4988 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4989 continue;
4990 p2p_add_wps_vendor_extension(
4991 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4992 }
4993
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004994 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 return 0;
4997}
4998
4999
5000/**
5001 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5002 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5003 *
5004 * This function deinitialize per-interface P2P data.
5005 */
5006void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5007{
5008 if (wpa_s->driver && wpa_s->drv_priv)
5009 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005010
5011 if (wpa_s->go_params) {
5012 /* Clear any stored provisioning info */
5013 p2p_clear_provisioning_info(
5014 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005015 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005016 }
5017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 os_free(wpa_s->go_params);
5019 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005020 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5022 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005023 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005024 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5025 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5026 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005027 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005028 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005029 wpas_p2p_listen_work_done(wpa_s);
5030 if (wpa_s->p2p_send_action_work) {
5031 os_free(wpa_s->p2p_send_action_work->ctx);
5032 radio_work_done(wpa_s->p2p_send_action_work);
5033 wpa_s->p2p_send_action_work = NULL;
5034 }
5035 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005037 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5038 wpa_s->p2p_oob_dev_pw = NULL;
5039
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005040 os_free(wpa_s->p2p_group_common_freqs);
5041 wpa_s->p2p_group_common_freqs = NULL;
5042 wpa_s->p2p_group_common_freqs_num = 0;
5043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 /* TODO: remove group interface from the driver if this wpa_s instance
5045 * is on top of a P2P group interface */
5046}
5047
5048
5049/**
5050 * wpas_p2p_deinit_global - Deinitialize global P2P module
5051 * @global: Pointer to global data from wpa_supplicant_init()
5052 *
5053 * This function deinitializes the global (per device) P2P module.
5054 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005055static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056{
5057 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005059 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005060
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005061 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062
5063 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5065 wpa_s = wpa_s->next;
5066 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067 tmp = global->ifaces;
5068 while (tmp &&
5069 (tmp == wpa_s ||
5070 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5071 tmp = tmp->next;
5072 }
5073 if (tmp == NULL)
5074 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005075 /* Disconnect from the P2P group and deinit the interface */
5076 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077 }
5078
5079 /*
5080 * Deinit GO data on any possibly remaining interface (if main
5081 * interface is used as GO).
5082 */
5083 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5084 if (wpa_s->ap_iface)
5085 wpas_p2p_group_deinit(wpa_s);
5086 }
5087
5088 p2p_deinit(global->p2p);
5089 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005090 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091}
5092
5093
5094static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5095{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005096 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005097 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 if (wpa_s->drv_flags &
5099 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5100 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5101 return 1; /* P2P group requires a new interface in every case
5102 */
5103 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5104 return 0; /* driver does not support concurrent operations */
5105 if (wpa_s->global->ifaces->next)
5106 return 1; /* more that one interface already in use */
5107 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5108 return 1; /* this interface is already in use */
5109 return 0;
5110}
5111
5112
5113static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5114 const u8 *peer_addr,
5115 enum p2p_wps_method wps_method,
5116 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005117 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005118 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005120 if (persistent_group && wpa_s->conf->persistent_reconnect)
5121 persistent_group = 2;
5122
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005123 /*
5124 * Increase GO config timeout if HT40 is used since it takes some time
5125 * to scan channels for coex purposes before the BSS can be started.
5126 */
5127 p2p_set_config_timeout(wpa_s->global->p2p,
5128 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5131 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 persistent_group, ssid ? ssid->ssid : NULL,
5133 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005134 wpa_s->p2p_pd_before_go_neg, pref_freq,
5135 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5136 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137}
5138
5139
5140static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5141 const u8 *peer_addr,
5142 enum p2p_wps_method wps_method,
5143 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005144 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005145 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005146{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005147 if (persistent_group && wpa_s->conf->persistent_reconnect)
5148 persistent_group = 2;
5149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5151 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005152 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005153 ssid ? ssid->ssid_len : 0, pref_freq,
5154 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5155 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005156}
5157
5158
5159static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5160{
5161 wpa_s->p2p_join_scan_count++;
5162 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5163 wpa_s->p2p_join_scan_count);
5164 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5165 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5166 " for join operationg - stop join attempt",
5167 MAC2STR(wpa_s->pending_join_iface_addr));
5168 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005169 if (wpa_s->p2p_auto_pd) {
5170 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005171 wpa_msg_global(wpa_s, MSG_INFO,
5172 P2P_EVENT_PROV_DISC_FAILURE
5173 " p2p_dev_addr=" MACSTR " status=N/A",
5174 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005175 return;
5176 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005177 if (wpa_s->p2p_fallback_to_go_neg) {
5178 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5179 "failed - fall back to GO Negotiation");
5180 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5181 P2P_EVENT_FALLBACK_TO_GO_NEG
5182 "reason=join-failed");
5183 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5184 return;
5185 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005186 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005187 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005188 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189 }
5190}
5191
5192
Dmitry Shmidt04949592012-07-19 12:16:46 -07005193static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5194{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005195 int res;
5196 unsigned int num, i;
5197 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005198
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005199 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5200 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005201 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005202 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005203
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005204 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5205 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005206 if (!freqs)
5207 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005209 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5210 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005211
5212 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005213 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005214 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5215 freq);
5216 res = 0;
5217 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005218 }
5219 }
5220
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005221 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005222 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005223
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005224exit_free:
5225 os_free(freqs);
5226 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005227}
5228
5229
5230static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5231 const u8 *peer_dev_addr)
5232{
5233 struct wpa_bss *bss;
5234 int updated;
5235
5236 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5237 if (bss == NULL)
5238 return -1;
5239 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5240 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5241 "last scan");
5242 return 0;
5243 }
5244
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005245 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5246 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005247 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5248 "%ld.%06ld (%supdated in last scan)",
5249 bss->last_update.sec, bss->last_update.usec,
5250 updated ? "": "not ");
5251
5252 return updated;
5253}
5254
5255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5257 struct wpa_scan_results *scan_res)
5258{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005259 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 int freq;
5261 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5264
5265 if (wpa_s->global->p2p_disabled)
5266 return;
5267
Dmitry Shmidt04949592012-07-19 12:16:46 -07005268 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5269 scan_res ? (int) scan_res->num : -1,
5270 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005271
5272 if (scan_res)
5273 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5274
Dmitry Shmidt04949592012-07-19 12:16:46 -07005275 if (wpa_s->p2p_auto_pd) {
5276 int join = wpas_p2p_peer_go(wpa_s,
5277 wpa_s->pending_join_dev_addr);
5278 if (join == 0 &&
5279 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5280 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005281 bss = wpa_bss_get_bssid_latest(
5282 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005283 if (bss) {
5284 freq = bss->freq;
5285 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5286 "the peer " MACSTR " at %d MHz",
5287 wpa_s->auto_pd_scan_retry,
5288 MAC2STR(wpa_s->
5289 pending_join_dev_addr),
5290 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005291 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005292 return;
5293 }
5294 }
5295
5296 if (join < 0)
5297 join = 0;
5298
5299 wpa_s->p2p_auto_pd = 0;
5300 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5301 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5302 MAC2STR(wpa_s->pending_join_dev_addr), join);
5303 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005304 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005305 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005306 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005307 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005308 wpa_msg_global(wpa_s, MSG_INFO,
5309 P2P_EVENT_PROV_DISC_FAILURE
5310 " p2p_dev_addr=" MACSTR " status=N/A",
5311 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005312 }
5313 return;
5314 }
5315
5316 if (wpa_s->p2p_auto_join) {
5317 int join = wpas_p2p_peer_go(wpa_s,
5318 wpa_s->pending_join_dev_addr);
5319 if (join < 0) {
5320 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5321 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005322 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005323 P2P_EVENT_FALLBACK_TO_GO_NEG
5324 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005325 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5326 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5327 wpa_s->p2p_persistent_group, 0, 0, 0,
5328 wpa_s->p2p_go_intent,
5329 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005330 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005331 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005332 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005333 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005334 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005335 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005336 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005337 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005338 NULL, 0,
5339 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005340 return;
5341 }
5342
5343 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5344 "try to join the group", join ? "" :
5345 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005346 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005347 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005348 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005349 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005350 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005351 }
5352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5354 wpa_s->pending_join_iface_addr);
5355 if (freq < 0 &&
5356 p2p_get_interface_addr(wpa_s->global->p2p,
5357 wpa_s->pending_join_dev_addr,
5358 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005359 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5360 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5362 "address for join from " MACSTR " to " MACSTR
5363 " based on newly discovered P2P peer entry",
5364 MAC2STR(wpa_s->pending_join_iface_addr),
5365 MAC2STR(iface_addr));
5366 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5367 ETH_ALEN);
5368
5369 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5370 wpa_s->pending_join_iface_addr);
5371 }
5372 if (freq >= 0) {
5373 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5374 "from P2P peer table: %d MHz", freq);
5375 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005376 if (wpa_s->p2p_join_ssid_len) {
5377 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5378 MACSTR " and SSID %s",
5379 MAC2STR(wpa_s->pending_join_iface_addr),
5380 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5381 wpa_s->p2p_join_ssid_len));
5382 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5383 wpa_s->p2p_join_ssid,
5384 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005385 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005386 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5387 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5388 bss = wpa_bss_get_bssid_latest(wpa_s,
5389 wpa_s->pending_join_iface_addr);
5390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005392 u8 dev_addr[ETH_ALEN];
5393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394 freq = bss->freq;
5395 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005396 "from BSS table: %d MHz (SSID %s)", freq,
5397 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005398 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005399 dev_addr) == 0 &&
5400 os_memcmp(wpa_s->pending_join_dev_addr,
5401 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5402 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5403 ETH_ALEN) != 0) {
5404 wpa_printf(MSG_DEBUG,
5405 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5406 MAC2STR(dev_addr),
5407 MAC2STR(wpa_s->pending_join_dev_addr));
5408 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5409 ETH_ALEN);
5410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411 }
5412 if (freq > 0) {
5413 u16 method;
5414
Dmitry Shmidt04949592012-07-19 12:16:46 -07005415 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005416 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005417 P2P_EVENT_GROUP_FORMATION_FAILURE
5418 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005419 wpas_notify_p2p_group_formation_failure(
5420 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005421 return;
5422 }
5423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005424 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5425 "prior to joining an existing group (GO " MACSTR
5426 " freq=%u MHz)",
5427 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5428 wpa_s->pending_pd_before_join = 1;
5429
5430 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 case WPS_PIN_DISPLAY:
5432 method = WPS_CONFIG_KEYPAD;
5433 break;
5434 case WPS_PIN_KEYPAD:
5435 method = WPS_CONFIG_DISPLAY;
5436 break;
5437 case WPS_PBC:
5438 method = WPS_CONFIG_PUSHBUTTON;
5439 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005440 case WPS_P2PS:
5441 method = WPS_CONFIG_P2PS;
5442 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443 default:
5444 method = 0;
5445 break;
5446 }
5447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005448 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5449 wpa_s->pending_join_dev_addr) ==
5450 method)) {
5451 /*
5452 * We have already performed provision discovery for
5453 * joining the group. Proceed directly to join
5454 * operation without duplicated provision discovery. */
5455 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5456 "with " MACSTR " already done - proceed to "
5457 "join",
5458 MAC2STR(wpa_s->pending_join_dev_addr));
5459 wpa_s->pending_pd_before_join = 0;
5460 goto start;
5461 }
5462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005464 wpa_s->pending_join_dev_addr,
5465 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005466 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5468 "Discovery Request before joining an "
5469 "existing group");
5470 wpa_s->pending_pd_before_join = 0;
5471 goto start;
5472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 return;
5474 }
5475
5476 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5477 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5478 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5479 wpas_p2p_check_join_scan_limit(wpa_s);
5480 return;
5481
5482start:
5483 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005484 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5485 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486}
5487
5488
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005489static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5490 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005491{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492 int ret;
5493 struct wpa_driver_scan_params params;
5494 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005495 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005496 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005497 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005498
5499 os_memset(&params, 0, sizeof(params));
5500
5501 /* P2P Wildcard SSID */
5502 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005503 if (ssid && ssid_len) {
5504 params.ssids[0].ssid = ssid;
5505 params.ssids[0].ssid_len = ssid_len;
5506 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5507 wpa_s->p2p_join_ssid_len = ssid_len;
5508 } else {
5509 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5510 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5511 wpa_s->p2p_join_ssid_len = 0;
5512 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513
5514 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005515 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5516 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5517 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005518 if (wps_ie == NULL) {
5519 wpas_p2p_scan_res_join(wpa_s, NULL);
5520 return;
5521 }
5522
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005523 if (!freq) {
5524 int oper_freq;
5525 /*
5526 * If freq is not provided, check the operating freq of the GO
5527 * and use a single channel scan on if possible.
5528 */
5529 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5530 wpa_s->pending_join_iface_addr);
5531 if (oper_freq > 0)
5532 freq = oper_freq;
5533 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005534 if (freq > 0) {
5535 freqs[0] = freq;
5536 params.freqs = freqs;
Hai Shalomc1a21442022-02-04 13:43:00 -08005537 } else if (wpa_s->conf->p2p_6ghz_disable ||
5538 !is_p2p_allow_6ghz(wpa_s->global->p2p)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07005539 wpa_printf(MSG_DEBUG,
5540 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005541 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5542 0);
5543 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5544 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005547 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5548 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5549 if (ies == NULL) {
5550 wpabuf_free(wps_ie);
5551 wpas_p2p_scan_res_join(wpa_s, NULL);
5552 return;
5553 }
5554 wpabuf_put_buf(ies, wps_ie);
5555 wpabuf_free(wps_ie);
5556
5557 bands = wpas_get_bands(wpa_s, freqs);
5558 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5559
5560 params.p2p_probe = 1;
5561 params.extra_ies = wpabuf_head(ies);
5562 params.extra_ies_len = wpabuf_len(ies);
5563
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005564 if (wpa_s->clear_driver_scan_cache) {
5565 wpa_printf(MSG_DEBUG,
5566 "Request driver to clear scan cache due to local BSS flush");
5567 params.only_new_results = 1;
5568 }
5569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570 /*
5571 * Run a scan to update BSS table and start Provision Discovery once
5572 * the new scan results become available.
5573 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005574 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005575 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005576 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005577 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005578 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005579 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005580 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005581 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005582 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583
5584 wpabuf_free(ies);
5585
5586 if (ret) {
5587 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5588 "try again later");
5589 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5590 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5591 wpas_p2p_check_join_scan_limit(wpa_s);
5592 }
5593}
5594
5595
Dmitry Shmidt04949592012-07-19 12:16:46 -07005596static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5597{
5598 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005599 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005600}
5601
5602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005604 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005605 int auto_join, int op_freq,
5606 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607{
5608 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005609 MACSTR " dev " MACSTR " op_freq=%d)%s",
5610 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005611 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005612 if (ssid && ssid_len) {
5613 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5614 wpa_ssid_txt(ssid, ssid_len));
5615 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005616
Dmitry Shmidt04949592012-07-19 12:16:46 -07005617 wpa_s->p2p_auto_pd = 0;
5618 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5620 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5621 wpa_s->pending_join_wps_method = wps_method;
5622
5623 /* Make sure we are not running find during connection establishment */
5624 wpas_p2p_stop_find(wpa_s);
5625
5626 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005627 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628 return 0;
5629}
5630
5631
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005632static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5633 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634{
5635 struct wpa_supplicant *group;
5636 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005637 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638
5639 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5640 if (group == NULL)
5641 return -1;
5642 if (group != wpa_s) {
5643 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5644 sizeof(group->p2p_pin));
5645 group->p2p_wps_method = wpa_s->p2p_wps_method;
5646 }
5647
Hai Shalom74f70d42019-02-11 14:42:39 -08005648 /*
5649 * Need to mark the current interface for p2p_group_formation
5650 * when a separate group interface is not used. This is needed
5651 * to allow p2p_cancel stop a pending p2p_connect-join.
5652 * wpas_p2p_init_group_interface() addresses this for the case
5653 * where a separate group interface is used.
5654 */
5655 if (group == wpa_s->parent)
5656 wpa_s->global->p2p_group_formation = group;
5657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005658 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005659 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005660
5661 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005662 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5664 ETH_ALEN);
5665 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005666 if (freq && ssid && ssid_len) {
5667 res.freq = freq;
5668 res.ssid_len = ssid_len;
5669 os_memcpy(res.ssid, ssid, ssid_len);
5670 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005671 if (ssid && ssid_len) {
5672 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5673 ssid, ssid_len);
5674 } else {
5675 bss = wpa_bss_get_bssid_latest(
5676 wpa_s, wpa_s->pending_join_iface_addr);
5677 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005678 if (bss) {
5679 res.freq = bss->freq;
5680 res.ssid_len = bss->ssid_len;
5681 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5682 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5683 bss->freq,
5684 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005685 } else if (ssid && ssid_len) {
5686 res.ssid_len = ssid_len;
5687 os_memcpy(res.ssid, ssid, ssid_len);
5688 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5689 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005690 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005691 }
5692
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005693 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5694 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5695 "starting client");
5696 wpa_drv_cancel_remain_on_channel(wpa_s);
5697 wpa_s->off_channel_freq = 0;
5698 wpa_s->roc_waiting_drv_freq = 0;
5699 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700 wpas_start_wps_enrollee(group, &res);
5701
5702 /*
5703 * Allow a longer timeout for join-a-running-group than normal 15
5704 * second group formation timeout since the GO may not have authorized
5705 * our connection yet.
5706 */
5707 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5708 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5709 wpa_s, NULL);
5710
5711 return 0;
5712}
5713
5714
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005715static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005716 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005717 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005718 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005719{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005720 struct wpa_used_freq_data *freqs;
5721 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005722 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305723 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005724
5725 max_pref_freq = *num_pref_freq;
5726 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005727
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005728 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5729 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005730 if (!freqs)
5731 return -1;
5732
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005733 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5734 wpa_s->num_multichan_concurrent);
5735
5736 /*
5737 * It is possible that the total number of used frequencies is bigger
5738 * than the number of frequencies used for P2P, so get the system wide
5739 * number of unused frequencies.
5740 */
5741 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5742
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005743 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005744 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5745 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005746
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005747 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005748 int ret;
5749 if (go)
5750 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5751 else
5752 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5753 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005754 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005755 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5756 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005757 /*
5758 * If freq is a DFS channel and DFS is offloaded
5759 * to the driver, allow P2P GO to use it.
5760 */
5761 wpa_printf(MSG_DEBUG,
5762 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5763 freq);
5764 } else {
5765 wpa_printf(MSG_DEBUG,
5766 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5767 freq);
5768 res = -3;
5769 goto exit_free;
5770 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005771 }
5772
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005773 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005774 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005775 freq_in_use = 1;
5776 }
5777
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005778 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005779 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5780 freq);
5781 res = -2;
5782 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005783 }
5784 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5785 "requested channel (%u MHz)", freq);
5786 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005787 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005788 }
5789
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005790 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005791
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305792 if (*pref_freq == 0) {
5793 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005794 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305795 while (i < wpa_s->conf->num_p2p_pref_chan) {
5796 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5797 wpa_s->conf->p2p_pref_chan[i].op_class,
5798 wpa_s->conf->p2p_pref_chan[i].chan);
5799
5800 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5801 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5802 best_freq = p2p_pref_freq;
5803 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5804 "from P2P preferred channel list", best_freq);
5805 break;
5806 } else {
5807 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5808 "frequency (%u MHz) ", p2p_pref_freq);
5809 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005810 i++;
5811 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305812 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305813 enum wpa_driver_if_type iface_type;
5814
5815 if (go)
5816 iface_type = WPA_IF_P2P_GO;
5817 else
5818 iface_type = WPA_IF_P2P_CLIENT;
5819
5820 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5821 best_freq, go);
5822
5823 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5824 &max_pref_freq,
5825 pref_freq_list);
5826 if (!res && max_pref_freq > 0) {
5827 *num_pref_freq = max_pref_freq;
5828 i = 0;
5829 while (i < *num_pref_freq &&
5830 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005831 pref_freq_list[i].freq) ||
5832 wpas_p2p_disallowed_freq(
5833 wpa_s->global,
5834 pref_freq_list[i].freq) ||
5835 !p2p_pref_freq_allowed(&pref_freq_list[i],
5836 go))) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305837 wpa_printf(MSG_DEBUG,
5838 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00005839 i, pref_freq_list[i].freq);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305840 i++;
5841 }
5842 if (i != *num_pref_freq) {
Sunil8cd6f4d2022-06-28 18:40:46 +00005843 best_freq = pref_freq_list[i].freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305844 wpa_printf(MSG_DEBUG,
5845 "P2P: Using preferred_freq_list[%d]=%d",
5846 i, best_freq);
5847 } else {
5848 wpa_printf(MSG_DEBUG,
5849 "P2P: All driver preferred frequencies are "
5850 "disallowed for P2P use");
5851 *num_pref_freq = 0;
5852 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005853 } else {
5854 wpa_printf(MSG_DEBUG,
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305855 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005856 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005857 }
5858 }
5859
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005860 /* We have a candidate frequency to use */
5861 if (best_freq > 0) {
5862 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005863 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005864 best_freq);
5865 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005866 } else {
5867 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 -07005868 best_freq);
5869 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005870 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005871 } else if (num_unused > 0) {
5872 wpa_printf(MSG_DEBUG,
5873 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5874 *force_freq = 0;
5875 } else {
5876 wpa_printf(MSG_DEBUG,
5877 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5878 res = -2;
5879 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005880 }
5881
5882exit_ok:
5883 res = 0;
5884exit_free:
5885 os_free(freqs);
5886 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005887}
5888
5889
Hai Shalomc1a21442022-02-04 13:43:00 -08005890static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5891 const u8 *peer_addr)
5892{
5893 if (wpa_s->conf->p2p_6ghz_disable ||
5894 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5895 HOSTAPD_MODE_IEEE80211A, true))
5896 return false;
5897
5898 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5899 return false;
5900 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5901 return false;
5902
5903 return true;
5904}
5905
5906
5907static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5908 const u8 *peer_addr, bool allow_6ghz, int freq)
5909{
5910 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5911 wpa_printf(MSG_DEBUG,
5912 "P2P: Allow connection on 6 GHz channels");
5913 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5914 } else {
5915 if (is_6ghz_freq(freq))
5916 return -2;
5917 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5918 }
5919
5920 return 0;
5921}
5922
5923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005924/**
5925 * wpas_p2p_connect - Request P2P Group Formation to be started
5926 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5927 * @peer_addr: Address of the peer P2P Device
5928 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5929 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005930 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005931 * @join: Whether to join an existing group (as a client) instead of starting
5932 * Group Owner negotiation; @peer_addr is BSSID in that case
5933 * @auth: Whether to only authorize the connection instead of doing that and
5934 * initiating Group Owner negotiation
5935 * @go_intent: GO Intent or -1 to use default
5936 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005937 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005938 * @persistent_id: Persistent group credentials to use for forcing GO
5939 * parameters or -1 to generate new values (SSID/passphrase)
5940 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5941 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005942 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005943 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005944 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005945 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005946 * @group_ssid: Specific Group SSID for join or %NULL if not set
5947 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08005948 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005949 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5950 * failure, -2 on failure due to channel not currently available,
5951 * -3 if forced channel is not supported
5952 */
5953int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5954 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005955 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005956 int go_intent, int freq, unsigned int vht_center_freq2,
5957 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005958 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005959 const u8 *group_ssid, size_t group_ssid_len,
5960 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005962 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005963 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005965 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005966 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00005967 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
5968 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969
5970 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5971 return -1;
5972
Dmitry Shmidt04949592012-07-19 12:16:46 -07005973 if (persistent_id >= 0) {
5974 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5975 if (ssid == NULL || ssid->disabled != 2 ||
5976 ssid->mode != WPAS_MODE_P2P_GO)
5977 return -1;
5978 }
5979
Hai Shalomc1a21442022-02-04 13:43:00 -08005980 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07005981 return -2;
5982
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005983 os_free(wpa_s->global->add_psk);
5984 wpa_s->global->add_psk = NULL;
5985
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005986 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005987 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005988 wpa_s->global->pending_p2ps_group_freq = 0;
5989 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991 if (go_intent < 0)
5992 go_intent = wpa_s->conf->p2p_go_intent;
5993
5994 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005995 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005996
5997 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005998 wpa_s->p2p_persistent_group = !!persistent_group;
5999 wpa_s->p2p_persistent_id = persistent_id;
6000 wpa_s->p2p_go_intent = go_intent;
6001 wpa_s->p2p_connect_freq = freq;
6002 wpa_s->p2p_fallback_to_go_neg = 0;
6003 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006004 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006005 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006006 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6007 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006008 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006009 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010
6011 if (pin)
6012 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6013 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006014 if (wps_generate_pin((unsigned int *) &ret) < 0)
6015 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006016 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6017 "%08d", ret);
6018 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6019 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006020 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6021 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006022 } else if (wps_method == WPS_P2PS) {
6023 /* Force the P2Ps default PIN to be used */
6024 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006025 } else
6026 wpa_s->p2p_pin[0] = '\0';
6027
Dmitry Shmidt04949592012-07-19 12:16:46 -07006028 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006029 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6030 if (auth) {
6031 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6032 "connect a running group from " MACSTR,
6033 MAC2STR(peer_addr));
6034 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6035 return ret;
6036 }
6037 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6038 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6039 iface_addr) < 0) {
6040 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6041 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6042 dev_addr);
6043 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006044 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006045 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006046 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6047 "%ld.%06ld",
6048 wpa_s->p2p_auto_started.sec,
6049 wpa_s->p2p_auto_started.usec);
6050 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006051 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006052 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006053 auto_join, freq,
6054 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006055 return -1;
6056 return ret;
6057 }
6058
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006059 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006060 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006061 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006062 if (res)
6063 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006064 wpas_p2p_set_own_freq_preference(wpa_s,
6065 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006067 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6070
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006071 if (wpa_s->create_p2p_iface) {
6072 /* Prepare to add a new interface for the group */
6073 iftype = WPA_IF_P2P_GROUP;
6074 if (go_intent == 15)
6075 iftype = WPA_IF_P2P_GO;
6076 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6077 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6078 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006080 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006081
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006082 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006083 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006084 if (wpa_s->p2p_mgmt)
6085 if_addr = wpa_s->parent->own_addr;
6086 else
6087 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006088 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006090
6091 if (auth) {
6092 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006093 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006094 force_freq, persistent_group, ssid,
6095 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006096 return -1;
6097 return ret;
6098 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006099
6100 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6101 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006102 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006103 if (wpa_s->create_p2p_iface)
6104 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006105 return -1;
6106 }
6107 return ret;
6108}
6109
6110
6111/**
6112 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6113 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6114 * @freq: Frequency of the channel in MHz
6115 * @duration: Duration of the stay on the channel in milliseconds
6116 *
6117 * This callback is called when the driver indicates that it has started the
6118 * requested remain-on-channel duration.
6119 */
6120void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6121 unsigned int freq, unsigned int duration)
6122{
6123 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6124 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006125 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)",
6126 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6127 wpa_s->roc_waiting_drv_freq, freq, duration);
6128 if (wpa_s->off_channel_freq &&
6129 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006130 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6131 wpa_s->pending_listen_duration);
6132 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006133 } else {
6134 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6135 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6136 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006137 }
6138}
6139
6140
Jithu Jance57cea1a2014-08-20 10:22:04 -07006141int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142{
6143 /* Limit maximum Listen state time based on driver limitation. */
6144 if (timeout > wpa_s->max_remain_on_chan)
6145 timeout = wpa_s->max_remain_on_chan;
6146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006147 return p2p_listen(wpa_s->global->p2p, timeout);
6148}
6149
6150
6151/**
6152 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6153 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6154 * @freq: Frequency of the channel in MHz
6155 *
6156 * This callback is called when the driver indicates that a remain-on-channel
6157 * operation has been completed, i.e., the duration on the requested channel
6158 * has timed out.
6159 */
6160void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6161 unsigned int freq)
6162{
6163 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6164 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006165 wpa_s->global->p2p_long_listen,
6166 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006167 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6169 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006170 if (wpa_s->global->p2p_long_listen > 0)
6171 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6173 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006174 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006175 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006176 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006178 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006179 } else {
6180 /*
6181 * When listen duration is over, stop listen & update p2p_state
6182 * to IDLE.
6183 */
6184 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006185 }
6186}
6187
6188
6189/**
6190 * wpas_p2p_group_remove - Remove a P2P group
6191 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6192 * @ifname: Network interface name of the group interface or "*" to remove all
6193 * groups
6194 * Returns: 0 on success, -1 on failure
6195 *
6196 * This function is used to remove a P2P group. This can be used to disconnect
6197 * from a group in which the local end is a P2P Client or to end a P2P Group in
6198 * case the local end is the Group Owner. If a virtual network interface was
6199 * created for this group, that interface will be removed. Otherwise, only the
6200 * configured P2P group network will be removed from the interface.
6201 */
6202int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6203{
6204 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006205 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006206
6207 if (os_strcmp(ifname, "*") == 0) {
6208 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006209 bool calling_wpa_s_group_removed = false;
6210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211 wpa_s = global->ifaces;
6212 while (wpa_s) {
6213 prev = wpa_s;
6214 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006215 if (prev->p2p_group_interface !=
6216 NOT_P2P_GROUP_INTERFACE ||
6217 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006218 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006219 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006220 if (prev == calling_wpa_s)
6221 calling_wpa_s_group_removed = true;
6222 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006223 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006224
6225 if (!calling_wpa_s_group_removed &&
6226 (calling_wpa_s->p2p_group_interface !=
6227 NOT_P2P_GROUP_INTERFACE ||
6228 (calling_wpa_s->current_ssid &&
6229 calling_wpa_s->current_ssid->p2p_group))) {
6230 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6231 wpas_p2p_disconnect_safely(calling_wpa_s,
6232 calling_wpa_s);
6233 }
6234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006235 return 0;
6236 }
6237
6238 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6239 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6240 break;
6241 }
6242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006243 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006244}
6245
6246
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006247static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6248{
6249 unsigned int r;
6250
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006251 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6252 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006253 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006254 int res;
6255
6256 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6257 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006258 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6259 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6260
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006261 if (!res && size > 0) {
6262 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006263 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006264 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006265 pref_freq_list[i].freq) ||
6266 wpas_p2p_disallowed_freq(
6267 wpa_s->global,
6268 pref_freq_list[i].freq) ||
6269 !p2p_pref_freq_allowed(&pref_freq_list[i],
6270 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006271 wpa_printf(MSG_DEBUG,
6272 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006273 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006274 i++;
6275 }
6276 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006277 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006278 wpa_printf(MSG_DEBUG,
6279 "P2P: Using preferred_freq_list[%d]=%d",
6280 i, freq);
6281 } else {
6282 wpa_printf(MSG_DEBUG,
6283 "P2P: All driver preferred frequencies are disallowed for P2P use");
6284 }
6285 } else {
6286 wpa_printf(MSG_DEBUG,
6287 "P2P: No preferred frequency list available");
6288 }
6289 }
6290
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006291 if (freq == 2) {
6292 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6293 "band");
6294 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006295 p2p_supported_freq_go(wpa_s->global->p2p,
6296 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006297 freq = wpa_s->best_24_freq;
6298 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6299 "channel: %d MHz", freq);
6300 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006301 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6302 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006303 int possible_2g_freqs[] = {
6304 /* operating class 81 */
6305 2412, 2437, 2462,
6306 };
6307 int possible_2g_freqs_num =
6308 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6309 int i;
6310 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6311 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6312 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6313 break;
6314 }
6315 }
6316
6317 if (i >= possible_2g_freqs_num) {
6318 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6319 "2.4 GHz channel for P2P group");
6320 return -1;
6321 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006322 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6323 "channel: %d MHz", freq);
6324 }
6325 }
6326
6327 if (freq == 5) {
6328 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6329 "band");
6330 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006331 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006332 wpa_s->best_5_freq)) {
6333 freq = wpa_s->best_5_freq;
6334 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6335 "channel: %d MHz", freq);
6336 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006337 const int freqs[] = {
6338 /* operating class 115 */
6339 5180, 5200, 5220, 5240,
6340 /* operating class 124 */
6341 5745, 5765, 5785, 5805,
6342 };
6343 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6344
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006345 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6346 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006347
6348 /*
6349 * most of 5G channels are DFS, only operating class 115 and 124
6350 * are available possibly, randomly pick a start to check them.
6351 */
6352 int possible_5g_freqs[] = {
6353 /* operating class 115 */
6354 5180, 5200, 5220, 5240,
6355 /* operating class 124 */
6356 5745, 5765, 5785, 5805,
6357 };
6358 int possible_5g_freqs_num =
6359 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6360
Jimmy Chen64b90632020-09-29 17:27:34 +08006361 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6362 if (p2p_supported_freq_go(
6363 wpa_s->global->p2p,
6364 possible_5g_freqs[r % possible_5g_freqs_num])) {
6365 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6366 break;
6367 }
6368 }
6369
6370 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006371 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6372 "5 GHz channel for P2P group");
6373 return -1;
6374 }
6375 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6376 "channel: %d MHz", freq);
6377 }
6378 }
6379
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006380 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006381 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006382 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6383 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006384 /*
6385 * If freq is a DFS channel and DFS is offloaded to the
6386 * driver, allow P2P GO to use it.
6387 */
6388 wpa_printf(MSG_DEBUG, "P2P: "
6389 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6390 __func__, freq);
6391 return freq;
6392 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006393 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6394 "(%u MHz) is not supported for P2P uses",
6395 freq);
6396 return -1;
6397 }
6398
6399 return freq;
6400}
6401
6402
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006403static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6404 const struct p2p_channels *channels,
6405 int freq)
6406{
6407 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6408 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6409 freq_included(wpa_s, channels, freq))
6410 return 1;
6411 return 0;
6412}
6413
6414
6415static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6416 struct p2p_go_neg_results *params,
6417 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006418{
6419 unsigned int i, r;
6420
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006421 /* try all channels in operating class 115 */
6422 for (i = 0; i < 4; i++) {
6423 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006424 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006425 goto out;
6426 }
6427
6428 /* try all channels in operating class 124 */
6429 for (i = 0; i < 4; i++) {
6430 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006431 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006432 goto out;
6433 }
6434
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006435 /* try social channel class 180 channel 2 */
6436 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006437 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006438 goto out;
6439
6440 /* try all channels in reg. class 180 */
6441 for (i = 0; i < 4; i++) {
6442 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006443 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006444 goto out;
6445 }
6446
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006447 /* try some random selection of the social channels */
6448 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6449 return;
6450
6451 for (i = 0; i < 3; i++) {
6452 params->freq = 2412 + ((r + i) % 3) * 25;
6453 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6454 goto out;
6455 }
6456
6457 /* try all other channels in operating class 81 */
6458 for (i = 0; i < 11; i++) {
6459 params->freq = 2412 + i * 5;
6460
6461 /* skip social channels; covered in the previous loop */
6462 if (params->freq == 2412 ||
6463 params->freq == 2437 ||
6464 params->freq == 2462)
6465 continue;
6466
6467 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6468 goto out;
6469 }
6470
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006471 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006472 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006473 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006474out:
6475 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6476 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006477}
6478
6479
Roshan Pius3a1667e2018-07-03 15:17:14 -07006480static int wpas_same_band(int freq1, int freq2)
6481{
6482 enum hostapd_hw_mode mode1, mode2;
6483 u8 chan1, chan2;
6484
6485 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6486 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6487 if (mode1 == NUM_HOSTAPD_MODES)
6488 return 0;
6489 return mode1 == mode2;
6490}
6491
6492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006493static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6494 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006495 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006496 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006497 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006498 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006499{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006500 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006501 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006502 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006503 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006504 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505
6506 os_memset(params, 0, sizeof(*params));
6507 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006508 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006509 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006510 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006511 params->max_oper_chwidth = max_oper_chwidth;
6512 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006513 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006514
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006515 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6516 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006517 if (!freqs)
6518 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006519
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006520 num = get_shared_radio_freqs_data(wpa_s, freqs,
6521 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006522
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006523 if (wpa_s->current_ssid &&
6524 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6525 wpa_s->wpa_state == WPA_COMPLETED) {
6526 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6527 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006528
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006529 /*
6530 * If the frequency selection is done for an active P2P GO that
6531 * is not sharing a frequency, allow to select a new frequency
6532 * even if there are no unused frequencies as we are about to
6533 * move the P2P GO so its frequency can be re-used.
6534 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006535 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006536 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6537 freqs[i].flags == 0) {
6538 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006539 break;
6540 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006541 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006542 }
6543
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006544 /* Try to use EDMG channel */
6545 if (params->edmg) {
6546 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6547 goto success;
6548 params->edmg = 0;
6549 }
6550
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006551 /* try using the forced freq */
6552 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006553 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6554 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006555 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006556 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006557 freq);
6558 goto fail;
6559 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006560 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6561 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006562 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6563 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006564 /*
6565 * If freq is a DFS channel and DFS is offloaded
6566 * to the driver, allow P2P GO to use it.
6567 */
6568 wpa_printf(MSG_DEBUG,
6569 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6570 __func__, freq);
6571 } else {
6572 wpa_printf(MSG_DEBUG,
6573 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6574 freq);
6575 goto fail;
6576 }
6577 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006578
6579 for (i = 0; i < num; i++) {
6580 if (freqs[i].freq == freq) {
6581 wpa_printf(MSG_DEBUG,
6582 "P2P: forced freq (%d MHz) is also shared",
6583 freq);
6584 params->freq = freq;
6585 goto success;
6586 }
6587 }
6588
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006589 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006590 wpa_printf(MSG_DEBUG,
6591 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6592 freq);
6593 goto fail;
6594 }
6595
6596 wpa_printf(MSG_DEBUG,
6597 "P2P: force GO freq (%d MHz) on a free channel",
6598 freq);
6599 params->freq = freq;
6600 goto success;
6601 }
6602
6603 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006604 if (num &&
6605 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006606 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6607 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6608 wpa_printf(MSG_DEBUG,
6609 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006610 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006611 params->freq = cand;
6612 goto success;
6613 }
6614
6615 /* try using one of the shared freqs */
6616 for (i = 0; i < num; i++) {
6617 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6618 freqs[i].freq)) {
6619 wpa_printf(MSG_DEBUG,
6620 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006621 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006622 params->freq = freqs[i].freq;
6623 goto success;
6624 }
6625 }
6626 }
6627
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006628 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006629 wpa_printf(MSG_DEBUG,
6630 "P2P: Cannot force GO on any of the channels we are already using");
6631 goto fail;
6632 }
6633
6634 /* try using the setting from the configuration file */
6635 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6636 wpa_s->conf->p2p_oper_channel >= 1 &&
6637 wpa_s->conf->p2p_oper_channel <= 11 &&
6638 wpas_p2p_supported_freq_go(
6639 wpa_s, channels,
6640 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6641 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6642 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6643 "frequency %d MHz", params->freq);
6644 goto success;
6645 }
6646
6647 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6648 wpa_s->conf->p2p_oper_reg_class == 116 ||
6649 wpa_s->conf->p2p_oper_reg_class == 117 ||
6650 wpa_s->conf->p2p_oper_reg_class == 124 ||
6651 wpa_s->conf->p2p_oper_reg_class == 125 ||
6652 wpa_s->conf->p2p_oper_reg_class == 126 ||
6653 wpa_s->conf->p2p_oper_reg_class == 127) &&
6654 wpas_p2p_supported_freq_go(wpa_s, channels,
6655 5000 +
6656 5 * wpa_s->conf->p2p_oper_channel)) {
6657 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6658 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6659 "frequency %d MHz", params->freq);
6660 goto success;
6661 }
6662
6663 /* Try using best channels */
6664 if (wpa_s->conf->p2p_oper_channel == 0 &&
6665 wpa_s->best_overall_freq > 0 &&
6666 wpas_p2p_supported_freq_go(wpa_s, channels,
6667 wpa_s->best_overall_freq)) {
6668 params->freq = wpa_s->best_overall_freq;
6669 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6670 "channel %d MHz", params->freq);
6671 goto success;
6672 }
6673
6674 if (wpa_s->conf->p2p_oper_channel == 0 &&
6675 wpa_s->best_24_freq > 0 &&
6676 wpas_p2p_supported_freq_go(wpa_s, channels,
6677 wpa_s->best_24_freq)) {
6678 params->freq = wpa_s->best_24_freq;
6679 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6680 "channel %d MHz", params->freq);
6681 goto success;
6682 }
6683
6684 if (wpa_s->conf->p2p_oper_channel == 0 &&
6685 wpa_s->best_5_freq > 0 &&
6686 wpas_p2p_supported_freq_go(wpa_s, channels,
6687 wpa_s->best_5_freq)) {
6688 params->freq = wpa_s->best_5_freq;
6689 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6690 "channel %d MHz", params->freq);
6691 goto success;
6692 }
6693
6694 /* try using preferred channels */
6695 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6696 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6697 params->freq = cand;
6698 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6699 "channels", params->freq);
6700 goto success;
6701 }
6702
Roshan Pius3a1667e2018-07-03 15:17:14 -07006703 /* Try using a channel that allows VHT to be used with 80 MHz */
6704 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6705 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6706 enum hostapd_hw_mode mode;
6707 struct hostapd_hw_modes *hwmode;
6708 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006709 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006710
6711 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006712 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006713 mode = ieee80211_freq_to_chan(cand, &chan);
6714 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006715 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006716 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006717 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6718 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006719 continue;
6720 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6721 params->freq = cand;
6722 wpa_printf(MSG_DEBUG,
6723 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6724 params->freq);
6725 goto success;
6726 }
6727 }
6728 }
6729
6730 /* Try using a channel that allows HT to be used with 40 MHz on the same
6731 * band so that CSA can be used */
6732 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6733 wpa_s->p2p_group_common_freqs) {
6734 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6735 enum hostapd_hw_mode mode;
6736 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006737 u8 chan, op_class;
6738 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006739
Hai Shalomc1a21442022-02-04 13:43:00 -08006740 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006741 cand = wpa_s->p2p_group_common_freqs[i];
6742 mode = ieee80211_freq_to_chan(cand, &chan);
6743 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006744 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006745 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6746 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006747 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006748 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006749 if (is_6ghz &&
6750 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6751 BW40) == ALLOWED)
6752 supported = true;
6753
6754 if (!is_6ghz &&
6755 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006756 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6757 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006758 wpas_p2p_verify_channel(
6759 wpa_s, hwmode, op_class, chan,
6760 BW40MINUS) == ALLOWED)
6761 supported = true;
6762
6763 if (!supported && !is_6ghz &&
6764 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006765 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6766 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006767 wpas_p2p_verify_channel(
6768 wpa_s, hwmode, op_class, chan,
6769 BW40PLUS) == ALLOWED)
6770 supported = true;
6771
6772 if (!supported)
6773 continue;
6774
Roshan Pius3a1667e2018-07-03 15:17:14 -07006775 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6776 params->freq = cand;
6777 wpa_printf(MSG_DEBUG,
6778 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6779 params->freq);
6780 goto success;
6781 }
6782 }
6783 }
6784
6785 /* Try using one of the group common freqs on the same band so that CSA
6786 * can be used */
6787 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6788 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6789 cand = wpa_s->p2p_group_common_freqs[i];
6790 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6791 cand))
6792 continue;
6793 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6794 params->freq = cand;
6795 wpa_printf(MSG_DEBUG,
6796 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6797 params->freq);
6798 goto success;
6799 }
6800 }
6801 }
6802
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006803 /* Try using one of the group common freqs */
6804 if (wpa_s->p2p_group_common_freqs) {
6805 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6806 cand = wpa_s->p2p_group_common_freqs[i];
6807 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6808 params->freq = cand;
6809 wpa_printf(MSG_DEBUG,
6810 "P2P: Use freq %d MHz common with the peer",
6811 params->freq);
6812 goto success;
6813 }
6814 }
6815 }
6816
6817 /* no preference, select some channel */
6818 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6819
6820 if (params->freq == 0) {
6821 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6822 goto fail;
6823 }
6824
6825success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006826 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006827 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006828fail:
6829 os_free(freqs);
6830 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006831}
6832
6833
6834static struct wpa_supplicant *
6835wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6836 int go)
6837{
6838 struct wpa_supplicant *group_wpa_s;
6839
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006840 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006841 if (wpa_s->p2p_mgmt) {
6842 /*
6843 * We may be called on the p2p_dev interface which
6844 * cannot be used for group operations, so always use
6845 * the primary interface.
6846 */
6847 wpa_s->parent->p2pdev = wpa_s;
6848 wpa_s = wpa_s->parent;
6849 }
6850 wpa_dbg(wpa_s, MSG_DEBUG,
6851 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006852 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006853 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006854 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006856 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857
6858 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006859 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006860 wpa_msg_global(wpa_s, MSG_ERROR,
6861 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006862 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006864 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6865 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006866 wpa_msg_global(wpa_s, MSG_ERROR,
6867 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006868 wpas_p2p_remove_pending_group_interface(wpa_s);
6869 return NULL;
6870 }
6871
Roshan Pius3a1667e2018-07-03 15:17:14 -07006872 if (go && wpa_s->p2p_go_do_acs) {
6873 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6874 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6875 wpa_s->p2p_go_do_acs = 0;
6876 }
6877
Hai Shalomc1a21442022-02-04 13:43:00 -08006878 if (go && wpa_s->p2p_go_allow_dfs) {
6879 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6880 wpa_s->p2p_go_allow_dfs = 0;
6881 }
6882
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006883 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6884 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006885 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886 return group_wpa_s;
6887}
6888
6889
6890/**
6891 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6892 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6893 * @persistent_group: Whether to create a persistent group
6894 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006895 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006896 * @ht40: Start GO with 40 MHz channel width
6897 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006898 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006899 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006900 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006901 * Returns: 0 on success, -1 on failure
6902 *
6903 * This function creates a new P2P group with the local end as the Group Owner,
6904 * i.e., without using Group Owner Negotiation.
6905 */
6906int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006907 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006908 int max_oper_chwidth, int he, int edmg,
6909 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006910{
6911 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006912
6913 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6914 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006915 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6916 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006918 os_free(wpa_s->global->add_psk);
6919 wpa_s->global->add_psk = NULL;
6920
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006921 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006922 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006923 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006924
Roshan Pius3a1667e2018-07-03 15:17:14 -07006925 if (!wpa_s->p2p_go_do_acs) {
6926 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6927 if (freq < 0)
6928 return -1;
6929 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006930
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006931 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006932 ht40, vht, max_oper_chwidth, he, edmg,
6933 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006934 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936 p2p_go_params(wpa_s->global->p2p, &params);
6937 params.persistent_group = persistent_group;
6938
6939 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6940 if (wpa_s == NULL)
6941 return -1;
6942 wpas_start_wps_go(wpa_s, &params, 0);
6943
6944 return 0;
6945}
6946
6947
6948static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006949 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006950 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006951{
6952 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07006953 int other_iface_found = 0;
6954 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955
6956 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6957 if (wpa_s == NULL)
6958 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006959 if (force_scan)
6960 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006961 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962
6963 wpa_supplicant_ap_deinit(wpa_s);
6964
6965 ssid = wpa_config_add_network(wpa_s->conf);
6966 if (ssid == NULL)
6967 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006968 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006969 wpa_config_set_network_defaults(ssid);
6970 ssid->temporary = 1;
6971 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006972 ssid->pbss = params->pbss;
6973 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6974 WPA_CIPHER_CCMP;
6975 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6977 ssid->ssid = os_malloc(params->ssid_len);
6978 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006979 wpa_config_remove_network(wpa_s->conf, ssid->id);
6980 return -1;
6981 }
6982 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6983 ssid->ssid_len = params->ssid_len;
6984 ssid->p2p_group = 1;
6985 ssid->export_keys = 1;
6986 if (params->psk_set) {
6987 os_memcpy(ssid->psk, params->psk, 32);
6988 ssid->psk_set = 1;
6989 }
6990 if (params->passphrase)
6991 ssid->passphrase = os_strdup(params->passphrase);
6992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006993 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006994 wpa_s->p2p_in_invitation = 1;
6995 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006996 wpa_s->p2p_go_group_formation_completed = 0;
6997 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006998
Matthew Wang9ae940b2022-09-14 21:25:34 -07006999 /*
7000 * Get latest scan results from driver in case cached scan results from
7001 * interfaces on the same wiphy allow us to skip the next scan by fast
7002 * associating. Also update the scan time to the most recent scan result
7003 * fetch time on the same radio so it reflects the actual time the last
7004 * scan result event occurred.
7005 */
7006 wpa_supplicant_update_scan_results(wpa_s);
7007 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7008 radio_list) {
7009 if (ifs == wpa_s)
7010 continue;
7011 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7012 &ifs->last_scan)) {
7013 other_iface_found = 1;
7014 wpa_s->last_scan.sec = ifs->last_scan.sec;
7015 wpa_s->last_scan.usec = ifs->last_scan.usec;
7016 }
7017 }
7018
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007019 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007020 NULL);
7021 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7022 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007023 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007024 wpa_supplicant_select_network(wpa_s, ssid);
7025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026 return 0;
7027}
7028
7029
7030int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7031 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007032 int force_freq, int neg_freq,
7033 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007034 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007035 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007036 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007037 int connection_timeout, int force_scan,
7038 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007039{
7040 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007041 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042
7043 if (ssid->disabled != 2 || ssid->ssid == NULL)
7044 return -1;
7045
7046 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7047 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7048 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7049 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007050 if (go == 0 &&
7051 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007052 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007053 /*
7054 * This can happen if Invitation Response frame was lost
7055 * and the peer (GO of a persistent group) tries to
7056 * invite us again. Reschedule the timeout to avoid
7057 * terminating the wait for the connection too early
7058 * since we now know that the peer is still trying to
7059 * invite us instead of having already started the GO.
7060 */
7061 wpa_printf(MSG_DEBUG,
7062 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7063 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7064 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007065 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007066 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067 return 0;
7068 }
7069
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007070 os_free(wpa_s->global->add_psk);
7071 wpa_s->global->add_psk = NULL;
7072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007073 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007074 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007075
Dmitry Shmidt04949592012-07-19 12:16:46 -07007076 wpa_s->p2p_fallback_to_go_neg = 0;
7077
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007078 if (ssid->mode == WPAS_MODE_P2P_GO) {
7079 if (force_freq > 0) {
7080 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7081 if (freq < 0)
7082 return -1;
7083 } else {
7084 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7085 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007086 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007087 freq = 0;
7088 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007089 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007090 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007091 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007092 struct os_reltime now;
7093 struct wpa_bss *bss =
7094 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007095
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007096 os_get_reltime(&now);
7097 if (bss &&
7098 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007099 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007100 freq = bss->freq;
7101 else
7102 freq = 0;
7103 }
7104
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007105 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
7106 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007107 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007108 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007109 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007110
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007111 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007112 ht40, vht, max_oper_chwidth, he, edmg,
7113 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007114 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115
7116 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007117 params.psk_set = ssid->psk_set;
7118 if (params.psk_set)
7119 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007120 if (ssid->passphrase) {
7121 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7122 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7123 "persistent group");
7124 return -1;
7125 }
7126 os_strlcpy(params.passphrase, ssid->passphrase,
7127 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007129 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7130 params.ssid_len = ssid->ssid_len;
7131 params.persistent_group = 1;
7132
7133 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7134 if (wpa_s == NULL)
7135 return -1;
7136
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007137 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7138
Dmitry Shmidt56052862013-10-04 10:23:25 -07007139 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140 wpas_start_wps_go(wpa_s, &params, 0);
7141
7142 return 0;
7143}
7144
7145
7146static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7147 struct wpabuf *proberesp_ies)
7148{
7149 struct wpa_supplicant *wpa_s = ctx;
7150 if (wpa_s->ap_iface) {
7151 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007152 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7153 wpabuf_free(beacon_ies);
7154 wpabuf_free(proberesp_ies);
7155 return;
7156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007157 if (beacon_ies) {
7158 wpabuf_free(hapd->p2p_beacon_ie);
7159 hapd->p2p_beacon_ie = beacon_ies;
7160 }
7161 wpabuf_free(hapd->p2p_probe_resp_ie);
7162 hapd->p2p_probe_resp_ie = proberesp_ies;
7163 } else {
7164 wpabuf_free(beacon_ies);
7165 wpabuf_free(proberesp_ies);
7166 }
7167 wpa_supplicant_ap_update_beacon(wpa_s);
7168}
7169
7170
7171static void wpas_p2p_idle_update(void *ctx, int idle)
7172{
7173 struct wpa_supplicant *wpa_s = ctx;
7174 if (!wpa_s->ap_iface)
7175 return;
7176 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007177 if (idle) {
7178 if (wpa_s->global->p2p_fail_on_wps_complete &&
7179 wpa_s->p2p_in_provisioning) {
7180 wpas_p2p_grpform_fail_after_wps(wpa_s);
7181 return;
7182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007183 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007184 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007185 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7186}
7187
7188
7189struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007190 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007191{
7192 struct p2p_group *group;
7193 struct p2p_group_config *cfg;
7194
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007195 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7196 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007197 return NULL;
7198
7199 cfg = os_zalloc(sizeof(*cfg));
7200 if (cfg == NULL)
7201 return NULL;
7202
Dmitry Shmidt04949592012-07-19 12:16:46 -07007203 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007204 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007205 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007206 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7208 if (wpa_s->max_stations &&
7209 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7210 cfg->max_clients = wpa_s->max_stations;
7211 else
7212 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007213 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7214 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007215 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007216 cfg->cb_ctx = wpa_s;
7217 cfg->ie_update = wpas_p2p_ie_update;
7218 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007219 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7220 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007221
7222 group = p2p_group_init(wpa_s->global->p2p, cfg);
7223 if (group == NULL)
7224 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007225 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226 p2p_group_notif_formation_done(group);
7227 wpa_s->p2p_group = group;
7228 return group;
7229}
7230
7231
7232void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7233 int registrar)
7234{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007235 struct wpa_ssid *ssid = wpa_s->current_ssid;
7236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007237 if (!wpa_s->p2p_in_provisioning) {
7238 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7239 "provisioning not in progress");
7240 return;
7241 }
7242
Dmitry Shmidt04949592012-07-19 12:16:46 -07007243 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7244 u8 go_dev_addr[ETH_ALEN];
7245 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7246 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7247 ssid->ssid_len);
7248 /* Clear any stored provisioning info */
7249 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7250 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007251
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007252 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007254 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007255 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7256 /*
7257 * Use a separate timeout for initial data connection to
7258 * complete to allow the group to be removed automatically if
7259 * something goes wrong in this step before the P2P group idle
7260 * timeout mechanism is taken into use.
7261 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007262 wpa_dbg(wpa_s, MSG_DEBUG,
7263 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7264 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007265 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7266 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007267 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007268 /* Complete group formation on successful data connection. */
7269 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007270 } else if (ssid) {
7271 /*
7272 * Use a separate timeout for initial data connection to
7273 * complete to allow the group to be removed automatically if
7274 * the client does not complete data connection successfully.
7275 */
7276 wpa_dbg(wpa_s, MSG_DEBUG,
7277 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7278 P2P_MAX_INITIAL_CONN_WAIT_GO);
7279 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7280 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007281 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007282 /*
7283 * Complete group formation on first successful data connection
7284 */
7285 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007287 if (wpa_s->global->p2p)
7288 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007289 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007290}
7291
7292
Jouni Malinen75ecf522011-06-27 15:19:46 -07007293void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7294 struct wps_event_fail *fail)
7295{
7296 if (!wpa_s->p2p_in_provisioning) {
7297 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7298 "provisioning not in progress");
7299 return;
7300 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007301
7302 if (wpa_s->go_params) {
7303 p2p_clear_provisioning_info(
7304 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007305 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007306 }
7307
Jouni Malinen75ecf522011-06-27 15:19:46 -07007308 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007309
7310 if (wpa_s == wpa_s->global->p2p_group_formation) {
7311 /*
7312 * Allow some time for the failed WPS negotiation exchange to
7313 * complete, but remove the group since group formation cannot
7314 * succeed after provisioning failure.
7315 */
7316 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7317 wpa_s->global->p2p_fail_on_wps_complete = 1;
7318 eloop_deplete_timeout(0, 50000,
7319 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007320 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007321 }
7322}
7323
7324
7325int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7326{
7327 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7328 !wpa_s->p2p_in_provisioning)
7329 return 0;
7330
7331 wpas_p2p_grpform_fail_after_wps(wpa_s);
7332
7333 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007334}
7335
7336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007337int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007338 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007339 enum wpas_p2p_prov_disc_use use,
7340 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007341{
7342 u16 config_methods;
7343
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007344 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007345 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007346 wpa_s->p2p_fallback_to_go_neg = 0;
7347 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007348 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7349 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007350 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7351 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7352
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007353 wpa_printf(MSG_DEBUG,
7354 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7355 __func__, p2ps_prov->conncap,
7356 p2ps_prov->adv_id, p2ps_prov->conncap,
7357 p2ps_prov->status, p2ps_prov->info);
7358
7359 config_methods = 0;
7360 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007361 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007362 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007364 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7365 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007366 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007367 else {
7368 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007369 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007370 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007372
Dmitry Shmidt04949592012-07-19 12:16:46 -07007373 if (use == WPAS_P2P_PD_AUTO) {
7374 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7375 wpa_s->pending_pd_config_methods = config_methods;
7376 wpa_s->p2p_auto_pd = 1;
7377 wpa_s->p2p_auto_join = 0;
7378 wpa_s->pending_pd_before_join = 0;
7379 wpa_s->auto_pd_scan_retry = 0;
7380 wpas_p2p_stop_find(wpa_s);
7381 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007382 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007383 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7384 wpa_s->p2p_auto_started.sec,
7385 wpa_s->p2p_auto_started.usec);
7386 wpas_p2p_join_scan(wpa_s, NULL);
7387 return 0;
7388 }
7389
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007390 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7391 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007392 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007393 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007395 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007396 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007397 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007398}
7399
7400
7401int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7402 char *end)
7403{
7404 return p2p_scan_result_text(ies, ies_len, buf, end);
7405}
7406
7407
7408static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7409{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007410 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007411 return;
7412
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007413 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007414 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007415 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7416 wpa_s, NULL);
7417 offchannel_send_action_done(wpa_s);
7418 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7421 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007422 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007423}
7424
7425
7426int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7427 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007428 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007429 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007430 u8 seek_cnt, const char **seek_string, int freq,
7431 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007432{
7433 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007434 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007435
Dmitry Shmidt04949592012-07-19 12:16:46 -07007436 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007437 wpa_s->p2p_in_provisioning) {
7438 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7439 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7440 " (P2P disabled)" : "",
7441 wpa_s->p2p_in_provisioning ?
7442 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007443 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007445
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007446 wpa_supplicant_cancel_sched_scan(wpa_s);
7447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007448 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007449 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007450 search_delay, seek_cnt, seek_string, freq,
7451 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007452}
7453
7454
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007455static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7456 struct wpa_scan_results *scan_res)
7457{
7458 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7459
7460 if (wpa_s->p2p_scan_work) {
7461 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7462 wpa_s->p2p_scan_work = NULL;
7463 radio_work_done(work);
7464 }
7465
7466 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7467 return;
7468
7469 /*
7470 * Indicate that results have been processed so that the P2P module can
7471 * continue pending tasks.
7472 */
Hai Shalom60840252021-02-19 19:02:11 -08007473 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007474}
7475
7476
7477static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007478{
7479 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007480 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007481 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7482 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007483
7484 if (wpa_s->global->p2p)
7485 p2p_stop_find(wpa_s->global->p2p);
7486
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007487 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7488 wpa_printf(MSG_DEBUG,
7489 "P2P: Do not consider the scan results after stop_find");
7490 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7491 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007492}
7493
7494
7495void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7496{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007497 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007498 if (!wpa_s->global->pending_group_iface_for_p2ps)
7499 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007500}
7501
7502
7503static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7504{
7505 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007506 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507}
7508
7509
7510int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7511{
7512 int res;
7513
7514 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7515 return -1;
7516
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007517 if (wpa_s->p2p_lo_started) {
7518 wpa_printf(MSG_DEBUG,
7519 "P2P: Cannot start P2P listen, it is offloaded");
7520 return -1;
7521 }
7522
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007523 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007524 wpas_p2p_clear_pending_action_tx(wpa_s);
7525
7526 if (timeout == 0) {
7527 /*
7528 * This is a request for unlimited Listen state. However, at
7529 * least for now, this is mapped to a Listen state for one
7530 * hour.
7531 */
7532 timeout = 3600;
7533 }
7534 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007535 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007537 /*
7538 * Stop previous find/listen operation to avoid trying to request a new
7539 * remain-on-channel operation while the driver is still running the
7540 * previous one.
7541 */
7542 if (wpa_s->global->p2p)
7543 p2p_stop_find(wpa_s->global->p2p);
7544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007545 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7546 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007547 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007548 eloop_register_timeout(timeout, 0,
7549 wpas_p2p_long_listen_timeout,
7550 wpa_s, NULL);
7551 }
7552
7553 return res;
7554}
7555
7556
7557int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7558 u8 *buf, size_t len, int p2p_group)
7559{
7560 struct wpabuf *p2p_ie;
7561 int ret;
7562
7563 if (wpa_s->global->p2p_disabled)
7564 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007565 /*
7566 * Advertize mandatory cross connection capability even on
7567 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7568 */
7569 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007570 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007571 if (wpa_s->global->p2p == NULL)
7572 return -1;
7573 if (bss == NULL)
7574 return -1;
7575
7576 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7577 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7578 p2p_group, p2p_ie);
7579 wpabuf_free(p2p_ie);
7580
7581 return ret;
7582}
7583
7584
7585int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007586 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007587 const u8 *ie, size_t ie_len,
7588 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007589{
7590 if (wpa_s->global->p2p_disabled)
7591 return 0;
7592 if (wpa_s->global->p2p == NULL)
7593 return 0;
7594
Dmitry Shmidt04949592012-07-19 12:16:46 -07007595 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007596 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007597 case P2P_PREQ_NOT_P2P:
7598 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7599 ssi_signal);
7600 /* fall through */
7601 case P2P_PREQ_MALFORMED:
7602 case P2P_PREQ_NOT_LISTEN:
7603 case P2P_PREQ_NOT_PROCESSED:
7604 default: /* make gcc happy */
7605 return 0;
7606 case P2P_PREQ_PROCESSED:
7607 return 1;
7608 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007609}
7610
7611
7612void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7613 const u8 *sa, const u8 *bssid,
7614 u8 category, const u8 *data, size_t len, int freq)
7615{
7616 if (wpa_s->global->p2p_disabled)
7617 return;
7618 if (wpa_s->global->p2p == NULL)
7619 return;
7620
7621 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7622 freq);
7623}
7624
7625
7626void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7627{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007628 unsigned int bands;
7629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007630 if (wpa_s->global->p2p_disabled)
7631 return;
7632 if (wpa_s->global->p2p == NULL)
7633 return;
7634
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007635 bands = wpas_get_bands(wpa_s, NULL);
7636 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637}
7638
7639
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007640static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007641{
7642 p2p_group_deinit(wpa_s->p2p_group);
7643 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007644
7645 wpa_s->ap_configured_cb = NULL;
7646 wpa_s->ap_configured_cb_ctx = NULL;
7647 wpa_s->ap_configured_cb_data = NULL;
7648 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007649}
7650
7651
7652int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7653{
Hai Shalomfdcde762020-04-02 11:19:20 -07007654 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007656 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7657 return -1;
7658
7659 return p2p_reject(wpa_s->global->p2p, addr);
7660}
7661
7662
7663/* Invite to reinvoke a persistent group */
7664int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007665 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007666 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007667 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007668{
7669 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007670 u8 *bssid = NULL;
7671 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007672 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007673 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007674 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7675 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007676
Hai Shalomc1a21442022-02-04 13:43:00 -08007677 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7678 return -1;
7679
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007680 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007681 if (peer_addr)
7682 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7683 else
7684 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007685
Jouni Malinen31be0a42012-08-31 21:20:51 +03007686 wpa_s->p2p_persistent_go_freq = freq;
7687 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007688 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007689 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007690 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7691 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007692 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007693 if (ssid->mode == WPAS_MODE_P2P_GO) {
7694 role = P2P_INVITE_ROLE_GO;
7695 if (peer_addr == NULL) {
7696 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7697 "address in invitation command");
7698 return -1;
7699 }
7700 if (wpas_p2p_create_iface(wpa_s)) {
7701 if (wpas_p2p_add_group_interface(wpa_s,
7702 WPA_IF_P2P_GO) < 0) {
7703 wpa_printf(MSG_ERROR, "P2P: Failed to "
7704 "allocate a new interface for the "
7705 "group");
7706 return -1;
7707 }
7708 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007709 } else if (wpa_s->p2p_mgmt)
7710 bssid = wpa_s->parent->own_addr;
7711 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007712 bssid = wpa_s->own_addr;
7713 } else {
7714 role = P2P_INVITE_ROLE_CLIENT;
7715 peer_addr = ssid->bssid;
7716 }
7717 wpa_s->pending_invite_ssid_id = ssid->id;
7718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007719 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007720 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007721 role == P2P_INVITE_ROLE_GO,
7722 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007723 if (res)
7724 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007726 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7727 return -1;
7728
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007729 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7730
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007731 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7732 no_pref_freq_given && pref_freq > 0 &&
7733 wpa_s->num_multichan_concurrent > 1 &&
7734 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7735 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7736 pref_freq);
7737 pref_freq = 0;
7738 }
7739
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007740 /*
7741 * Stop any find/listen operations before invitation and possibly
7742 * connection establishment.
7743 */
7744 wpas_p2p_stop_find_oper(wpa_s);
7745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007746 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007747 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007748 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007749}
7750
7751
7752/* Invite to join an active group */
7753int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007754 const u8 *peer_addr, const u8 *go_dev_addr,
7755 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007756{
7757 struct wpa_global *global = wpa_s->global;
7758 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007759 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007760 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007761 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007762 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007763 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007764 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7765 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007766
Jouni Malinen31be0a42012-08-31 21:20:51 +03007767 wpa_s->p2p_persistent_go_freq = 0;
7768 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007769 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007770 wpa_s->p2p_go_vht_center_freq2 = 0;
7771 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007772 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007774 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7775 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7776 break;
7777 }
7778 if (wpa_s == NULL) {
7779 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7780 return -1;
7781 }
7782
7783 ssid = wpa_s->current_ssid;
7784 if (ssid == NULL) {
7785 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7786 "invitation");
7787 return -1;
7788 }
7789
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007790 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007791 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007792 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007793 ssid->ssid, ssid->ssid_len);
7794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007795 if (ssid->mode == WPAS_MODE_P2P_GO) {
7796 role = P2P_INVITE_ROLE_ACTIVE_GO;
7797 bssid = wpa_s->own_addr;
7798 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007799 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007800 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007801 } else {
7802 role = P2P_INVITE_ROLE_CLIENT;
7803 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7804 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7805 "invite to current group");
7806 return -1;
7807 }
7808 bssid = wpa_s->bssid;
7809 if (go_dev_addr == NULL &&
7810 !is_zero_ether_addr(wpa_s->go_dev_addr))
7811 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007812 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7813 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007814 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007815 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007817 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7818 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007819 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7820 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007822 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007823 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007824 role == P2P_INVITE_ROLE_ACTIVE_GO,
7825 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007826 if (res)
7827 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007828 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007830 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007831 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007832 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007833}
7834
7835
7836void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7837{
7838 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007839 u8 go_dev_addr[ETH_ALEN];
7840 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007841 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007842 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007843 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007844
Dmitry Shmidt04949592012-07-19 12:16:46 -07007845 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7846 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007847 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007848 }
7849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007850 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007851 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852
7853 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007854 if (!wpa_s->p2p_go_group_formation_completed &&
7855 wpa_s->global->p2p_group_formation == wpa_s) {
7856 wpa_dbg(wpa_s, MSG_DEBUG,
7857 "P2P: Marking group formation completed on client on data connection");
7858 wpa_s->p2p_go_group_formation_completed = 1;
7859 wpa_s->global->p2p_group_formation = NULL;
7860 wpa_s->p2p_in_provisioning = 0;
7861 wpa_s->p2p_in_invitation = 0;
7862 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007864 os_memset(go_dev_addr, 0, ETH_ALEN);
7865 if (ssid->bssid_set)
7866 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7867 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7868 ssid->ssid_len);
7869 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7870
7871 if (wpa_s->global->p2p_group_formation == wpa_s)
7872 wpa_s->global->p2p_group_formation = NULL;
7873
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007874 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7875 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007876
7877 ip_addr[0] = '\0';
7878 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007879 int res;
7880
7881 res = os_snprintf(ip_addr, sizeof(ip_addr),
7882 " ip_addr=%u.%u.%u.%u "
7883 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7884 ip[0], ip[1], ip[2], ip[3],
7885 ip[4], ip[5], ip[6], ip[7],
7886 ip[8], ip[9], ip[10], ip[11]);
7887 if (os_snprintf_error(sizeof(ip_addr), res))
7888 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007889 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007890 }
7891
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007892 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7893 ssid->passphrase == NULL && ssid->psk_set ?
7894 ssid->psk : NULL,
7895 ssid->passphrase, go_dev_addr, persistent,
7896 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007897
7898 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007899 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7900 ssid, go_dev_addr);
7901
Hai Shalom5f92bc92019-04-18 11:54:11 -07007902 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007903}
7904
7905
7906int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7907 u32 interval1, u32 duration2, u32 interval2)
7908{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007909 int ret;
7910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7912 return -1;
7913
7914 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7915 wpa_s->current_ssid == NULL ||
7916 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7917 return -1;
7918
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007919 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7920 wpa_s->own_addr, wpa_s->assoc_freq,
7921 duration1, interval1, duration2, interval2);
7922 if (ret == 0)
7923 wpa_s->waiting_presence_resp = 1;
7924
7925 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007926}
7927
7928
7929int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7930 unsigned int interval)
7931{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007932 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7933 return -1;
7934
7935 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7936}
7937
7938
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007939static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7940{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007941 if (wpa_s->current_ssid == NULL) {
7942 /*
7943 * current_ssid can be cleared when P2P client interface gets
7944 * disconnected, so assume this interface was used as P2P
7945 * client.
7946 */
7947 return 1;
7948 }
7949 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007950 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7951}
7952
7953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007954static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7955{
7956 struct wpa_supplicant *wpa_s = eloop_ctx;
7957
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007958 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007959 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7960 "disabled");
7961 return;
7962 }
7963
Dmitry Shmidt04949592012-07-19 12:16:46 -07007964 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7965 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007966 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007967}
7968
7969
7970static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7971{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007972 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007973
Dmitry Shmidt04949592012-07-19 12:16:46 -07007974 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7975 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007977 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7978 return;
7979
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007980 timeout = wpa_s->conf->p2p_group_idle;
7981 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7982 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7983 timeout = P2P_MAX_CLIENT_IDLE;
7984
7985 if (timeout == 0)
7986 return;
7987
Dmitry Shmidt04949592012-07-19 12:16:46 -07007988 if (timeout < 0) {
7989 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7990 timeout = 0; /* special client mode no-timeout */
7991 else
7992 return;
7993 }
7994
7995 if (wpa_s->p2p_in_provisioning) {
7996 /*
7997 * Use the normal group formation timeout during the
7998 * provisioning phase to avoid terminating this process too
7999 * early due to group idle timeout.
8000 */
8001 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8002 "during provisioning");
8003 return;
8004 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308005
Dmitry Shmidt04949592012-07-19 12:16:46 -07008006 if (wpa_s->show_group_started) {
8007 /*
8008 * Use the normal group formation timeout between the end of
8009 * the provisioning phase and completion of 4-way handshake to
8010 * avoid terminating this process too early due to group idle
8011 * timeout.
8012 */
8013 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8014 "while waiting for initial 4-way handshake to "
8015 "complete");
8016 return;
8017 }
8018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008019 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008020 timeout);
8021 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8022 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008023}
8024
8025
Jouni Malinen2b89da82012-08-31 22:04:41 +03008026/* Returns 1 if the interface was removed */
8027int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8028 u16 reason_code, const u8 *ie, size_t ie_len,
8029 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008030{
8031 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008032 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008033
Dmitry Shmidt04949592012-07-19 12:16:46 -07008034 if (!locally_generated)
8035 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8036 ie_len);
8037
8038 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8039 wpa_s->current_ssid &&
8040 wpa_s->current_ssid->p2p_group &&
8041 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8042 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8043 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008044 if (wpas_p2p_group_delete(wpa_s,
8045 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8046 > 0)
8047 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008048 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008049
8050 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008051}
8052
8053
8054void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008055 u16 reason_code, const u8 *ie, size_t ie_len,
8056 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008057{
8058 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8059 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008060
Dmitry Shmidt04949592012-07-19 12:16:46 -07008061 if (!locally_generated)
8062 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8063 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008064}
8065
8066
8067void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8068{
8069 struct p2p_data *p2p = wpa_s->global->p2p;
8070
8071 if (p2p == NULL)
8072 return;
8073
8074 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8075 return;
8076
8077 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8078 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8079
8080 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8081 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8082
8083 if (wpa_s->wps &&
8084 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8085 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8086
8087 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8088 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8089
8090 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8091 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8092 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8093 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8094 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8095 }
8096
8097 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8098 p2p_set_sec_dev_types(p2p,
8099 (void *) wpa_s->conf->sec_device_type,
8100 wpa_s->conf->num_sec_device_types);
8101
8102 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8103 int i;
8104 p2p_remove_wps_vendor_extensions(p2p);
8105 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8106 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8107 continue;
8108 p2p_add_wps_vendor_extension(
8109 p2p, wpa_s->conf->wps_vendor_ext[i]);
8110 }
8111 }
8112
8113 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8114 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8115 char country[3];
8116 country[0] = wpa_s->conf->country[0];
8117 country[1] = wpa_s->conf->country[1];
8118 country[2] = 0x04;
8119 p2p_set_country(p2p, country);
8120 }
8121
8122 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8123 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8124 wpa_s->conf->p2p_ssid_postfix ?
8125 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8126 0);
8127 }
8128
8129 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8130 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008131
8132 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8133 u8 reg_class, channel;
8134 int ret;
8135 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008136 u8 channel_forced;
8137
Jouni Malinen75ecf522011-06-27 15:19:46 -07008138 if (wpa_s->conf->p2p_listen_reg_class &&
8139 wpa_s->conf->p2p_listen_channel) {
8140 reg_class = wpa_s->conf->p2p_listen_reg_class;
8141 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008142 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008143 } else {
8144 reg_class = 81;
8145 /*
8146 * Pick one of the social channels randomly as the
8147 * listen channel.
8148 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008149 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8150 channel = 1;
8151 else
8152 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008153 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008154 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008155 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8156 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008157 if (ret)
8158 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8159 "failed: %d", ret);
8160 }
8161 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8162 u8 op_reg_class, op_channel, cfg_op_channel;
8163 int ret = 0;
8164 unsigned int r;
8165 if (wpa_s->conf->p2p_oper_reg_class &&
8166 wpa_s->conf->p2p_oper_channel) {
8167 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8168 op_channel = wpa_s->conf->p2p_oper_channel;
8169 cfg_op_channel = 1;
8170 } else {
8171 op_reg_class = 81;
8172 /*
8173 * Use random operation channel from (1, 6, 11)
8174 *if no other preference is indicated.
8175 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008176 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8177 op_channel = 1;
8178 else
8179 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008180 cfg_op_channel = 0;
8181 }
8182 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8183 cfg_op_channel);
8184 if (ret)
8185 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8186 "failed: %d", ret);
8187 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008188
8189 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8190 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8191 wpa_s->conf->p2p_pref_chan) < 0) {
8192 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8193 "update failed");
8194 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008195
8196 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8197 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8198 "update failed");
8199 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008200 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008201
8202 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8203 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008204}
8205
8206
8207int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8208 int duration)
8209{
8210 if (!wpa_s->ap_iface)
8211 return -1;
8212 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8213 duration);
8214}
8215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008216
8217int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8218{
8219 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8220 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008221
8222 wpa_s->global->cross_connection = enabled;
8223 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8224
8225 if (!enabled) {
8226 struct wpa_supplicant *iface;
8227
8228 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8229 {
8230 if (iface->cross_connect_enabled == 0)
8231 continue;
8232
8233 iface->cross_connect_enabled = 0;
8234 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008235 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008236 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8237 iface->ifname,
8238 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008239 }
8240 }
8241
8242 return 0;
8243}
8244
8245
8246static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8247{
8248 struct wpa_supplicant *iface;
8249
8250 if (!uplink->global->cross_connection)
8251 return;
8252
8253 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8254 if (!iface->cross_connect_enabled)
8255 continue;
8256 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8257 0)
8258 continue;
8259 if (iface->ap_iface == NULL)
8260 continue;
8261 if (iface->cross_connect_in_use)
8262 continue;
8263
8264 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008265 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008266 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8267 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008268 }
8269}
8270
8271
8272static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8273{
8274 struct wpa_supplicant *iface;
8275
8276 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8277 if (!iface->cross_connect_enabled)
8278 continue;
8279 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8280 0)
8281 continue;
8282 if (!iface->cross_connect_in_use)
8283 continue;
8284
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008285 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008286 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8287 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008288 iface->cross_connect_in_use = 0;
8289 }
8290}
8291
8292
8293void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8294{
8295 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8296 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8297 wpa_s->cross_connect_disallowed)
8298 wpas_p2p_disable_cross_connect(wpa_s);
8299 else
8300 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008301 if (!wpa_s->ap_iface &&
8302 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8303 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008304}
8305
8306
8307void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8308{
8309 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008310 if (!wpa_s->ap_iface &&
8311 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8312 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008313 wpas_p2p_set_group_idle_timeout(wpa_s);
8314}
8315
8316
8317static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8318{
8319 struct wpa_supplicant *iface;
8320
8321 if (!wpa_s->global->cross_connection)
8322 return;
8323
8324 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8325 if (iface == wpa_s)
8326 continue;
8327 if (iface->drv_flags &
8328 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8329 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008330 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8331 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008332 continue;
8333
8334 wpa_s->cross_connect_enabled = 1;
8335 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8336 sizeof(wpa_s->cross_connect_uplink));
8337 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8338 "%s to %s whenever uplink is available",
8339 wpa_s->ifname, wpa_s->cross_connect_uplink);
8340
8341 if (iface->ap_iface || iface->current_ssid == NULL ||
8342 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8343 iface->cross_connect_disallowed ||
8344 iface->wpa_state != WPA_COMPLETED)
8345 break;
8346
8347 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008348 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008349 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8350 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008351 break;
8352 }
8353}
8354
8355
8356int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8357{
8358 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8359 !wpa_s->p2p_in_provisioning)
8360 return 0; /* not P2P client operation */
8361
8362 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8363 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008364 if (wpa_s != wpa_s->p2pdev)
8365 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008366 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008367 return 1;
8368}
8369
8370
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008371void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8372{
8373 struct wpa_supplicant *wpa_s = eloop_ctx;
8374 wpas_p2p_notif_pbc_overlap(wpa_s);
8375}
8376
8377
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008378void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8379 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008380{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008381 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008382 struct wpa_used_freq_data *freqs = NULL;
8383 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008384
8385 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8386 return;
8387
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008388 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8389 if (!freqs)
8390 return;
8391
8392 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008394 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008395 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008396 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8397 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008398 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8399 "channel list");
8400 return;
8401 }
8402
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008403 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008405 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008407 /*
8408 * The used frequencies map changed, so it is possible that a GO is
8409 * using a channel that is no longer valid for P2P use. It is also
8410 * possible that due to policy consideration, it would be preferable to
8411 * move it to a frequency already used by other station interfaces.
8412 */
8413 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8414
8415 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008416}
8417
8418
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008419static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8420 struct wpa_scan_results *scan_res)
8421{
8422 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8423}
8424
8425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008426int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8427{
8428 struct wpa_global *global = wpa_s->global;
8429 int found = 0;
8430 const u8 *peer;
8431
8432 if (global->p2p == NULL)
8433 return -1;
8434
8435 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8436
8437 if (wpa_s->pending_interface_name[0] &&
8438 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8439 found = 1;
8440
8441 peer = p2p_get_go_neg_peer(global->p2p);
8442 if (peer) {
8443 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8444 MACSTR, MAC2STR(peer));
8445 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008446 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008447 }
8448
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008449 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8450 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8451 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8452 found = 1;
8453 }
8454
8455 if (wpa_s->pending_pd_before_join) {
8456 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8457 wpa_s->pending_pd_before_join = 0;
8458 found = 1;
8459 }
8460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008461 wpas_p2p_stop_find(wpa_s);
8462
8463 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8464 if (wpa_s == global->p2p_group_formation &&
8465 (wpa_s->p2p_in_provisioning ||
8466 wpa_s->parent->pending_interface_type ==
8467 WPA_IF_P2P_CLIENT)) {
8468 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8469 "formation found - cancelling",
8470 wpa_s->ifname);
8471 found = 1;
8472 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008473 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008474 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008475 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008476 break;
8477 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008478 wpas_p2p_group_delete(wpa_s,
8479 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008480 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008481 } else if (wpa_s->p2p_in_invitation) {
8482 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8483 wpa_s->ifname);
8484 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008485 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008486 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008487 }
8488 }
8489
8490 if (!found) {
8491 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8492 return -1;
8493 }
8494
8495 return 0;
8496}
8497
8498
8499void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8500{
8501 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8502 return;
8503
8504 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8505 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008506 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008507}
8508
8509
8510void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8511 int freq_24, int freq_5, int freq_overall)
8512{
8513 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008514 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008515 return;
8516 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8517}
8518
8519
8520int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8521{
8522 u8 peer[ETH_ALEN];
8523 struct p2p_data *p2p = wpa_s->global->p2p;
8524
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008525 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008526 return -1;
8527
8528 if (hwaddr_aton(addr, peer))
8529 return -1;
8530
8531 return p2p_unauthorize(p2p, peer);
8532}
8533
8534
8535/**
8536 * wpas_p2p_disconnect - Disconnect from a P2P Group
8537 * @wpa_s: Pointer to wpa_supplicant data
8538 * Returns: 0 on success, -1 on failure
8539 *
8540 * This can be used to disconnect from a group in which the local end is a P2P
8541 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8542 * virtual network interface was created for this group, that interface will be
8543 * removed. Otherwise, only the configured P2P group network will be removed
8544 * from the interface.
8545 */
8546int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8547{
8548
8549 if (wpa_s == NULL)
8550 return -1;
8551
Jouni Malinen2b89da82012-08-31 22:04:41 +03008552 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8553 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008554}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008555
8556
8557int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8558{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008559 int ret;
8560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008561 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8562 return 0;
8563
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008564 ret = p2p_in_progress(wpa_s->global->p2p);
8565 if (ret == 0) {
8566 /*
8567 * Check whether there is an ongoing WPS provisioning step (or
8568 * other parts of group formation) on another interface since
8569 * p2p_in_progress() does not report this to avoid issues for
8570 * scans during such provisioning step.
8571 */
8572 if (wpa_s->global->p2p_group_formation &&
8573 wpa_s->global->p2p_group_formation != wpa_s) {
8574 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8575 "in group formation",
8576 wpa_s->global->p2p_group_formation->ifname);
8577 ret = 1;
8578 }
8579 }
8580
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008581 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008582 struct os_reltime now;
8583 os_get_reltime(&now);
8584 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8585 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008586 /* Wait for the first client has expired */
8587 wpa_s->global->p2p_go_wait_client.sec = 0;
8588 } else {
8589 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8590 ret = 1;
8591 }
8592 }
8593
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008594 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008595}
8596
8597
8598void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8599 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008600{
8601 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8602 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008603 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008604 /**
8605 * Remove the network by scheduling the group formation
8606 * timeout to happen immediately. The teardown code
8607 * needs to be scheduled to run asynch later so that we
8608 * don't delete data from under ourselves unexpectedly.
8609 * Calling wpas_p2p_group_formation_timeout directly
8610 * causes a series of crashes in WPS failure scenarios.
8611 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008612 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8613 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008614 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008615 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008616 }
8617}
8618
8619
8620struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008621 const u8 *addr, const u8 *ssid,
8622 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008623{
8624 struct wpa_ssid *s;
8625 size_t i;
8626
8627 for (s = wpa_s->conf->ssid; s; s = s->next) {
8628 if (s->disabled != 2)
8629 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008630 if (ssid &&
8631 (ssid_len != s->ssid_len ||
8632 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8633 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008634 if (addr == NULL) {
8635 if (s->mode == WPAS_MODE_P2P_GO)
8636 return s;
8637 continue;
8638 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008639 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8640 return s; /* peer is GO in the persistent group */
8641 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8642 continue;
8643 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008644 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008645 addr, ETH_ALEN) == 0)
8646 return s; /* peer is P2P client in persistent
8647 * group */
8648 }
8649 }
8650
8651 return NULL;
8652}
8653
8654
8655void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8656 const u8 *addr)
8657{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008658 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008659 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008660 /*
8661 * This can happen if WPS provisioning step is not terminated
8662 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8663 * peer was able to connect, there is no need to time out group
8664 * formation after this, though. In addition, this is used with
8665 * the initial connection wait on the GO as a separate formation
8666 * timeout and as such, expected to be hit after the initial WPS
8667 * provisioning step.
8668 */
8669 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008670
8671 if (!wpa_s->p2p_go_group_formation_completed &&
8672 !wpa_s->group_formation_reported) {
8673 /*
8674 * GO has not yet notified group formation success since
8675 * the WPS step was not completed cleanly. Do that
8676 * notification now since the P2P Client was able to
8677 * connect and as such, must have received the
8678 * credential from the WPS step.
8679 */
8680 if (wpa_s->global->p2p)
8681 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008682 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008683 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008684 }
8685 if (!wpa_s->p2p_go_group_formation_completed) {
8686 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8687 wpa_s->p2p_go_group_formation_completed = 1;
8688 wpa_s->global->p2p_group_formation = NULL;
8689 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008690 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008691 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008692 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008693 if (addr == NULL)
8694 return;
8695 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8696}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008697
Dmitry Shmidt04949592012-07-19 12:16:46 -07008698
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008699static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8700 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008701{
8702 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008703 int ret = 0;
8704
Dmitry Shmidt04949592012-07-19 12:16:46 -07008705 if (wpa_s->global->p2p_group_formation)
8706 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008707 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008708 offchannel_send_action_done(wpa_s);
8709 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008710 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008711 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8712 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8713 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8714 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008715 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008716 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008717 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008718 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008719 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008720 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008721 wpa_s->p2p_go_he,
8722 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008723 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008724 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008725}
8726
8727
8728int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8729{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008730 int res;
8731
Dmitry Shmidt04949592012-07-19 12:16:46 -07008732 if (!wpa_s->p2p_fallback_to_go_neg ||
8733 wpa_s->p2p_in_provisioning <= 5)
8734 return 0;
8735
8736 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8737 return 0; /* peer operating as a GO */
8738
8739 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8740 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008741 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008742 "reason=GO-not-found");
8743 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008744
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008745 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008746}
8747
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008748
8749unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8750{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008751 struct wpa_supplicant *ifs;
8752
8753 if (wpa_s->wpa_state > WPA_SCANNING) {
8754 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8755 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008756 wpa_s->conf->p2p_search_delay);
8757 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008758 }
8759
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008760 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8761 radio_list) {
8762 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008763 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8764 "delay due to concurrent operation on "
8765 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008766 wpa_s->conf->p2p_search_delay,
8767 ifs->ifname);
8768 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008769 }
8770 }
8771
8772 return 0;
8773}
8774
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008775
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008776static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8777 struct wpa_ssid *s, const u8 *addr,
8778 int iface_addr)
8779{
8780 struct psk_list_entry *psk, *tmp;
8781 int changed = 0;
8782
8783 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8784 list) {
8785 if ((iface_addr && !psk->p2p &&
8786 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8787 (!iface_addr && psk->p2p &&
8788 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8789 wpa_dbg(wpa_s, MSG_DEBUG,
8790 "P2P: Remove persistent group PSK list entry for "
8791 MACSTR " p2p=%u",
8792 MAC2STR(psk->addr), psk->p2p);
8793 dl_list_del(&psk->list);
8794 os_free(psk);
8795 changed++;
8796 }
8797 }
8798
8799 return changed;
8800}
8801
8802
8803void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8804 const u8 *p2p_dev_addr,
8805 const u8 *psk, size_t psk_len)
8806{
8807 struct wpa_ssid *ssid = wpa_s->current_ssid;
8808 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008809 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008810
8811 if (psk_len != sizeof(p->psk))
8812 return;
8813
8814 if (p2p_dev_addr) {
8815 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8816 " p2p_dev_addr=" MACSTR,
8817 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8818 if (is_zero_ether_addr(p2p_dev_addr))
8819 p2p_dev_addr = NULL;
8820 } else {
8821 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8822 MAC2STR(mac_addr));
8823 }
8824
8825 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8826 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8827 /* To be added to persistent group once created */
8828 if (wpa_s->global->add_psk == NULL) {
8829 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8830 if (wpa_s->global->add_psk == NULL)
8831 return;
8832 }
8833 p = wpa_s->global->add_psk;
8834 if (p2p_dev_addr) {
8835 p->p2p = 1;
8836 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8837 } else {
8838 p->p2p = 0;
8839 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8840 }
8841 os_memcpy(p->psk, psk, psk_len);
8842 return;
8843 }
8844
8845 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8846 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8847 return;
8848 }
8849
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008850 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008851 ssid->ssid_len);
8852 if (!persistent) {
8853 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8854 return;
8855 }
8856
8857 p = os_zalloc(sizeof(*p));
8858 if (p == NULL)
8859 return;
8860 if (p2p_dev_addr) {
8861 p->p2p = 1;
8862 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8863 } else {
8864 p->p2p = 0;
8865 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8866 }
8867 os_memcpy(p->psk, psk, psk_len);
8868
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008869 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8870 (last = dl_list_last(&persistent->psk_list,
8871 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008872 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8873 MACSTR " (p2p=%u) to make room for a new one",
8874 MAC2STR(last->addr), last->p2p);
8875 dl_list_del(&last->list);
8876 os_free(last);
8877 }
8878
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008879 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008880 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8881 p2p_dev_addr == NULL);
8882 if (p2p_dev_addr) {
8883 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8884 MACSTR, MAC2STR(p2p_dev_addr));
8885 } else {
8886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8887 MAC2STR(mac_addr));
8888 }
8889 dl_list_add(&persistent->psk_list, &p->list);
8890
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008891 if (wpa_s->p2pdev->conf->update_config &&
8892 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008893 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008894}
8895
8896
8897static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8898 struct wpa_ssid *s, const u8 *addr,
8899 int iface_addr)
8900{
8901 int res;
8902
8903 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008904 if (res > 0 && wpa_s->conf->update_config &&
8905 wpa_config_write(wpa_s->confname, wpa_s->conf))
8906 wpa_dbg(wpa_s, MSG_DEBUG,
8907 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008908}
8909
8910
8911static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8912 const u8 *peer, int iface_addr)
8913{
8914 struct hostapd_data *hapd;
8915 struct hostapd_wpa_psk *psk, *prev, *rem;
8916 struct sta_info *sta;
8917
8918 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8919 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8920 return;
8921
8922 /* Remove per-station PSK entry */
8923 hapd = wpa_s->ap_iface->bss[0];
8924 prev = NULL;
8925 psk = hapd->conf->ssid.wpa_psk;
8926 while (psk) {
8927 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8928 (!iface_addr &&
8929 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8930 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8931 MACSTR " iface_addr=%d",
8932 MAC2STR(peer), iface_addr);
8933 if (prev)
8934 prev->next = psk->next;
8935 else
8936 hapd->conf->ssid.wpa_psk = psk->next;
8937 rem = psk;
8938 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07008939 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008940 } else {
8941 prev = psk;
8942 psk = psk->next;
8943 }
8944 }
8945
8946 /* Disconnect from group */
8947 if (iface_addr)
8948 sta = ap_get_sta(hapd, peer);
8949 else
8950 sta = ap_get_sta_p2p(hapd, peer);
8951 if (sta) {
8952 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8953 " (iface_addr=%d) from group",
8954 MAC2STR(peer), iface_addr);
8955 hostapd_drv_sta_deauth(hapd, sta->addr,
8956 WLAN_REASON_DEAUTH_LEAVING);
8957 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8958 }
8959}
8960
8961
8962void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8963 int iface_addr)
8964{
8965 struct wpa_ssid *s;
8966 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008967 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008968
8969 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8970
8971 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008972 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008973 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8974 continue;
8975 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008976 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8977 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008978 }
8979
8980 /* Remove from any operating group */
8981 for (w = wpa_s->global->ifaces; w; w = w->next)
8982 wpas_p2p_remove_client_go(w, peer, iface_addr);
8983}
8984
8985
8986static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8987{
8988 struct wpa_supplicant *wpa_s = eloop_ctx;
8989 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8990}
8991
8992
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008993static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8994{
8995 struct wpa_supplicant *wpa_s = eloop_ctx;
8996
8997 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8998 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8999}
9000
9001
9002int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9003 struct wpa_ssid *ssid)
9004{
9005 struct wpa_supplicant *iface;
9006
9007 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9008 if (!iface->current_ssid ||
9009 iface->current_ssid->frequency == freq ||
9010 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9011 !iface->current_ssid->p2p_group))
9012 continue;
9013
9014 /* Remove the connection with least priority */
9015 if (!wpas_is_p2p_prioritized(iface)) {
9016 /* STA connection has priority over existing
9017 * P2P connection, so remove the interface. */
9018 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9019 eloop_register_timeout(0, 0,
9020 wpas_p2p_group_freq_conflict,
9021 iface, NULL);
9022 /* If connection in progress is P2P connection, do not
9023 * proceed for the connection. */
9024 if (wpa_s == iface)
9025 return -1;
9026 else
9027 return 0;
9028 } else {
9029 /* P2P connection has priority, disable the STA network
9030 */
9031 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9032 ssid);
9033 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9034 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9035 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9036 ETH_ALEN);
9037 /* If P2P connection is in progress, continue
9038 * connecting...*/
9039 if (wpa_s == iface)
9040 return 0;
9041 else
9042 return -1;
9043 }
9044 }
9045
9046 return 0;
9047}
9048
9049
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009050int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9051{
9052 struct wpa_ssid *ssid = wpa_s->current_ssid;
9053
9054 if (ssid == NULL || !ssid->p2p_group)
9055 return 0;
9056
9057 if (wpa_s->p2p_last_4way_hs_fail &&
9058 wpa_s->p2p_last_4way_hs_fail == ssid) {
9059 u8 go_dev_addr[ETH_ALEN];
9060 struct wpa_ssid *persistent;
9061
9062 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9063 ssid->ssid,
9064 ssid->ssid_len) <= 0) {
9065 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9066 goto disconnect;
9067 }
9068
9069 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9070 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009071 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009072 ssid->ssid,
9073 ssid->ssid_len);
9074 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9075 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9076 goto disconnect;
9077 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009078 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009079 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9080 persistent->id);
9081 disconnect:
9082 wpa_s->p2p_last_4way_hs_fail = NULL;
9083 /*
9084 * Remove the group from a timeout to avoid issues with caller
9085 * continuing to use the interface if this is on a P2P group
9086 * interface.
9087 */
9088 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9089 wpa_s, NULL);
9090 return 1;
9091 }
9092
9093 wpa_s->p2p_last_4way_hs_fail = ssid;
9094 return 0;
9095}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009096
9097
9098#ifdef CONFIG_WPS_NFC
9099
9100static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9101 struct wpabuf *p2p)
9102{
9103 struct wpabuf *ret;
9104 size_t wsc_len;
9105
9106 if (p2p == NULL) {
9107 wpabuf_free(wsc);
9108 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9109 return NULL;
9110 }
9111
9112 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9113 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9114 if (ret == NULL) {
9115 wpabuf_free(wsc);
9116 wpabuf_free(p2p);
9117 return NULL;
9118 }
9119
9120 wpabuf_put_be16(ret, wsc_len);
9121 if (wsc)
9122 wpabuf_put_buf(ret, wsc);
9123 wpabuf_put_be16(ret, wpabuf_len(p2p));
9124 wpabuf_put_buf(ret, p2p);
9125
9126 wpabuf_free(wsc);
9127 wpabuf_free(p2p);
9128 wpa_hexdump_buf(MSG_DEBUG,
9129 "P2P: Generated NFC connection handover message", ret);
9130
9131 if (ndef && ret) {
9132 struct wpabuf *tmp;
9133 tmp = ndef_build_p2p(ret);
9134 wpabuf_free(ret);
9135 if (tmp == NULL) {
9136 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9137 return NULL;
9138 }
9139 ret = tmp;
9140 }
9141
9142 return ret;
9143}
9144
9145
9146static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9147 struct wpa_ssid **ssid, u8 *go_dev_addr)
9148{
9149 struct wpa_supplicant *iface;
9150
9151 if (go_dev_addr)
9152 os_memset(go_dev_addr, 0, ETH_ALEN);
9153 if (ssid)
9154 *ssid = NULL;
9155 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9156 if (iface->wpa_state < WPA_ASSOCIATING ||
9157 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9158 !iface->current_ssid->p2p_group ||
9159 iface->current_ssid->mode != WPAS_MODE_INFRA)
9160 continue;
9161 if (ssid)
9162 *ssid = iface->current_ssid;
9163 if (go_dev_addr)
9164 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9165 return iface->assoc_freq;
9166 }
9167 return 0;
9168}
9169
9170
9171struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9172 int ndef)
9173{
9174 struct wpabuf *wsc, *p2p;
9175 struct wpa_ssid *ssid;
9176 u8 go_dev_addr[ETH_ALEN];
9177 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9178
9179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9180 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9181 return NULL;
9182 }
9183
9184 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9185 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9186 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9187 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9188 return NULL;
9189 }
9190
9191 if (cli_freq == 0) {
9192 wsc = wps_build_nfc_handover_req_p2p(
9193 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9194 } else
9195 wsc = NULL;
9196 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9197 go_dev_addr, ssid ? ssid->ssid : NULL,
9198 ssid ? ssid->ssid_len : 0);
9199
9200 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9201}
9202
9203
9204struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9205 int ndef, int tag)
9206{
9207 struct wpabuf *wsc, *p2p;
9208 struct wpa_ssid *ssid;
9209 u8 go_dev_addr[ETH_ALEN];
9210 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9211
9212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9213 return NULL;
9214
9215 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9216 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9217 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9218 return NULL;
9219
9220 if (cli_freq == 0) {
9221 wsc = wps_build_nfc_handover_sel_p2p(
9222 wpa_s->parent->wps,
9223 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9224 DEV_PW_NFC_CONNECTION_HANDOVER,
9225 wpa_s->conf->wps_nfc_dh_pubkey,
9226 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9227 } else
9228 wsc = NULL;
9229 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9230 go_dev_addr, ssid ? ssid->ssid : NULL,
9231 ssid ? ssid->ssid_len : 0);
9232
9233 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9234}
9235
9236
9237static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9238 struct p2p_nfc_params *params)
9239{
9240 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9241 "connection handover (freq=%d)",
9242 params->go_freq);
9243
9244 if (params->go_freq && params->go_ssid_len) {
9245 wpa_s->p2p_wps_method = WPS_NFC;
9246 wpa_s->pending_join_wps_method = WPS_NFC;
9247 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9248 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9249 ETH_ALEN);
9250 return wpas_p2p_join_start(wpa_s, params->go_freq,
9251 params->go_ssid,
9252 params->go_ssid_len);
9253 }
9254
9255 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9256 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009257 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009258 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009259 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009260 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009261 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009262}
9263
9264
9265static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9266 struct p2p_nfc_params *params, int tag)
9267{
9268 int res, persistent;
9269 struct wpa_ssid *ssid;
9270
9271 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9272 "connection handover");
9273 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9274 ssid = wpa_s->current_ssid;
9275 if (ssid == NULL)
9276 continue;
9277 if (ssid->mode != WPAS_MODE_P2P_GO)
9278 continue;
9279 if (wpa_s->ap_iface == NULL)
9280 continue;
9281 break;
9282 }
9283 if (wpa_s == NULL) {
9284 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9285 return -1;
9286 }
9287
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009288 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009289 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009290 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009291 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9292 return -1;
9293 }
9294 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009295 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9296 wpa_s->p2pdev->p2p_oob_dev_pw,
9297 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9298 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009299 if (res)
9300 return res;
9301
9302 if (!tag) {
9303 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9304 return 0;
9305 }
9306
9307 if (!params->peer ||
9308 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9309 return 0;
9310
9311 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9312 " to join", MAC2STR(params->peer->p2p_device_addr));
9313
9314 wpa_s->global->p2p_invite_group = wpa_s;
9315 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009316 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009317 params->peer->p2p_device_addr,
9318 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009319 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009320
9321 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9322 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9323 ssid->ssid, ssid->ssid_len, ssid->frequency,
9324 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009325 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009326}
9327
9328
9329static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9330 struct p2p_nfc_params *params,
9331 int forced_freq)
9332{
9333 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9334 "connection handover");
9335 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9336 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009337 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009338 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009339 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009340 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009341}
9342
9343
9344static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9345 struct p2p_nfc_params *params,
9346 int forced_freq)
9347{
9348 int res;
9349
9350 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9351 "connection handover");
9352 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9353 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009354 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009355 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009356 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009357 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009358 if (res)
9359 return res;
9360
9361 res = wpas_p2p_listen(wpa_s, 60);
9362 if (res) {
9363 p2p_unauthorize(wpa_s->global->p2p,
9364 params->peer->p2p_device_addr);
9365 }
9366
9367 return res;
9368}
9369
9370
9371static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9372 const struct wpabuf *data,
9373 int sel, int tag, int forced_freq)
9374{
9375 const u8 *pos, *end;
9376 u16 len, id;
9377 struct p2p_nfc_params params;
9378 int res;
9379
9380 os_memset(&params, 0, sizeof(params));
9381 params.sel = sel;
9382
9383 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9384
9385 pos = wpabuf_head(data);
9386 end = pos + wpabuf_len(data);
9387
9388 if (end - pos < 2) {
9389 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9390 "attributes");
9391 return -1;
9392 }
9393 len = WPA_GET_BE16(pos);
9394 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009395 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009396 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9397 "attributes");
9398 return -1;
9399 }
9400 params.wsc_attr = pos;
9401 params.wsc_len = len;
9402 pos += len;
9403
9404 if (end - pos < 2) {
9405 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9406 "attributes");
9407 return -1;
9408 }
9409 len = WPA_GET_BE16(pos);
9410 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009411 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009412 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9413 "attributes");
9414 return -1;
9415 }
9416 params.p2p_attr = pos;
9417 params.p2p_len = len;
9418 pos += len;
9419
9420 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9421 params.wsc_attr, params.wsc_len);
9422 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9423 params.p2p_attr, params.p2p_len);
9424 if (pos < end) {
9425 wpa_hexdump(MSG_DEBUG,
9426 "P2P: Ignored extra data after P2P attributes",
9427 pos, end - pos);
9428 }
9429
9430 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9431 if (res)
9432 return res;
9433
9434 if (params.next_step == NO_ACTION)
9435 return 0;
9436
9437 if (params.next_step == BOTH_GO) {
9438 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9439 MAC2STR(params.peer->p2p_device_addr));
9440 return 0;
9441 }
9442
9443 if (params.next_step == PEER_CLIENT) {
9444 if (!is_zero_ether_addr(params.go_dev_addr)) {
9445 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9446 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9447 " ssid=\"%s\"",
9448 MAC2STR(params.peer->p2p_device_addr),
9449 params.go_freq,
9450 MAC2STR(params.go_dev_addr),
9451 wpa_ssid_txt(params.go_ssid,
9452 params.go_ssid_len));
9453 } else {
9454 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9455 "peer=" MACSTR " freq=%d",
9456 MAC2STR(params.peer->p2p_device_addr),
9457 params.go_freq);
9458 }
9459 return 0;
9460 }
9461
9462 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9463 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9464 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9465 return 0;
9466 }
9467
9468 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9469 wpa_s->p2p_oob_dev_pw = NULL;
9470
9471 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9472 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9473 "received");
9474 return -1;
9475 }
9476
9477 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9478 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9479 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9480 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9481 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9482 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9483 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9484
9485 if (tag) {
9486 if (id < 0x10) {
9487 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9488 "peer OOB Device Password Id %u", id);
9489 return -1;
9490 }
9491 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9492 "Device Password Id %u", id);
9493 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9494 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9495 params.oob_dev_pw_len -
9496 WPS_OOB_PUBKEY_HASH_LEN - 2);
9497 wpa_s->p2p_oob_dev_pw_id = id;
9498 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9499 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9500 params.oob_dev_pw_len -
9501 WPS_OOB_PUBKEY_HASH_LEN - 2);
9502 if (wpa_s->p2p_oob_dev_pw == NULL)
9503 return -1;
9504
9505 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9506 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9507 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9508 return -1;
9509 } else {
9510 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9511 "without Device Password");
9512 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9513 }
9514
9515 switch (params.next_step) {
9516 case NO_ACTION:
9517 case BOTH_GO:
9518 case PEER_CLIENT:
9519 /* already covered above */
9520 return 0;
9521 case JOIN_GROUP:
9522 return wpas_p2p_nfc_join_group(wpa_s, &params);
9523 case AUTH_JOIN:
9524 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9525 case INIT_GO_NEG:
9526 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9527 case RESP_GO_NEG:
9528 /* TODO: use own OOB Dev Pw */
9529 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9530 }
9531
9532 return -1;
9533}
9534
9535
9536int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9537 const struct wpabuf *data, int forced_freq)
9538{
9539 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9540 return -1;
9541
9542 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9543}
9544
9545
9546int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9547 const struct wpabuf *req,
9548 const struct wpabuf *sel, int forced_freq)
9549{
9550 struct wpabuf *tmp;
9551 int ret;
9552
9553 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9554 return -1;
9555
9556 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9557
9558 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9559 wpabuf_head(req), wpabuf_len(req));
9560 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9561 wpabuf_head(sel), wpabuf_len(sel));
9562 if (forced_freq)
9563 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9564 tmp = ndef_parse_p2p(init ? sel : req);
9565 if (tmp == NULL) {
9566 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9567 return -1;
9568 }
9569
9570 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9571 forced_freq);
9572 wpabuf_free(tmp);
9573
9574 return ret;
9575}
9576
9577
9578int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9579{
9580 const u8 *if_addr;
9581 int go_intent = wpa_s->conf->p2p_go_intent;
9582 struct wpa_supplicant *iface;
9583
9584 if (wpa_s->global->p2p == NULL)
9585 return -1;
9586
9587 if (!enabled) {
9588 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9589 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9590 {
9591 if (!iface->ap_iface)
9592 continue;
9593 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9594 }
9595 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9596 0, NULL);
9597 if (wpa_s->p2p_nfc_tag_enabled)
9598 wpas_p2p_remove_pending_group_interface(wpa_s);
9599 wpa_s->p2p_nfc_tag_enabled = 0;
9600 return 0;
9601 }
9602
9603 if (wpa_s->global->p2p_disabled)
9604 return -1;
9605
9606 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9607 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9608 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9609 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9610 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9611 "to allow static handover cases");
9612 return -1;
9613 }
9614
9615 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9616
9617 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9618 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9619 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9620 if (wpa_s->p2p_oob_dev_pw == NULL)
9621 return -1;
9622 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9623
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009624 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9625 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9626 /*
9627 * P2P Group Interface present and the command came on group
9628 * interface, so enable the token for the current interface.
9629 */
9630 wpa_s->create_p2p_iface = 0;
9631 } else {
9632 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9633 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009634
9635 if (wpa_s->create_p2p_iface) {
9636 enum wpa_driver_if_type iftype;
9637 /* Prepare to add a new interface for the group */
9638 iftype = WPA_IF_P2P_GROUP;
9639 if (go_intent == 15)
9640 iftype = WPA_IF_P2P_GO;
9641 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9642 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9643 "interface for the group");
9644 return -1;
9645 }
9646
9647 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009648 } else if (wpa_s->p2p_mgmt)
9649 if_addr = wpa_s->parent->own_addr;
9650 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009651 if_addr = wpa_s->own_addr;
9652
9653 wpa_s->p2p_nfc_tag_enabled = enabled;
9654
9655 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9656 struct hostapd_data *hapd;
9657 if (iface->ap_iface == NULL)
9658 continue;
9659 hapd = iface->ap_iface->bss[0];
9660 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9661 hapd->conf->wps_nfc_dh_pubkey =
9662 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9663 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9664 hapd->conf->wps_nfc_dh_privkey =
9665 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9666 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9667 hapd->conf->wps_nfc_dev_pw =
9668 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9669 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9670
9671 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9672 wpa_dbg(iface, MSG_DEBUG,
9673 "P2P: Failed to enable NFC Tag for GO");
9674 }
9675 }
9676 p2p_set_authorized_oob_dev_pw_id(
9677 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9678 if_addr);
9679
9680 return 0;
9681}
9682
9683#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009684
9685
9686static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9687 struct wpa_used_freq_data *freqs,
9688 unsigned int num)
9689{
9690 u8 curr_chan, cand, chan;
9691 unsigned int i;
9692
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009693 /*
9694 * If possible, optimize the Listen channel to be a channel that is
9695 * already used by one of the other interfaces.
9696 */
9697 if (!wpa_s->conf->p2p_optimize_listen_chan)
9698 return;
9699
9700 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9701 return;
9702
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009703 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9704 for (i = 0, cand = 0; i < num; i++) {
9705 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9706 if (curr_chan == chan) {
9707 cand = 0;
9708 break;
9709 }
9710
9711 if (chan == 1 || chan == 6 || chan == 11)
9712 cand = chan;
9713 }
9714
9715 if (cand) {
9716 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009717 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009718 cand);
9719 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9720 }
9721}
9722
9723
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009724static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009725{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009726 struct hostapd_config *conf;
9727 struct p2p_go_neg_results params;
9728 struct csa_settings csa_settings;
9729 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9730 int old_freq = current_ssid->frequency;
9731 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009733 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9734 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9735 return -1;
9736 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009737
9738 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009739 * TODO: This function may not always work correctly. For example,
9740 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009741 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009742 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9743 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009744 wpa_dbg(wpa_s, MSG_DEBUG,
9745 "P2P CSA: Failed to select new frequency for GO");
9746 return -1;
9747 }
9748
9749 if (current_ssid->frequency == params.freq) {
9750 wpa_dbg(wpa_s, MSG_DEBUG,
9751 "P2P CSA: Selected same frequency - not moving GO");
9752 return 0;
9753 }
9754
9755 conf = hostapd_config_defaults();
9756 if (!conf) {
9757 wpa_dbg(wpa_s, MSG_DEBUG,
9758 "P2P CSA: Failed to allocate default config");
9759 return -1;
9760 }
9761
9762 current_ssid->frequency = params.freq;
9763 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9764 wpa_dbg(wpa_s, MSG_DEBUG,
9765 "P2P CSA: Failed to create new GO config");
9766 ret = -1;
9767 goto out;
9768 }
9769
Sunil4a3f9f52022-07-19 22:04:39 +00009770 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9771 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
9772 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9773 wpa_dbg(wpa_s, MSG_INFO,
9774 "P2P CSA: CSA from Hardware mode %d to %d is not supported",
9775 wpa_s->ap_iface->current_mode->mode, conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009776 ret = -1;
9777 goto out;
9778 }
9779
9780 os_memset(&csa_settings, 0, sizeof(csa_settings));
9781 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9782 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9783 csa_settings.freq_params.freq = params.freq;
9784 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9785 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9786 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9787
9788 if (conf->ieee80211ac) {
9789 int freq1 = 0, freq2 = 0;
9790 u8 chan, opclass;
9791
9792 if (ieee80211_freq_to_channel_ext(params.freq,
9793 conf->secondary_channel,
9794 conf->vht_oper_chwidth,
9795 &opclass, &chan) ==
9796 NUM_HOSTAPD_MODES) {
9797 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9798 ret = -1;
9799 goto out;
9800 }
9801
9802 if (conf->vht_oper_centr_freq_seg0_idx)
9803 freq1 = ieee80211_chan_to_freq(
9804 NULL, opclass,
9805 conf->vht_oper_centr_freq_seg0_idx);
9806
9807 if (conf->vht_oper_centr_freq_seg1_idx)
9808 freq2 = ieee80211_chan_to_freq(
9809 NULL, opclass,
9810 conf->vht_oper_centr_freq_seg1_idx);
9811
9812 if (freq1 < 0 || freq2 < 0) {
9813 wpa_dbg(wpa_s, MSG_DEBUG,
9814 "P2P CSA: Selected invalid VHT center freqs");
9815 ret = -1;
9816 goto out;
9817 }
9818
9819 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9820 csa_settings.freq_params.center_freq1 = freq1;
9821 csa_settings.freq_params.center_freq2 = freq2;
9822
9823 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009824 case CONF_OPER_CHWIDTH_80MHZ:
9825 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009826 csa_settings.freq_params.bandwidth = 80;
9827 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009828 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009829 csa_settings.freq_params.bandwidth = 160;
9830 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009831 default:
9832 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009833 }
9834 }
9835
9836 ret = ap_switch_channel(wpa_s, &csa_settings);
9837out:
9838 current_ssid->frequency = old_freq;
9839 hostapd_config_free(conf);
9840 return ret;
9841}
9842
9843
9844static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9845{
9846 struct p2p_go_neg_results params;
9847 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009848 void (*ap_configured_cb)(void *ctx, void *data);
9849 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009850
9851 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9852
9853 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9854 current_ssid->frequency);
9855
9856 /* Stop the AP functionality */
9857 /* TODO: Should do this in a way that does not indicated to possible
9858 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009859 /* If this action occurs before a group is started, the callback should
9860 * be preserved, or GROUP-STARTED event would be lost. If this action
9861 * occurs after a group is started, these pointers are all NULL and
9862 * harmless. */
9863 ap_configured_cb = wpa_s->ap_configured_cb;
9864 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9865 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009866 wpa_supplicant_ap_deinit(wpa_s);
9867
9868 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009869 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9870 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009871 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9872 wpas_p2p_group_delete(wpa_s,
9873 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9874 return;
9875 }
9876 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9877 params.freq);
9878
9879 if (params.freq &&
9880 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9881 wpa_printf(MSG_DEBUG,
9882 "P2P: Selected freq (%u MHz) is not valid for P2P",
9883 params.freq);
9884 wpas_p2p_group_delete(wpa_s,
9885 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9886 return;
9887 }
9888
Hai Shalom899fcc72020-10-19 14:38:18 -07009889 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009890 wpa_s->ap_configured_cb = ap_configured_cb;
9891 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9892 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009893
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009894 /* Update the frequency */
9895 current_ssid->frequency = params.freq;
9896 wpa_s->connect_without_scan = current_ssid;
9897 wpa_s->reassociate = 1;
9898 wpa_s->disconnected = 0;
9899 wpa_supplicant_req_scan(wpa_s, 0, 0);
9900}
9901
9902
9903static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9904{
9905 struct wpa_supplicant *wpa_s = eloop_ctx;
9906
9907 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009908 return;
9909
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009910 wpas_p2p_go_update_common_freqs(wpa_s);
9911
9912 /* Do not move GO in the middle of a CSA */
9913 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9914 wpa_printf(MSG_DEBUG,
9915 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009916 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009917 }
9918
9919 /*
9920 * First, try a channel switch flow. If it is not supported or fails,
9921 * take down the GO and bring it up again.
9922 */
9923 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9924 wpas_p2p_move_go_no_csa(wpa_s);
9925}
9926
9927
9928static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9929{
9930 struct wpa_supplicant *wpa_s = eloop_ctx;
9931 struct wpa_used_freq_data *freqs = NULL;
9932 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009933
9934 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9935 if (!freqs)
9936 return;
9937
9938 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9939
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009940 /* Previous attempt to move a GO was not possible -- try again. */
9941 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9942 WPAS_P2P_CHANNEL_UPDATE_ANY);
9943
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009944 os_free(freqs);
9945}
9946
9947
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009948/*
9949 * Consider moving a GO from its currently used frequency:
9950 * 1. It is possible that due to regulatory consideration the frequency
9951 * can no longer be used and there is a need to evacuate the GO.
9952 * 2. It is possible that due to MCC considerations, it would be preferable
9953 * to move the GO to a channel that is currently used by some other
9954 * station interface.
9955 *
9956 * In case a frequency that became invalid is once again valid, cancel a
9957 * previously initiated GO frequency change.
9958 */
9959static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9960 struct wpa_used_freq_data *freqs,
9961 unsigned int num)
9962{
9963 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9964 unsigned int timeout;
9965 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009966 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009967
9968 wpas_p2p_go_update_common_freqs(wpa_s);
9969
9970 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009971 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009972 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009973 for (i = 0, invalid_freq = 0; i < num; i++) {
9974 if (freqs[i].freq == freq) {
9975 flags = freqs[i].flags;
9976
9977 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009978 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9979 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009980 wpa_dbg(wpa_s, MSG_DEBUG,
9981 "P2P: Freq=%d MHz no longer valid for GO",
9982 freq);
9983 invalid_freq = 1;
9984 }
9985 } else if (freqs[i].flags == 0) {
9986 /* Freq is not used by any other station interface */
9987 continue;
9988 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009989 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009990 /* Freq is not valid for P2P use cases */
9991 continue;
9992 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9993 P2P_GO_FREQ_MOVE_SCM) {
9994 policy_move = 1;
9995 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9996 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9997 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9998 policy_move = 1;
9999 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10000 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10001 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10002 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10003 policy_move = 1;
10004 } else if ((wpa_s->drv_flags &
10005 WPA_DRIVER_FLAGS_AP_CSA) &&
10006 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10007 u8 chan;
10008
10009 /*
10010 * We do not support CSA between bands, so move
10011 * GO only within the same band.
10012 */
10013 if (wpa_s->ap_iface->current_mode->mode ==
10014 ieee80211_freq_to_chan(freqs[i].freq,
10015 &chan))
10016 policy_move = 1;
10017 }
10018 }
10019 }
10020
10021 wpa_dbg(wpa_s, MSG_DEBUG,
10022 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10023 invalid_freq, policy_move, flags);
10024
10025 /*
10026 * The channel is valid, or we are going to have a policy move, so
10027 * cancel timeout.
10028 */
10029 if (!invalid_freq || policy_move) {
10030 wpa_dbg(wpa_s, MSG_DEBUG,
10031 "P2P: Cancel a GO move from freq=%d MHz", freq);
10032 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10033
10034 if (wpas_p2p_in_progress(wpa_s)) {
10035 wpa_dbg(wpa_s, MSG_DEBUG,
10036 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10037 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10038 wpa_s, NULL);
10039 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10040 wpas_p2p_reconsider_moving_go,
10041 wpa_s, NULL);
10042 return;
10043 }
10044 }
10045
10046 if (!invalid_freq && (!policy_move || flags != 0)) {
10047 wpa_dbg(wpa_s, MSG_DEBUG,
10048 "P2P: Not initiating a GO frequency change");
10049 return;
10050 }
10051
10052 /*
10053 * Do not consider moving GO if it is in the middle of a CSA. When the
10054 * CSA is finished this flow should be retriggered.
10055 */
10056 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10057 wpa_dbg(wpa_s, MSG_DEBUG,
10058 "P2P: Not initiating a GO frequency change - CSA is in progress");
10059 return;
10060 }
10061
10062 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10063 timeout = P2P_GO_FREQ_CHANGE_TIME;
10064 else
10065 timeout = 0;
10066
10067 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10068 freq, timeout);
10069 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10070 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10071}
10072
10073
10074static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10075 struct wpa_used_freq_data *freqs,
10076 unsigned int num,
10077 enum wpas_p2p_channel_update_trig trig)
10078{
10079 struct wpa_supplicant *ifs;
10080
10081 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10082 NULL);
10083
10084 /*
10085 * Travers all the radio interfaces, and for each GO interface, check
10086 * if there is a need to move the GO from the frequency it is using,
10087 * or in case the frequency is valid again, cancel the evacuation flow.
10088 */
10089 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10090 radio_list) {
10091 if (ifs->current_ssid == NULL ||
10092 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10093 continue;
10094
10095 /*
10096 * The GO was just started or completed channel switch, no need
10097 * to move it.
10098 */
10099 if (wpa_s == ifs &&
10100 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10101 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10102 wpa_dbg(wpa_s, MSG_DEBUG,
10103 "P2P: GO move - schedule re-consideration");
10104 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10105 wpas_p2p_reconsider_moving_go,
10106 wpa_s, NULL);
10107 continue;
10108 }
10109
10110 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10111 }
10112}
10113
10114
10115void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10116{
10117 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10118 return;
10119
10120 wpas_p2p_update_channel_list(wpa_s,
10121 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10122}
10123
10124
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010125void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10126{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010127 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10128 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10129 "the management interface is being removed");
10130 wpas_p2p_deinit_global(wpa_s->global);
10131 }
10132}
10133
10134
10135void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10136{
10137 if (wpa_s->ap_iface->bss)
10138 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10139 wpas_p2p_group_deinit(wpa_s);
10140}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010141
10142
10143int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10144 unsigned int period, unsigned int interval,
10145 unsigned int count)
10146{
10147 struct p2p_data *p2p = wpa_s->global->p2p;
10148 u8 *device_types;
10149 size_t dev_types_len;
10150 struct wpabuf *buf;
10151 int ret;
10152
10153 if (wpa_s->p2p_lo_started) {
10154 wpa_dbg(wpa_s, MSG_DEBUG,
10155 "P2P Listen offload is already started");
10156 return 0;
10157 }
10158
10159 if (wpa_s->global->p2p == NULL ||
10160 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10161 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10162 return -1;
10163 }
10164
10165 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10166 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10167 freq);
10168 return -1;
10169 }
10170
10171 /* Get device type */
10172 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10173 WPS_DEV_TYPE_LEN;
10174 device_types = os_malloc(dev_types_len);
10175 if (!device_types)
10176 return -1;
10177 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10178 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10179 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10180
10181 /* Get Probe Response IE(s) */
10182 buf = p2p_build_probe_resp_template(p2p, freq);
10183 if (!buf) {
10184 os_free(device_types);
10185 return -1;
10186 }
10187
10188 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10189 device_types, dev_types_len,
10190 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10191 if (ret < 0)
10192 wpa_dbg(wpa_s, MSG_DEBUG,
10193 "P2P: Failed to start P2P listen offload");
10194
10195 os_free(device_types);
10196 wpabuf_free(buf);
10197
10198 if (ret == 0) {
10199 wpa_s->p2p_lo_started = 1;
10200
10201 /* Stop current P2P listen if any */
10202 wpas_stop_listen(wpa_s);
10203 }
10204
10205 return ret;
10206}
10207
10208
10209int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10210{
10211 int ret;
10212
10213 if (!wpa_s->p2p_lo_started)
10214 return 0;
10215
10216 ret = wpa_drv_p2p_lo_stop(wpa_s);
10217 if (ret < 0)
10218 wpa_dbg(wpa_s, MSG_DEBUG,
10219 "P2P: Failed to stop P2P listen offload");
10220
10221 wpa_s->p2p_lo_started = 0;
10222 return ret;
10223}