blob: 0bfb9627e257c5949672d715bbd2a62179fe0dff [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
Sunil Ravi89eba102022-09-13 21:04:37 -07003836 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3837 p2p_channel_to_freq(op_class, channel)))
3838 return NOT_ALLOWED;
3839
Hai Shalomc1a21442022-02-04 13:43:00 -08003840 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003841 if (bw == BW40MINUS) {
3842 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3843 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003844 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3845 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003846 } else if (bw == BW40PLUS) {
3847 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3848 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003849 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3850 NULL);
3851 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3852 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3853 return NOT_ALLOWED;
3854 if (get_6ghz_sec_channel(channel) < 0)
3855 res2 = has_channel(wpa_s->global, mode, op_class,
3856 channel - 4, NULL);
3857 else
3858 res2 = has_channel(wpa_s->global, mode, op_class,
3859 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003860 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003861 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3862 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003863 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003864 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3865 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003866 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3867 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003868 }
3869
3870 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3871 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003872 if (res == NO_IR || res2 == NO_IR)
3873 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003874 if (res == RADAR || res2 == RADAR)
3875 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003876 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003877}
3878
3879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003881 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003882 struct p2p_channels *cli_chan,
3883 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003885 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003886 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003888 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3890 "of all supported channels; assume dualband "
3891 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003892 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893 }
3894
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003895 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003897 for (op = 0; global_op_class[op].op_class; op++) {
3898 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003899 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003900 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901
Hai Shalom899fcc72020-10-19 14:38:18 -07003902 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003903 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003904 continue;
3905
Hai Shalomfdcde762020-04-02 11:19:20 -07003906 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3907 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908 if (mode == NULL)
3909 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003910 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3911 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003913 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003914
3915 /* Check for non-continuous jump in channel index
3916 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003917 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003918 ch < 149 && ch + o->inc > 149)
3919 ch = 149;
3920
Hai Shalomc1a21442022-02-04 13:43:00 -08003921 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
3922 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003923 if (res == ALLOWED) {
3924 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003925 if (cla == P2P_MAX_REG_CLASSES)
3926 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003927 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3928 o->op_class);
3929 reg = &chan->reg_class[cla];
3930 cla++;
3931 reg->reg_class = o->op_class;
3932 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003933 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3934 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003935 reg->channel[reg->channels] = ch;
3936 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003937 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003938 wpa_s->conf->p2p_add_cli_chan) {
3939 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003940 if (cli_cla == P2P_MAX_REG_CLASSES)
3941 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003942 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3943 o->op_class);
3944 cli_reg = &cli_chan->reg_class[cli_cla];
3945 cli_cla++;
3946 cli_reg->reg_class = o->op_class;
3947 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003948 if (cli_reg->channels ==
3949 P2P_MAX_REG_CLASS_CHANNELS)
3950 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003951 cli_reg->channel[cli_reg->channels] = ch;
3952 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 }
3955 if (reg) {
3956 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3957 reg->channel, reg->channels);
3958 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003959 if (cli_reg) {
3960 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3961 cli_reg->channel, cli_reg->channels);
3962 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 }
3964
3965 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003966 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 return 0;
3969}
3970
3971
Hai Shalomc1a21442022-02-04 13:43:00 -08003972int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
3973 struct hostapd_hw_modes *mode,
3974 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003975{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003976 int op;
3977 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003978
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003979 for (op = 0; global_op_class[op].op_class; op++) {
3980 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08003981 u16 ch;
3982 int chan = channel;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003983
Hai Shalomc1a21442022-02-04 13:43:00 -08003984 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
3985 * driver offloaded DFS. */
3986 if ((o->p2p == NO_P2P_SUPP &&
3987 (!is_dfs_global_op_class(o->op_class) ||
3988 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07003989 (is_6ghz_op_class(o->op_class) &&
3990 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003991 continue;
3992
Hai Shalomc1a21442022-02-04 13:43:00 -08003993 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
3994 get_6ghz_sec_channel(channel) < 0)
3995 chan = channel - 4;
3996
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003997 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3998 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08003999 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4000 o->bw != BW40) ||
4001 ch != chan)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004002 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004003 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4004 ch, o->bw);
4005 if (ret == ALLOWED) {
4006 if (is_6ghz_op_class(o->op_class) &&
4007 o->bw == BW40)
4008 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004009 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004010 }
4011 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4012 /* Allow RADAR channels used for driver
4013 * offloaded DFS */
4014 return (o->bw == BW40MINUS) ? -1 : 1;
4015 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004016 }
4017 }
4018 return 0;
4019}
4020
4021
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004022int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004023 struct hostapd_hw_modes *mode, u8 channel,
4024 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004025{
Hai Shalomc1a21442022-02-04 13:43:00 -08004026 const u8 *chans;
4027 size_t num_chans;
4028 enum chan_allowed ret;
4029
4030 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4031 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004032 return 0;
4033
Hai Shalomc1a21442022-02-04 13:43:00 -08004034 if (is_6ghz_op_class(op_class)) {
4035 chans = center_channels_6ghz_80mhz;
4036 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4037 } else {
4038 chans = center_channels_5ghz_80mhz;
4039 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4040 }
4041 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4042 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004043}
4044
4045
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004046int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004047 struct hostapd_hw_modes *mode, u8 channel,
4048 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004049{
Hai Shalomc1a21442022-02-04 13:43:00 -08004050 const u8 *chans;
4051 size_t num_chans;
4052 enum chan_allowed ret;
4053
4054 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4055 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004056 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004057 if (is_6ghz_op_class(op_class)) {
4058 chans = center_channels_6ghz_160mhz;
4059 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4060 } else {
4061 chans = center_channels_5ghz_160mhz;
4062 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4063 }
4064 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4065 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004066}
4067
4068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4070 size_t buf_len)
4071{
4072 struct wpa_supplicant *wpa_s = ctx;
4073
4074 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4075 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4076 break;
4077 }
4078 if (wpa_s == NULL)
4079 return -1;
4080
4081 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4082}
4083
4084
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004085struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4086 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004087{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004088 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4089 struct wpa_ssid *s = wpa_s->current_ssid;
4090 if (s == NULL)
4091 continue;
4092 if (s->mode != WPAS_MODE_P2P_GO &&
4093 s->mode != WPAS_MODE_AP &&
4094 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4095 continue;
4096 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004097 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004098 continue;
4099 return wpa_s;
4100 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004101
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004102 return NULL;
4103
4104}
4105
4106
4107struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4108 const u8 *peer_dev_addr)
4109{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004110 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4111 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004112 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004113 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004114 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4115 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004116 }
4117
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004118 return NULL;
4119}
4120
4121
4122static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4123{
4124 struct wpa_supplicant *wpa_s = ctx;
4125
4126 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004127}
4128
4129
Dmitry Shmidt18463232014-01-24 12:29:41 -08004130static int wpas_is_concurrent_session_active(void *ctx)
4131{
4132 struct wpa_supplicant *wpa_s = ctx;
4133 struct wpa_supplicant *ifs;
4134
4135 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4136 if (ifs == wpa_s)
4137 continue;
4138 if (ifs->wpa_state > WPA_ASSOCIATED)
4139 return 1;
4140 }
4141 return 0;
4142}
4143
4144
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004145static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4146{
4147 struct wpa_supplicant *wpa_s = ctx;
4148 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4149}
4150
4151
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004152int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4153 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004154{
4155 struct wpa_interface iface;
4156 struct wpa_supplicant *p2pdev_wpa_s;
4157 char ifname[100];
4158 char force_name[100];
4159 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004160 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004161
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004162 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4163 wpa_s->ifname);
4164 if (os_snprintf_error(sizeof(ifname), ret))
4165 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004166 /* Cut length at the maximum size. Note that we don't need to ensure
4167 * collision free names here as the created interface is not a netdev.
4168 */
4169 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004170 force_name[0] = '\0';
4171 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004172
4173 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4174 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4175 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4176
4177 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004178 force_name, wpa_s->pending_interface_addr, NULL);
4179 if (ret < 0) {
4180 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4181 return ret;
4182 }
4183 os_strlcpy(wpa_s->pending_interface_name, ifname,
4184 sizeof(wpa_s->pending_interface_name));
4185
4186 os_memset(&iface, 0, sizeof(iface));
4187 iface.p2p_mgmt = 1;
4188 iface.ifname = wpa_s->pending_interface_name;
4189 iface.driver = wpa_s->driver->name;
4190 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004191
4192 /*
4193 * If a P2P Device configuration file was given, use it as the interface
4194 * configuration file (instead of using parent's configuration file.
4195 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004196 if (conf_p2p_dev) {
4197 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004198 iface.ctrl_interface = NULL;
4199 } else {
4200 iface.confname = wpa_s->confname;
4201 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4202 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004203
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004204 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004205 if (!p2pdev_wpa_s) {
4206 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4207 return -1;
4208 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004209
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004210 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004211 wpa_s->pending_interface_name[0] = '\0';
4212 return 0;
4213}
4214
4215
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004216static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4217 const u8 *noa, size_t noa_len)
4218{
4219 struct wpa_supplicant *wpa_s, *intf = ctx;
4220 char hex[100];
4221
4222 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4223 if (wpa_s->waiting_presence_resp)
4224 break;
4225 }
4226 if (!wpa_s) {
4227 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4228 return;
4229 }
4230 wpa_s->waiting_presence_resp = 0;
4231
4232 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4233 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4234 " status=%u noa=%s", MAC2STR(src), status, hex);
4235}
4236
4237
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004238static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4239 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004240 u8 *ret_ssid, size_t *ret_ssid_len,
4241 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004242{
4243 struct wpa_supplicant *wpa_s = ctx;
4244 struct wpa_ssid *s;
4245
4246 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4247 if (s) {
4248 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4249 *ret_ssid_len = s->ssid_len;
4250 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004251
4252 if (s->mode != WPAS_MODE_P2P_GO) {
4253 os_memset(intended_iface_addr, 0, ETH_ALEN);
4254 } else if (wpas_p2p_create_iface(wpa_s)) {
4255 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4256 return 0;
4257
4258 os_memcpy(intended_iface_addr,
4259 wpa_s->pending_interface_addr, ETH_ALEN);
4260 } else {
4261 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4262 ETH_ALEN);
4263 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004264 return 1;
4265 }
4266
4267 return 0;
4268}
4269
4270
4271static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004272 u8 *ssid, size_t *ssid_len, int *group_iface,
4273 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274{
4275 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004276 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004277 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004278
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004279 /*
4280 * group_iface will be set to 1 only if a dedicated interface for P2P
4281 * role is required. First, we try to reuse an active GO. However,
4282 * if it is not present, we will try to reactivate an existing
4283 * persistent group and set group_iface to 1, so the caller will know
4284 * that the pending interface should be used.
4285 */
4286 *group_iface = 0;
4287
4288 if (freq)
4289 *freq = 0;
4290
4291 go = wpas_p2p_get_go_group(wpa_s);
4292 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004293 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004294 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004295 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004296 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4297 else
4298 return 0;
4299 } else {
4300 s = go->current_ssid;
4301 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4302 if (freq)
4303 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004304 }
4305
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004306 os_memcpy(ssid, s->ssid, s->ssid_len);
4307 *ssid_len = s->ssid_len;
4308
4309 return 1;
4310}
4311
4312
4313static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4314 const u8 *ssid, size_t ssid_len)
4315{
4316 struct wpa_supplicant *wpa_s = ctx;
4317 struct wpa_ssid *s;
4318 int save_config = 0;
4319 size_t i;
4320
4321 /* Start with our first choice of Persistent Groups */
4322 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4323 if (go && ssid && ssid_len &&
4324 s->ssid_len == ssid_len &&
4325 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4326 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4327 break;
4328
4329 /* Remove stale persistent group */
4330 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004331 wpa_dbg(wpa_s, MSG_DEBUG,
4332 "P2P: Remove stale persistent group id=%d",
4333 s->id);
4334 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004335 wpa_config_remove_network(wpa_s->conf, s->id);
4336 save_config = 1;
4337 continue;
4338 }
4339
4340 for (i = 0; i < s->num_p2p_clients; i++) {
4341 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4342 peer, ETH_ALEN) != 0)
4343 continue;
4344
4345 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4346 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4347 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4348 break;
4349 }
4350 s->num_p2p_clients--;
4351 save_config = 1;
4352 }
4353
4354 if (save_config)
4355 p2p_config_write(wpa_s);
4356
4357 /* Return TRUE if valid SSID remains */
4358 return s != NULL;
4359}
4360
4361
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004362static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4363 const u8 *feat_cap, size_t feat_cap_len)
4364{
4365 static const char pref[] = " feature_cap=";
4366 int ret;
4367
4368 buf[0] = '\0';
4369
4370 /*
4371 * We expect a feature capability to contain at least one byte to be
4372 * reported. The string buffer provided by the caller function is
4373 * expected to be big enough to contain all bytes of the attribute for
4374 * known specifications. This function truncates the reported bytes if
4375 * the feature capability data exceeds the string buffer size.
4376 */
4377 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4378 return;
4379
4380 os_memcpy(buf, pref, sizeof(pref));
4381 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4382 buf_len - sizeof(pref) + 1,
4383 feat_cap, feat_cap_len);
4384
4385 if (ret != (2 * (int) feat_cap_len))
4386 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4387}
4388
4389
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004390static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4391 const u8 *adv_mac, const u8 *ses_mac,
4392 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4393 u8 conncap, int passwd_id,
4394 const u8 *persist_ssid,
4395 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004396 int prov_start, const char *session_info,
4397 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004398 unsigned int freq,
4399 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004400{
4401 struct wpa_supplicant *wpa_s = ctx;
4402 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004403 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004404 int save_config = 0;
4405 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004406 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004407
4408 if (!dev)
4409 return;
4410
4411 os_memset(mac, 0, ETH_ALEN);
4412 if (!adv_mac)
4413 adv_mac = mac;
4414 if (!ses_mac)
4415 ses_mac = mac;
4416 if (!grp_mac)
4417 grp_mac = mac;
4418
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004419 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4420 feat_cap, feat_cap_len);
4421
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004422 if (prov_start) {
4423 if (session_info == NULL) {
4424 wpa_msg_global(wpa_s, MSG_INFO,
4425 P2P_EVENT_P2PS_PROVISION_START MACSTR
4426 " adv_id=%x conncap=%x"
4427 " adv_mac=" MACSTR
4428 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004429 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004430 MAC2STR(dev), adv_id, conncap,
4431 MAC2STR(adv_mac),
4432 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004433 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004434 } else {
4435 wpa_msg_global(wpa_s, MSG_INFO,
4436 P2P_EVENT_P2PS_PROVISION_START MACSTR
4437 " adv_id=%x conncap=%x"
4438 " adv_mac=" MACSTR
4439 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004440 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004441 MAC2STR(dev), adv_id, conncap,
4442 MAC2STR(adv_mac),
4443 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004444 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004445 }
4446 return;
4447 }
4448
4449 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4450 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4451
4452 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4453 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4454 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4455
4456 if (persistent_go && !persistent_go->num_p2p_clients) {
4457 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004458 wpa_dbg(wpa_s, MSG_DEBUG,
4459 "P2P: Remove empty persistent group id=%d",
4460 persistent_go->id);
4461 wpas_notify_persistent_group_removed(wpa_s,
4462 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004463 wpa_config_remove_network(wpa_s->conf,
4464 persistent_go->id);
4465 }
4466
4467 wpa_msg_global(wpa_s, MSG_INFO,
4468 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4469 " status=%d"
4470 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004471 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004472 MAC2STR(dev), status,
4473 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004474 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004475 return;
4476 }
4477
4478 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004479 if (persist_ssid && persist_ssid_size)
4480 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4481 persist_ssid_size);
4482
4483 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4484 is_zero_ether_addr(grp_mac)) {
4485 wpa_dbg(wpa_s, MSG_ERROR,
4486 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4487 return;
4488 }
4489
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004490 for (;;) {
4491 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4492 if (!stale)
4493 break;
4494
4495 if (s && s->ssid_len == stale->ssid_len &&
4496 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4497 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4498 break;
4499
4500 /* Remove stale persistent group */
4501 if (stale->mode != WPAS_MODE_P2P_GO ||
4502 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004503 wpa_dbg(wpa_s, MSG_DEBUG,
4504 "P2P: Remove stale persistent group id=%d",
4505 stale->id);
4506 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004507 wpa_config_remove_network(wpa_s->conf, stale->id);
4508 } else {
4509 size_t i;
4510
4511 for (i = 0; i < stale->num_p2p_clients; i++) {
4512 if (os_memcmp(stale->p2p_client_list +
4513 i * ETH_ALEN,
4514 dev, ETH_ALEN) == 0) {
4515 os_memmove(stale->p2p_client_list +
4516 i * ETH_ALEN,
4517 stale->p2p_client_list +
4518 (i + 1) * ETH_ALEN,
4519 (stale->num_p2p_clients -
4520 i - 1) * ETH_ALEN);
4521 break;
4522 }
4523 }
4524 stale->num_p2p_clients--;
4525 }
4526 save_config = 1;
4527 }
4528
4529 if (save_config)
4530 p2p_config_write(wpa_s);
4531
4532 if (s) {
4533 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4534 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4535
4536 if (persistent_go && s != persistent_go &&
4537 !persistent_go->num_p2p_clients) {
4538 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004539 wpa_dbg(wpa_s, MSG_DEBUG,
4540 "P2P: Remove empty persistent group id=%d",
4541 persistent_go->id);
4542 wpas_notify_persistent_group_removed(wpa_s,
4543 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004544 wpa_config_remove_network(wpa_s->conf,
4545 persistent_go->id);
4546 /* Save config */
4547 }
4548
4549 wpa_msg_global(wpa_s, MSG_INFO,
4550 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4551 " status=%d"
4552 " adv_id=%x adv_mac=" MACSTR
4553 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004554 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004555 MAC2STR(dev), status,
4556 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004557 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004558 return;
4559 }
4560
Hai Shalom5f92bc92019-04-18 11:54:11 -07004561 wpa_s->global->pending_p2ps_group = 0;
4562 wpa_s->global->pending_p2ps_group_freq = 0;
4563
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004564 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004565 /*
4566 * We need to copy the interface name. Simply saving a
4567 * pointer isn't enough, since if we use pending_interface_name
4568 * it will be overwritten when the group is added.
4569 */
4570 char go_ifname[100];
4571
4572 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004573 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004574 if (!response_done) {
4575 wpa_s->global->pending_p2ps_group = 1;
4576 wpa_s->global->pending_p2ps_group_freq = freq;
4577 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004578
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004579 if (!wpas_p2p_create_iface(wpa_s))
4580 os_memcpy(go_ifname, wpa_s->ifname,
4581 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004582 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004583 os_memcpy(go_ifname,
4584 wpa_s->pending_interface_name,
4585 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004586
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004587 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004588 wpas_p2ps_prov_complete(
4589 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4590 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004591 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004592 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4593 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004594 return;
4595 }
4596
4597 /* If PD Resp complete, start up the GO */
4598 if (response_done && persistent_go) {
4599 wpas_p2p_group_add_persistent(
4600 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004601 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004602 persistent_go->mode ==
4603 WPAS_MODE_P2P_GO ?
4604 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08004605 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004606 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004607 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004608 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004609 }
4610
4611 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004612 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4613 ETH_ALEN);
4614 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004615 }
4616 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004617 os_memcpy(go_ifname, go_wpa_s->ifname,
4618 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004619
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004620 if (is_zero_ether_addr(grp_mac)) {
4621 wpa_dbg(go_wpa_s, MSG_DEBUG,
4622 "P2P: Setting PIN-1 for ANY");
4623 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4624 "12345670", NULL, 0,
4625 0);
4626 } else {
4627 wpa_dbg(go_wpa_s, MSG_DEBUG,
4628 "P2P: Setting PIN-1 for " MACSTR,
4629 MAC2STR(grp_mac));
4630 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4631 "12345670", NULL, 0,
4632 0);
4633 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004634
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004635 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4636 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004637 }
4638
4639 wpa_msg_global(wpa_s, MSG_INFO,
4640 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4641 " status=%d conncap=%x"
4642 " adv_id=%x adv_mac=" MACSTR
4643 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004644 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004645 MAC2STR(dev), status, conncap,
4646 adv_id, MAC2STR(adv_mac),
4647 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004648 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004649 return;
4650 }
4651
4652 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4653 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4654
4655 if (persistent_go && !persistent_go->num_p2p_clients) {
4656 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004657 wpa_dbg(wpa_s, MSG_DEBUG,
4658 "P2P: Remove empty persistent group id=%d",
4659 persistent_go->id);
4660 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004661 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4662 }
4663
4664 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004665 char ssid_hex[32 * 2 + 1];
4666
4667 if (group_ssid)
4668 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4669 group_ssid, group_ssid_len);
4670 else
4671 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004672 wpa_msg_global(wpa_s, MSG_INFO,
4673 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4674 " status=%d conncap=%x"
4675 " adv_id=%x adv_mac=" MACSTR
4676 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004677 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004678 MAC2STR(dev), status, conncap,
4679 adv_id, MAC2STR(adv_mac),
4680 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004681 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4682 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004683 } else {
4684 wpa_msg_global(wpa_s, MSG_INFO,
4685 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4686 " status=%d conncap=%x"
4687 " adv_id=%x adv_mac=" MACSTR
4688 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004689 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004690 MAC2STR(dev), status, conncap,
4691 adv_id, MAC2STR(adv_mac),
4692 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004693 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004694 }
4695}
4696
4697
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004698static int _wpas_p2p_in_progress(void *ctx)
4699{
4700 struct wpa_supplicant *wpa_s = ctx;
4701 return wpas_p2p_in_progress(wpa_s);
4702}
4703
4704
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004705static int wpas_prov_disc_resp_cb(void *ctx)
4706{
4707 struct wpa_supplicant *wpa_s = ctx;
4708 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004709 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004710
4711 if (!wpa_s->global->pending_p2ps_group)
4712 return 0;
4713
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004714 freq = wpa_s->global->pending_p2ps_group_freq;
4715 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004716 wpa_s->global->pending_p2ps_group = 0;
4717
4718 if (wpas_p2p_get_go_group(wpa_s))
4719 return 0;
4720 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4721
4722 if (persistent_go) {
4723 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004724 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4725 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004726 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004727 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
4728 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004729 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004730 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4731 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004732 }
4733
4734 return 1;
4735}
4736
4737
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004738static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4739 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004740 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004741{
4742 struct wpa_supplicant *wpa_s = ctx;
4743
4744 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4745 WPA_IF_P2P_CLIENT, len, freq_list);
4746}
4747
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004748int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4749{
JaeMan Park9de97322022-07-11 15:36:43 +09004750 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004751 u8 addr[ETH_ALEN] = {0};
4752
4753 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4754 return 0;
4755
Hai Shalom60840252021-02-19 19:02:11 -08004756 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4757 if (is_zero_ether_addr(
4758 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4759 !is_zero_ether_addr(wpa_s->own_addr)) {
4760 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4761 wpa_s->own_addr, ETH_ALEN);
4762 }
4763 return 0;
4764 }
4765
4766 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004767 if (random_mac_addr(addr) < 0) {
4768 wpa_msg(wpa_s, MSG_INFO,
4769 "Failed to generate random MAC address");
4770 return -EINVAL;
4771 }
4772
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004773 /* Store generated MAC address. */
4774 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4775 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004776 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004777 /* If there are existing saved groups, restore last MAC address.
4778 * if there is no last used MAC address, the last one is
4779 * factory MAC. */
4780 if (is_zero_ether_addr(
4781 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004782 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004783 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4784 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004785 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4786 }
4787
JaeMan Park9de97322022-07-11 15:36:43 +09004788 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4789
4790 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004791 wpa_msg(wpa_s, MSG_INFO,
4792 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004793 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004794 }
4795
JaeMan Park9de97322022-07-11 15:36:43 +09004796 ret = wpa_supplicant_update_mac_addr(wpa_s);
4797
4798 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004799 wpa_msg(wpa_s, MSG_INFO,
4800 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004801 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004802 }
4803
4804 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4805 MAC2STR(addr));
4806
4807 return 0;
4808}
4809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810/**
4811 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4812 * @global: Pointer to global data from wpa_supplicant_init()
4813 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4814 * Returns: 0 on success, -1 on failure
4815 */
4816int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4817{
4818 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004819 int i;
4820
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004821 if (wpa_s->conf->p2p_disabled)
4822 return 0;
4823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4825 return 0;
4826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827 if (global->p2p)
4828 return 0;
4829
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004830 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4831 wpa_msg(wpa_s, MSG_ERROR,
4832 "Failed to initialize P2P random MAC address.");
4833 return -1;
4834 }
4835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004838 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004839 p2p.p2p_scan = wpas_p2p_scan;
4840 p2p.send_action = wpas_send_action;
4841 p2p.send_action_done = wpas_send_action_done;
4842 p2p.go_neg_completed = wpas_go_neg_completed;
4843 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4844 p2p.dev_found = wpas_dev_found;
4845 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004846 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847 p2p.start_listen = wpas_start_listen;
4848 p2p.stop_listen = wpas_stop_listen;
4849 p2p.send_probe_resp = wpas_send_probe_resp;
4850 p2p.sd_request = wpas_sd_request;
4851 p2p.sd_response = wpas_sd_response;
4852 p2p.prov_disc_req = wpas_prov_disc_req;
4853 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004854 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004855 p2p.invitation_process = wpas_invitation_process;
4856 p2p.invitation_received = wpas_invitation_received;
4857 p2p.invitation_result = wpas_invitation_result;
4858 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004859 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004860 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004861 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004862 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004863 p2p.get_persistent_group = wpas_get_persistent_group;
4864 p2p.get_go_info = wpas_get_go_info;
4865 p2p.remove_stale_groups = wpas_remove_stale_groups;
4866 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4867 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4868 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004869 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004870 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871
4872 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004873 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 p2p.dev_name = wpa_s->conf->device_name;
4875 p2p.manufacturer = wpa_s->conf->manufacturer;
4876 p2p.model_name = wpa_s->conf->model_name;
4877 p2p.model_number = wpa_s->conf->model_number;
4878 p2p.serial_number = wpa_s->conf->serial_number;
4879 if (wpa_s->wps) {
4880 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4881 p2p.config_methods = wpa_s->wps->config_methods;
4882 }
4883
Hai Shalom60840252021-02-19 19:02:11 -08004884 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4885 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004886 wpa_printf(MSG_ERROR,
4887 "P2P: Failed to configure supported channel list");
4888 return -1;
4889 }
4890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891 if (wpa_s->conf->p2p_listen_reg_class &&
4892 wpa_s->conf->p2p_listen_channel) {
4893 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4894 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004895 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897 /*
4898 * Pick one of the social channels randomly as the listen
4899 * channel.
4900 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004901 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004902 &p2p.channel,
4903 &global->p2p_go_avoid_freq,
4904 &global->p2p_disallow_freq) !=
4905 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004906 wpa_printf(MSG_INFO,
4907 "P2P: No social channels supported by the driver - do not enable P2P");
4908 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004909 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004910 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004912 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4913 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914
4915 if (wpa_s->conf->p2p_oper_reg_class &&
4916 wpa_s->conf->p2p_oper_channel) {
4917 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4918 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4919 p2p.cfg_op_channel = 1;
4920 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4921 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4922
4923 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004925 * Use random operation channel from 2.4 GHz band social
4926 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4927 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004929 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004930 &p2p.op_channel, NULL,
4931 NULL) != 0) {
4932 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004933 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004934 p2p.op_reg_class = 0;
4935 p2p.op_channel = 0;
4936 /* This will be overridden during group setup in
4937 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004938 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939 p2p.cfg_op_channel = 0;
4940 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4941 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4942 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004943
4944 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4945 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4946 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4947 }
4948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4950 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4951 p2p.country[2] = 0x04;
4952 } else
4953 os_memcpy(p2p.country, "XX\x04", 3);
4954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4956 WPS_DEV_TYPE_LEN);
4957
4958 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4959 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4960 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4961
4962 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4963 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4964
4965 p2p.max_peers = 100;
4966
4967 if (wpa_s->conf->p2p_ssid_postfix) {
4968 p2p.ssid_postfix_len =
4969 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4970 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4971 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4972 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4973 p2p.ssid_postfix_len);
4974 }
4975
4976 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4977
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004978 p2p.max_listen = wpa_s->max_remain_on_chan;
4979
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004980 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4981 wpa_s->conf->p2p_passphrase_len <= 63)
4982 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4983 else
4984 p2p.passphrase_len = 8;
4985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986 global->p2p = p2p_init(&p2p);
4987 if (global->p2p == NULL)
4988 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004989 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990
4991 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4992 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4993 continue;
4994 p2p_add_wps_vendor_extension(
4995 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4996 }
4997
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004998 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000 return 0;
5001}
5002
5003
5004/**
5005 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5006 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5007 *
5008 * This function deinitialize per-interface P2P data.
5009 */
5010void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5011{
5012 if (wpa_s->driver && wpa_s->drv_priv)
5013 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005014
5015 if (wpa_s->go_params) {
5016 /* Clear any stored provisioning info */
5017 p2p_clear_provisioning_info(
5018 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005019 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005020 }
5021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 os_free(wpa_s->go_params);
5023 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005024 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5026 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005027 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5029 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5030 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005031 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005032 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005033 wpas_p2p_listen_work_done(wpa_s);
5034 if (wpa_s->p2p_send_action_work) {
5035 os_free(wpa_s->p2p_send_action_work->ctx);
5036 radio_work_done(wpa_s->p2p_send_action_work);
5037 wpa_s->p2p_send_action_work = NULL;
5038 }
5039 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005041 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5042 wpa_s->p2p_oob_dev_pw = NULL;
5043
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005044 os_free(wpa_s->p2p_group_common_freqs);
5045 wpa_s->p2p_group_common_freqs = NULL;
5046 wpa_s->p2p_group_common_freqs_num = 0;
5047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048 /* TODO: remove group interface from the driver if this wpa_s instance
5049 * is on top of a P2P group interface */
5050}
5051
5052
5053/**
5054 * wpas_p2p_deinit_global - Deinitialize global P2P module
5055 * @global: Pointer to global data from wpa_supplicant_init()
5056 *
5057 * This function deinitializes the global (per device) P2P module.
5058 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005059static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060{
5061 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005063 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005064
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005065 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066
5067 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5069 wpa_s = wpa_s->next;
5070 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071 tmp = global->ifaces;
5072 while (tmp &&
5073 (tmp == wpa_s ||
5074 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5075 tmp = tmp->next;
5076 }
5077 if (tmp == NULL)
5078 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005079 /* Disconnect from the P2P group and deinit the interface */
5080 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081 }
5082
5083 /*
5084 * Deinit GO data on any possibly remaining interface (if main
5085 * interface is used as GO).
5086 */
5087 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5088 if (wpa_s->ap_iface)
5089 wpas_p2p_group_deinit(wpa_s);
5090 }
5091
5092 p2p_deinit(global->p2p);
5093 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005094 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095}
5096
5097
5098static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5099{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005100 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005101 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 if (wpa_s->drv_flags &
5103 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5104 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5105 return 1; /* P2P group requires a new interface in every case
5106 */
5107 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5108 return 0; /* driver does not support concurrent operations */
5109 if (wpa_s->global->ifaces->next)
5110 return 1; /* more that one interface already in use */
5111 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5112 return 1; /* this interface is already in use */
5113 return 0;
5114}
5115
5116
5117static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5118 const u8 *peer_addr,
5119 enum p2p_wps_method wps_method,
5120 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005121 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005122 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005123{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005124 if (persistent_group && wpa_s->conf->persistent_reconnect)
5125 persistent_group = 2;
5126
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005127 /*
5128 * Increase GO config timeout if HT40 is used since it takes some time
5129 * to scan channels for coex purposes before the BSS can be started.
5130 */
5131 p2p_set_config_timeout(wpa_s->global->p2p,
5132 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5135 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005136 persistent_group, ssid ? ssid->ssid : NULL,
5137 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005138 wpa_s->p2p_pd_before_go_neg, pref_freq,
5139 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5140 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141}
5142
5143
5144static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5145 const u8 *peer_addr,
5146 enum p2p_wps_method wps_method,
5147 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005148 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005149 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005150{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005151 if (persistent_group && wpa_s->conf->persistent_reconnect)
5152 persistent_group = 2;
5153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5155 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005156 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005157 ssid ? ssid->ssid_len : 0, pref_freq,
5158 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5159 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160}
5161
5162
5163static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5164{
5165 wpa_s->p2p_join_scan_count++;
5166 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5167 wpa_s->p2p_join_scan_count);
5168 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5169 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5170 " for join operationg - stop join attempt",
5171 MAC2STR(wpa_s->pending_join_iface_addr));
5172 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005173 if (wpa_s->p2p_auto_pd) {
5174 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005175 wpa_msg_global(wpa_s, MSG_INFO,
5176 P2P_EVENT_PROV_DISC_FAILURE
5177 " p2p_dev_addr=" MACSTR " status=N/A",
5178 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005179 return;
5180 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005181 if (wpa_s->p2p_fallback_to_go_neg) {
5182 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5183 "failed - fall back to GO Negotiation");
5184 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5185 P2P_EVENT_FALLBACK_TO_GO_NEG
5186 "reason=join-failed");
5187 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5188 return;
5189 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005190 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005191 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005192 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 }
5194}
5195
5196
Dmitry Shmidt04949592012-07-19 12:16:46 -07005197static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5198{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005199 int res;
5200 unsigned int num, i;
5201 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005202
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005203 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5204 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005205 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005206 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005207
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005208 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5209 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005210 if (!freqs)
5211 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005212
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005213 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5214 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005215
5216 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005217 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005218 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5219 freq);
5220 res = 0;
5221 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005222 }
5223 }
5224
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005225 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005226 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005227
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005228exit_free:
5229 os_free(freqs);
5230 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005231}
5232
5233
5234static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5235 const u8 *peer_dev_addr)
5236{
5237 struct wpa_bss *bss;
5238 int updated;
5239
5240 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5241 if (bss == NULL)
5242 return -1;
5243 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5244 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5245 "last scan");
5246 return 0;
5247 }
5248
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005249 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5250 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005251 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5252 "%ld.%06ld (%supdated in last scan)",
5253 bss->last_update.sec, bss->last_update.usec,
5254 updated ? "": "not ");
5255
5256 return updated;
5257}
5258
5259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5261 struct wpa_scan_results *scan_res)
5262{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005263 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264 int freq;
5265 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5268
5269 if (wpa_s->global->p2p_disabled)
5270 return;
5271
Dmitry Shmidt04949592012-07-19 12:16:46 -07005272 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5273 scan_res ? (int) scan_res->num : -1,
5274 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005275
5276 if (scan_res)
5277 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5278
Dmitry Shmidt04949592012-07-19 12:16:46 -07005279 if (wpa_s->p2p_auto_pd) {
5280 int join = wpas_p2p_peer_go(wpa_s,
5281 wpa_s->pending_join_dev_addr);
5282 if (join == 0 &&
5283 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5284 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005285 bss = wpa_bss_get_bssid_latest(
5286 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005287 if (bss) {
5288 freq = bss->freq;
5289 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5290 "the peer " MACSTR " at %d MHz",
5291 wpa_s->auto_pd_scan_retry,
5292 MAC2STR(wpa_s->
5293 pending_join_dev_addr),
5294 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005295 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005296 return;
5297 }
5298 }
5299
5300 if (join < 0)
5301 join = 0;
5302
5303 wpa_s->p2p_auto_pd = 0;
5304 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5305 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5306 MAC2STR(wpa_s->pending_join_dev_addr), join);
5307 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005308 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005309 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005310 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005311 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005312 wpa_msg_global(wpa_s, MSG_INFO,
5313 P2P_EVENT_PROV_DISC_FAILURE
5314 " p2p_dev_addr=" MACSTR " status=N/A",
5315 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005316 }
5317 return;
5318 }
5319
5320 if (wpa_s->p2p_auto_join) {
5321 int join = wpas_p2p_peer_go(wpa_s,
5322 wpa_s->pending_join_dev_addr);
5323 if (join < 0) {
5324 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5325 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005326 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005327 P2P_EVENT_FALLBACK_TO_GO_NEG
5328 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005329 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5330 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5331 wpa_s->p2p_persistent_group, 0, 0, 0,
5332 wpa_s->p2p_go_intent,
5333 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005334 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005335 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005336 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005337 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005338 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005339 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005340 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005341 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005342 NULL, 0,
5343 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 return;
5345 }
5346
5347 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5348 "try to join the group", join ? "" :
5349 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005350 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005351 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005352 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005353 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005354 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005355 }
5356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5358 wpa_s->pending_join_iface_addr);
5359 if (freq < 0 &&
5360 p2p_get_interface_addr(wpa_s->global->p2p,
5361 wpa_s->pending_join_dev_addr,
5362 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005363 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5364 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5366 "address for join from " MACSTR " to " MACSTR
5367 " based on newly discovered P2P peer entry",
5368 MAC2STR(wpa_s->pending_join_iface_addr),
5369 MAC2STR(iface_addr));
5370 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5371 ETH_ALEN);
5372
5373 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5374 wpa_s->pending_join_iface_addr);
5375 }
5376 if (freq >= 0) {
5377 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5378 "from P2P peer table: %d MHz", freq);
5379 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005380 if (wpa_s->p2p_join_ssid_len) {
5381 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5382 MACSTR " and SSID %s",
5383 MAC2STR(wpa_s->pending_join_iface_addr),
5384 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5385 wpa_s->p2p_join_ssid_len));
5386 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5387 wpa_s->p2p_join_ssid,
5388 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005389 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005390 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5391 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5392 bss = wpa_bss_get_bssid_latest(wpa_s,
5393 wpa_s->pending_join_iface_addr);
5394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005395 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005396 u8 dev_addr[ETH_ALEN];
5397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 freq = bss->freq;
5399 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005400 "from BSS table: %d MHz (SSID %s)", freq,
5401 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005402 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005403 dev_addr) == 0 &&
5404 os_memcmp(wpa_s->pending_join_dev_addr,
5405 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5406 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5407 ETH_ALEN) != 0) {
5408 wpa_printf(MSG_DEBUG,
5409 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5410 MAC2STR(dev_addr),
5411 MAC2STR(wpa_s->pending_join_dev_addr));
5412 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5413 ETH_ALEN);
5414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005415 }
5416 if (freq > 0) {
5417 u16 method;
5418
Dmitry Shmidt04949592012-07-19 12:16:46 -07005419 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005420 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005421 P2P_EVENT_GROUP_FORMATION_FAILURE
5422 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005423 wpas_notify_p2p_group_formation_failure(
5424 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005425 return;
5426 }
5427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005428 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5429 "prior to joining an existing group (GO " MACSTR
5430 " freq=%u MHz)",
5431 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5432 wpa_s->pending_pd_before_join = 1;
5433
5434 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435 case WPS_PIN_DISPLAY:
5436 method = WPS_CONFIG_KEYPAD;
5437 break;
5438 case WPS_PIN_KEYPAD:
5439 method = WPS_CONFIG_DISPLAY;
5440 break;
5441 case WPS_PBC:
5442 method = WPS_CONFIG_PUSHBUTTON;
5443 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005444 case WPS_P2PS:
5445 method = WPS_CONFIG_P2PS;
5446 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447 default:
5448 method = 0;
5449 break;
5450 }
5451
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005452 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5453 wpa_s->pending_join_dev_addr) ==
5454 method)) {
5455 /*
5456 * We have already performed provision discovery for
5457 * joining the group. Proceed directly to join
5458 * operation without duplicated provision discovery. */
5459 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5460 "with " MACSTR " already done - proceed to "
5461 "join",
5462 MAC2STR(wpa_s->pending_join_dev_addr));
5463 wpa_s->pending_pd_before_join = 0;
5464 goto start;
5465 }
5466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005468 wpa_s->pending_join_dev_addr,
5469 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005470 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5472 "Discovery Request before joining an "
5473 "existing group");
5474 wpa_s->pending_pd_before_join = 0;
5475 goto start;
5476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 return;
5478 }
5479
5480 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5481 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5482 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5483 wpas_p2p_check_join_scan_limit(wpa_s);
5484 return;
5485
5486start:
5487 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005488 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5489 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490}
5491
5492
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005493static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5494 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005495{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 int ret;
5497 struct wpa_driver_scan_params params;
5498 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005499 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005500 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005501 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502
5503 os_memset(&params, 0, sizeof(params));
5504
5505 /* P2P Wildcard SSID */
5506 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005507 if (ssid && ssid_len) {
5508 params.ssids[0].ssid = ssid;
5509 params.ssids[0].ssid_len = ssid_len;
5510 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5511 wpa_s->p2p_join_ssid_len = ssid_len;
5512 } else {
5513 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5514 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5515 wpa_s->p2p_join_ssid_len = 0;
5516 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517
5518 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005519 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5520 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5521 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005522 if (wps_ie == NULL) {
5523 wpas_p2p_scan_res_join(wpa_s, NULL);
5524 return;
5525 }
5526
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005527 if (!freq) {
5528 int oper_freq;
5529 /*
5530 * If freq is not provided, check the operating freq of the GO
5531 * and use a single channel scan on if possible.
5532 */
5533 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5534 wpa_s->pending_join_iface_addr);
5535 if (oper_freq > 0)
5536 freq = oper_freq;
5537 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 if (freq > 0) {
5539 freqs[0] = freq;
5540 params.freqs = freqs;
Hai Shalomc1a21442022-02-04 13:43:00 -08005541 } else if (wpa_s->conf->p2p_6ghz_disable ||
5542 !is_p2p_allow_6ghz(wpa_s->global->p2p)) {
Hai Shalom899fcc72020-10-19 14:38:18 -07005543 wpa_printf(MSG_DEBUG,
5544 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005545 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5546 0);
5547 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5548 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005549 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005550
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005551 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5552 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5553 if (ies == NULL) {
5554 wpabuf_free(wps_ie);
5555 wpas_p2p_scan_res_join(wpa_s, NULL);
5556 return;
5557 }
5558 wpabuf_put_buf(ies, wps_ie);
5559 wpabuf_free(wps_ie);
5560
5561 bands = wpas_get_bands(wpa_s, freqs);
5562 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5563
5564 params.p2p_probe = 1;
5565 params.extra_ies = wpabuf_head(ies);
5566 params.extra_ies_len = wpabuf_len(ies);
5567
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005568 if (wpa_s->clear_driver_scan_cache) {
5569 wpa_printf(MSG_DEBUG,
5570 "Request driver to clear scan cache due to local BSS flush");
5571 params.only_new_results = 1;
5572 }
5573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005574 /*
5575 * Run a scan to update BSS table and start Provision Discovery once
5576 * the new scan results become available.
5577 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005578 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005579 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005580 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005581 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005582 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005583 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005584 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005585 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005586 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587
5588 wpabuf_free(ies);
5589
5590 if (ret) {
5591 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5592 "try again later");
5593 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5594 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5595 wpas_p2p_check_join_scan_limit(wpa_s);
5596 }
5597}
5598
5599
Dmitry Shmidt04949592012-07-19 12:16:46 -07005600static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5601{
5602 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005603 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005604}
5605
5606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005608 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005609 int auto_join, int op_freq,
5610 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611{
5612 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005613 MACSTR " dev " MACSTR " op_freq=%d)%s",
5614 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005615 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005616 if (ssid && ssid_len) {
5617 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5618 wpa_ssid_txt(ssid, ssid_len));
5619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005620
Dmitry Shmidt04949592012-07-19 12:16:46 -07005621 wpa_s->p2p_auto_pd = 0;
5622 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5624 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5625 wpa_s->pending_join_wps_method = wps_method;
5626
5627 /* Make sure we are not running find during connection establishment */
5628 wpas_p2p_stop_find(wpa_s);
5629
5630 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005631 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632 return 0;
5633}
5634
5635
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005636static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5637 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638{
5639 struct wpa_supplicant *group;
5640 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005641 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642
5643 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5644 if (group == NULL)
5645 return -1;
5646 if (group != wpa_s) {
5647 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5648 sizeof(group->p2p_pin));
5649 group->p2p_wps_method = wpa_s->p2p_wps_method;
5650 }
5651
Hai Shalom74f70d42019-02-11 14:42:39 -08005652 /*
5653 * Need to mark the current interface for p2p_group_formation
5654 * when a separate group interface is not used. This is needed
5655 * to allow p2p_cancel stop a pending p2p_connect-join.
5656 * wpas_p2p_init_group_interface() addresses this for the case
5657 * where a separate group interface is used.
5658 */
5659 if (group == wpa_s->parent)
5660 wpa_s->global->p2p_group_formation = group;
5661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005663 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005664
5665 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005666 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5668 ETH_ALEN);
5669 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005670 if (freq && ssid && ssid_len) {
5671 res.freq = freq;
5672 res.ssid_len = ssid_len;
5673 os_memcpy(res.ssid, ssid, ssid_len);
5674 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005675 if (ssid && ssid_len) {
5676 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5677 ssid, ssid_len);
5678 } else {
5679 bss = wpa_bss_get_bssid_latest(
5680 wpa_s, wpa_s->pending_join_iface_addr);
5681 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005682 if (bss) {
5683 res.freq = bss->freq;
5684 res.ssid_len = bss->ssid_len;
5685 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5686 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5687 bss->freq,
5688 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005689 } else if (ssid && ssid_len) {
5690 res.ssid_len = ssid_len;
5691 os_memcpy(res.ssid, ssid, ssid_len);
5692 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5693 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005694 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005695 }
5696
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005697 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5698 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5699 "starting client");
5700 wpa_drv_cancel_remain_on_channel(wpa_s);
5701 wpa_s->off_channel_freq = 0;
5702 wpa_s->roc_waiting_drv_freq = 0;
5703 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704 wpas_start_wps_enrollee(group, &res);
5705
5706 /*
5707 * Allow a longer timeout for join-a-running-group than normal 15
5708 * second group formation timeout since the GO may not have authorized
5709 * our connection yet.
5710 */
5711 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5712 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5713 wpa_s, NULL);
5714
5715 return 0;
5716}
5717
5718
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005719static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005720 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005721 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005722 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005723{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005724 struct wpa_used_freq_data *freqs;
5725 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005726 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305727 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005728
5729 max_pref_freq = *num_pref_freq;
5730 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005731
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005732 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5733 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005734 if (!freqs)
5735 return -1;
5736
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005737 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5738 wpa_s->num_multichan_concurrent);
5739
5740 /*
5741 * It is possible that the total number of used frequencies is bigger
5742 * than the number of frequencies used for P2P, so get the system wide
5743 * number of unused frequencies.
5744 */
5745 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5746
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005747 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005748 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5749 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005750
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005751 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005752 int ret;
5753 if (go)
5754 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5755 else
5756 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5757 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005758 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005759 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5760 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005761 /*
5762 * If freq is a DFS channel and DFS is offloaded
5763 * to the driver, allow P2P GO to use it.
5764 */
5765 wpa_printf(MSG_DEBUG,
5766 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5767 freq);
5768 } else {
5769 wpa_printf(MSG_DEBUG,
5770 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5771 freq);
5772 res = -3;
5773 goto exit_free;
5774 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005775 }
5776
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005777 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005778 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005779 freq_in_use = 1;
5780 }
5781
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005782 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005783 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5784 freq);
5785 res = -2;
5786 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005787 }
5788 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5789 "requested channel (%u MHz)", freq);
5790 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005791 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005792 }
5793
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005794 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005795
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305796 if (*pref_freq == 0) {
5797 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005798 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305799 while (i < wpa_s->conf->num_p2p_pref_chan) {
5800 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5801 wpa_s->conf->p2p_pref_chan[i].op_class,
5802 wpa_s->conf->p2p_pref_chan[i].chan);
5803
5804 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5805 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5806 best_freq = p2p_pref_freq;
5807 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5808 "from P2P preferred channel list", best_freq);
5809 break;
5810 } else {
5811 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5812 "frequency (%u MHz) ", p2p_pref_freq);
5813 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005814 i++;
5815 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305816 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305817 enum wpa_driver_if_type iface_type;
5818
5819 if (go)
5820 iface_type = WPA_IF_P2P_GO;
5821 else
5822 iface_type = WPA_IF_P2P_CLIENT;
5823
5824 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5825 best_freq, go);
5826
5827 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5828 &max_pref_freq,
5829 pref_freq_list);
5830 if (!res && max_pref_freq > 0) {
5831 *num_pref_freq = max_pref_freq;
5832 i = 0;
5833 while (i < *num_pref_freq &&
5834 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00005835 pref_freq_list[i].freq) ||
5836 wpas_p2p_disallowed_freq(
5837 wpa_s->global,
5838 pref_freq_list[i].freq) ||
5839 !p2p_pref_freq_allowed(&pref_freq_list[i],
5840 go))) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305841 wpa_printf(MSG_DEBUG,
5842 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00005843 i, pref_freq_list[i].freq);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305844 i++;
5845 }
5846 if (i != *num_pref_freq) {
Sunil8cd6f4d2022-06-28 18:40:46 +00005847 best_freq = pref_freq_list[i].freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305848 wpa_printf(MSG_DEBUG,
5849 "P2P: Using preferred_freq_list[%d]=%d",
5850 i, best_freq);
5851 } else {
5852 wpa_printf(MSG_DEBUG,
5853 "P2P: All driver preferred frequencies are "
5854 "disallowed for P2P use");
5855 *num_pref_freq = 0;
5856 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005857 } else {
5858 wpa_printf(MSG_DEBUG,
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305859 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005860 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005861 }
5862 }
5863
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005864 /* We have a candidate frequency to use */
5865 if (best_freq > 0) {
5866 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005867 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005868 best_freq);
5869 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005870 } else {
5871 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 -07005872 best_freq);
5873 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005874 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005875 } else if (num_unused > 0) {
5876 wpa_printf(MSG_DEBUG,
5877 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5878 *force_freq = 0;
5879 } else {
5880 wpa_printf(MSG_DEBUG,
5881 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5882 res = -2;
5883 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005884 }
5885
5886exit_ok:
5887 res = 0;
5888exit_free:
5889 os_free(freqs);
5890 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005891}
5892
5893
Hai Shalomc1a21442022-02-04 13:43:00 -08005894static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5895 const u8 *peer_addr)
5896{
5897 if (wpa_s->conf->p2p_6ghz_disable ||
5898 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5899 HOSTAPD_MODE_IEEE80211A, true))
5900 return false;
5901
5902 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5903 return false;
5904 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5905 return false;
5906
5907 return true;
5908}
5909
5910
5911static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5912 const u8 *peer_addr, bool allow_6ghz, int freq)
5913{
5914 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5915 wpa_printf(MSG_DEBUG,
5916 "P2P: Allow connection on 6 GHz channels");
5917 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5918 } else {
5919 if (is_6ghz_freq(freq))
5920 return -2;
5921 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5922 }
5923
5924 return 0;
5925}
5926
5927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005928/**
5929 * wpas_p2p_connect - Request P2P Group Formation to be started
5930 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5931 * @peer_addr: Address of the peer P2P Device
5932 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5933 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005934 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 * @join: Whether to join an existing group (as a client) instead of starting
5936 * Group Owner negotiation; @peer_addr is BSSID in that case
5937 * @auth: Whether to only authorize the connection instead of doing that and
5938 * initiating Group Owner negotiation
5939 * @go_intent: GO Intent or -1 to use default
5940 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005941 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005942 * @persistent_id: Persistent group credentials to use for forcing GO
5943 * parameters or -1 to generate new values (SSID/passphrase)
5944 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5945 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005946 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005947 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005948 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005949 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005950 * @group_ssid: Specific Group SSID for join or %NULL if not set
5951 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08005952 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005953 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5954 * failure, -2 on failure due to channel not currently available,
5955 * -3 if forced channel is not supported
5956 */
5957int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5958 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005959 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005960 int go_intent, int freq, unsigned int vht_center_freq2,
5961 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005962 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005963 const u8 *group_ssid, size_t group_ssid_len,
5964 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005966 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005967 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005968 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005969 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005970 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00005971 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
5972 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005973
5974 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5975 return -1;
5976
Dmitry Shmidt04949592012-07-19 12:16:46 -07005977 if (persistent_id >= 0) {
5978 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5979 if (ssid == NULL || ssid->disabled != 2 ||
5980 ssid->mode != WPAS_MODE_P2P_GO)
5981 return -1;
5982 }
5983
Hai Shalomc1a21442022-02-04 13:43:00 -08005984 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07005985 return -2;
5986
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005987 os_free(wpa_s->global->add_psk);
5988 wpa_s->global->add_psk = NULL;
5989
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005990 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005991 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005992 wpa_s->global->pending_p2ps_group_freq = 0;
5993 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005995 if (go_intent < 0)
5996 go_intent = wpa_s->conf->p2p_go_intent;
5997
5998 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005999 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000
6001 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006002 wpa_s->p2p_persistent_group = !!persistent_group;
6003 wpa_s->p2p_persistent_id = persistent_id;
6004 wpa_s->p2p_go_intent = go_intent;
6005 wpa_s->p2p_connect_freq = freq;
6006 wpa_s->p2p_fallback_to_go_neg = 0;
6007 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006008 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006009 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006010 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6011 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006012 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006013 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006014
6015 if (pin)
6016 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6017 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006018 if (wps_generate_pin((unsigned int *) &ret) < 0)
6019 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006020 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6021 "%08d", ret);
6022 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6023 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006024 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6025 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006026 } else if (wps_method == WPS_P2PS) {
6027 /* Force the P2Ps default PIN to be used */
6028 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006029 } else
6030 wpa_s->p2p_pin[0] = '\0';
6031
Dmitry Shmidt04949592012-07-19 12:16:46 -07006032 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006033 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6034 if (auth) {
6035 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6036 "connect a running group from " MACSTR,
6037 MAC2STR(peer_addr));
6038 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6039 return ret;
6040 }
6041 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6042 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6043 iface_addr) < 0) {
6044 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6045 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6046 dev_addr);
6047 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006048 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006049 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006050 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6051 "%ld.%06ld",
6052 wpa_s->p2p_auto_started.sec,
6053 wpa_s->p2p_auto_started.usec);
6054 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006055 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006056 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006057 auto_join, freq,
6058 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006059 return -1;
6060 return ret;
6061 }
6062
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006063 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006064 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006065 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006066 if (res)
6067 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006068 wpas_p2p_set_own_freq_preference(wpa_s,
6069 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006070
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006071 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006073 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6074
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006075 if (wpa_s->create_p2p_iface) {
6076 /* Prepare to add a new interface for the group */
6077 iftype = WPA_IF_P2P_GROUP;
6078 if (go_intent == 15)
6079 iftype = WPA_IF_P2P_GO;
6080 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6081 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6082 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006083 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006084 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006085
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006086 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006087 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006088 if (wpa_s->p2p_mgmt)
6089 if_addr = wpa_s->parent->own_addr;
6090 else
6091 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006092 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094
6095 if (auth) {
6096 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006097 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006098 force_freq, persistent_group, ssid,
6099 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006100 return -1;
6101 return ret;
6102 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006103
6104 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6105 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006106 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006107 if (wpa_s->create_p2p_iface)
6108 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109 return -1;
6110 }
6111 return ret;
6112}
6113
6114
6115/**
6116 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6117 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6118 * @freq: Frequency of the channel in MHz
6119 * @duration: Duration of the stay on the channel in milliseconds
6120 *
6121 * This callback is called when the driver indicates that it has started the
6122 * requested remain-on-channel duration.
6123 */
6124void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6125 unsigned int freq, unsigned int duration)
6126{
6127 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6128 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006129 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)",
6130 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6131 wpa_s->roc_waiting_drv_freq, freq, duration);
6132 if (wpa_s->off_channel_freq &&
6133 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006134 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6135 wpa_s->pending_listen_duration);
6136 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006137 } else {
6138 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6139 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6140 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006141 }
6142}
6143
6144
Jithu Jance57cea1a2014-08-20 10:22:04 -07006145int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006146{
6147 /* Limit maximum Listen state time based on driver limitation. */
6148 if (timeout > wpa_s->max_remain_on_chan)
6149 timeout = wpa_s->max_remain_on_chan;
6150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151 return p2p_listen(wpa_s->global->p2p, timeout);
6152}
6153
6154
6155/**
6156 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6157 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6158 * @freq: Frequency of the channel in MHz
6159 *
6160 * This callback is called when the driver indicates that a remain-on-channel
6161 * operation has been completed, i.e., the duration on the requested channel
6162 * has timed out.
6163 */
6164void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6165 unsigned int freq)
6166{
6167 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6168 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006169 wpa_s->global->p2p_long_listen,
6170 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006171 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006172 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6173 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006174 if (wpa_s->global->p2p_long_listen > 0)
6175 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006176 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6177 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006178 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006179 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006180 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006182 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006183 } else {
6184 /*
6185 * When listen duration is over, stop listen & update p2p_state
6186 * to IDLE.
6187 */
6188 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006189 }
6190}
6191
6192
6193/**
6194 * wpas_p2p_group_remove - Remove a P2P group
6195 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6196 * @ifname: Network interface name of the group interface or "*" to remove all
6197 * groups
6198 * Returns: 0 on success, -1 on failure
6199 *
6200 * This function is used to remove a P2P group. This can be used to disconnect
6201 * from a group in which the local end is a P2P Client or to end a P2P Group in
6202 * case the local end is the Group Owner. If a virtual network interface was
6203 * created for this group, that interface will be removed. Otherwise, only the
6204 * configured P2P group network will be removed from the interface.
6205 */
6206int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6207{
6208 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006209 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006210
6211 if (os_strcmp(ifname, "*") == 0) {
6212 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006213 bool calling_wpa_s_group_removed = false;
6214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215 wpa_s = global->ifaces;
6216 while (wpa_s) {
6217 prev = wpa_s;
6218 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006219 if (prev->p2p_group_interface !=
6220 NOT_P2P_GROUP_INTERFACE ||
6221 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006222 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006223 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006224 if (prev == calling_wpa_s)
6225 calling_wpa_s_group_removed = true;
6226 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006227 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006228
6229 if (!calling_wpa_s_group_removed &&
6230 (calling_wpa_s->p2p_group_interface !=
6231 NOT_P2P_GROUP_INTERFACE ||
6232 (calling_wpa_s->current_ssid &&
6233 calling_wpa_s->current_ssid->p2p_group))) {
6234 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6235 wpas_p2p_disconnect_safely(calling_wpa_s,
6236 calling_wpa_s);
6237 }
6238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006239 return 0;
6240 }
6241
6242 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6243 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6244 break;
6245 }
6246
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006247 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248}
6249
6250
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006251static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6252{
6253 unsigned int r;
6254
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006255 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6256 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006257 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006258 int res;
6259
6260 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6261 &size, pref_freq_list);
Hai Shalomc1a21442022-02-04 13:43:00 -08006262 if (!is_p2p_allow_6ghz(wpa_s->global->p2p))
6263 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6264
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006265 if (!res && size > 0) {
6266 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006267 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006268 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006269 pref_freq_list[i].freq) ||
6270 wpas_p2p_disallowed_freq(
6271 wpa_s->global,
6272 pref_freq_list[i].freq) ||
6273 !p2p_pref_freq_allowed(&pref_freq_list[i],
6274 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006275 wpa_printf(MSG_DEBUG,
6276 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006277 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006278 i++;
6279 }
6280 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006281 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006282 wpa_printf(MSG_DEBUG,
6283 "P2P: Using preferred_freq_list[%d]=%d",
6284 i, freq);
6285 } else {
6286 wpa_printf(MSG_DEBUG,
6287 "P2P: All driver preferred frequencies are disallowed for P2P use");
6288 }
6289 } else {
6290 wpa_printf(MSG_DEBUG,
6291 "P2P: No preferred frequency list available");
6292 }
6293 }
6294
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006295 if (freq == 2) {
6296 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6297 "band");
6298 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006299 p2p_supported_freq_go(wpa_s->global->p2p,
6300 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006301 freq = wpa_s->best_24_freq;
6302 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6303 "channel: %d MHz", freq);
6304 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006305 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6306 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006307 int possible_2g_freqs[] = {
6308 /* operating class 81 */
6309 2412, 2437, 2462,
6310 };
6311 int possible_2g_freqs_num =
6312 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6313 int i;
6314 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6315 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6316 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6317 break;
6318 }
6319 }
6320
6321 if (i >= possible_2g_freqs_num) {
6322 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6323 "2.4 GHz channel for P2P group");
6324 return -1;
6325 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006326 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6327 "channel: %d MHz", freq);
6328 }
6329 }
6330
6331 if (freq == 5) {
6332 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6333 "band");
6334 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006335 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006336 wpa_s->best_5_freq)) {
6337 freq = wpa_s->best_5_freq;
6338 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6339 "channel: %d MHz", freq);
6340 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006341 const int freqs[] = {
6342 /* operating class 115 */
6343 5180, 5200, 5220, 5240,
6344 /* operating class 124 */
6345 5745, 5765, 5785, 5805,
6346 };
6347 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6348
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006349 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6350 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006351
6352 /*
6353 * most of 5G channels are DFS, only operating class 115 and 124
6354 * are available possibly, randomly pick a start to check them.
6355 */
6356 int possible_5g_freqs[] = {
6357 /* operating class 115 */
6358 5180, 5200, 5220, 5240,
6359 /* operating class 124 */
6360 5745, 5765, 5785, 5805,
6361 };
6362 int possible_5g_freqs_num =
6363 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6364
Jimmy Chen64b90632020-09-29 17:27:34 +08006365 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6366 if (p2p_supported_freq_go(
6367 wpa_s->global->p2p,
6368 possible_5g_freqs[r % possible_5g_freqs_num])) {
6369 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6370 break;
6371 }
6372 }
6373
6374 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006375 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6376 "5 GHz channel for P2P group");
6377 return -1;
6378 }
6379 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6380 "channel: %d MHz", freq);
6381 }
6382 }
6383
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006384 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006385 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006386 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6387 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006388 /*
6389 * If freq is a DFS channel and DFS is offloaded to the
6390 * driver, allow P2P GO to use it.
6391 */
6392 wpa_printf(MSG_DEBUG, "P2P: "
6393 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6394 __func__, freq);
6395 return freq;
6396 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006397 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6398 "(%u MHz) is not supported for P2P uses",
6399 freq);
6400 return -1;
6401 }
6402
6403 return freq;
6404}
6405
6406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006407static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6408 const struct p2p_channels *channels,
6409 int freq)
6410{
6411 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6412 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6413 freq_included(wpa_s, channels, freq))
6414 return 1;
6415 return 0;
6416}
6417
6418
6419static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6420 struct p2p_go_neg_results *params,
6421 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006422{
6423 unsigned int i, r;
6424
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006425 /* try all channels in operating class 115 */
6426 for (i = 0; i < 4; i++) {
6427 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006428 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006429 goto out;
6430 }
6431
6432 /* try all channels in operating class 124 */
6433 for (i = 0; i < 4; i++) {
6434 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006435 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006436 goto out;
6437 }
6438
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006439 /* try social channel class 180 channel 2 */
6440 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006441 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006442 goto out;
6443
6444 /* try all channels in reg. class 180 */
6445 for (i = 0; i < 4; i++) {
6446 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006447 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006448 goto out;
6449 }
6450
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006451 /* try some random selection of the social channels */
6452 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6453 return;
6454
6455 for (i = 0; i < 3; i++) {
6456 params->freq = 2412 + ((r + i) % 3) * 25;
6457 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6458 goto out;
6459 }
6460
6461 /* try all other channels in operating class 81 */
6462 for (i = 0; i < 11; i++) {
6463 params->freq = 2412 + i * 5;
6464
6465 /* skip social channels; covered in the previous loop */
6466 if (params->freq == 2412 ||
6467 params->freq == 2437 ||
6468 params->freq == 2462)
6469 continue;
6470
6471 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6472 goto out;
6473 }
6474
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006475 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006476 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006477 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006478out:
6479 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6480 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006481}
6482
6483
Roshan Pius3a1667e2018-07-03 15:17:14 -07006484static int wpas_same_band(int freq1, int freq2)
6485{
6486 enum hostapd_hw_mode mode1, mode2;
6487 u8 chan1, chan2;
6488
6489 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6490 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6491 if (mode1 == NUM_HOSTAPD_MODES)
6492 return 0;
6493 return mode1 == mode2;
6494}
6495
6496
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006497static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6498 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006499 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006500 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006501 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006502 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006503{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006504 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006505 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006506 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006507 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006508 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006509
6510 os_memset(params, 0, sizeof(*params));
6511 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006512 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006513 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006514 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006515 params->max_oper_chwidth = max_oper_chwidth;
6516 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006517 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006518
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006519 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6520 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006521 if (!freqs)
6522 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006524 num = get_shared_radio_freqs_data(wpa_s, freqs,
6525 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006526
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006527 if (wpa_s->current_ssid &&
6528 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6529 wpa_s->wpa_state == WPA_COMPLETED) {
6530 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6531 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006532
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006533 /*
6534 * If the frequency selection is done for an active P2P GO that
6535 * is not sharing a frequency, allow to select a new frequency
6536 * even if there are no unused frequencies as we are about to
6537 * move the P2P GO so its frequency can be re-used.
6538 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006539 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006540 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6541 freqs[i].flags == 0) {
6542 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006543 break;
6544 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006545 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006546 }
6547
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006548 /* Try to use EDMG channel */
6549 if (params->edmg) {
6550 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6551 goto success;
6552 params->edmg = 0;
6553 }
6554
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006555 /* try using the forced freq */
6556 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006557 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6558 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006559 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006560 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006561 freq);
6562 goto fail;
6563 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006564 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6565 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006566 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6567 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006568 /*
6569 * If freq is a DFS channel and DFS is offloaded
6570 * to the driver, allow P2P GO to use it.
6571 */
6572 wpa_printf(MSG_DEBUG,
6573 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6574 __func__, freq);
6575 } else {
6576 wpa_printf(MSG_DEBUG,
6577 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6578 freq);
6579 goto fail;
6580 }
6581 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006582
6583 for (i = 0; i < num; i++) {
6584 if (freqs[i].freq == freq) {
6585 wpa_printf(MSG_DEBUG,
6586 "P2P: forced freq (%d MHz) is also shared",
6587 freq);
6588 params->freq = freq;
6589 goto success;
6590 }
6591 }
6592
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006593 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006594 wpa_printf(MSG_DEBUG,
6595 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6596 freq);
6597 goto fail;
6598 }
6599
6600 wpa_printf(MSG_DEBUG,
6601 "P2P: force GO freq (%d MHz) on a free channel",
6602 freq);
6603 params->freq = freq;
6604 goto success;
6605 }
6606
6607 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006608 if (num &&
6609 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006610 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6611 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6612 wpa_printf(MSG_DEBUG,
6613 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006614 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006615 params->freq = cand;
6616 goto success;
6617 }
6618
6619 /* try using one of the shared freqs */
6620 for (i = 0; i < num; i++) {
6621 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6622 freqs[i].freq)) {
6623 wpa_printf(MSG_DEBUG,
6624 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006625 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006626 params->freq = freqs[i].freq;
6627 goto success;
6628 }
6629 }
6630 }
6631
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006632 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006633 wpa_printf(MSG_DEBUG,
6634 "P2P: Cannot force GO on any of the channels we are already using");
6635 goto fail;
6636 }
6637
6638 /* try using the setting from the configuration file */
6639 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6640 wpa_s->conf->p2p_oper_channel >= 1 &&
6641 wpa_s->conf->p2p_oper_channel <= 11 &&
6642 wpas_p2p_supported_freq_go(
6643 wpa_s, channels,
6644 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6645 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6646 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6647 "frequency %d MHz", params->freq);
6648 goto success;
6649 }
6650
6651 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6652 wpa_s->conf->p2p_oper_reg_class == 116 ||
6653 wpa_s->conf->p2p_oper_reg_class == 117 ||
6654 wpa_s->conf->p2p_oper_reg_class == 124 ||
6655 wpa_s->conf->p2p_oper_reg_class == 125 ||
6656 wpa_s->conf->p2p_oper_reg_class == 126 ||
6657 wpa_s->conf->p2p_oper_reg_class == 127) &&
6658 wpas_p2p_supported_freq_go(wpa_s, channels,
6659 5000 +
6660 5 * wpa_s->conf->p2p_oper_channel)) {
6661 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6662 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6663 "frequency %d MHz", params->freq);
6664 goto success;
6665 }
6666
6667 /* Try using best channels */
6668 if (wpa_s->conf->p2p_oper_channel == 0 &&
6669 wpa_s->best_overall_freq > 0 &&
6670 wpas_p2p_supported_freq_go(wpa_s, channels,
6671 wpa_s->best_overall_freq)) {
6672 params->freq = wpa_s->best_overall_freq;
6673 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6674 "channel %d MHz", params->freq);
6675 goto success;
6676 }
6677
6678 if (wpa_s->conf->p2p_oper_channel == 0 &&
6679 wpa_s->best_24_freq > 0 &&
6680 wpas_p2p_supported_freq_go(wpa_s, channels,
6681 wpa_s->best_24_freq)) {
6682 params->freq = wpa_s->best_24_freq;
6683 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6684 "channel %d MHz", params->freq);
6685 goto success;
6686 }
6687
6688 if (wpa_s->conf->p2p_oper_channel == 0 &&
6689 wpa_s->best_5_freq > 0 &&
6690 wpas_p2p_supported_freq_go(wpa_s, channels,
6691 wpa_s->best_5_freq)) {
6692 params->freq = wpa_s->best_5_freq;
6693 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6694 "channel %d MHz", params->freq);
6695 goto success;
6696 }
6697
6698 /* try using preferred channels */
6699 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6700 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6701 params->freq = cand;
6702 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6703 "channels", params->freq);
6704 goto success;
6705 }
6706
Roshan Pius3a1667e2018-07-03 15:17:14 -07006707 /* Try using a channel that allows VHT to be used with 80 MHz */
6708 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6709 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6710 enum hostapd_hw_mode mode;
6711 struct hostapd_hw_modes *hwmode;
6712 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006713 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006714
6715 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006716 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006717 mode = ieee80211_freq_to_chan(cand, &chan);
6718 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006719 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006720 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006721 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6722 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006723 continue;
6724 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6725 params->freq = cand;
6726 wpa_printf(MSG_DEBUG,
6727 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6728 params->freq);
6729 goto success;
6730 }
6731 }
6732 }
6733
6734 /* Try using a channel that allows HT to be used with 40 MHz on the same
6735 * band so that CSA can be used */
6736 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6737 wpa_s->p2p_group_common_freqs) {
6738 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6739 enum hostapd_hw_mode mode;
6740 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006741 u8 chan, op_class;
6742 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006743
Hai Shalomc1a21442022-02-04 13:43:00 -08006744 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006745 cand = wpa_s->p2p_group_common_freqs[i];
6746 mode = ieee80211_freq_to_chan(cand, &chan);
6747 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006748 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006749 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6750 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006751 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006752 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006753 if (is_6ghz &&
6754 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6755 BW40) == ALLOWED)
6756 supported = true;
6757
6758 if (!is_6ghz &&
6759 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006760 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6761 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006762 wpas_p2p_verify_channel(
6763 wpa_s, hwmode, op_class, chan,
6764 BW40MINUS) == ALLOWED)
6765 supported = true;
6766
6767 if (!supported && !is_6ghz &&
6768 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006769 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6770 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006771 wpas_p2p_verify_channel(
6772 wpa_s, hwmode, op_class, chan,
6773 BW40PLUS) == ALLOWED)
6774 supported = true;
6775
6776 if (!supported)
6777 continue;
6778
Roshan Pius3a1667e2018-07-03 15:17:14 -07006779 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6780 params->freq = cand;
6781 wpa_printf(MSG_DEBUG,
6782 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6783 params->freq);
6784 goto success;
6785 }
6786 }
6787 }
6788
6789 /* Try using one of the group common freqs on the same band so that CSA
6790 * can be used */
6791 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6792 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6793 cand = wpa_s->p2p_group_common_freqs[i];
6794 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6795 cand))
6796 continue;
6797 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6798 params->freq = cand;
6799 wpa_printf(MSG_DEBUG,
6800 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6801 params->freq);
6802 goto success;
6803 }
6804 }
6805 }
6806
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006807 /* Try using one of the group common freqs */
6808 if (wpa_s->p2p_group_common_freqs) {
6809 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6810 cand = wpa_s->p2p_group_common_freqs[i];
6811 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6812 params->freq = cand;
6813 wpa_printf(MSG_DEBUG,
6814 "P2P: Use freq %d MHz common with the peer",
6815 params->freq);
6816 goto success;
6817 }
6818 }
6819 }
6820
6821 /* no preference, select some channel */
6822 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6823
6824 if (params->freq == 0) {
6825 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6826 goto fail;
6827 }
6828
6829success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006830 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006831 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006832fail:
6833 os_free(freqs);
6834 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835}
6836
6837
6838static struct wpa_supplicant *
6839wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6840 int go)
6841{
6842 struct wpa_supplicant *group_wpa_s;
6843
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006844 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006845 if (wpa_s->p2p_mgmt) {
6846 /*
6847 * We may be called on the p2p_dev interface which
6848 * cannot be used for group operations, so always use
6849 * the primary interface.
6850 */
6851 wpa_s->parent->p2pdev = wpa_s;
6852 wpa_s = wpa_s->parent;
6853 }
6854 wpa_dbg(wpa_s, MSG_DEBUG,
6855 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006856 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006857 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006858 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006859 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006861
6862 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006863 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006864 wpa_msg_global(wpa_s, MSG_ERROR,
6865 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006866 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006868 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6869 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006870 wpa_msg_global(wpa_s, MSG_ERROR,
6871 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 wpas_p2p_remove_pending_group_interface(wpa_s);
6873 return NULL;
6874 }
6875
Roshan Pius3a1667e2018-07-03 15:17:14 -07006876 if (go && wpa_s->p2p_go_do_acs) {
6877 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6878 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6879 wpa_s->p2p_go_do_acs = 0;
6880 }
6881
Hai Shalomc1a21442022-02-04 13:43:00 -08006882 if (go && wpa_s->p2p_go_allow_dfs) {
6883 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6884 wpa_s->p2p_go_allow_dfs = 0;
6885 }
6886
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006887 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6888 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006889 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006890 return group_wpa_s;
6891}
6892
6893
6894/**
6895 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6896 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6897 * @persistent_group: Whether to create a persistent group
6898 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006899 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006900 * @ht40: Start GO with 40 MHz channel width
6901 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006902 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006903 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006904 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905 * Returns: 0 on success, -1 on failure
6906 *
6907 * This function creates a new P2P group with the local end as the Group Owner,
6908 * i.e., without using Group Owner Negotiation.
6909 */
6910int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006911 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006912 int max_oper_chwidth, int he, int edmg,
6913 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006914{
6915 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006916 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917
6918 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6919 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006920 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6921 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006922
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006923 os_free(wpa_s->global->add_psk);
6924 wpa_s->global->add_psk = NULL;
6925
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006926 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006927 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006928 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006929
Roshan Pius3a1667e2018-07-03 15:17:14 -07006930 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00006931 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
6932 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006933 return -1;
6934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935
Sunil Ravi77a0f092022-10-03 00:53:41 +00006936 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006937 ht40, vht, max_oper_chwidth, he, edmg,
6938 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006939 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941 p2p_go_params(wpa_s->global->p2p, &params);
6942 params.persistent_group = persistent_group;
6943
6944 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6945 if (wpa_s == NULL)
6946 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006947 if (freq > 0)
6948 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006949 wpas_start_wps_go(wpa_s, &params, 0);
6950
6951 return 0;
6952}
6953
6954
6955static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006956 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006957 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006958{
6959 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07006960 int other_iface_found = 0;
6961 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962
6963 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6964 if (wpa_s == NULL)
6965 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006966 if (force_scan)
6967 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006968 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006969
6970 wpa_supplicant_ap_deinit(wpa_s);
6971
6972 ssid = wpa_config_add_network(wpa_s->conf);
6973 if (ssid == NULL)
6974 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006975 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 wpa_config_set_network_defaults(ssid);
6977 ssid->temporary = 1;
6978 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006979 ssid->pbss = params->pbss;
6980 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6981 WPA_CIPHER_CCMP;
6982 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6984 ssid->ssid = os_malloc(params->ssid_len);
6985 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986 wpa_config_remove_network(wpa_s->conf, ssid->id);
6987 return -1;
6988 }
6989 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6990 ssid->ssid_len = params->ssid_len;
6991 ssid->p2p_group = 1;
6992 ssid->export_keys = 1;
6993 if (params->psk_set) {
6994 os_memcpy(ssid->psk, params->psk, 32);
6995 ssid->psk_set = 1;
6996 }
6997 if (params->passphrase)
6998 ssid->passphrase = os_strdup(params->passphrase);
6999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007001 wpa_s->p2p_in_invitation = 1;
7002 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007003 wpa_s->p2p_go_group_formation_completed = 0;
7004 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007005
Matthew Wang9ae940b2022-09-14 21:25:34 -07007006 /*
7007 * Get latest scan results from driver in case cached scan results from
7008 * interfaces on the same wiphy allow us to skip the next scan by fast
7009 * associating. Also update the scan time to the most recent scan result
7010 * fetch time on the same radio so it reflects the actual time the last
7011 * scan result event occurred.
7012 */
7013 wpa_supplicant_update_scan_results(wpa_s);
7014 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7015 radio_list) {
7016 if (ifs == wpa_s)
7017 continue;
7018 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7019 &ifs->last_scan)) {
7020 other_iface_found = 1;
7021 wpa_s->last_scan.sec = ifs->last_scan.sec;
7022 wpa_s->last_scan.usec = ifs->last_scan.usec;
7023 }
7024 }
7025
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007026 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007027 NULL);
7028 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7029 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007030 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007031 wpa_supplicant_select_network(wpa_s, ssid);
7032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033 return 0;
7034}
7035
7036
7037int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7038 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007039 int force_freq, int neg_freq,
7040 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007041 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007042 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007043 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007044 int connection_timeout, int force_scan,
7045 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007046{
7047 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007048 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007049
7050 if (ssid->disabled != 2 || ssid->ssid == NULL)
7051 return -1;
7052
7053 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7054 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7055 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7056 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007057 if (go == 0 &&
7058 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007059 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007060 /*
7061 * This can happen if Invitation Response frame was lost
7062 * and the peer (GO of a persistent group) tries to
7063 * invite us again. Reschedule the timeout to avoid
7064 * terminating the wait for the connection too early
7065 * since we now know that the peer is still trying to
7066 * invite us instead of having already started the GO.
7067 */
7068 wpa_printf(MSG_DEBUG,
7069 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7070 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7071 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007072 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007074 return 0;
7075 }
7076
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007077 os_free(wpa_s->global->add_psk);
7078 wpa_s->global->add_psk = NULL;
7079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007081 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082
Dmitry Shmidt04949592012-07-19 12:16:46 -07007083 wpa_s->p2p_fallback_to_go_neg = 0;
7084
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007085 if (ssid->mode == WPAS_MODE_P2P_GO) {
7086 if (force_freq > 0) {
7087 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7088 if (freq < 0)
7089 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007090 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007091 } else {
7092 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7093 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007094 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007095 freq = 0;
7096 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007097 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007098 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007099 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007100 struct os_reltime now;
7101 struct wpa_bss *bss =
7102 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007103
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007104 os_get_reltime(&now);
7105 if (bss &&
7106 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007107 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007108 freq = bss->freq;
7109 else
7110 freq = 0;
7111 }
7112
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007113 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
7114 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007115 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007116 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007117 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007118
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007119 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007120 ht40, vht, max_oper_chwidth, he, edmg,
7121 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007122 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007123
7124 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007125 params.psk_set = ssid->psk_set;
7126 if (params.psk_set)
7127 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007128 if (ssid->passphrase) {
7129 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7130 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7131 "persistent group");
7132 return -1;
7133 }
7134 os_strlcpy(params.passphrase, ssid->passphrase,
7135 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007136 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7138 params.ssid_len = ssid->ssid_len;
7139 params.persistent_group = 1;
7140
7141 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7142 if (wpa_s == NULL)
7143 return -1;
7144
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007145 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7146
Dmitry Shmidt56052862013-10-04 10:23:25 -07007147 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148 wpas_start_wps_go(wpa_s, &params, 0);
7149
7150 return 0;
7151}
7152
7153
7154static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7155 struct wpabuf *proberesp_ies)
7156{
7157 struct wpa_supplicant *wpa_s = ctx;
7158 if (wpa_s->ap_iface) {
7159 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007160 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7161 wpabuf_free(beacon_ies);
7162 wpabuf_free(proberesp_ies);
7163 return;
7164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165 if (beacon_ies) {
7166 wpabuf_free(hapd->p2p_beacon_ie);
7167 hapd->p2p_beacon_ie = beacon_ies;
7168 }
7169 wpabuf_free(hapd->p2p_probe_resp_ie);
7170 hapd->p2p_probe_resp_ie = proberesp_ies;
7171 } else {
7172 wpabuf_free(beacon_ies);
7173 wpabuf_free(proberesp_ies);
7174 }
7175 wpa_supplicant_ap_update_beacon(wpa_s);
7176}
7177
7178
7179static void wpas_p2p_idle_update(void *ctx, int idle)
7180{
7181 struct wpa_supplicant *wpa_s = ctx;
7182 if (!wpa_s->ap_iface)
7183 return;
7184 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007185 if (idle) {
7186 if (wpa_s->global->p2p_fail_on_wps_complete &&
7187 wpa_s->p2p_in_provisioning) {
7188 wpas_p2p_grpform_fail_after_wps(wpa_s);
7189 return;
7190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007191 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007192 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007193 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7194}
7195
7196
7197struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007198 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007199{
7200 struct p2p_group *group;
7201 struct p2p_group_config *cfg;
7202
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007203 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7204 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007205 return NULL;
7206
7207 cfg = os_zalloc(sizeof(*cfg));
7208 if (cfg == NULL)
7209 return NULL;
7210
Dmitry Shmidt04949592012-07-19 12:16:46 -07007211 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007212 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007213 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007214 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007215 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7216 if (wpa_s->max_stations &&
7217 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7218 cfg->max_clients = wpa_s->max_stations;
7219 else
7220 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007221 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7222 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007223 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007224 cfg->cb_ctx = wpa_s;
7225 cfg->ie_update = wpas_p2p_ie_update;
7226 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007227 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7228 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007229
7230 group = p2p_group_init(wpa_s->global->p2p, cfg);
7231 if (group == NULL)
7232 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007233 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007234 p2p_group_notif_formation_done(group);
7235 wpa_s->p2p_group = group;
7236 return group;
7237}
7238
7239
7240void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7241 int registrar)
7242{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007243 struct wpa_ssid *ssid = wpa_s->current_ssid;
7244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007245 if (!wpa_s->p2p_in_provisioning) {
7246 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7247 "provisioning not in progress");
7248 return;
7249 }
7250
Dmitry Shmidt04949592012-07-19 12:16:46 -07007251 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7252 u8 go_dev_addr[ETH_ALEN];
7253 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7254 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7255 ssid->ssid_len);
7256 /* Clear any stored provisioning info */
7257 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7258 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007259
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007260 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007261 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007262 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007263 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7264 /*
7265 * Use a separate timeout for initial data connection to
7266 * complete to allow the group to be removed automatically if
7267 * something goes wrong in this step before the P2P group idle
7268 * timeout mechanism is taken into use.
7269 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007270 wpa_dbg(wpa_s, MSG_DEBUG,
7271 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7272 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007273 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7274 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007275 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007276 /* Complete group formation on successful data connection. */
7277 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007278 } else if (ssid) {
7279 /*
7280 * Use a separate timeout for initial data connection to
7281 * complete to allow the group to be removed automatically if
7282 * the client does not complete data connection successfully.
7283 */
7284 wpa_dbg(wpa_s, MSG_DEBUG,
7285 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7286 P2P_MAX_INITIAL_CONN_WAIT_GO);
7287 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7288 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007289 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007290 /*
7291 * Complete group formation on first successful data connection
7292 */
7293 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007295 if (wpa_s->global->p2p)
7296 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007297 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007298}
7299
7300
Jouni Malinen75ecf522011-06-27 15:19:46 -07007301void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7302 struct wps_event_fail *fail)
7303{
7304 if (!wpa_s->p2p_in_provisioning) {
7305 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7306 "provisioning not in progress");
7307 return;
7308 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007309
7310 if (wpa_s->go_params) {
7311 p2p_clear_provisioning_info(
7312 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007313 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007314 }
7315
Jouni Malinen75ecf522011-06-27 15:19:46 -07007316 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007317
7318 if (wpa_s == wpa_s->global->p2p_group_formation) {
7319 /*
7320 * Allow some time for the failed WPS negotiation exchange to
7321 * complete, but remove the group since group formation cannot
7322 * succeed after provisioning failure.
7323 */
7324 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7325 wpa_s->global->p2p_fail_on_wps_complete = 1;
7326 eloop_deplete_timeout(0, 50000,
7327 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007328 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007329 }
7330}
7331
7332
7333int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7334{
7335 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7336 !wpa_s->p2p_in_provisioning)
7337 return 0;
7338
7339 wpas_p2p_grpform_fail_after_wps(wpa_s);
7340
7341 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007342}
7343
7344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007346 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007347 enum wpas_p2p_prov_disc_use use,
7348 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007349{
7350 u16 config_methods;
7351
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007352 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007353 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007354 wpa_s->p2p_fallback_to_go_neg = 0;
7355 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007356 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7357 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007358 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7359 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7360
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007361 wpa_printf(MSG_DEBUG,
7362 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7363 __func__, p2ps_prov->conncap,
7364 p2ps_prov->adv_id, p2ps_prov->conncap,
7365 p2ps_prov->status, p2ps_prov->info);
7366
7367 config_methods = 0;
7368 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007369 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007370 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007372 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7373 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007374 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007375 else {
7376 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007377 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007378 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007380
Dmitry Shmidt04949592012-07-19 12:16:46 -07007381 if (use == WPAS_P2P_PD_AUTO) {
7382 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7383 wpa_s->pending_pd_config_methods = config_methods;
7384 wpa_s->p2p_auto_pd = 1;
7385 wpa_s->p2p_auto_join = 0;
7386 wpa_s->pending_pd_before_join = 0;
7387 wpa_s->auto_pd_scan_retry = 0;
7388 wpas_p2p_stop_find(wpa_s);
7389 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007390 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007391 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7392 wpa_s->p2p_auto_started.sec,
7393 wpa_s->p2p_auto_started.usec);
7394 wpas_p2p_join_scan(wpa_s, NULL);
7395 return 0;
7396 }
7397
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007398 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7399 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007400 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007402
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007403 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007404 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007405 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007406}
7407
7408
7409int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7410 char *end)
7411{
7412 return p2p_scan_result_text(ies, ies_len, buf, end);
7413}
7414
7415
7416static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7417{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007418 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007419 return;
7420
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007421 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007422 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007423 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7424 wpa_s, NULL);
7425 offchannel_send_action_done(wpa_s);
7426 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007428 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7429 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007430 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431}
7432
7433
7434int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7435 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007436 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007437 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007438 u8 seek_cnt, const char **seek_string, int freq,
7439 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007440{
7441 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007442 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007443
Dmitry Shmidt04949592012-07-19 12:16:46 -07007444 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007445 wpa_s->p2p_in_provisioning) {
7446 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7447 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7448 " (P2P disabled)" : "",
7449 wpa_s->p2p_in_provisioning ?
7450 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007451 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007453
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007454 wpa_supplicant_cancel_sched_scan(wpa_s);
7455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007456 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007457 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007458 search_delay, seek_cnt, seek_string, freq,
7459 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007460}
7461
7462
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007463static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7464 struct wpa_scan_results *scan_res)
7465{
7466 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7467
7468 if (wpa_s->p2p_scan_work) {
7469 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7470 wpa_s->p2p_scan_work = NULL;
7471 radio_work_done(work);
7472 }
7473
7474 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7475 return;
7476
7477 /*
7478 * Indicate that results have been processed so that the P2P module can
7479 * continue pending tasks.
7480 */
Hai Shalom60840252021-02-19 19:02:11 -08007481 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007482}
7483
7484
7485static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007486{
7487 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007488 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007489 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7490 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007491
7492 if (wpa_s->global->p2p)
7493 p2p_stop_find(wpa_s->global->p2p);
7494
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007495 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7496 wpa_printf(MSG_DEBUG,
7497 "P2P: Do not consider the scan results after stop_find");
7498 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7499 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007500}
7501
7502
7503void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7504{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007505 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007506 if (!wpa_s->global->pending_group_iface_for_p2ps)
7507 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508}
7509
7510
7511static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7512{
7513 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007514 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007515}
7516
7517
7518int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7519{
7520 int res;
7521
7522 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7523 return -1;
7524
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007525 if (wpa_s->p2p_lo_started) {
7526 wpa_printf(MSG_DEBUG,
7527 "P2P: Cannot start P2P listen, it is offloaded");
7528 return -1;
7529 }
7530
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007531 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007532 wpas_p2p_clear_pending_action_tx(wpa_s);
7533
7534 if (timeout == 0) {
7535 /*
7536 * This is a request for unlimited Listen state. However, at
7537 * least for now, this is mapped to a Listen state for one
7538 * hour.
7539 */
7540 timeout = 3600;
7541 }
7542 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007543 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007545 /*
7546 * Stop previous find/listen operation to avoid trying to request a new
7547 * remain-on-channel operation while the driver is still running the
7548 * previous one.
7549 */
7550 if (wpa_s->global->p2p)
7551 p2p_stop_find(wpa_s->global->p2p);
7552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7554 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007555 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007556 eloop_register_timeout(timeout, 0,
7557 wpas_p2p_long_listen_timeout,
7558 wpa_s, NULL);
7559 }
7560
7561 return res;
7562}
7563
7564
7565int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7566 u8 *buf, size_t len, int p2p_group)
7567{
7568 struct wpabuf *p2p_ie;
7569 int ret;
7570
7571 if (wpa_s->global->p2p_disabled)
7572 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007573 /*
7574 * Advertize mandatory cross connection capability even on
7575 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7576 */
7577 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007578 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007579 if (wpa_s->global->p2p == NULL)
7580 return -1;
7581 if (bss == NULL)
7582 return -1;
7583
7584 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7585 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7586 p2p_group, p2p_ie);
7587 wpabuf_free(p2p_ie);
7588
7589 return ret;
7590}
7591
7592
7593int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007594 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007595 const u8 *ie, size_t ie_len,
7596 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007597{
7598 if (wpa_s->global->p2p_disabled)
7599 return 0;
7600 if (wpa_s->global->p2p == NULL)
7601 return 0;
7602
Dmitry Shmidt04949592012-07-19 12:16:46 -07007603 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007604 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007605 case P2P_PREQ_NOT_P2P:
7606 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7607 ssi_signal);
7608 /* fall through */
7609 case P2P_PREQ_MALFORMED:
7610 case P2P_PREQ_NOT_LISTEN:
7611 case P2P_PREQ_NOT_PROCESSED:
7612 default: /* make gcc happy */
7613 return 0;
7614 case P2P_PREQ_PROCESSED:
7615 return 1;
7616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007617}
7618
7619
7620void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7621 const u8 *sa, const u8 *bssid,
7622 u8 category, const u8 *data, size_t len, int freq)
7623{
7624 if (wpa_s->global->p2p_disabled)
7625 return;
7626 if (wpa_s->global->p2p == NULL)
7627 return;
7628
7629 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7630 freq);
7631}
7632
7633
7634void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7635{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007636 unsigned int bands;
7637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007638 if (wpa_s->global->p2p_disabled)
7639 return;
7640 if (wpa_s->global->p2p == NULL)
7641 return;
7642
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007643 bands = wpas_get_bands(wpa_s, NULL);
7644 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007645}
7646
7647
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007648static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007649{
7650 p2p_group_deinit(wpa_s->p2p_group);
7651 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007652
7653 wpa_s->ap_configured_cb = NULL;
7654 wpa_s->ap_configured_cb_ctx = NULL;
7655 wpa_s->ap_configured_cb_data = NULL;
7656 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007657}
7658
7659
7660int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7661{
Hai Shalomfdcde762020-04-02 11:19:20 -07007662 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007664 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7665 return -1;
7666
7667 return p2p_reject(wpa_s->global->p2p, addr);
7668}
7669
7670
7671/* Invite to reinvoke a persistent group */
7672int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007673 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007674 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007675 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007676{
7677 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007678 u8 *bssid = NULL;
7679 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007680 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007681 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007682 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7683 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007684
Hai Shalomc1a21442022-02-04 13:43:00 -08007685 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7686 return -1;
7687
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007688 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007689 if (peer_addr)
7690 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7691 else
7692 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007693
Jouni Malinen31be0a42012-08-31 21:20:51 +03007694 wpa_s->p2p_persistent_go_freq = freq;
7695 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007696 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007697 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007698 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7699 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007700 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007701 if (ssid->mode == WPAS_MODE_P2P_GO) {
7702 role = P2P_INVITE_ROLE_GO;
7703 if (peer_addr == NULL) {
7704 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7705 "address in invitation command");
7706 return -1;
7707 }
7708 if (wpas_p2p_create_iface(wpa_s)) {
7709 if (wpas_p2p_add_group_interface(wpa_s,
7710 WPA_IF_P2P_GO) < 0) {
7711 wpa_printf(MSG_ERROR, "P2P: Failed to "
7712 "allocate a new interface for the "
7713 "group");
7714 return -1;
7715 }
7716 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007717 } else if (wpa_s->p2p_mgmt)
7718 bssid = wpa_s->parent->own_addr;
7719 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007720 bssid = wpa_s->own_addr;
7721 } else {
7722 role = P2P_INVITE_ROLE_CLIENT;
7723 peer_addr = ssid->bssid;
7724 }
7725 wpa_s->pending_invite_ssid_id = ssid->id;
7726
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007727 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007728 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007729 role == P2P_INVITE_ROLE_GO,
7730 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007731 if (res)
7732 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007734 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7735 return -1;
7736
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007737 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7738
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007739 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7740 no_pref_freq_given && pref_freq > 0 &&
7741 wpa_s->num_multichan_concurrent > 1 &&
7742 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7743 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7744 pref_freq);
7745 pref_freq = 0;
7746 }
7747
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007748 /*
7749 * Stop any find/listen operations before invitation and possibly
7750 * connection establishment.
7751 */
7752 wpas_p2p_stop_find_oper(wpa_s);
7753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007754 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007755 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007756 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757}
7758
7759
7760/* Invite to join an active group */
7761int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007762 const u8 *peer_addr, const u8 *go_dev_addr,
7763 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007764{
7765 struct wpa_global *global = wpa_s->global;
7766 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007767 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007768 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007769 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007770 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007771 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007772 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7773 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007774
Jouni Malinen31be0a42012-08-31 21:20:51 +03007775 wpa_s->p2p_persistent_go_freq = 0;
7776 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007777 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007778 wpa_s->p2p_go_vht_center_freq2 = 0;
7779 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007780 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007782 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7783 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7784 break;
7785 }
7786 if (wpa_s == NULL) {
7787 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7788 return -1;
7789 }
7790
7791 ssid = wpa_s->current_ssid;
7792 if (ssid == NULL) {
7793 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7794 "invitation");
7795 return -1;
7796 }
7797
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007798 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007799 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007800 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007801 ssid->ssid, ssid->ssid_len);
7802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 if (ssid->mode == WPAS_MODE_P2P_GO) {
7804 role = P2P_INVITE_ROLE_ACTIVE_GO;
7805 bssid = wpa_s->own_addr;
7806 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007807 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007808 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007809 } else {
7810 role = P2P_INVITE_ROLE_CLIENT;
7811 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7812 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7813 "invite to current group");
7814 return -1;
7815 }
7816 bssid = wpa_s->bssid;
7817 if (go_dev_addr == NULL &&
7818 !is_zero_ether_addr(wpa_s->go_dev_addr))
7819 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007820 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7821 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007822 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007823 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007825 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7826 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007827 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7828 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007829
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007830 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007831 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007832 role == P2P_INVITE_ROLE_ACTIVE_GO,
7833 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007834 if (res)
7835 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007836 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007838 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007839 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007840 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007841}
7842
7843
7844void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7845{
7846 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007847 u8 go_dev_addr[ETH_ALEN];
7848 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007849 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007850 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007851 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852
Dmitry Shmidt04949592012-07-19 12:16:46 -07007853 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7854 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007855 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007856 }
7857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007858 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007859 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007860
7861 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007862 if (!wpa_s->p2p_go_group_formation_completed &&
7863 wpa_s->global->p2p_group_formation == wpa_s) {
7864 wpa_dbg(wpa_s, MSG_DEBUG,
7865 "P2P: Marking group formation completed on client on data connection");
7866 wpa_s->p2p_go_group_formation_completed = 1;
7867 wpa_s->global->p2p_group_formation = NULL;
7868 wpa_s->p2p_in_provisioning = 0;
7869 wpa_s->p2p_in_invitation = 0;
7870 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007872 os_memset(go_dev_addr, 0, ETH_ALEN);
7873 if (ssid->bssid_set)
7874 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7875 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7876 ssid->ssid_len);
7877 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7878
7879 if (wpa_s->global->p2p_group_formation == wpa_s)
7880 wpa_s->global->p2p_group_formation = NULL;
7881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007882 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7883 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007884
7885 ip_addr[0] = '\0';
7886 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007887 int res;
7888
7889 res = os_snprintf(ip_addr, sizeof(ip_addr),
7890 " ip_addr=%u.%u.%u.%u "
7891 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7892 ip[0], ip[1], ip[2], ip[3],
7893 ip[4], ip[5], ip[6], ip[7],
7894 ip[8], ip[9], ip[10], ip[11]);
7895 if (os_snprintf_error(sizeof(ip_addr), res))
7896 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007897 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007898 }
7899
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007900 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7901 ssid->passphrase == NULL && ssid->psk_set ?
7902 ssid->psk : NULL,
7903 ssid->passphrase, go_dev_addr, persistent,
7904 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905
7906 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007907 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7908 ssid, go_dev_addr);
7909
Hai Shalom5f92bc92019-04-18 11:54:11 -07007910 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911}
7912
7913
7914int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7915 u32 interval1, u32 duration2, u32 interval2)
7916{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007917 int ret;
7918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007919 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7920 return -1;
7921
7922 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7923 wpa_s->current_ssid == NULL ||
7924 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7925 return -1;
7926
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007927 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7928 wpa_s->own_addr, wpa_s->assoc_freq,
7929 duration1, interval1, duration2, interval2);
7930 if (ret == 0)
7931 wpa_s->waiting_presence_resp = 1;
7932
7933 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007934}
7935
7936
7937int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7938 unsigned int interval)
7939{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007940 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7941 return -1;
7942
7943 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7944}
7945
7946
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007947static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7948{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007949 if (wpa_s->current_ssid == NULL) {
7950 /*
7951 * current_ssid can be cleared when P2P client interface gets
7952 * disconnected, so assume this interface was used as P2P
7953 * client.
7954 */
7955 return 1;
7956 }
7957 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007958 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7959}
7960
7961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007962static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7963{
7964 struct wpa_supplicant *wpa_s = eloop_ctx;
7965
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007966 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007967 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7968 "disabled");
7969 return;
7970 }
7971
Dmitry Shmidt04949592012-07-19 12:16:46 -07007972 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7973 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007974 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007975}
7976
7977
7978static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7979{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007980 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007981
Dmitry Shmidt04949592012-07-19 12:16:46 -07007982 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7983 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007985 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7986 return;
7987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007988 timeout = wpa_s->conf->p2p_group_idle;
7989 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7990 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7991 timeout = P2P_MAX_CLIENT_IDLE;
7992
7993 if (timeout == 0)
7994 return;
7995
Dmitry Shmidt04949592012-07-19 12:16:46 -07007996 if (timeout < 0) {
7997 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7998 timeout = 0; /* special client mode no-timeout */
7999 else
8000 return;
8001 }
8002
8003 if (wpa_s->p2p_in_provisioning) {
8004 /*
8005 * Use the normal group formation timeout during the
8006 * provisioning phase to avoid terminating this process too
8007 * early due to group idle timeout.
8008 */
8009 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8010 "during provisioning");
8011 return;
8012 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308013
Dmitry Shmidt04949592012-07-19 12:16:46 -07008014 if (wpa_s->show_group_started) {
8015 /*
8016 * Use the normal group formation timeout between the end of
8017 * the provisioning phase and completion of 4-way handshake to
8018 * avoid terminating this process too early due to group idle
8019 * timeout.
8020 */
8021 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8022 "while waiting for initial 4-way handshake to "
8023 "complete");
8024 return;
8025 }
8026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008027 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008028 timeout);
8029 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8030 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008031}
8032
8033
Jouni Malinen2b89da82012-08-31 22:04:41 +03008034/* Returns 1 if the interface was removed */
8035int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8036 u16 reason_code, const u8 *ie, size_t ie_len,
8037 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008038{
8039 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008040 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008041
Dmitry Shmidt04949592012-07-19 12:16:46 -07008042 if (!locally_generated)
8043 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8044 ie_len);
8045
8046 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8047 wpa_s->current_ssid &&
8048 wpa_s->current_ssid->p2p_group &&
8049 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8050 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8051 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008052 if (wpas_p2p_group_delete(wpa_s,
8053 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8054 > 0)
8055 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008056 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008057
8058 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008059}
8060
8061
8062void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008063 u16 reason_code, const u8 *ie, size_t ie_len,
8064 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008065{
8066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8067 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008068
Dmitry Shmidt04949592012-07-19 12:16:46 -07008069 if (!locally_generated)
8070 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8071 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008072}
8073
8074
8075void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8076{
8077 struct p2p_data *p2p = wpa_s->global->p2p;
8078
8079 if (p2p == NULL)
8080 return;
8081
8082 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8083 return;
8084
8085 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8086 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8087
8088 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8089 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8090
8091 if (wpa_s->wps &&
8092 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8093 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8094
8095 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8096 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8097
8098 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8099 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8100 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8101 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8102 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8103 }
8104
8105 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8106 p2p_set_sec_dev_types(p2p,
8107 (void *) wpa_s->conf->sec_device_type,
8108 wpa_s->conf->num_sec_device_types);
8109
8110 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8111 int i;
8112 p2p_remove_wps_vendor_extensions(p2p);
8113 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8114 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8115 continue;
8116 p2p_add_wps_vendor_extension(
8117 p2p, wpa_s->conf->wps_vendor_ext[i]);
8118 }
8119 }
8120
8121 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8122 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8123 char country[3];
8124 country[0] = wpa_s->conf->country[0];
8125 country[1] = wpa_s->conf->country[1];
8126 country[2] = 0x04;
8127 p2p_set_country(p2p, country);
8128 }
8129
8130 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8131 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8132 wpa_s->conf->p2p_ssid_postfix ?
8133 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8134 0);
8135 }
8136
8137 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8138 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008139
8140 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8141 u8 reg_class, channel;
8142 int ret;
8143 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008144 u8 channel_forced;
8145
Jouni Malinen75ecf522011-06-27 15:19:46 -07008146 if (wpa_s->conf->p2p_listen_reg_class &&
8147 wpa_s->conf->p2p_listen_channel) {
8148 reg_class = wpa_s->conf->p2p_listen_reg_class;
8149 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008150 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008151 } else {
8152 reg_class = 81;
8153 /*
8154 * Pick one of the social channels randomly as the
8155 * listen channel.
8156 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008157 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8158 channel = 1;
8159 else
8160 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008161 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008162 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008163 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8164 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008165 if (ret)
8166 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8167 "failed: %d", ret);
8168 }
8169 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8170 u8 op_reg_class, op_channel, cfg_op_channel;
8171 int ret = 0;
8172 unsigned int r;
8173 if (wpa_s->conf->p2p_oper_reg_class &&
8174 wpa_s->conf->p2p_oper_channel) {
8175 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8176 op_channel = wpa_s->conf->p2p_oper_channel;
8177 cfg_op_channel = 1;
8178 } else {
8179 op_reg_class = 81;
8180 /*
8181 * Use random operation channel from (1, 6, 11)
8182 *if no other preference is indicated.
8183 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008184 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8185 op_channel = 1;
8186 else
8187 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008188 cfg_op_channel = 0;
8189 }
8190 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8191 cfg_op_channel);
8192 if (ret)
8193 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8194 "failed: %d", ret);
8195 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008196
8197 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8198 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8199 wpa_s->conf->p2p_pref_chan) < 0) {
8200 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8201 "update failed");
8202 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008203
8204 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8205 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8206 "update failed");
8207 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008208 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008209
8210 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8211 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008212}
8213
8214
8215int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8216 int duration)
8217{
8218 if (!wpa_s->ap_iface)
8219 return -1;
8220 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8221 duration);
8222}
8223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008224
8225int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8226{
8227 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8228 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008229
8230 wpa_s->global->cross_connection = enabled;
8231 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8232
8233 if (!enabled) {
8234 struct wpa_supplicant *iface;
8235
8236 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8237 {
8238 if (iface->cross_connect_enabled == 0)
8239 continue;
8240
8241 iface->cross_connect_enabled = 0;
8242 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008243 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008244 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8245 iface->ifname,
8246 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008247 }
8248 }
8249
8250 return 0;
8251}
8252
8253
8254static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8255{
8256 struct wpa_supplicant *iface;
8257
8258 if (!uplink->global->cross_connection)
8259 return;
8260
8261 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8262 if (!iface->cross_connect_enabled)
8263 continue;
8264 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8265 0)
8266 continue;
8267 if (iface->ap_iface == NULL)
8268 continue;
8269 if (iface->cross_connect_in_use)
8270 continue;
8271
8272 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008273 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008274 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8275 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008276 }
8277}
8278
8279
8280static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8281{
8282 struct wpa_supplicant *iface;
8283
8284 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8285 if (!iface->cross_connect_enabled)
8286 continue;
8287 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8288 0)
8289 continue;
8290 if (!iface->cross_connect_in_use)
8291 continue;
8292
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008293 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008294 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8295 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008296 iface->cross_connect_in_use = 0;
8297 }
8298}
8299
8300
8301void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8302{
8303 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8304 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8305 wpa_s->cross_connect_disallowed)
8306 wpas_p2p_disable_cross_connect(wpa_s);
8307 else
8308 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008309 if (!wpa_s->ap_iface &&
8310 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8311 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008312}
8313
8314
8315void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8316{
8317 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008318 if (!wpa_s->ap_iface &&
8319 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8320 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008321 wpas_p2p_set_group_idle_timeout(wpa_s);
8322}
8323
8324
8325static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8326{
8327 struct wpa_supplicant *iface;
8328
8329 if (!wpa_s->global->cross_connection)
8330 return;
8331
8332 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8333 if (iface == wpa_s)
8334 continue;
8335 if (iface->drv_flags &
8336 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8337 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008338 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8339 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008340 continue;
8341
8342 wpa_s->cross_connect_enabled = 1;
8343 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8344 sizeof(wpa_s->cross_connect_uplink));
8345 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8346 "%s to %s whenever uplink is available",
8347 wpa_s->ifname, wpa_s->cross_connect_uplink);
8348
8349 if (iface->ap_iface || iface->current_ssid == NULL ||
8350 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8351 iface->cross_connect_disallowed ||
8352 iface->wpa_state != WPA_COMPLETED)
8353 break;
8354
8355 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008356 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008357 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8358 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008359 break;
8360 }
8361}
8362
8363
8364int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8365{
8366 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8367 !wpa_s->p2p_in_provisioning)
8368 return 0; /* not P2P client operation */
8369
8370 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8371 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008372 if (wpa_s != wpa_s->p2pdev)
8373 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008374 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008375 return 1;
8376}
8377
8378
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008379void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8380{
8381 struct wpa_supplicant *wpa_s = eloop_ctx;
8382 wpas_p2p_notif_pbc_overlap(wpa_s);
8383}
8384
8385
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008386void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8387 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008388{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008389 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008390 struct wpa_used_freq_data *freqs = NULL;
8391 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008392
8393 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8394 return;
8395
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008396 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8397 if (!freqs)
8398 return;
8399
8400 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008402 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008403 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008404 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8405 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008406 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8407 "channel list");
8408 return;
8409 }
8410
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008411 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008412
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008413 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008414
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008415 /*
8416 * The used frequencies map changed, so it is possible that a GO is
8417 * using a channel that is no longer valid for P2P use. It is also
8418 * possible that due to policy consideration, it would be preferable to
8419 * move it to a frequency already used by other station interfaces.
8420 */
8421 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8422
8423 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008424}
8425
8426
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008427static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8428 struct wpa_scan_results *scan_res)
8429{
8430 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8431}
8432
8433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008434int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8435{
8436 struct wpa_global *global = wpa_s->global;
8437 int found = 0;
8438 const u8 *peer;
8439
8440 if (global->p2p == NULL)
8441 return -1;
8442
8443 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8444
8445 if (wpa_s->pending_interface_name[0] &&
8446 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8447 found = 1;
8448
8449 peer = p2p_get_go_neg_peer(global->p2p);
8450 if (peer) {
8451 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8452 MACSTR, MAC2STR(peer));
8453 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008454 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008455 }
8456
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008457 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8458 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8459 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8460 found = 1;
8461 }
8462
8463 if (wpa_s->pending_pd_before_join) {
8464 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8465 wpa_s->pending_pd_before_join = 0;
8466 found = 1;
8467 }
8468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008469 wpas_p2p_stop_find(wpa_s);
8470
8471 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8472 if (wpa_s == global->p2p_group_formation &&
8473 (wpa_s->p2p_in_provisioning ||
8474 wpa_s->parent->pending_interface_type ==
8475 WPA_IF_P2P_CLIENT)) {
8476 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8477 "formation found - cancelling",
8478 wpa_s->ifname);
8479 found = 1;
8480 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008481 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008482 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008483 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008484 break;
8485 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008486 wpas_p2p_group_delete(wpa_s,
8487 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008488 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008489 } else if (wpa_s->p2p_in_invitation) {
8490 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8491 wpa_s->ifname);
8492 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008493 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008494 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008495 }
8496 }
8497
8498 if (!found) {
8499 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8500 return -1;
8501 }
8502
8503 return 0;
8504}
8505
8506
8507void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8508{
8509 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8510 return;
8511
8512 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8513 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008514 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008515}
8516
8517
8518void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8519 int freq_24, int freq_5, int freq_overall)
8520{
8521 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008522 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008523 return;
8524 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8525}
8526
8527
8528int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8529{
8530 u8 peer[ETH_ALEN];
8531 struct p2p_data *p2p = wpa_s->global->p2p;
8532
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008533 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008534 return -1;
8535
8536 if (hwaddr_aton(addr, peer))
8537 return -1;
8538
8539 return p2p_unauthorize(p2p, peer);
8540}
8541
8542
8543/**
8544 * wpas_p2p_disconnect - Disconnect from a P2P Group
8545 * @wpa_s: Pointer to wpa_supplicant data
8546 * Returns: 0 on success, -1 on failure
8547 *
8548 * This can be used to disconnect from a group in which the local end is a P2P
8549 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8550 * virtual network interface was created for this group, that interface will be
8551 * removed. Otherwise, only the configured P2P group network will be removed
8552 * from the interface.
8553 */
8554int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8555{
8556
8557 if (wpa_s == NULL)
8558 return -1;
8559
Jouni Malinen2b89da82012-08-31 22:04:41 +03008560 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8561 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008562}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008563
8564
8565int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8566{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008567 int ret;
8568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008569 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8570 return 0;
8571
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008572 ret = p2p_in_progress(wpa_s->global->p2p);
8573 if (ret == 0) {
8574 /*
8575 * Check whether there is an ongoing WPS provisioning step (or
8576 * other parts of group formation) on another interface since
8577 * p2p_in_progress() does not report this to avoid issues for
8578 * scans during such provisioning step.
8579 */
8580 if (wpa_s->global->p2p_group_formation &&
8581 wpa_s->global->p2p_group_formation != wpa_s) {
8582 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8583 "in group formation",
8584 wpa_s->global->p2p_group_formation->ifname);
8585 ret = 1;
8586 }
8587 }
8588
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008589 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008590 struct os_reltime now;
8591 os_get_reltime(&now);
8592 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8593 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008594 /* Wait for the first client has expired */
8595 wpa_s->global->p2p_go_wait_client.sec = 0;
8596 } else {
8597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8598 ret = 1;
8599 }
8600 }
8601
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008602 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008603}
8604
8605
8606void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8607 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008608{
8609 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8610 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008611 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008612 /**
8613 * Remove the network by scheduling the group formation
8614 * timeout to happen immediately. The teardown code
8615 * needs to be scheduled to run asynch later so that we
8616 * don't delete data from under ourselves unexpectedly.
8617 * Calling wpas_p2p_group_formation_timeout directly
8618 * causes a series of crashes in WPS failure scenarios.
8619 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008620 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8621 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008622 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008623 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008624 }
8625}
8626
8627
8628struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008629 const u8 *addr, const u8 *ssid,
8630 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008631{
8632 struct wpa_ssid *s;
8633 size_t i;
8634
8635 for (s = wpa_s->conf->ssid; s; s = s->next) {
8636 if (s->disabled != 2)
8637 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008638 if (ssid &&
8639 (ssid_len != s->ssid_len ||
8640 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8641 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008642 if (addr == NULL) {
8643 if (s->mode == WPAS_MODE_P2P_GO)
8644 return s;
8645 continue;
8646 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008647 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8648 return s; /* peer is GO in the persistent group */
8649 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8650 continue;
8651 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008652 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008653 addr, ETH_ALEN) == 0)
8654 return s; /* peer is P2P client in persistent
8655 * group */
8656 }
8657 }
8658
8659 return NULL;
8660}
8661
8662
8663void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8664 const u8 *addr)
8665{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008666 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008667 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008668 /*
8669 * This can happen if WPS provisioning step is not terminated
8670 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8671 * peer was able to connect, there is no need to time out group
8672 * formation after this, though. In addition, this is used with
8673 * the initial connection wait on the GO as a separate formation
8674 * timeout and as such, expected to be hit after the initial WPS
8675 * provisioning step.
8676 */
8677 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008678
8679 if (!wpa_s->p2p_go_group_formation_completed &&
8680 !wpa_s->group_formation_reported) {
8681 /*
8682 * GO has not yet notified group formation success since
8683 * the WPS step was not completed cleanly. Do that
8684 * notification now since the P2P Client was able to
8685 * connect and as such, must have received the
8686 * credential from the WPS step.
8687 */
8688 if (wpa_s->global->p2p)
8689 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008690 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008691 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008692 }
8693 if (!wpa_s->p2p_go_group_formation_completed) {
8694 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8695 wpa_s->p2p_go_group_formation_completed = 1;
8696 wpa_s->global->p2p_group_formation = NULL;
8697 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008698 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008699 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008700 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008701 if (addr == NULL)
8702 return;
8703 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8704}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008705
Dmitry Shmidt04949592012-07-19 12:16:46 -07008706
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008707static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8708 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008709{
8710 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008711 int ret = 0;
8712
Dmitry Shmidt04949592012-07-19 12:16:46 -07008713 if (wpa_s->global->p2p_group_formation)
8714 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008715 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008716 offchannel_send_action_done(wpa_s);
8717 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008718 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008719 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8720 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8721 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8722 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008723 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008724 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008725 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008726 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008727 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008728 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008729 wpa_s->p2p_go_he,
8730 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008731 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008732 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008733}
8734
8735
8736int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8737{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008738 int res;
8739
Dmitry Shmidt04949592012-07-19 12:16:46 -07008740 if (!wpa_s->p2p_fallback_to_go_neg ||
8741 wpa_s->p2p_in_provisioning <= 5)
8742 return 0;
8743
8744 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8745 return 0; /* peer operating as a GO */
8746
8747 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8748 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008749 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008750 "reason=GO-not-found");
8751 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008752
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008753 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008754}
8755
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008756
8757unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8758{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008759 struct wpa_supplicant *ifs;
8760
8761 if (wpa_s->wpa_state > WPA_SCANNING) {
8762 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8763 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008764 wpa_s->conf->p2p_search_delay);
8765 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008766 }
8767
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008768 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8769 radio_list) {
8770 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008771 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8772 "delay due to concurrent operation on "
8773 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008774 wpa_s->conf->p2p_search_delay,
8775 ifs->ifname);
8776 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008777 }
8778 }
8779
8780 return 0;
8781}
8782
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008783
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008784static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8785 struct wpa_ssid *s, const u8 *addr,
8786 int iface_addr)
8787{
8788 struct psk_list_entry *psk, *tmp;
8789 int changed = 0;
8790
8791 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8792 list) {
8793 if ((iface_addr && !psk->p2p &&
8794 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8795 (!iface_addr && psk->p2p &&
8796 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8797 wpa_dbg(wpa_s, MSG_DEBUG,
8798 "P2P: Remove persistent group PSK list entry for "
8799 MACSTR " p2p=%u",
8800 MAC2STR(psk->addr), psk->p2p);
8801 dl_list_del(&psk->list);
8802 os_free(psk);
8803 changed++;
8804 }
8805 }
8806
8807 return changed;
8808}
8809
8810
8811void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8812 const u8 *p2p_dev_addr,
8813 const u8 *psk, size_t psk_len)
8814{
8815 struct wpa_ssid *ssid = wpa_s->current_ssid;
8816 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008817 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008818
8819 if (psk_len != sizeof(p->psk))
8820 return;
8821
8822 if (p2p_dev_addr) {
8823 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8824 " p2p_dev_addr=" MACSTR,
8825 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8826 if (is_zero_ether_addr(p2p_dev_addr))
8827 p2p_dev_addr = NULL;
8828 } else {
8829 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8830 MAC2STR(mac_addr));
8831 }
8832
8833 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8834 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8835 /* To be added to persistent group once created */
8836 if (wpa_s->global->add_psk == NULL) {
8837 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8838 if (wpa_s->global->add_psk == NULL)
8839 return;
8840 }
8841 p = wpa_s->global->add_psk;
8842 if (p2p_dev_addr) {
8843 p->p2p = 1;
8844 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8845 } else {
8846 p->p2p = 0;
8847 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8848 }
8849 os_memcpy(p->psk, psk, psk_len);
8850 return;
8851 }
8852
8853 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8854 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8855 return;
8856 }
8857
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008858 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008859 ssid->ssid_len);
8860 if (!persistent) {
8861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8862 return;
8863 }
8864
8865 p = os_zalloc(sizeof(*p));
8866 if (p == NULL)
8867 return;
8868 if (p2p_dev_addr) {
8869 p->p2p = 1;
8870 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8871 } else {
8872 p->p2p = 0;
8873 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8874 }
8875 os_memcpy(p->psk, psk, psk_len);
8876
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008877 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8878 (last = dl_list_last(&persistent->psk_list,
8879 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008880 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8881 MACSTR " (p2p=%u) to make room for a new one",
8882 MAC2STR(last->addr), last->p2p);
8883 dl_list_del(&last->list);
8884 os_free(last);
8885 }
8886
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008887 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008888 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8889 p2p_dev_addr == NULL);
8890 if (p2p_dev_addr) {
8891 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8892 MACSTR, MAC2STR(p2p_dev_addr));
8893 } else {
8894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8895 MAC2STR(mac_addr));
8896 }
8897 dl_list_add(&persistent->psk_list, &p->list);
8898
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008899 if (wpa_s->p2pdev->conf->update_config &&
8900 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008901 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008902}
8903
8904
8905static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8906 struct wpa_ssid *s, const u8 *addr,
8907 int iface_addr)
8908{
8909 int res;
8910
8911 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008912 if (res > 0 && wpa_s->conf->update_config &&
8913 wpa_config_write(wpa_s->confname, wpa_s->conf))
8914 wpa_dbg(wpa_s, MSG_DEBUG,
8915 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008916}
8917
8918
8919static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8920 const u8 *peer, int iface_addr)
8921{
8922 struct hostapd_data *hapd;
8923 struct hostapd_wpa_psk *psk, *prev, *rem;
8924 struct sta_info *sta;
8925
8926 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8927 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8928 return;
8929
8930 /* Remove per-station PSK entry */
8931 hapd = wpa_s->ap_iface->bss[0];
8932 prev = NULL;
8933 psk = hapd->conf->ssid.wpa_psk;
8934 while (psk) {
8935 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8936 (!iface_addr &&
8937 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8938 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8939 MACSTR " iface_addr=%d",
8940 MAC2STR(peer), iface_addr);
8941 if (prev)
8942 prev->next = psk->next;
8943 else
8944 hapd->conf->ssid.wpa_psk = psk->next;
8945 rem = psk;
8946 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07008947 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008948 } else {
8949 prev = psk;
8950 psk = psk->next;
8951 }
8952 }
8953
8954 /* Disconnect from group */
8955 if (iface_addr)
8956 sta = ap_get_sta(hapd, peer);
8957 else
8958 sta = ap_get_sta_p2p(hapd, peer);
8959 if (sta) {
8960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8961 " (iface_addr=%d) from group",
8962 MAC2STR(peer), iface_addr);
8963 hostapd_drv_sta_deauth(hapd, sta->addr,
8964 WLAN_REASON_DEAUTH_LEAVING);
8965 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8966 }
8967}
8968
8969
8970void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8971 int iface_addr)
8972{
8973 struct wpa_ssid *s;
8974 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008975 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008976
8977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8978
8979 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008980 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008981 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8982 continue;
8983 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008984 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8985 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008986 }
8987
8988 /* Remove from any operating group */
8989 for (w = wpa_s->global->ifaces; w; w = w->next)
8990 wpas_p2p_remove_client_go(w, peer, iface_addr);
8991}
8992
8993
8994static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8995{
8996 struct wpa_supplicant *wpa_s = eloop_ctx;
8997 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8998}
8999
9000
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009001static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9002{
9003 struct wpa_supplicant *wpa_s = eloop_ctx;
9004
9005 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9006 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9007}
9008
9009
9010int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9011 struct wpa_ssid *ssid)
9012{
9013 struct wpa_supplicant *iface;
9014
9015 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9016 if (!iface->current_ssid ||
9017 iface->current_ssid->frequency == freq ||
9018 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9019 !iface->current_ssid->p2p_group))
9020 continue;
9021
9022 /* Remove the connection with least priority */
9023 if (!wpas_is_p2p_prioritized(iface)) {
9024 /* STA connection has priority over existing
9025 * P2P connection, so remove the interface. */
9026 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9027 eloop_register_timeout(0, 0,
9028 wpas_p2p_group_freq_conflict,
9029 iface, NULL);
9030 /* If connection in progress is P2P connection, do not
9031 * proceed for the connection. */
9032 if (wpa_s == iface)
9033 return -1;
9034 else
9035 return 0;
9036 } else {
9037 /* P2P connection has priority, disable the STA network
9038 */
9039 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9040 ssid);
9041 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9042 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9043 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9044 ETH_ALEN);
9045 /* If P2P connection is in progress, continue
9046 * connecting...*/
9047 if (wpa_s == iface)
9048 return 0;
9049 else
9050 return -1;
9051 }
9052 }
9053
9054 return 0;
9055}
9056
9057
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009058int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9059{
9060 struct wpa_ssid *ssid = wpa_s->current_ssid;
9061
9062 if (ssid == NULL || !ssid->p2p_group)
9063 return 0;
9064
9065 if (wpa_s->p2p_last_4way_hs_fail &&
9066 wpa_s->p2p_last_4way_hs_fail == ssid) {
9067 u8 go_dev_addr[ETH_ALEN];
9068 struct wpa_ssid *persistent;
9069
9070 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9071 ssid->ssid,
9072 ssid->ssid_len) <= 0) {
9073 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9074 goto disconnect;
9075 }
9076
9077 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9078 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009079 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009080 ssid->ssid,
9081 ssid->ssid_len);
9082 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9083 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9084 goto disconnect;
9085 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009086 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009087 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9088 persistent->id);
9089 disconnect:
9090 wpa_s->p2p_last_4way_hs_fail = NULL;
9091 /*
9092 * Remove the group from a timeout to avoid issues with caller
9093 * continuing to use the interface if this is on a P2P group
9094 * interface.
9095 */
9096 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9097 wpa_s, NULL);
9098 return 1;
9099 }
9100
9101 wpa_s->p2p_last_4way_hs_fail = ssid;
9102 return 0;
9103}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009104
9105
9106#ifdef CONFIG_WPS_NFC
9107
9108static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9109 struct wpabuf *p2p)
9110{
9111 struct wpabuf *ret;
9112 size_t wsc_len;
9113
9114 if (p2p == NULL) {
9115 wpabuf_free(wsc);
9116 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9117 return NULL;
9118 }
9119
9120 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9121 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9122 if (ret == NULL) {
9123 wpabuf_free(wsc);
9124 wpabuf_free(p2p);
9125 return NULL;
9126 }
9127
9128 wpabuf_put_be16(ret, wsc_len);
9129 if (wsc)
9130 wpabuf_put_buf(ret, wsc);
9131 wpabuf_put_be16(ret, wpabuf_len(p2p));
9132 wpabuf_put_buf(ret, p2p);
9133
9134 wpabuf_free(wsc);
9135 wpabuf_free(p2p);
9136 wpa_hexdump_buf(MSG_DEBUG,
9137 "P2P: Generated NFC connection handover message", ret);
9138
9139 if (ndef && ret) {
9140 struct wpabuf *tmp;
9141 tmp = ndef_build_p2p(ret);
9142 wpabuf_free(ret);
9143 if (tmp == NULL) {
9144 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9145 return NULL;
9146 }
9147 ret = tmp;
9148 }
9149
9150 return ret;
9151}
9152
9153
9154static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9155 struct wpa_ssid **ssid, u8 *go_dev_addr)
9156{
9157 struct wpa_supplicant *iface;
9158
9159 if (go_dev_addr)
9160 os_memset(go_dev_addr, 0, ETH_ALEN);
9161 if (ssid)
9162 *ssid = NULL;
9163 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9164 if (iface->wpa_state < WPA_ASSOCIATING ||
9165 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9166 !iface->current_ssid->p2p_group ||
9167 iface->current_ssid->mode != WPAS_MODE_INFRA)
9168 continue;
9169 if (ssid)
9170 *ssid = iface->current_ssid;
9171 if (go_dev_addr)
9172 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9173 return iface->assoc_freq;
9174 }
9175 return 0;
9176}
9177
9178
9179struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9180 int ndef)
9181{
9182 struct wpabuf *wsc, *p2p;
9183 struct wpa_ssid *ssid;
9184 u8 go_dev_addr[ETH_ALEN];
9185 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9186
9187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9188 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9189 return NULL;
9190 }
9191
9192 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9193 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9194 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9195 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9196 return NULL;
9197 }
9198
9199 if (cli_freq == 0) {
9200 wsc = wps_build_nfc_handover_req_p2p(
9201 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9202 } else
9203 wsc = NULL;
9204 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9205 go_dev_addr, ssid ? ssid->ssid : NULL,
9206 ssid ? ssid->ssid_len : 0);
9207
9208 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9209}
9210
9211
9212struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9213 int ndef, int tag)
9214{
9215 struct wpabuf *wsc, *p2p;
9216 struct wpa_ssid *ssid;
9217 u8 go_dev_addr[ETH_ALEN];
9218 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9219
9220 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9221 return NULL;
9222
9223 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9224 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9225 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9226 return NULL;
9227
9228 if (cli_freq == 0) {
9229 wsc = wps_build_nfc_handover_sel_p2p(
9230 wpa_s->parent->wps,
9231 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9232 DEV_PW_NFC_CONNECTION_HANDOVER,
9233 wpa_s->conf->wps_nfc_dh_pubkey,
9234 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9235 } else
9236 wsc = NULL;
9237 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9238 go_dev_addr, ssid ? ssid->ssid : NULL,
9239 ssid ? ssid->ssid_len : 0);
9240
9241 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9242}
9243
9244
9245static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9246 struct p2p_nfc_params *params)
9247{
9248 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9249 "connection handover (freq=%d)",
9250 params->go_freq);
9251
9252 if (params->go_freq && params->go_ssid_len) {
9253 wpa_s->p2p_wps_method = WPS_NFC;
9254 wpa_s->pending_join_wps_method = WPS_NFC;
9255 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9256 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9257 ETH_ALEN);
9258 return wpas_p2p_join_start(wpa_s, params->go_freq,
9259 params->go_ssid,
9260 params->go_ssid_len);
9261 }
9262
9263 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9264 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009265 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009266 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009267 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009268 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009269 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009270}
9271
9272
9273static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9274 struct p2p_nfc_params *params, int tag)
9275{
9276 int res, persistent;
9277 struct wpa_ssid *ssid;
9278
9279 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9280 "connection handover");
9281 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9282 ssid = wpa_s->current_ssid;
9283 if (ssid == NULL)
9284 continue;
9285 if (ssid->mode != WPAS_MODE_P2P_GO)
9286 continue;
9287 if (wpa_s->ap_iface == NULL)
9288 continue;
9289 break;
9290 }
9291 if (wpa_s == NULL) {
9292 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9293 return -1;
9294 }
9295
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009296 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009297 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009298 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009299 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9300 return -1;
9301 }
9302 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009303 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9304 wpa_s->p2pdev->p2p_oob_dev_pw,
9305 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9306 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009307 if (res)
9308 return res;
9309
9310 if (!tag) {
9311 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9312 return 0;
9313 }
9314
9315 if (!params->peer ||
9316 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9317 return 0;
9318
9319 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9320 " to join", MAC2STR(params->peer->p2p_device_addr));
9321
9322 wpa_s->global->p2p_invite_group = wpa_s;
9323 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009324 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009325 params->peer->p2p_device_addr,
9326 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009327 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009328
9329 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9330 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9331 ssid->ssid, ssid->ssid_len, ssid->frequency,
9332 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009333 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009334}
9335
9336
9337static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9338 struct p2p_nfc_params *params,
9339 int forced_freq)
9340{
9341 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9342 "connection handover");
9343 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9344 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009345 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009346 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009347 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009348 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009349}
9350
9351
9352static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9353 struct p2p_nfc_params *params,
9354 int forced_freq)
9355{
9356 int res;
9357
9358 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9359 "connection handover");
9360 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9361 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009362 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009363 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009364 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009365 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009366 if (res)
9367 return res;
9368
9369 res = wpas_p2p_listen(wpa_s, 60);
9370 if (res) {
9371 p2p_unauthorize(wpa_s->global->p2p,
9372 params->peer->p2p_device_addr);
9373 }
9374
9375 return res;
9376}
9377
9378
9379static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9380 const struct wpabuf *data,
9381 int sel, int tag, int forced_freq)
9382{
9383 const u8 *pos, *end;
9384 u16 len, id;
9385 struct p2p_nfc_params params;
9386 int res;
9387
9388 os_memset(&params, 0, sizeof(params));
9389 params.sel = sel;
9390
9391 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9392
9393 pos = wpabuf_head(data);
9394 end = pos + wpabuf_len(data);
9395
9396 if (end - pos < 2) {
9397 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9398 "attributes");
9399 return -1;
9400 }
9401 len = WPA_GET_BE16(pos);
9402 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009403 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009404 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9405 "attributes");
9406 return -1;
9407 }
9408 params.wsc_attr = pos;
9409 params.wsc_len = len;
9410 pos += len;
9411
9412 if (end - pos < 2) {
9413 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9414 "attributes");
9415 return -1;
9416 }
9417 len = WPA_GET_BE16(pos);
9418 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009419 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009420 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9421 "attributes");
9422 return -1;
9423 }
9424 params.p2p_attr = pos;
9425 params.p2p_len = len;
9426 pos += len;
9427
9428 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9429 params.wsc_attr, params.wsc_len);
9430 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9431 params.p2p_attr, params.p2p_len);
9432 if (pos < end) {
9433 wpa_hexdump(MSG_DEBUG,
9434 "P2P: Ignored extra data after P2P attributes",
9435 pos, end - pos);
9436 }
9437
9438 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9439 if (res)
9440 return res;
9441
9442 if (params.next_step == NO_ACTION)
9443 return 0;
9444
9445 if (params.next_step == BOTH_GO) {
9446 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9447 MAC2STR(params.peer->p2p_device_addr));
9448 return 0;
9449 }
9450
9451 if (params.next_step == PEER_CLIENT) {
9452 if (!is_zero_ether_addr(params.go_dev_addr)) {
9453 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9454 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9455 " ssid=\"%s\"",
9456 MAC2STR(params.peer->p2p_device_addr),
9457 params.go_freq,
9458 MAC2STR(params.go_dev_addr),
9459 wpa_ssid_txt(params.go_ssid,
9460 params.go_ssid_len));
9461 } else {
9462 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9463 "peer=" MACSTR " freq=%d",
9464 MAC2STR(params.peer->p2p_device_addr),
9465 params.go_freq);
9466 }
9467 return 0;
9468 }
9469
9470 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9471 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9472 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9473 return 0;
9474 }
9475
9476 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9477 wpa_s->p2p_oob_dev_pw = NULL;
9478
9479 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9480 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9481 "received");
9482 return -1;
9483 }
9484
9485 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9486 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9487 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9488 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9489 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9490 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9491 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9492
9493 if (tag) {
9494 if (id < 0x10) {
9495 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9496 "peer OOB Device Password Id %u", id);
9497 return -1;
9498 }
9499 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9500 "Device Password Id %u", id);
9501 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9502 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9503 params.oob_dev_pw_len -
9504 WPS_OOB_PUBKEY_HASH_LEN - 2);
9505 wpa_s->p2p_oob_dev_pw_id = id;
9506 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9507 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9508 params.oob_dev_pw_len -
9509 WPS_OOB_PUBKEY_HASH_LEN - 2);
9510 if (wpa_s->p2p_oob_dev_pw == NULL)
9511 return -1;
9512
9513 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9514 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9515 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9516 return -1;
9517 } else {
9518 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9519 "without Device Password");
9520 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9521 }
9522
9523 switch (params.next_step) {
9524 case NO_ACTION:
9525 case BOTH_GO:
9526 case PEER_CLIENT:
9527 /* already covered above */
9528 return 0;
9529 case JOIN_GROUP:
9530 return wpas_p2p_nfc_join_group(wpa_s, &params);
9531 case AUTH_JOIN:
9532 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9533 case INIT_GO_NEG:
9534 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9535 case RESP_GO_NEG:
9536 /* TODO: use own OOB Dev Pw */
9537 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9538 }
9539
9540 return -1;
9541}
9542
9543
9544int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9545 const struct wpabuf *data, int forced_freq)
9546{
9547 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9548 return -1;
9549
9550 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9551}
9552
9553
9554int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9555 const struct wpabuf *req,
9556 const struct wpabuf *sel, int forced_freq)
9557{
9558 struct wpabuf *tmp;
9559 int ret;
9560
9561 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9562 return -1;
9563
9564 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9565
9566 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9567 wpabuf_head(req), wpabuf_len(req));
9568 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9569 wpabuf_head(sel), wpabuf_len(sel));
9570 if (forced_freq)
9571 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9572 tmp = ndef_parse_p2p(init ? sel : req);
9573 if (tmp == NULL) {
9574 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9575 return -1;
9576 }
9577
9578 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9579 forced_freq);
9580 wpabuf_free(tmp);
9581
9582 return ret;
9583}
9584
9585
9586int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9587{
9588 const u8 *if_addr;
9589 int go_intent = wpa_s->conf->p2p_go_intent;
9590 struct wpa_supplicant *iface;
9591
9592 if (wpa_s->global->p2p == NULL)
9593 return -1;
9594
9595 if (!enabled) {
9596 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9597 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9598 {
9599 if (!iface->ap_iface)
9600 continue;
9601 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9602 }
9603 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9604 0, NULL);
9605 if (wpa_s->p2p_nfc_tag_enabled)
9606 wpas_p2p_remove_pending_group_interface(wpa_s);
9607 wpa_s->p2p_nfc_tag_enabled = 0;
9608 return 0;
9609 }
9610
9611 if (wpa_s->global->p2p_disabled)
9612 return -1;
9613
9614 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9615 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9616 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9617 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9618 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9619 "to allow static handover cases");
9620 return -1;
9621 }
9622
9623 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9624
9625 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9626 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9627 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9628 if (wpa_s->p2p_oob_dev_pw == NULL)
9629 return -1;
9630 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9631
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009632 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9633 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9634 /*
9635 * P2P Group Interface present and the command came on group
9636 * interface, so enable the token for the current interface.
9637 */
9638 wpa_s->create_p2p_iface = 0;
9639 } else {
9640 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9641 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009642
9643 if (wpa_s->create_p2p_iface) {
9644 enum wpa_driver_if_type iftype;
9645 /* Prepare to add a new interface for the group */
9646 iftype = WPA_IF_P2P_GROUP;
9647 if (go_intent == 15)
9648 iftype = WPA_IF_P2P_GO;
9649 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9650 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9651 "interface for the group");
9652 return -1;
9653 }
9654
9655 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009656 } else if (wpa_s->p2p_mgmt)
9657 if_addr = wpa_s->parent->own_addr;
9658 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009659 if_addr = wpa_s->own_addr;
9660
9661 wpa_s->p2p_nfc_tag_enabled = enabled;
9662
9663 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9664 struct hostapd_data *hapd;
9665 if (iface->ap_iface == NULL)
9666 continue;
9667 hapd = iface->ap_iface->bss[0];
9668 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9669 hapd->conf->wps_nfc_dh_pubkey =
9670 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9671 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9672 hapd->conf->wps_nfc_dh_privkey =
9673 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9674 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9675 hapd->conf->wps_nfc_dev_pw =
9676 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9677 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9678
9679 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9680 wpa_dbg(iface, MSG_DEBUG,
9681 "P2P: Failed to enable NFC Tag for GO");
9682 }
9683 }
9684 p2p_set_authorized_oob_dev_pw_id(
9685 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9686 if_addr);
9687
9688 return 0;
9689}
9690
9691#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009692
9693
9694static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9695 struct wpa_used_freq_data *freqs,
9696 unsigned int num)
9697{
9698 u8 curr_chan, cand, chan;
9699 unsigned int i;
9700
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009701 /*
9702 * If possible, optimize the Listen channel to be a channel that is
9703 * already used by one of the other interfaces.
9704 */
9705 if (!wpa_s->conf->p2p_optimize_listen_chan)
9706 return;
9707
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009708 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9709 for (i = 0, cand = 0; i < num; i++) {
9710 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9711 if (curr_chan == chan) {
9712 cand = 0;
9713 break;
9714 }
9715
9716 if (chan == 1 || chan == 6 || chan == 11)
9717 cand = chan;
9718 }
9719
9720 if (cand) {
9721 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009722 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009723 cand);
9724 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9725 }
9726}
9727
9728
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009729static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009730{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009731 struct hostapd_config *conf;
9732 struct p2p_go_neg_results params;
9733 struct csa_settings csa_settings;
9734 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9735 int old_freq = current_ssid->frequency;
9736 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009737
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009738 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9739 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9740 return -1;
9741 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009742
9743 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009744 * TODO: This function may not always work correctly. For example,
9745 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009746 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009747 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9748 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009749 wpa_dbg(wpa_s, MSG_DEBUG,
9750 "P2P CSA: Failed to select new frequency for GO");
9751 return -1;
9752 }
9753
9754 if (current_ssid->frequency == params.freq) {
9755 wpa_dbg(wpa_s, MSG_DEBUG,
9756 "P2P CSA: Selected same frequency - not moving GO");
9757 return 0;
9758 }
9759
9760 conf = hostapd_config_defaults();
9761 if (!conf) {
9762 wpa_dbg(wpa_s, MSG_DEBUG,
9763 "P2P CSA: Failed to allocate default config");
9764 return -1;
9765 }
9766
9767 current_ssid->frequency = params.freq;
9768 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9769 wpa_dbg(wpa_s, MSG_DEBUG,
9770 "P2P CSA: Failed to create new GO config");
9771 ret = -1;
9772 goto out;
9773 }
9774
Sunil4a3f9f52022-07-19 22:04:39 +00009775 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9776 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009777 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009778 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9779 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009780 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9781 wpa_s->ap_iface->current_mode->mode,
9782 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9783 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009784 ret = -1;
9785 goto out;
9786 }
9787
9788 os_memset(&csa_settings, 0, sizeof(csa_settings));
9789 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9790 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9791 csa_settings.freq_params.freq = params.freq;
9792 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9793 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9794 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9795
9796 if (conf->ieee80211ac) {
9797 int freq1 = 0, freq2 = 0;
9798 u8 chan, opclass;
9799
9800 if (ieee80211_freq_to_channel_ext(params.freq,
9801 conf->secondary_channel,
9802 conf->vht_oper_chwidth,
9803 &opclass, &chan) ==
9804 NUM_HOSTAPD_MODES) {
9805 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9806 ret = -1;
9807 goto out;
9808 }
9809
9810 if (conf->vht_oper_centr_freq_seg0_idx)
9811 freq1 = ieee80211_chan_to_freq(
9812 NULL, opclass,
9813 conf->vht_oper_centr_freq_seg0_idx);
9814
9815 if (conf->vht_oper_centr_freq_seg1_idx)
9816 freq2 = ieee80211_chan_to_freq(
9817 NULL, opclass,
9818 conf->vht_oper_centr_freq_seg1_idx);
9819
9820 if (freq1 < 0 || freq2 < 0) {
9821 wpa_dbg(wpa_s, MSG_DEBUG,
9822 "P2P CSA: Selected invalid VHT center freqs");
9823 ret = -1;
9824 goto out;
9825 }
9826
9827 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9828 csa_settings.freq_params.center_freq1 = freq1;
9829 csa_settings.freq_params.center_freq2 = freq2;
9830
9831 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009832 case CONF_OPER_CHWIDTH_80MHZ:
9833 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009834 csa_settings.freq_params.bandwidth = 80;
9835 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009836 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009837 csa_settings.freq_params.bandwidth = 160;
9838 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009839 default:
9840 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009841 }
9842 }
9843
9844 ret = ap_switch_channel(wpa_s, &csa_settings);
9845out:
9846 current_ssid->frequency = old_freq;
9847 hostapd_config_free(conf);
9848 return ret;
9849}
9850
9851
9852static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9853{
9854 struct p2p_go_neg_results params;
9855 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009856 void (*ap_configured_cb)(void *ctx, void *data);
9857 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009858
9859 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9860
9861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9862 current_ssid->frequency);
9863
9864 /* Stop the AP functionality */
9865 /* TODO: Should do this in a way that does not indicated to possible
9866 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009867 /* If this action occurs before a group is started, the callback should
9868 * be preserved, or GROUP-STARTED event would be lost. If this action
9869 * occurs after a group is started, these pointers are all NULL and
9870 * harmless. */
9871 ap_configured_cb = wpa_s->ap_configured_cb;
9872 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9873 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009874 wpa_supplicant_ap_deinit(wpa_s);
9875
9876 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009877 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9878 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009879 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9880 wpas_p2p_group_delete(wpa_s,
9881 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9882 return;
9883 }
9884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9885 params.freq);
9886
9887 if (params.freq &&
9888 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9889 wpa_printf(MSG_DEBUG,
9890 "P2P: Selected freq (%u MHz) is not valid for P2P",
9891 params.freq);
9892 wpas_p2p_group_delete(wpa_s,
9893 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9894 return;
9895 }
9896
Hai Shalom899fcc72020-10-19 14:38:18 -07009897 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009898 wpa_s->ap_configured_cb = ap_configured_cb;
9899 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9900 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009901
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009902 /* Update the frequency */
9903 current_ssid->frequency = params.freq;
9904 wpa_s->connect_without_scan = current_ssid;
9905 wpa_s->reassociate = 1;
9906 wpa_s->disconnected = 0;
9907 wpa_supplicant_req_scan(wpa_s, 0, 0);
9908}
9909
9910
9911static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9912{
9913 struct wpa_supplicant *wpa_s = eloop_ctx;
9914
9915 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009916 return;
9917
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009918 wpas_p2p_go_update_common_freqs(wpa_s);
9919
9920 /* Do not move GO in the middle of a CSA */
9921 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9922 wpa_printf(MSG_DEBUG,
9923 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009924 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009925 }
9926
9927 /*
9928 * First, try a channel switch flow. If it is not supported or fails,
9929 * take down the GO and bring it up again.
9930 */
9931 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9932 wpas_p2p_move_go_no_csa(wpa_s);
9933}
9934
9935
9936static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9937{
9938 struct wpa_supplicant *wpa_s = eloop_ctx;
9939 struct wpa_used_freq_data *freqs = NULL;
9940 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009941
9942 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9943 if (!freqs)
9944 return;
9945
9946 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9947
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009948 /* Previous attempt to move a GO was not possible -- try again. */
9949 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9950 WPAS_P2P_CHANNEL_UPDATE_ANY);
9951
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009952 os_free(freqs);
9953}
9954
9955
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009956/*
9957 * Consider moving a GO from its currently used frequency:
9958 * 1. It is possible that due to regulatory consideration the frequency
9959 * can no longer be used and there is a need to evacuate the GO.
9960 * 2. It is possible that due to MCC considerations, it would be preferable
9961 * to move the GO to a channel that is currently used by some other
9962 * station interface.
9963 *
9964 * In case a frequency that became invalid is once again valid, cancel a
9965 * previously initiated GO frequency change.
9966 */
9967static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9968 struct wpa_used_freq_data *freqs,
9969 unsigned int num)
9970{
9971 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9972 unsigned int timeout;
9973 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009974 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009975
9976 wpas_p2p_go_update_common_freqs(wpa_s);
9977
9978 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009979 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009980 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009981 for (i = 0, invalid_freq = 0; i < num; i++) {
9982 if (freqs[i].freq == freq) {
9983 flags = freqs[i].flags;
9984
9985 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009986 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9987 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009988 wpa_dbg(wpa_s, MSG_DEBUG,
9989 "P2P: Freq=%d MHz no longer valid for GO",
9990 freq);
9991 invalid_freq = 1;
9992 }
9993 } else if (freqs[i].flags == 0) {
9994 /* Freq is not used by any other station interface */
9995 continue;
9996 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009997 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009998 /* Freq is not valid for P2P use cases */
9999 continue;
10000 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10001 P2P_GO_FREQ_MOVE_SCM) {
10002 policy_move = 1;
10003 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10004 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10005 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10006 policy_move = 1;
10007 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10008 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10009 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10010 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10011 policy_move = 1;
10012 } else if ((wpa_s->drv_flags &
10013 WPA_DRIVER_FLAGS_AP_CSA) &&
10014 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10015 u8 chan;
10016
10017 /*
10018 * We do not support CSA between bands, so move
10019 * GO only within the same band.
10020 */
10021 if (wpa_s->ap_iface->current_mode->mode ==
10022 ieee80211_freq_to_chan(freqs[i].freq,
10023 &chan))
10024 policy_move = 1;
10025 }
10026 }
10027 }
10028
10029 wpa_dbg(wpa_s, MSG_DEBUG,
10030 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10031 invalid_freq, policy_move, flags);
10032
10033 /*
10034 * The channel is valid, or we are going to have a policy move, so
10035 * cancel timeout.
10036 */
10037 if (!invalid_freq || policy_move) {
10038 wpa_dbg(wpa_s, MSG_DEBUG,
10039 "P2P: Cancel a GO move from freq=%d MHz", freq);
10040 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10041
10042 if (wpas_p2p_in_progress(wpa_s)) {
10043 wpa_dbg(wpa_s, MSG_DEBUG,
10044 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10045 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10046 wpa_s, NULL);
10047 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10048 wpas_p2p_reconsider_moving_go,
10049 wpa_s, NULL);
10050 return;
10051 }
10052 }
10053
10054 if (!invalid_freq && (!policy_move || flags != 0)) {
10055 wpa_dbg(wpa_s, MSG_DEBUG,
10056 "P2P: Not initiating a GO frequency change");
10057 return;
10058 }
10059
10060 /*
10061 * Do not consider moving GO if it is in the middle of a CSA. When the
10062 * CSA is finished this flow should be retriggered.
10063 */
10064 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10065 wpa_dbg(wpa_s, MSG_DEBUG,
10066 "P2P: Not initiating a GO frequency change - CSA is in progress");
10067 return;
10068 }
10069
10070 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10071 timeout = P2P_GO_FREQ_CHANGE_TIME;
10072 else
10073 timeout = 0;
10074
10075 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10076 freq, timeout);
10077 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10078 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10079}
10080
10081
10082static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10083 struct wpa_used_freq_data *freqs,
10084 unsigned int num,
10085 enum wpas_p2p_channel_update_trig trig)
10086{
10087 struct wpa_supplicant *ifs;
10088
10089 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10090 NULL);
10091
10092 /*
10093 * Travers all the radio interfaces, and for each GO interface, check
10094 * if there is a need to move the GO from the frequency it is using,
10095 * or in case the frequency is valid again, cancel the evacuation flow.
10096 */
10097 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10098 radio_list) {
10099 if (ifs->current_ssid == NULL ||
10100 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10101 continue;
10102
10103 /*
10104 * The GO was just started or completed channel switch, no need
10105 * to move it.
10106 */
10107 if (wpa_s == ifs &&
10108 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10109 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10110 wpa_dbg(wpa_s, MSG_DEBUG,
10111 "P2P: GO move - schedule re-consideration");
10112 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10113 wpas_p2p_reconsider_moving_go,
10114 wpa_s, NULL);
10115 continue;
10116 }
10117
10118 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10119 }
10120}
10121
10122
10123void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10124{
10125 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10126 return;
10127
10128 wpas_p2p_update_channel_list(wpa_s,
10129 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10130}
10131
10132
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010133void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10134{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010135 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10136 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10137 "the management interface is being removed");
10138 wpas_p2p_deinit_global(wpa_s->global);
10139 }
10140}
10141
10142
10143void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10144{
10145 if (wpa_s->ap_iface->bss)
10146 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10147 wpas_p2p_group_deinit(wpa_s);
10148}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010149
10150
10151int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10152 unsigned int period, unsigned int interval,
10153 unsigned int count)
10154{
10155 struct p2p_data *p2p = wpa_s->global->p2p;
10156 u8 *device_types;
10157 size_t dev_types_len;
10158 struct wpabuf *buf;
10159 int ret;
10160
10161 if (wpa_s->p2p_lo_started) {
10162 wpa_dbg(wpa_s, MSG_DEBUG,
10163 "P2P Listen offload is already started");
10164 return 0;
10165 }
10166
10167 if (wpa_s->global->p2p == NULL ||
10168 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10169 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10170 return -1;
10171 }
10172
10173 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10174 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10175 freq);
10176 return -1;
10177 }
10178
10179 /* Get device type */
10180 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10181 WPS_DEV_TYPE_LEN;
10182 device_types = os_malloc(dev_types_len);
10183 if (!device_types)
10184 return -1;
10185 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10186 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10187 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10188
10189 /* Get Probe Response IE(s) */
10190 buf = p2p_build_probe_resp_template(p2p, freq);
10191 if (!buf) {
10192 os_free(device_types);
10193 return -1;
10194 }
10195
10196 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10197 device_types, dev_types_len,
10198 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10199 if (ret < 0)
10200 wpa_dbg(wpa_s, MSG_DEBUG,
10201 "P2P: Failed to start P2P listen offload");
10202
10203 os_free(device_types);
10204 wpabuf_free(buf);
10205
10206 if (ret == 0) {
10207 wpa_s->p2p_lo_started = 1;
10208
10209 /* Stop current P2P listen if any */
10210 wpas_stop_listen(wpa_s);
10211 }
10212
10213 return ret;
10214}
10215
10216
10217int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10218{
10219 int ret;
10220
10221 if (!wpa_s->p2p_lo_started)
10222 return 0;
10223
10224 ret = wpa_drv_p2p_lo_stop(wpa_s);
10225 if (ret < 0)
10226 wpa_dbg(wpa_s, MSG_DEBUG,
10227 "P2P: Failed to stop P2P listen offload");
10228
10229 wpa_s->p2p_lo_started = 0;
10230 return ret;
10231}