blob: 45c8744cc93bed7869166528b63f7ffe0abacb5b [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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107enum p2p_group_removal_reason {
108 P2P_GROUP_REMOVAL_UNKNOWN,
109 P2P_GROUP_REMOVAL_SILENT,
110 P2P_GROUP_REMOVAL_FORMATION_FAILED,
111 P2P_GROUP_REMOVAL_REQUESTED,
112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
113 P2P_GROUP_REMOVAL_UNAVAILABLE,
114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800115 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118};
119
Jouni Malinendc7b7132012-09-14 12:53:47 -0700120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122static struct wpa_supplicant *
123wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
124 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800125static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800127static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
128 int *force_freq, int *pref_freq, int go,
129 unsigned int *pref_freq_list,
130 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800131static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800142static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
143 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800144static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800145static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
146 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800147static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800148static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700149static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700150static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800151static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700153static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800155static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
157 unsigned int num);
158static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
160static void
161wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *freqs, unsigned int num,
163 enum wpas_p2p_channel_update_trig trig);
164static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
166
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167/*
168 * Get the number of concurrent channels that the HW can operate, but that are
169 * currently not in use by any of the wpa_supplicant interfaces.
170 */
171static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
172{
173 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800174 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
177 if (!freqs)
178 return -1;
179
180 num = get_shared_radio_freqs(wpa_s, freqs,
181 wpa_s->num_multichan_concurrent);
182 os_free(freqs);
183
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
186 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700187}
188
189
190/*
191 * Get the frequencies that are currently in use by one or more of the virtual
192 * interfaces, and that are also valid for P2P operation.
193 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194static unsigned int
195wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
197 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700199 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700200 unsigned int num, i, j;
201
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700204 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700211
212 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 p2p_freqs[j++] = freqs[i];
215 }
216
217 os_free(freqs);
218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800220
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700221 return j;
222}
223
224
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700225static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
226 int freq)
227{
228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
229 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700230
231 /* Use the wpa_s used to control the P2P Device operation */
232 wpa_s = wpa_s->global->p2p_init_wpa_s;
233
234 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800235 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
236 wpas_p2p_num_unused_channels(wpa_s) > 0) {
237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
238 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800240 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
242}
243
244
Hai Shalom60840252021-02-19 19:02:11 -0800245static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
246{
247 unsigned int delay = wpas_p2p_search_delay(wpa_s);
248
249 /* In case of concurrent P2P and external scans, delay P2P search. */
250 if (external_scan_running(wpa_s->radio)) {
251 delay = wpa_s->conf->p2p_search_delay;
252 wpa_printf(MSG_DEBUG,
253 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
254 delay);
255 }
256
257 p2p_scan_res_handled(wpa_s->global->p2p, delay);
258}
259
260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
262 struct wpa_scan_results *scan_res)
263{
264 size_t i;
265
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800266 if (wpa_s->p2p_scan_work) {
267 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
268 wpa_s->p2p_scan_work = NULL;
269 radio_work_done(work);
270 }
271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
273 return;
274
275 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
276 (int) scan_res->num);
277
278 for (i = 0; i < scan_res->num; i++) {
279 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800280 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700281 const u8 *ies;
282 size_t ies_len;
283
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800284 time_tmp_age.sec = bss->age / 1000;
285 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700287
288 ies = (const u8 *) (bss + 1);
289 ies_len = bss->ie_len;
290 if (bss->beacon_ie_len > 0 &&
291 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
292 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
293 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
294 MACSTR, MAC2STR(bss->bssid));
295 ies = ies + ies_len;
296 ies_len = bss->beacon_ie_len;
297 }
298
299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800301 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700302 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 break;
304 }
305
Hai Shalom60840252021-02-19 19:02:11 -0800306 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307}
308
309
Hai Shalom60840252021-02-19 19:02:11 -0800310static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700311{
Hai Shalom60840252021-02-19 19:02:11 -0800312 if (wpa_s->p2p_scan_work) {
313 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700314
Hai Shalom60840252021-02-19 19:02:11 -0800315 wpa_s->p2p_scan_work = NULL;
316 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700317 }
318
Hai Shalom60840252021-02-19 19:02:11 -0800319 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
320 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700321
Hai Shalom60840252021-02-19 19:02:11 -0800322 wpa_dbg(wpa_s, MSG_DEBUG,
323 "P2P: Failed to get scan results - try to continue");
324 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700325}
326
327
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800328static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
329{
330 struct wpa_supplicant *wpa_s = work->wpa_s;
331 struct wpa_driver_scan_params *params = work->ctx;
332 int ret;
333
334 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800335 if (!work->started) {
336 wpa_scan_free_params(params);
337 return;
338 }
339
340 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800341 return;
342 }
343
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800344 if (wpa_s->clear_driver_scan_cache) {
345 wpa_printf(MSG_DEBUG,
346 "Request driver to clear scan cache due to local BSS flush");
347 params->only_new_results = 1;
348 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700349
350 if (wpa_s->conf->p2p_6ghz_disable && !params->freqs) {
351 wpa_printf(MSG_DEBUG,
352 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -0800353 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
354 0);
355 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
356 0);
Hai Shalom899fcc72020-10-19 14:38:18 -0700357 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800358 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800359 if (ret == 0)
360 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361 wpa_scan_free_params(params);
362 work->ctx = NULL;
363 if (ret) {
364 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800365 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800366 return;
367 }
368
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800369 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800370 os_get_reltime(&wpa_s->scan_trigger_time);
371 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800372 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800373 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800374 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 wpa_s->p2p_scan_work = work;
376}
377
378
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800379static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
380 int freq)
381{
382 if (wpa_s->global->p2p_24ghz_social_channels &&
383 (freq == 2412 || freq == 2437 || freq == 2462)) {
384 /*
385 * Search all social channels regardless of whether these have
386 * been disabled for P2P operating channel use to avoid missing
387 * peers.
388 */
389 return 1;
390 }
391 return p2p_supported_freq(wpa_s->global->p2p, freq);
392}
393
394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
396 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700397 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398{
399 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800400 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700402 unsigned int num_channels = 0;
403 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800404 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700405 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800406 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407
408 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
409 return -1;
410
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800411 if (wpa_s->p2p_scan_work) {
412 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
413 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700414 }
415
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800416 params = os_zalloc(sizeof(*params));
417 if (params == NULL)
418 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419
420 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800421 params->num_ssids = 1;
422 n = os_malloc(P2P_WILDCARD_SSID_LEN);
423 if (n == NULL)
424 goto fail;
425 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
426 params->ssids[0].ssid = n;
427 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428
429 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700430 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
431 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 num_req_dev_types, req_dev_types);
433 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800434 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 switch (type) {
437 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700438 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
439 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800440 if (params->freqs == NULL)
441 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700442 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800443 if (wpas_p2p_search_social_channel(
444 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700445 params->freqs[num_channels++] =
446 social_channels_freq[i];
447 }
448 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449 break;
450 case P2P_SCAN_FULL:
451 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800452 case P2P_SCAN_SPECIFIC:
453 params->freqs = os_calloc(2, sizeof(int));
454 if (params->freqs == NULL)
455 goto fail;
456 params->freqs[0] = freq;
457 params->freqs[1] = 0;
458 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700460 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
461 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800462 if (params->freqs == NULL)
463 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700464 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800465 if (wpas_p2p_search_social_channel(
466 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700467 params->freqs[num_channels++] =
468 social_channels_freq[i];
469 }
470 if (p2p_supported_freq(wpa_s->global->p2p, freq))
471 params->freqs[num_channels++] = freq;
472 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 break;
474 }
475
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800476 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
477 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
478 if (ies == NULL) {
479 wpabuf_free(wps_ie);
480 goto fail;
481 }
482 wpabuf_put_buf(ies, wps_ie);
483 wpabuf_free(wps_ie);
484
485 bands = wpas_get_bands(wpa_s, params->freqs);
486 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
487
488 params->p2p_probe = 1;
489 n = os_malloc(wpabuf_len(ies));
490 if (n == NULL) {
491 wpabuf_free(ies);
492 goto fail;
493 }
494 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
495 params->extra_ies = n;
496 params->extra_ies_len = wpabuf_len(ies);
497 wpabuf_free(ies);
498
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800499 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800500 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
501 params) < 0)
502 goto fail;
503 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800505fail:
506 wpa_scan_free_params(params);
507 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508}
509
510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
512{
513 switch (p2p_group_interface) {
514 case P2P_GROUP_INTERFACE_PENDING:
515 return WPA_IF_P2P_GROUP;
516 case P2P_GROUP_INTERFACE_GO:
517 return WPA_IF_P2P_GO;
518 case P2P_GROUP_INTERFACE_CLIENT:
519 return WPA_IF_P2P_CLIENT;
520 }
521
522 return WPA_IF_P2P_GROUP;
523}
524
525
526static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
527 const u8 *ssid,
528 size_t ssid_len, int *go)
529{
530 struct wpa_ssid *s;
531
532 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
533 for (s = wpa_s->conf->ssid; s; s = s->next) {
534 if (s->disabled != 0 || !s->p2p_group ||
535 s->ssid_len != ssid_len ||
536 os_memcmp(ssid, s->ssid, ssid_len) != 0)
537 continue;
538 if (s->mode == WPAS_MODE_P2P_GO &&
539 s != wpa_s->current_ssid)
540 continue;
541 if (go)
542 *go = s->mode == WPAS_MODE_P2P_GO;
543 return wpa_s;
544 }
545 }
546
547 return NULL;
548}
549
550
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800551static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
552{
553 struct wpa_supplicant *wpa_s = eloop_ctx;
554 wpa_printf(MSG_DEBUG,
555 "P2P: Complete previously requested removal of %s",
556 wpa_s->ifname);
557 wpas_p2p_disconnect(wpa_s);
558}
559
560
561static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
562 struct wpa_supplicant *calling_wpa_s)
563{
564 if (calling_wpa_s == wpa_s && wpa_s &&
565 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
566 /*
567 * The calling wpa_s instance is going to be removed. Do that
568 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700569 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800570 * control interface responses on the per-interface socket.
571 */
572 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
573 wpa_s, NULL) < 0)
574 return -1;
575 return 0;
576 }
577
578 return wpas_p2p_disconnect(wpa_s);
579}
580
581
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800582/* Determine total number of clients in active groups where we are the GO */
583static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
584{
585 unsigned int count = 0;
586 struct wpa_ssid *s;
587
588 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
589 for (s = wpa_s->conf->ssid; s; s = s->next) {
590 wpa_printf(MSG_DEBUG,
591 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
592 wpa_s, s, s->disabled, s->p2p_group,
593 s->mode);
594 if (!s->disabled && s->p2p_group &&
595 s->mode == WPAS_MODE_P2P_GO) {
596 count += p2p_get_group_num_members(
597 wpa_s->p2p_group);
598 }
599 }
600 }
601
602 return count;
603}
604
605
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800606static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
607{
608 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
609 !wpa_s->current_ssid->disabled &&
610 wpa_s->current_ssid->p2p_group &&
611 wpa_s->current_ssid->p2p_persistent_group;
612}
613
614
615static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
616{
617 return p2p_is_active_persistent_group(wpa_s) &&
618 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
619}
620
621
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800622/* Find an interface for a P2P group where we are the GO */
623static struct wpa_supplicant *
624wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
625{
626 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800627
628 if (!wpa_s)
629 return NULL;
630
631 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800632 if (!p2p_is_active_persistent_go(wpa_s))
633 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800634
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 /* Prefer a group with connected clients */
636 if (p2p_get_group_num_members(wpa_s->p2p_group))
637 return wpa_s;
638 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800639 }
640
641 /* No group with connected clients, so pick the one without (if any) */
642 return save;
643}
644
645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800646static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800647{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 return p2p_is_active_persistent_group(wpa_s) &&
649 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
650}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800651
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800652
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800653/* Find an interface for a P2P group where we are the P2P Client */
654static struct wpa_supplicant *
655wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
656{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800657 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800658 if (p2p_is_active_persistent_cli(wpa_s))
659 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660 }
661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800662 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800663}
664
665
666/* Find a persistent group where we are the GO */
667static struct wpa_ssid *
668wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
669{
670 struct wpa_ssid *s;
671
672 for (s = wpa_s->conf->ssid; s; s = s->next) {
673 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
674 return s;
675 }
676
677 return NULL;
678}
679
680
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800681static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
682 unsigned int *force_freq,
683 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800684{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800685 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800686 struct wpa_ssid *s;
687 u8 conncap = P2PS_SETUP_NONE;
688 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800689 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800690 struct wpa_ssid *persistent_go;
691 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800692 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800693
694 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
695
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800696 if (force_freq)
697 *force_freq = 0;
698 if (pref_freq)
699 *pref_freq = 0;
700
701 size = P2P_MAX_PREF_CHANNELS;
702 if (force_freq && pref_freq &&
703 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
704 (int *) pref_freq, 0, pref_freq_list, &size))
705 wpas_p2p_set_own_freq_preference(wpa_s,
706 *force_freq ? *force_freq :
707 *pref_freq);
708
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800709 /*
710 * For non-concurrent capable devices:
711 * If persistent_go, then no new.
712 * If GO, then no client.
713 * If client, then no GO.
714 */
715 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800716 if (go_wpa_s)
717 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800718 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
720 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800721
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800722 wpa_printf(MSG_DEBUG,
723 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
724 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800725
726 /* If not concurrent, restrict our choices */
727 if (p2p_no_group_iface) {
728 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
729
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800730 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800731 return P2PS_SETUP_NONE;
732
733 if (go_wpa_s) {
734 if (role == P2PS_SETUP_CLIENT ||
735 incoming == P2PS_SETUP_GROUP_OWNER ||
736 p2p_client_limit_reached(go_wpa_s->p2p_group))
737 return P2PS_SETUP_NONE;
738
739 return P2PS_SETUP_GROUP_OWNER;
740 }
741
742 if (persistent_go) {
743 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
744 if (!incoming)
745 return P2PS_SETUP_GROUP_OWNER |
746 P2PS_SETUP_CLIENT;
747 if (incoming == P2PS_SETUP_NEW) {
748 u8 r;
749
750 if (os_get_random(&r, sizeof(r)) < 0 ||
751 (r & 1))
752 return P2PS_SETUP_CLIENT;
753 return P2PS_SETUP_GROUP_OWNER;
754 }
755 }
756 }
757 }
758
759 /* If a required role has been specified, handle it here */
760 if (role && role != P2PS_SETUP_NEW) {
761 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800762 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
763 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
764 /*
765 * Peer has an active GO, so if the role allows it and
766 * we do not have any active roles, become client.
767 */
768 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
769 !cli_wpa_s)
770 return P2PS_SETUP_CLIENT;
771
772 /* fall through */
773
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800774 case P2PS_SETUP_NONE:
775 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800776 conncap = role;
777 goto grp_owner;
778
779 case P2PS_SETUP_GROUP_OWNER:
780 /*
781 * Must be a complimentary role - cannot be a client to
782 * more than one peer.
783 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800784 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800785 return P2PS_SETUP_NONE;
786
787 return P2PS_SETUP_CLIENT;
788
789 case P2PS_SETUP_CLIENT:
790 /* Must be a complimentary role */
791 if (incoming != role) {
792 conncap = P2PS_SETUP_GROUP_OWNER;
793 goto grp_owner;
794 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700795 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800796
797 default:
798 return P2PS_SETUP_NONE;
799 }
800 }
801
802 /*
803 * For now, we only will support ownership of one group, and being a
804 * client of one group. Therefore, if we have either an existing GO
805 * group, or an existing client group, we will not do a new GO
806 * negotiation, but rather try to re-use the existing groups.
807 */
808 switch (incoming) {
809 case P2PS_SETUP_NONE:
810 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800811 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800812 conncap = P2PS_SETUP_GROUP_OWNER;
813 else if (!owned_members)
814 conncap = P2PS_SETUP_NEW;
815 else if (incoming == P2PS_SETUP_NONE)
816 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
817 else
818 conncap = P2PS_SETUP_CLIENT;
819 break;
820
821 case P2PS_SETUP_CLIENT:
822 conncap = P2PS_SETUP_GROUP_OWNER;
823 break;
824
825 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800826 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800827 conncap = P2PS_SETUP_CLIENT;
828 break;
829
830 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
831 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800832 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800833 conncap = P2PS_SETUP_GROUP_OWNER;
834 else {
835 u8 r;
836
837 if (os_get_random(&r, sizeof(r)) < 0 ||
838 (r & 1))
839 conncap = P2PS_SETUP_CLIENT;
840 else
841 conncap = P2PS_SETUP_GROUP_OWNER;
842 }
843 break;
844
845 default:
846 return P2PS_SETUP_NONE;
847 }
848
849grp_owner:
850 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
851 (!incoming && (conncap & P2PS_SETUP_NEW))) {
852 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
853 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800854
855 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800856 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800857 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800858 wpa_s->p2p_mgmt ?
859 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800860 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800861 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800862 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800863 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800864 wpa_printf(MSG_ERROR,
865 "P2P: Failed to allocate a new interface for the group");
866 return P2PS_SETUP_NONE;
867 }
868 wpa_s->global->pending_group_iface_for_p2ps = 1;
869 p2p_set_intended_addr(wpa_s->global->p2p,
870 wpa_s->pending_interface_addr);
871 }
872 }
873
874 return conncap;
875}
876
877
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700878static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
879 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880{
881 struct wpa_ssid *ssid;
882 char *gtype;
883 const char *reason;
884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 ssid = wpa_s->current_ssid;
886 if (ssid == NULL) {
887 /*
888 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700889 * pending P2P group interface waiting for provisioning or a
890 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 */
892 ssid = wpa_s->conf->ssid;
893 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700894 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 break;
896 ssid = ssid->next;
897 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300898 if (ssid == NULL &&
899 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
900 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700901 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
902 "not found");
903 return -1;
904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 }
906 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
907 gtype = "GO";
908 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
909 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
910 wpa_s->reassociate = 0;
911 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 gtype = "client";
913 } else
914 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700915
916 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
917 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
918
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800919 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700920 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800921 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
922 wpa_s, NULL)) {
923 wpa_printf(MSG_DEBUG,
924 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
925 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
926 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
927 wpa_s, NULL);
928 }
929 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 if (wpa_s->cross_connect_in_use) {
932 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800933 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700934 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
935 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700937 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 case P2P_GROUP_REMOVAL_REQUESTED:
939 reason = " reason=REQUESTED";
940 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700941 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
942 reason = " reason=FORMATION_FAILED";
943 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
945 reason = " reason=IDLE";
946 break;
947 case P2P_GROUP_REMOVAL_UNAVAILABLE:
948 reason = " reason=UNAVAILABLE";
949 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700950 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
951 reason = " reason=GO_ENDING_SESSION";
952 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700953 case P2P_GROUP_REMOVAL_PSK_FAILURE:
954 reason = " reason=PSK_FAILURE";
955 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800956 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
957 reason = " reason=FREQ_CONFLICT";
958 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 default:
960 reason = "";
961 break;
962 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700963 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800964 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700965 P2P_EVENT_GROUP_REMOVED "%s %s%s",
966 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800969 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
970 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700971 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
972 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800973 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800974 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800975 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
976 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700977 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800978 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700979 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980
Dmitry Shmidt15907092014-03-25 10:42:57 -0700981 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800982 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
983 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700984
Dmitry Shmidt96571392013-10-14 12:54:46 -0700985 /*
986 * Make sure wait for the first client does not remain active after the
987 * group has been removed.
988 */
989 wpa_s->global->p2p_go_wait_client.sec = 0;
990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
992 struct wpa_global *global;
993 char *ifname;
994 enum wpa_driver_if_type type;
995 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
996 wpa_s->ifname);
997 global = wpa_s->global;
998 ifname = os_strdup(wpa_s->ifname);
999 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001000 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001001 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 wpa_s = global->ifaces;
1003 if (wpa_s && ifname)
1004 wpa_drv_if_remove(wpa_s, type, ifname);
1005 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001006 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 }
1008
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001009 /*
1010 * The primary interface was used for P2P group operations, so
1011 * need to reset its p2pdev.
1012 */
1013 wpa_s->p2pdev = wpa_s->parent;
1014
Dmitry Shmidt96571392013-10-14 12:54:46 -07001015 if (!wpa_s->p2p_go_group_formation_completed) {
1016 wpa_s->global->p2p_group_formation = NULL;
1017 wpa_s->p2p_in_provisioning = 0;
1018 }
1019
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001020 wpa_s->show_group_started = 0;
1021 os_free(wpa_s->go_params);
1022 wpa_s->go_params = NULL;
1023
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001024 os_free(wpa_s->p2p_group_common_freqs);
1025 wpa_s->p2p_group_common_freqs = NULL;
1026 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001027 wpa_s->p2p_go_do_acs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001028
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001029 wpa_s->waiting_presence_resp = 0;
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1032 if (ssid && (ssid->p2p_group ||
1033 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1034 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1035 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001036 if (ssid == wpa_s->current_ssid) {
1037 wpa_sm_set_config(wpa_s->wpa, NULL);
1038 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001040 }
1041 /*
1042 * Networks objects created during any P2P activities are not
1043 * exposed out as they might/will confuse certain non-P2P aware
1044 * applications since these network objects won't behave like
1045 * regular ones.
1046 *
1047 * Likewise, we don't send out network removed signals for such
1048 * network objects.
1049 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 wpa_config_remove_network(wpa_s->conf, id);
1051 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001052 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 } else {
1054 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1055 "found");
1056 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001057 if (wpa_s->ap_iface)
1058 wpa_supplicant_ap_deinit(wpa_s);
1059 else
1060 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001061
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001062 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1063
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001064 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065}
1066
1067
1068static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1069 u8 *go_dev_addr,
1070 const u8 *ssid, size_t ssid_len)
1071{
1072 struct wpa_bss *bss;
1073 const u8 *bssid;
1074 struct wpabuf *p2p;
1075 u8 group_capab;
1076 const u8 *addr;
1077
1078 if (wpa_s->go_params)
1079 bssid = wpa_s->go_params->peer_interface_addr;
1080 else
1081 bssid = wpa_s->bssid;
1082
1083 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001084 if (bss == NULL && wpa_s->go_params &&
1085 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1086 bss = wpa_bss_get_p2p_dev_addr(
1087 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 if (bss == NULL) {
1089 u8 iface_addr[ETH_ALEN];
1090 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1091 iface_addr) == 0)
1092 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1093 }
1094 if (bss == NULL) {
1095 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1096 "group is persistent - BSS " MACSTR " not found",
1097 MAC2STR(bssid));
1098 return 0;
1099 }
1100
1101 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001102 if (p2p == NULL)
1103 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1104 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 if (p2p == NULL) {
1106 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1107 "group is persistent - BSS " MACSTR
1108 " did not include P2P IE", MAC2STR(bssid));
1109 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001110 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001112 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 bss->beacon_ie_len);
1114 return 0;
1115 }
1116
1117 group_capab = p2p_get_group_capab(p2p);
1118 addr = p2p_get_go_dev_addr(p2p);
1119 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1120 "group_capab=0x%x", group_capab);
1121 if (addr) {
1122 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1123 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1124 MAC2STR(addr));
1125 } else
1126 os_memset(go_dev_addr, 0, ETH_ALEN);
1127 wpabuf_free(p2p);
1128
1129 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1130 "go_dev_addr=" MACSTR,
1131 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1132
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001133 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134}
1135
1136
Jouni Malinen75ecf522011-06-27 15:19:46 -07001137static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1138 struct wpa_ssid *ssid,
1139 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140{
1141 struct wpa_ssid *s;
1142 int changed = 0;
1143
1144 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1145 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1146 for (s = wpa_s->conf->ssid; s; s = s->next) {
1147 if (s->disabled == 2 &&
1148 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1149 s->ssid_len == ssid->ssid_len &&
1150 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1151 break;
1152 }
1153
1154 if (s) {
1155 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1156 "entry");
1157 if (ssid->passphrase && !s->passphrase)
1158 changed = 1;
1159 else if (ssid->passphrase && s->passphrase &&
1160 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1161 changed = 1;
1162 } else {
1163 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1164 "entry");
1165 changed = 1;
1166 s = wpa_config_add_network(wpa_s->conf);
1167 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001168 return -1;
1169
1170 /*
1171 * Instead of network_added we emit persistent_group_added
1172 * notification. Also to keep the defense checks in
1173 * persistent_group obj registration method, we set the
1174 * relevant flags in s to designate it as a persistent group.
1175 */
1176 s->p2p_group = 1;
1177 s->p2p_persistent_group = 1;
1178 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 wpa_config_set_network_defaults(s);
1180 }
1181
1182 s->p2p_group = 1;
1183 s->p2p_persistent_group = 1;
1184 s->disabled = 2;
1185 s->bssid_set = 1;
1186 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1187 s->mode = ssid->mode;
1188 s->auth_alg = WPA_AUTH_ALG_OPEN;
1189 s->key_mgmt = WPA_KEY_MGMT_PSK;
1190 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001191 s->pbss = ssid->pbss;
1192 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 s->export_keys = 1;
1194 if (ssid->passphrase) {
1195 os_free(s->passphrase);
1196 s->passphrase = os_strdup(ssid->passphrase);
1197 }
1198 if (ssid->psk_set) {
1199 s->psk_set = 1;
1200 os_memcpy(s->psk, ssid->psk, 32);
1201 }
1202 if (s->passphrase && !s->psk_set)
1203 wpa_config_update_psk(s);
1204 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1205 os_free(s->ssid);
1206 s->ssid = os_malloc(ssid->ssid_len);
1207 }
1208 if (s->ssid) {
1209 s->ssid_len = ssid->ssid_len;
1210 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1211 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001212 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1213 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1214 wpa_s->global->add_psk = NULL;
1215 changed = 1;
1216 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 if (changed && wpa_s->conf->update_config &&
1219 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1220 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1221 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001222
1223 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224}
1225
1226
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001227static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1228 const u8 *addr)
1229{
1230 struct wpa_ssid *ssid, *s;
1231 u8 *n;
1232 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001233 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001234 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001235
1236 ssid = wpa_s->current_ssid;
1237 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1238 !ssid->p2p_persistent_group)
1239 return;
1240
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001241 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001242 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1243 continue;
1244
1245 if (s->ssid_len == ssid->ssid_len &&
1246 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1247 break;
1248 }
1249
1250 if (s == NULL)
1251 return;
1252
1253 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001254 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001255 ETH_ALEN) != 0)
1256 continue;
1257
1258 if (i == s->num_p2p_clients - 1)
1259 return; /* already the most recent entry */
1260
1261 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001262 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1263 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1264 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001265 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001266 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1267 ETH_ALEN);
1268 os_memset(s->p2p_client_list +
1269 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1270 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001271 found = 1;
1272 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001273 }
1274
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001275 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1276 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001277 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001278 if (n == NULL)
1279 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001280 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1281 ETH_ALEN);
1282 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1283 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001284 s->p2p_client_list = n;
1285 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001286 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001287 /* Not enough room for an additional entry - drop the oldest
1288 * entry */
1289 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001290 s->p2p_client_list + 2 * ETH_ALEN,
1291 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001292 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001293 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001294 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001295 os_memset(s->p2p_client_list +
1296 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1297 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001298 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001299
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001300 if (p2p_wpa_s->conf->update_config &&
1301 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001302 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001303}
1304
1305
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001306static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1307 int go, struct wpa_ssid *ssid, int freq,
1308 const u8 *psk, const char *passphrase,
1309 const u8 *go_dev_addr, int persistent,
1310 const char *extra)
1311{
1312 const char *ssid_txt;
1313 char psk_txt[65];
1314
1315 if (psk)
1316 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1317 else
1318 psk_txt[0] = '\0';
1319
1320 if (ssid)
1321 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1322 else
1323 ssid_txt = "";
1324
1325 if (passphrase && passphrase[0] == '\0')
1326 passphrase = NULL;
1327
1328 /*
1329 * Include PSK/passphrase only in the control interface message and
1330 * leave it out from the debug log entry.
1331 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001332 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001333 P2P_EVENT_GROUP_STARTED
1334 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1335 MACSTR "%s%s",
1336 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1337 psk ? " psk=" : "", psk_txt,
1338 passphrase ? " passphrase=\"" : "",
1339 passphrase ? passphrase : "",
1340 passphrase ? "\"" : "",
1341 MAC2STR(go_dev_addr),
1342 persistent ? " [PERSISTENT]" : "", extra);
1343 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1344 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1345 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1346 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1347 extra);
1348}
1349
1350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001352 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353{
1354 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 int client;
1356 int persistent;
1357 u8 go_dev_addr[ETH_ALEN];
1358
1359 /*
1360 * This callback is likely called for the main interface. Update wpa_s
1361 * to use the group interface if a new interface was created for the
1362 * group.
1363 */
1364 if (wpa_s->global->p2p_group_formation)
1365 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001366 if (wpa_s->p2p_go_group_formation_completed) {
1367 wpa_s->global->p2p_group_formation = NULL;
1368 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001369 } else if (wpa_s->p2p_in_provisioning && !success) {
1370 wpa_msg(wpa_s, MSG_DEBUG,
1371 "P2P: Stop provisioning state due to failure");
1372 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001373 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001374 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001375 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376
1377 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001378 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001379 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001380 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001381 if (already_deleted)
1382 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001383 wpas_p2p_group_delete(wpa_s,
1384 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 return;
1386 }
1387
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001388 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001389 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390
1391 ssid = wpa_s->current_ssid;
1392 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1393 ssid->mode = WPAS_MODE_P2P_GO;
1394 p2p_group_notif_formation_done(wpa_s->p2p_group);
1395 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1396 }
1397
1398 persistent = 0;
1399 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 client = ssid->mode == WPAS_MODE_INFRA;
1401 if (ssid->mode == WPAS_MODE_P2P_GO) {
1402 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001403 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1404 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 } else
1406 persistent = wpas_p2p_persistent_group(wpa_s,
1407 go_dev_addr,
1408 ssid->ssid,
1409 ssid->ssid_len);
1410 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 client = wpa_s->p2p_group_interface ==
1412 P2P_GROUP_INTERFACE_CLIENT;
1413 os_memset(go_dev_addr, 0, ETH_ALEN);
1414 }
1415
1416 wpa_s->show_group_started = 0;
1417 if (client) {
1418 /*
1419 * Indicate event only after successfully completed 4-way
1420 * handshake, i.e., when the interface is ready for data
1421 * packets.
1422 */
1423 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001425 wpas_p2p_group_started(wpa_s, 1, ssid,
1426 ssid ? ssid->frequency : 0,
1427 ssid && ssid->passphrase == NULL &&
1428 ssid->psk_set ? ssid->psk : NULL,
1429 ssid ? ssid->passphrase : NULL,
1430 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 wpas_p2p_cross_connect_setup(wpa_s);
1432 wpas_p2p_set_group_idle_timeout(wpa_s);
1433 }
1434
1435 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001436 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1437 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001438 else {
1439 os_free(wpa_s->global->add_psk);
1440 wpa_s->global->add_psk = NULL;
1441 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001442
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001443 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001444 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001445 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447}
1448
1449
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450struct send_action_work {
1451 unsigned int freq;
1452 u8 dst[ETH_ALEN];
1453 u8 src[ETH_ALEN];
1454 u8 bssid[ETH_ALEN];
1455 size_t len;
1456 unsigned int wait_time;
1457 u8 buf[0];
1458};
1459
1460
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001461static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1462{
1463 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1464
1465 wpa_printf(MSG_DEBUG,
1466 "P2P: Free Action frame radio work @%p (freq=%u dst="
1467 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1468 wpa_s->p2p_send_action_work, awork->freq,
1469 MAC2STR(awork->dst), MAC2STR(awork->src),
1470 MAC2STR(awork->bssid), awork->wait_time);
1471 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1472 awork->buf, awork->len);
1473 os_free(awork);
1474 wpa_s->p2p_send_action_work->ctx = NULL;
1475 radio_work_done(wpa_s->p2p_send_action_work);
1476 wpa_s->p2p_send_action_work = NULL;
1477}
1478
1479
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001480static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1481 void *timeout_ctx)
1482{
1483 struct wpa_supplicant *wpa_s = eloop_ctx;
1484
1485 if (!wpa_s->p2p_send_action_work)
1486 return;
1487
1488 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001489 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001490}
1491
1492
Dmitry Shmidt03658832014-08-13 11:03:49 -07001493static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495 if (wpa_s->p2p_send_action_work) {
1496 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001498 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001499 wpa_printf(MSG_DEBUG,
1500 "P2P: Clear Action TX work @%p (wait_time=%u)",
1501 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001502 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001503 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001504 } else {
1505 /*
1506 * In theory, this should not be needed, but number of
1507 * places in the P2P code is still using non-zero wait
1508 * time for the last Action frame in the sequence and
1509 * some of these do not call send_action_done().
1510 */
1511 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1512 wpa_s, NULL);
1513 eloop_register_timeout(
1514 0, awork->wait_time * 1000,
1515 wpas_p2p_send_action_work_timeout,
1516 wpa_s, NULL);
1517 }
1518 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001519}
1520
1521
1522static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1523 unsigned int freq,
1524 const u8 *dst, const u8 *src,
1525 const u8 *bssid,
1526 const u8 *data, size_t data_len,
1527 enum offchannel_send_action_result
1528 result)
1529{
1530 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1531
1532 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1535 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001537 switch (result) {
1538 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1539 res = P2P_SEND_ACTION_SUCCESS;
1540 break;
1541 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1542 res = P2P_SEND_ACTION_NO_ACK;
1543 break;
1544 case OFFCHANNEL_SEND_ACTION_FAILED:
1545 res = P2P_SEND_ACTION_FAILED;
1546 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 }
1548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001551 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1552 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001553 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001554 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1555 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001557 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1558 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001559 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001560 P2P_EVENT_FALLBACK_TO_GO_NEG
1561 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001562 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1563 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 }
1565}
1566
1567
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001568static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1569{
1570 struct wpa_supplicant *wpa_s = work->wpa_s;
1571 struct send_action_work *awork = work->ctx;
1572
1573 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001574 if (work->started) {
1575 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1576 wpa_s, NULL);
1577 wpa_s->p2p_send_action_work = NULL;
1578 offchannel_send_action_done(wpa_s);
1579 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001580 os_free(awork);
1581 return;
1582 }
1583
1584 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1585 awork->bssid, awork->buf, awork->len,
1586 awork->wait_time,
1587 wpas_p2p_send_action_tx_status, 1) < 0) {
1588 os_free(awork);
1589 radio_work_done(work);
1590 return;
1591 }
1592 wpa_s->p2p_send_action_work = work;
1593}
1594
1595
1596static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1597 unsigned int freq, const u8 *dst,
1598 const u8 *src, const u8 *bssid, const u8 *buf,
1599 size_t len, unsigned int wait_time)
1600{
1601 struct send_action_work *awork;
1602
Hai Shalom81f62d82019-07-22 12:10:00 -07001603 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001604 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1605 return -1;
1606 }
1607
1608 awork = os_zalloc(sizeof(*awork) + len);
1609 if (awork == NULL)
1610 return -1;
1611
1612 awork->freq = freq;
1613 os_memcpy(awork->dst, dst, ETH_ALEN);
1614 os_memcpy(awork->src, src, ETH_ALEN);
1615 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1616 awork->len = len;
1617 awork->wait_time = wait_time;
1618 os_memcpy(awork->buf, buf, len);
1619
Hai Shalom81f62d82019-07-22 12:10:00 -07001620 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001621 wpas_send_action_cb, awork) < 0) {
1622 os_free(awork);
1623 return -1;
1624 }
1625
1626 return 0;
1627}
1628
1629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1631 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001632 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633{
1634 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001635 int listen_freq = -1, send_freq = -1;
1636
Hai Shalom021b0b52019-04-10 11:17:58 -07001637 if (scheduled)
1638 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001639 if (wpa_s->p2p_listen_work)
1640 listen_freq = wpa_s->p2p_listen_work->freq;
1641 if (wpa_s->p2p_send_action_work)
1642 send_freq = wpa_s->p2p_send_action_work->freq;
1643 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001644 int res;
1645
1646 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1647 listen_freq, send_freq, freq);
1648 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1649 len, wait_time);
1650 if (res == 0 && scheduled)
1651 *scheduled = 1;
1652 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001653 }
1654
1655 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001656 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1657 wait_time,
1658 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659}
1660
1661
1662static void wpas_send_action_done(void *ctx)
1663{
1664 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001665
1666 if (wpa_s->p2p_send_action_work) {
1667 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1668 wpa_s, NULL);
1669 os_free(wpa_s->p2p_send_action_work->ctx);
1670 radio_work_done(wpa_s->p2p_send_action_work);
1671 wpa_s->p2p_send_action_work = NULL;
1672 }
1673
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675}
1676
1677
1678static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1679 struct p2p_go_neg_results *params)
1680{
1681 if (wpa_s->go_params == NULL) {
1682 wpa_s->go_params = os_malloc(sizeof(*params));
1683 if (wpa_s->go_params == NULL)
1684 return -1;
1685 }
1686 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1687 return 0;
1688}
1689
1690
1691static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1692 struct p2p_go_neg_results *res)
1693{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001694 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001695 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1696 " dev_addr " MACSTR " wps_method %d",
1697 MAC2STR(res->peer_interface_addr),
1698 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1700 res->ssid, res->ssid_len);
1701 wpa_supplicant_ap_deinit(wpa_s);
1702 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001703 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001704 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001705#ifdef CONFIG_WPS_NFC
1706 } else if (res->wps_method == WPS_NFC) {
1707 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1708 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001709 wpa_s->p2pdev->p2p_oob_dev_pw,
1710 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1711 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001712 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001713 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001714 NULL,
1715 NULL, 0, 0);
1716#endif /* CONFIG_WPS_NFC */
1717 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001719 if (wpa_s->p2p_wps_method == WPS_P2PS)
1720 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1722 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1723 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1724 wpa_s->p2p_pin, 1, dev_pw_id);
1725 }
1726}
1727
1728
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001729static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1730 struct wpa_ssid *ssid)
1731{
1732 struct wpa_ssid *persistent;
1733 struct psk_list_entry *psk;
1734 struct hostapd_data *hapd;
1735
1736 if (!wpa_s->ap_iface)
1737 return;
1738
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001739 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001740 ssid->ssid_len);
1741 if (persistent == NULL)
1742 return;
1743
1744 hapd = wpa_s->ap_iface->bss[0];
1745
1746 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1747 list) {
1748 struct hostapd_wpa_psk *hpsk;
1749
1750 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1751 MACSTR " psk=%d",
1752 MAC2STR(psk->addr), psk->p2p);
1753 hpsk = os_zalloc(sizeof(*hpsk));
1754 if (hpsk == NULL)
1755 break;
1756 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1757 if (psk->p2p)
1758 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1759 else
1760 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1761 hpsk->next = hapd->conf->ssid.wpa_psk;
1762 hapd->conf->ssid.wpa_psk = hpsk;
1763 }
1764}
1765
1766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001767static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1768{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001769 char buf[20 + P2P_MAX_CHANNELS * 6];
1770 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001771 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001772 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001773
Roshan Pius3a1667e2018-07-03 15:17:14 -07001774 pos = buf;
1775 end = pos + sizeof(buf);
1776 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1777 res = os_snprintf(pos, end - pos, " %d",
1778 wpa_s->p2p_group_common_freqs[i]);
1779 if (os_snprintf_error(end - pos, res))
1780 break;
1781 pos += res;
1782 }
1783 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001784
Roshan Pius3a1667e2018-07-03 15:17:14 -07001785 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001786}
1787
1788
1789static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1790 struct p2p_go_neg_results *params)
1791{
1792 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1793
1794 wpa_s->p2p_group_common_freqs_num = 0;
1795 os_free(wpa_s->p2p_group_common_freqs);
1796 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1797 if (!wpa_s->p2p_group_common_freqs)
1798 return;
1799
1800 for (i = 0; i < len; i++) {
1801 if (!wpa_s->go_params->freq_list[i])
1802 break;
1803 wpa_s->p2p_group_common_freqs[i] =
1804 wpa_s->go_params->freq_list[i];
1805 }
1806 wpa_s->p2p_group_common_freqs_num = i;
1807}
1808
1809
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001810static void p2p_config_write(struct wpa_supplicant *wpa_s)
1811{
1812#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001813 if (wpa_s->p2pdev->conf->update_config &&
1814 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001815 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1816#endif /* CONFIG_NO_CONFIG_WRITE */
1817}
1818
1819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820static void p2p_go_configured(void *ctx, void *data)
1821{
1822 struct wpa_supplicant *wpa_s = ctx;
1823 struct p2p_go_neg_results *params = data;
1824 struct wpa_ssid *ssid;
1825
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001826 wpa_s->ap_configured_cb = NULL;
1827 wpa_s->ap_configured_cb_ctx = NULL;
1828 wpa_s->ap_configured_cb_data = NULL;
1829 if (!wpa_s->go_params) {
1830 wpa_printf(MSG_ERROR,
1831 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1832 return;
1833 }
1834
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001835 p2p_go_save_group_common_freqs(wpa_s, params);
1836 p2p_go_dump_common_freqs(wpa_s);
1837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 ssid = wpa_s->current_ssid;
1839 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1840 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1841 if (wpa_s->global->p2p_group_formation == wpa_s)
1842 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001843 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1844 params->passphrase[0] == '\0' ?
1845 params->psk : NULL,
1846 params->passphrase,
1847 wpa_s->global->p2p_dev_addr,
1848 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001849 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001851 if (wpa_s->p2pdev->p2ps_method_config_any) {
1852 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001853 wpa_dbg(wpa_s, MSG_DEBUG,
1854 "P2PS: Setting default PIN for ANY");
1855 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1856 "12345670", NULL, 0,
1857 0);
1858 } else {
1859 wpa_dbg(wpa_s, MSG_DEBUG,
1860 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001861 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001862 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001863 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001864 "12345670", NULL, 0, 0);
1865 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001866 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001867 }
1868
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001869 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001870 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001871 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001872 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001873 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001874 wpas_p2p_add_psk_list(wpa_s, ssid);
1875 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001876
1877 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001878 params->persistent_group, 0,
1879 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 wpas_p2p_cross_connect_setup(wpa_s);
1881 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001882
1883 if (wpa_s->p2p_first_connection_timeout) {
1884 wpa_dbg(wpa_s, MSG_DEBUG,
1885 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1886 wpa_s->p2p_first_connection_timeout);
1887 wpa_s->p2p_go_group_formation_completed = 0;
1888 wpa_s->global->p2p_group_formation = wpa_s;
1889 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001890 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001891 eloop_register_timeout(
1892 wpa_s->p2p_first_connection_timeout, 0,
1893 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001894 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001895 }
1896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 return;
1898 }
1899
1900 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1901 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1902 params->peer_interface_addr)) {
1903 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1904 "filtering");
1905 return;
1906 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001907 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001909 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001910#ifdef CONFIG_WPS_NFC
1911 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001912 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001913 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001914 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001915 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1916 return;
1917 }
1918 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001919 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1920 wpa_s->p2pdev->p2p_oob_dev_pw,
1921 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1922 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001923#endif /* CONFIG_WPS_NFC */
1924 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001926 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 os_free(wpa_s->go_params);
1928 wpa_s->go_params = NULL;
1929}
1930
1931
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001932/**
1933 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1934 * @freq: Frequency (MHz) to convert
1935 * @op_class: Buffer for returning operating class
1936 * @op_edmg_channel: Buffer for returning channel number
1937 * Returns: 0 on success, -1 on failure
1938 *
1939 * This can be used to find the highest channel bonding which includes the
1940 * specified frequency.
1941 */
1942static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1943 unsigned int freq,
1944 u8 *op_class, u8 *op_edmg_channel)
1945{
1946 struct hostapd_hw_modes *hwmode;
1947 struct ieee80211_edmg_config edmg;
1948 unsigned int i;
1949 enum chan_width chanwidth[] = {
1950 CHAN_WIDTH_8640,
1951 CHAN_WIDTH_6480,
1952 CHAN_WIDTH_4320,
1953 };
1954
1955 if (!wpa_s->hw.modes)
1956 return -1;
1957
1958 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08001959 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001960 if (!hwmode) {
1961 wpa_printf(MSG_ERROR,
1962 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1963 return -1;
1964 }
1965
1966 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1967 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1968 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1969 op_class,
1970 op_edmg_channel) < 0)
1971 continue;
1972
1973 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1974 if (edmg.channels &&
1975 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1976 wpa_printf(MSG_DEBUG,
1977 "Freq %u to EDMG channel %u at opclass %u",
1978 freq, *op_edmg_channel, *op_class);
1979 return 0;
1980 }
1981 }
1982
1983 return -1;
1984}
1985
1986
1987int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
1988 struct p2p_go_neg_results *params)
1989{
1990 u8 op_channel, op_class;
1991 int freq;
1992
1993 /* Try social channel as primary channel frequency */
1994 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
1995
1996 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
1997 &op_channel) == 0) {
1998 wpa_printf(MSG_DEBUG,
1999 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2000 freq, op_channel, op_class);
2001 params->freq = freq;
2002 return 0;
2003 }
2004
2005 return -1;
2006}
2007
2008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2010 struct p2p_go_neg_results *params,
2011 int group_formation)
2012{
2013 struct wpa_ssid *ssid;
2014
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002015 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2016 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2017 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2018 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021
2022 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002023 if (ssid == NULL) {
2024 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002026 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002028 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002029 wpa_s->p2p_go_group_formation_completed = 0;
2030 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002031 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 wpa_config_set_network_defaults(ssid);
2034 ssid->temporary = 1;
2035 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002036 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2038 WPAS_MODE_P2P_GO;
2039 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002040 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002041 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002042 ssid->max_oper_chwidth = params->max_oper_chwidth;
2043 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002044 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002045 if (params->edmg) {
2046 u8 op_channel, op_class;
2047
2048 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2049 &op_class, &op_channel)) {
2050 ssid->edmg_channel = op_channel;
2051 ssid->enable_edmg = params->edmg;
2052 } else {
2053 wpa_dbg(wpa_s, MSG_DEBUG,
2054 "P2P: Could not match EDMG channel, freq %d, for GO",
2055 params->freq);
2056 }
2057 }
2058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 ssid->ssid = os_zalloc(params->ssid_len + 1);
2060 if (ssid->ssid) {
2061 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2062 ssid->ssid_len = params->ssid_len;
2063 }
2064 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2065 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2066 ssid->proto = WPA_PROTO_RSN;
2067 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002068 ssid->group_cipher = WPA_CIPHER_CCMP;
2069 if (params->freq > 56160) {
2070 /*
2071 * Enable GCMP instead of CCMP as pairwise_cipher and
2072 * group_cipher in 60 GHz.
2073 */
2074 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2075 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002076 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2077 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002078 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002079 if (os_strlen(params->passphrase) > 0) {
2080 ssid->passphrase = os_strdup(params->passphrase);
2081 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002082 wpa_msg_global(wpa_s, MSG_ERROR,
2083 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002084 wpa_config_remove_network(wpa_s->conf, ssid->id);
2085 return;
2086 }
2087 } else
2088 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002089 ssid->psk_set = params->psk_set;
2090 if (ssid->psk_set)
2091 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002092 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002093 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002094 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095
2096 wpa_s->ap_configured_cb = p2p_go_configured;
2097 wpa_s->ap_configured_cb_ctx = wpa_s;
2098 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002099 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002100 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 wpa_s->reassociate = 1;
2102 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002103 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2104 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 wpa_supplicant_req_scan(wpa_s, 0, 0);
2106}
2107
2108
2109static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2110 const struct wpa_supplicant *src)
2111{
2112 struct wpa_config *d;
2113 const struct wpa_config *s;
2114
2115 d = dst->conf;
2116 s = src->conf;
2117
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002118#define C(n) \
2119do { \
2120 if (s->n && !d->n) \
2121 d->n = os_strdup(s->n); \
2122} while (0)
2123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 C(device_name);
2125 C(manufacturer);
2126 C(model_name);
2127 C(model_number);
2128 C(serial_number);
2129 C(config_methods);
2130#undef C
2131
2132 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2133 os_memcpy(d->sec_device_type, s->sec_device_type,
2134 sizeof(d->sec_device_type));
2135 d->num_sec_device_types = s->num_sec_device_types;
2136
2137 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002138 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002141 d->max_num_sta = s->max_num_sta;
2142 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002143 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002144 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002145 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002146 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002147 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002148 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002149 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002150
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002151 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2152 !d->wps_nfc_pw_from_config) {
2153 wpabuf_free(d->wps_nfc_dh_privkey);
2154 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002155 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2156 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2157 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002158 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002159 d->go_interworking = s->go_interworking;
2160 d->go_access_network_type = s->go_access_network_type;
2161 d->go_internet = s->go_internet;
2162 d->go_venue_group = s->go_venue_group;
2163 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002164 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165}
2166
2167
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002168static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2169 char *ifname, size_t len)
2170{
2171 char *ifname_ptr = wpa_s->ifname;
2172
2173 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2174 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2175 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2176 }
2177
2178 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2179 if (os_strlen(ifname) >= IFNAMSIZ &&
2180 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002181 int res;
2182
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002183 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002184 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2185 if (os_snprintf_error(len, res) && len)
2186 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002187 }
2188}
2189
2190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2192 enum wpa_driver_if_type type)
2193{
2194 char ifname[120], force_ifname[120];
2195
2196 if (wpa_s->pending_interface_name[0]) {
2197 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2198 "- skip creation of a new one");
2199 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2200 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2201 "unknown?! ifname='%s'",
2202 wpa_s->pending_interface_name);
2203 return -1;
2204 }
2205 return 0;
2206 }
2207
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002208 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 force_ifname[0] = '\0';
2210
2211 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2212 ifname);
2213 wpa_s->p2p_group_idx++;
2214
2215 wpa_s->pending_interface_type = type;
2216 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2217 wpa_s->pending_interface_addr, NULL) < 0) {
2218 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2219 "interface");
2220 return -1;
2221 }
2222
Jimmy Chen36c21992018-11-29 16:46:43 +08002223 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002224 random_mac_addr(wpa_s->pending_interface_addr);
2225 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2226 " for the group",
2227 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002228 }
2229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 if (force_ifname[0]) {
2231 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2232 force_ifname);
2233 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2234 sizeof(wpa_s->pending_interface_name));
2235 } else
2236 os_strlcpy(wpa_s->pending_interface_name, ifname,
2237 sizeof(wpa_s->pending_interface_name));
2238 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2239 MACSTR, wpa_s->pending_interface_name,
2240 MAC2STR(wpa_s->pending_interface_addr));
2241
2242 return 0;
2243}
2244
2245
2246static void wpas_p2p_remove_pending_group_interface(
2247 struct wpa_supplicant *wpa_s)
2248{
2249 if (!wpa_s->pending_interface_name[0] ||
2250 is_zero_ether_addr(wpa_s->pending_interface_addr))
2251 return; /* No pending virtual interface */
2252
2253 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2254 wpa_s->pending_interface_name);
2255 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2256 wpa_s->pending_interface_name);
2257 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2258 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002259 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260}
2261
2262
2263static struct wpa_supplicant *
2264wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2265{
2266 struct wpa_interface iface;
2267 struct wpa_supplicant *group_wpa_s;
2268
2269 if (!wpa_s->pending_interface_name[0]) {
2270 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2271 if (!wpas_p2p_create_iface(wpa_s))
2272 return NULL;
2273 /*
2274 * Something has forced us to remove the pending interface; try
2275 * to create a new one and hope for the best that we will get
2276 * the same local address.
2277 */
2278 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2279 WPA_IF_P2P_CLIENT) < 0)
2280 return NULL;
2281 }
2282
2283 os_memset(&iface, 0, sizeof(iface));
2284 iface.ifname = wpa_s->pending_interface_name;
2285 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002286 if (wpa_s->conf->ctrl_interface == NULL &&
2287 wpa_s->parent != wpa_s &&
2288 wpa_s->p2p_mgmt &&
2289 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2290 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2291 else
2292 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002294 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 if (group_wpa_s == NULL) {
2296 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2297 "wpa_supplicant interface");
2298 return NULL;
2299 }
2300 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2302 P2P_GROUP_INTERFACE_CLIENT;
2303 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002304 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305
2306 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2307
Jimmy Chen36c21992018-11-29 16:46:43 +08002308 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002309 if (wpa_drv_set_mac_addr(group_wpa_s,
2310 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002311 wpa_msg(group_wpa_s, MSG_INFO,
2312 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002313 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2314 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002315 return NULL;
2316 }
2317
2318 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2319 wpa_msg(group_wpa_s, MSG_INFO,
2320 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002321 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2322 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002323 return NULL;
2324 }
2325
Hai Shalom74f70d42019-02-11 14:42:39 -08002326 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2327 " for the group",
2328 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002329 }
2330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 return group_wpa_s;
2332}
2333
2334
2335static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2336 void *timeout_ctx)
2337{
2338 struct wpa_supplicant *wpa_s = eloop_ctx;
2339 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002340 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002341}
2342
2343
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002344static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2345 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002346{
2347 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002348 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 if (wpa_s->global->p2p)
2350 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002351 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352}
2353
2354
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002355static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2356{
2357 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2358 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002359 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002360 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002361 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002362 wpa_s->global->p2p_fail_on_wps_complete = 0;
2363}
2364
2365
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002366void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2367{
2368 if (wpa_s->global->p2p_group_formation != wpa_s)
2369 return;
2370 /* Speed up group formation timeout since this cannot succeed */
2371 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002372 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002373 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002374 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002375}
2376
2377
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002378static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379{
2380 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002381 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382
2383 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2384 wpa_drv_cancel_remain_on_channel(wpa_s);
2385 wpa_s->off_channel_freq = 0;
2386 wpa_s->roc_waiting_drv_freq = 0;
2387 }
2388
2389 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002390 wpa_msg_global(wpa_s, MSG_INFO,
2391 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2392 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002393 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 wpas_p2p_remove_pending_group_interface(wpa_s);
2395 return;
2396 }
2397
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002398 if (!res->role_go) {
2399 /* Inform driver of the operating channel of GO. */
2400 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2401 }
2402
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002403 if (wpa_s->p2p_go_ht40)
2404 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002405 if (wpa_s->p2p_go_vht)
2406 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002407 if (wpa_s->p2p_go_he)
2408 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002409 if (wpa_s->p2p_go_edmg)
2410 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002411 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2412 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002413
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002414 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2415 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2416 " wps_method=%s",
2417 res->role_go ? "GO" : "client", res->freq, res->ht40,
2418 MAC2STR(res->peer_device_addr),
2419 MAC2STR(res->peer_interface_addr),
2420 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002421 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422
Dmitry Shmidt04949592012-07-19 12:16:46 -07002423 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2424 struct wpa_ssid *ssid;
2425 ssid = wpa_config_get_network(wpa_s->conf,
2426 wpa_s->p2p_persistent_id);
2427 if (ssid && ssid->disabled == 2 &&
2428 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2429 size_t len = os_strlen(ssid->passphrase);
2430 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2431 "on requested persistent group");
2432 os_memcpy(res->passphrase, ssid->passphrase, len);
2433 res->passphrase[len] = '\0';
2434 }
2435 }
2436
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002438 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2440 if (group_wpa_s == NULL) {
2441 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002442 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2443 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002444 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445 return;
2446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2448 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002450 group_wpa_s = wpa_s->parent;
2451 wpa_s->global->p2p_group_formation = group_wpa_s;
2452 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002453 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002456 group_wpa_s->p2p_in_provisioning = 1;
2457 group_wpa_s->p2pdev = wpa_s;
2458 if (group_wpa_s != wpa_s) {
2459 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2460 sizeof(group_wpa_s->p2p_pin));
2461 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2462 }
2463 if (res->role_go) {
2464 wpas_start_wps_go(group_wpa_s, res, 1);
2465 } else {
2466 os_get_reltime(&group_wpa_s->scan_min_time);
2467 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468 }
2469
Hai Shalomfdcde762020-04-02 11:19:20 -07002470 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2472
2473 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2474 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2475 (res->peer_config_timeout % 100) * 10000,
2476 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2477}
2478
2479
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002480static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2481 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482{
2483 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002484 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002485 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2486 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002488 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489}
2490
2491
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002492static void wpas_dev_found(void *ctx, const u8 *addr,
2493 const struct p2p_peer_info *info,
2494 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002496 u8 *wfd_dev_info = NULL;
2497 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 struct wpa_supplicant *wpa_s = ctx;
2500 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002501 char *wfd_dev_info_hex = NULL;
2502
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002503#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002504 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2505 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002506 if (wfd_dev_info_hex) {
2507 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2508 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2509 // Only used for notification, so not handling error.
2510 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2511 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002512#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002513
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002514 if (info->p2ps_instance) {
2515 char str[256];
2516 const u8 *buf = wpabuf_head(info->p2ps_instance);
2517 size_t len = wpabuf_len(info->p2ps_instance);
2518
2519 while (len) {
2520 u32 id;
2521 u16 methods;
2522 u8 str_len;
2523
2524 if (len < 4 + 2 + 1)
2525 break;
2526 id = WPA_GET_LE32(buf);
2527 buf += sizeof(u32);
2528 methods = WPA_GET_BE16(buf);
2529 buf += sizeof(u16);
2530 str_len = *buf++;
2531 if (str_len > len - 4 - 2 - 1)
2532 break;
2533 os_memcpy(str, buf, str_len);
2534 str[str_len] = '\0';
2535 buf += str_len;
2536 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2537
2538 wpa_msg_global(wpa_s, MSG_INFO,
2539 P2P_EVENT_DEVICE_FOUND MACSTR
2540 " p2p_dev_addr=" MACSTR
2541 " pri_dev_type=%s name='%s'"
2542 " config_methods=0x%x"
2543 " dev_capab=0x%x"
2544 " group_capab=0x%x"
2545 " adv_id=%x asp_svc=%s%s",
2546 MAC2STR(addr),
2547 MAC2STR(info->p2p_device_addr),
2548 wps_dev_type_bin2str(
2549 info->pri_dev_type,
2550 devtype, sizeof(devtype)),
2551 info->device_name, methods,
2552 info->dev_capab, info->group_capab,
2553 id, str,
2554 info->vendor_elems ?
2555 " vendor_elems=1" : "");
2556 }
2557 goto done;
2558 }
2559
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002560 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2561 " p2p_dev_addr=" MACSTR
2562 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002563 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002564 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2565 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2566 sizeof(devtype)),
2567 info->device_name, info->config_methods,
2568 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002569 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002570 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002571 info->vendor_elems ? " vendor_elems=1" : "",
2572 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002574done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002575 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002576#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002577
Roshan Piusfd2fd662017-01-23 13:41:57 -08002578 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2579 wfd_dev_info_len, new_device);
2580 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581}
2582
2583
2584static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2585{
2586 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002587
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002588 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2589 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2592}
2593
2594
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002595static void wpas_find_stopped(void *ctx)
2596{
2597 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002598
2599 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2600 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2601
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002602 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002603 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002604}
2605
2606
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002607struct wpas_p2p_listen_work {
2608 unsigned int freq;
2609 unsigned int duration;
2610 struct wpabuf *probe_resp_ie;
2611};
2612
2613
2614static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2615{
2616 if (lwork == NULL)
2617 return;
2618 wpabuf_free(lwork->probe_resp_ie);
2619 os_free(lwork);
2620}
2621
2622
2623static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2624{
2625 struct wpas_p2p_listen_work *lwork;
2626
2627 if (!wpa_s->p2p_listen_work)
2628 return;
2629
2630 lwork = wpa_s->p2p_listen_work->ctx;
2631 wpas_p2p_listen_work_free(lwork);
2632 radio_work_done(wpa_s->p2p_listen_work);
2633 wpa_s->p2p_listen_work = NULL;
2634}
2635
2636
2637static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2638{
2639 struct wpa_supplicant *wpa_s = work->wpa_s;
2640 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002641 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002642
2643 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002644 if (work->started) {
2645 wpa_s->p2p_listen_work = NULL;
2646 wpas_stop_listen(wpa_s);
2647 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002648 wpas_p2p_listen_work_free(lwork);
2649 return;
2650 }
2651
2652 wpa_s->p2p_listen_work = work;
2653
2654 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2655
2656 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2657 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2658 "report received Probe Request frames");
2659 wpas_p2p_listen_work_done(wpa_s);
2660 return;
2661 }
2662
2663 wpa_s->pending_listen_freq = lwork->freq;
2664 wpa_s->pending_listen_duration = lwork->duration;
2665
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002666 duration = lwork->duration;
2667#ifdef CONFIG_TESTING_OPTIONS
2668 if (wpa_s->extra_roc_dur) {
2669 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2670 duration, duration + wpa_s->extra_roc_dur);
2671 duration += wpa_s->extra_roc_dur;
2672 }
2673#endif /* CONFIG_TESTING_OPTIONS */
2674
2675 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002676 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2677 "to remain on channel (%u MHz) for Listen "
2678 "state", lwork->freq);
2679 wpas_p2p_listen_work_done(wpa_s);
2680 wpa_s->pending_listen_freq = 0;
2681 return;
2682 }
2683 wpa_s->off_channel_freq = 0;
2684 wpa_s->roc_waiting_drv_freq = lwork->freq;
2685}
2686
2687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688static int wpas_start_listen(void *ctx, unsigned int freq,
2689 unsigned int duration,
2690 const struct wpabuf *probe_resp_ie)
2691{
2692 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002693 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002695 if (wpa_s->p2p_listen_work) {
2696 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 return -1;
2698 }
2699
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002700 lwork = os_zalloc(sizeof(*lwork));
2701 if (lwork == NULL)
2702 return -1;
2703 lwork->freq = freq;
2704 lwork->duration = duration;
2705 if (probe_resp_ie) {
2706 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2707 if (lwork->probe_resp_ie == NULL) {
2708 wpas_p2p_listen_work_free(lwork);
2709 return -1;
2710 }
2711 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002713 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2714 lwork) < 0) {
2715 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716 return -1;
2717 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002718
2719 return 0;
2720}
2721
2722
2723static void wpas_stop_listen(void *ctx)
2724{
2725 struct wpa_supplicant *wpa_s = ctx;
2726 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2727 wpa_drv_cancel_remain_on_channel(wpa_s);
2728 wpa_s->off_channel_freq = 0;
2729 wpa_s->roc_waiting_drv_freq = 0;
2730 }
2731 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002732
2733 /*
2734 * Don't cancel Probe Request RX reporting for a connected P2P Client
2735 * handling Probe Request frames.
2736 */
2737 if (!wpa_s->p2p_cli_probe)
2738 wpa_drv_probe_req_report(wpa_s, 0);
2739
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002740 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741}
2742
2743
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002744static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2745 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746{
2747 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002748 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002749 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750}
2751
2752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2754 const u8 *peer, const char *params,
2755 unsigned int generated_pin)
2756{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002757 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2758 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002759}
2760
2761
2762static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2763 const u8 *peer, const char *params)
2764{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002765 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2766 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767}
2768
2769
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002770static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2771 const u8 *dev_addr, const u8 *pri_dev_type,
2772 const char *dev_name, u16 supp_config_methods,
2773 u8 dev_capab, u8 group_capab, const u8 *group_id,
2774 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775{
2776 struct wpa_supplicant *wpa_s = ctx;
2777 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002778 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779 u8 empty_dev_type[8];
2780 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002781 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002782 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002783
2784 if (group_id) {
2785 for (group = wpa_s->global->ifaces; group; group = group->next)
2786 {
2787 struct wpa_ssid *s = group->current_ssid;
2788 if (s != NULL &&
2789 s->mode == WPAS_MODE_P2P_GO &&
2790 group_id_len - ETH_ALEN == s->ssid_len &&
2791 os_memcmp(group_id + ETH_ALEN, s->ssid,
2792 s->ssid_len) == 0)
2793 break;
2794 }
2795 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796
2797 if (pri_dev_type == NULL) {
2798 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2799 pri_dev_type = empty_dev_type;
2800 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002801 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2802 " pri_dev_type=%s name='%s' config_methods=0x%x "
2803 "dev_capab=0x%x group_capab=0x%x%s%s",
2804 MAC2STR(dev_addr),
2805 wps_dev_type_bin2str(pri_dev_type, devtype,
2806 sizeof(devtype)),
2807 dev_name, supp_config_methods, dev_capab, group_capab,
2808 group ? " group=" : "",
2809 group ? group->ifname : "");
2810 if (os_snprintf_error(sizeof(params), res))
2811 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002815 if (wps_generate_pin(&generated_pin) < 0) {
2816 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2817 wpas_notify_p2p_provision_discovery(
2818 wpa_s, peer, 0 /* response */,
2819 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2820 return;
2821 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 wpas_prov_disc_local_display(wpa_s, peer, params,
2823 generated_pin);
2824 } else if (config_methods & WPS_CONFIG_KEYPAD)
2825 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2826 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002827 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2828 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002829
2830 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2831 P2P_PROV_DISC_SUCCESS,
2832 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833}
2834
2835
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002836static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837{
2838 struct wpa_supplicant *wpa_s = ctx;
2839 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002840 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002842 if (wpa_s->pending_pd_before_join &&
2843 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2844 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2845 wpa_s->pending_pd_before_join = 0;
2846 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2847 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002848 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002849 return;
2850 }
2851
Dmitry Shmidt04949592012-07-19 12:16:46 -07002852 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002853 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2854 int res;
2855
2856 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2857 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2858 if (os_snprintf_error(sizeof(params), res))
2859 params[sizeof(params) - 1] = '\0';
2860 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002861 params[0] = '\0';
2862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002864 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002866 if (wps_generate_pin(&generated_pin) < 0) {
2867 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2868 wpas_notify_p2p_provision_discovery(
2869 wpa_s, peer, 0 /* response */,
2870 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2871 return;
2872 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002873 wpas_prov_disc_local_display(wpa_s, peer, params,
2874 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002876 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2877 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878
Jouni Malinen75ecf522011-06-27 15:19:46 -07002879 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2880 P2P_PROV_DISC_SUCCESS,
2881 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882}
2883
2884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002885static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002886 enum p2p_prov_disc_status status,
2887 u32 adv_id, const u8 *adv_mac,
2888 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002889{
2890 struct wpa_supplicant *wpa_s = ctx;
2891
Dmitry Shmidt04949592012-07-19 12:16:46 -07002892 if (wpa_s->p2p_fallback_to_go_neg) {
2893 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2894 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002895 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002896 P2P_EVENT_FALLBACK_TO_GO_NEG
2897 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002898 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2899 return;
2900 }
2901
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002902 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002903 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002904 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2905 "join-existing-group operation (no ACK for PD "
2906 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002907 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002908 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002909 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002910
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002911 if (adv_id && adv_mac && deferred_session_resp) {
2912 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2913 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2914 " deferred_session_resp='%s'",
2915 MAC2STR(peer), status, adv_id,
2916 deferred_session_resp);
2917 } else if (adv_id && adv_mac) {
2918 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2919 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2920 MAC2STR(peer), status, adv_id);
2921 } else {
2922 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2923 " p2p_dev_addr=" MACSTR " status=%d",
2924 MAC2STR(peer), status);
2925 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002926
Jouni Malinen75ecf522011-06-27 15:19:46 -07002927 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2928 status, 0, 0);
2929}
2930
2931
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002932static int freq_included(struct wpa_supplicant *wpa_s,
2933 const struct p2p_channels *channels,
2934 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002935{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002936 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2937 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2938 return 1;
2939 return 0;
2940}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002941
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002942
2943static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2944{
2945 unsigned int num = P2P_MAX_CHANNELS;
2946 int *common_freqs;
2947 int ret;
2948
2949 p2p_go_dump_common_freqs(wpa_s);
2950 common_freqs = os_calloc(num, sizeof(int));
2951 if (!common_freqs)
2952 return;
2953
2954 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2955 if (ret < 0) {
2956 wpa_dbg(wpa_s, MSG_DEBUG,
2957 "P2P: Failed to get group common freqs");
2958 os_free(common_freqs);
2959 return;
2960 }
2961
2962 os_free(wpa_s->p2p_group_common_freqs);
2963 wpa_s->p2p_group_common_freqs = common_freqs;
2964 wpa_s->p2p_group_common_freqs_num = num;
2965 p2p_go_dump_common_freqs(wpa_s);
2966}
2967
2968
2969/*
2970 * Check if the given frequency is one of the possible operating frequencies
2971 * set after the completion of the GO Negotiation.
2972 */
2973static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2974{
2975 unsigned int i;
2976
2977 p2p_go_dump_common_freqs(wpa_s);
2978
2979 /* assume no restrictions */
2980 if (!wpa_s->p2p_group_common_freqs_num)
2981 return 1;
2982
2983 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2984 if (wpa_s->p2p_group_common_freqs[i] == freq)
2985 return 1;
2986 }
2987 return 0;
2988}
2989
2990
2991static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2992 struct sta_info *sta, void *ctx)
2993{
2994 int *ecsa_support = ctx;
2995
2996 *ecsa_support &= sta->ecsa_supported;
2997
2998 return 0;
2999}
3000
3001
3002/* Check if all the peers support eCSA */
3003static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3004{
3005 int ecsa_support = 1;
3006
3007 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3008 &ecsa_support);
3009
3010 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003011}
3012
3013
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003014/**
3015 * Pick the best frequency to use from all the currently used frequencies.
3016 */
3017static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3018 struct wpa_used_freq_data *freqs,
3019 unsigned int num)
3020{
3021 unsigned int i, c;
3022
3023 /* find a candidate freq that is supported by P2P */
3024 for (c = 0; c < num; c++)
3025 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3026 break;
3027
3028 if (c == num)
3029 return 0;
3030
3031 /* once we have a candidate, try to find a 'better' one */
3032 for (i = c + 1; i < num; i++) {
3033 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3034 continue;
3035
3036 /*
3037 * 1. Infrastructure station interfaces have higher preference.
3038 * 2. P2P Clients have higher preference.
3039 * 3. All others.
3040 */
3041 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3042 c = i;
3043 break;
3044 }
3045
3046 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3047 c = i;
3048 }
3049 return freqs[c].freq;
3050}
3051
3052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3054 const u8 *go_dev_addr, const u8 *ssid,
3055 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003056 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003057 const struct p2p_channels *channels,
3058 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059{
3060 struct wpa_supplicant *wpa_s = ctx;
3061 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003062 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003064 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003065
3066 if (!persistent_group) {
3067 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003068 " to join an active group (SSID: %s)",
3069 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3071 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3072 == 0 ||
3073 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3074 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3075 "authorized invitation");
3076 goto accept_inv;
3077 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003078
3079#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003080 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3081 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003082 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003083 wpa_s->p2p_wps_method = WPS_NFC;
3084 wpa_s->pending_join_wps_method = WPS_NFC;
3085 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003086 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003087 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003088 bssid, ETH_ALEN);
3089 goto accept_inv;
3090 }
3091#endif /* CONFIG_WPS_NFC */
3092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 /*
3094 * Do not accept the invitation automatically; notify user and
3095 * request approval.
3096 */
3097 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3098 }
3099
3100 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3101 if (grp) {
3102 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3103 "running persistent group");
3104 if (*go)
3105 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3106 goto accept_inv;
3107 }
3108
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003109 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3110 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3111 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3112 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003113 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003114 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3116
3117 for (s = wpa_s->conf->ssid; s; s = s->next) {
3118 if (s->disabled == 2 &&
3119 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3120 s->ssid_len == ssid_len &&
3121 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3122 break;
3123 }
3124
3125 if (!s) {
3126 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3127 " requested reinvocation of an unknown group",
3128 MAC2STR(sa));
3129 return P2P_SC_FAIL_UNKNOWN_GROUP;
3130 }
3131
3132 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3133 *go = 1;
3134 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3135 wpa_printf(MSG_DEBUG, "P2P: The only available "
3136 "interface is already in use - reject "
3137 "invitation");
3138 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3139 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003140 if (wpa_s->p2p_mgmt)
3141 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3142 ETH_ALEN);
3143 else
3144 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145 } else if (s->mode == WPAS_MODE_P2P_GO) {
3146 *go = 1;
3147 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3148 {
3149 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3150 "interface address for the group");
3151 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3152 }
3153 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3154 ETH_ALEN);
3155 }
3156
3157accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003158 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3159
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003160 best_freq = 0;
3161 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3162 sizeof(struct wpa_used_freq_data));
3163 if (freqs) {
3164 int num_channels = wpa_s->num_multichan_concurrent;
3165 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3166 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3167 os_free(freqs);
3168 }
3169
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003170 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003171 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003172 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003173 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003174
3175 if (wpa_s->num_multichan_concurrent < 2 ||
3176 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3177 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 -07003178 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003179 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180 }
3181
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003182 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3183 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003184 if (*go == 0) {
3185 /* We are the client */
3186 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3187 "running a GO but we are capable of MCC, "
3188 "figure out the best channel to use");
3189 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003190 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003191 /* We are the GO, and *force_freq is not in the
3192 * intersection */
3193 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3194 "in intersection but we are capable of MCC, "
3195 "figure out the best channel to use",
3196 *force_freq);
3197 *force_freq = 0;
3198 }
3199 }
3200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201 return P2P_SC_SUCCESS;
3202}
3203
3204
3205static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3206 const u8 *ssid, size_t ssid_len,
3207 const u8 *go_dev_addr, u8 status,
3208 int op_freq)
3209{
3210 struct wpa_supplicant *wpa_s = ctx;
3211 struct wpa_ssid *s;
3212
3213 for (s = wpa_s->conf->ssid; s; s = s->next) {
3214 if (s->disabled == 2 &&
3215 s->ssid_len == ssid_len &&
3216 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3217 break;
3218 }
3219
3220 if (status == P2P_SC_SUCCESS) {
3221 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003222 " was accepted; op_freq=%d MHz, SSID=%s",
3223 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003225 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003226 if (go) {
3227 wpa_msg_global(wpa_s, MSG_INFO,
3228 P2P_EVENT_INVITATION_ACCEPTED
3229 "sa=" MACSTR
3230 " persistent=%d freq=%d",
3231 MAC2STR(sa), s->id, op_freq);
3232 } else {
3233 wpa_msg_global(wpa_s, MSG_INFO,
3234 P2P_EVENT_INVITATION_ACCEPTED
3235 "sa=" MACSTR
3236 " persistent=%d",
3237 MAC2STR(sa), s->id);
3238 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003240 wpa_s, s, go, 0, op_freq, 0,
3241 wpa_s->conf->p2p_go_ht40,
3242 wpa_s->conf->p2p_go_vht,
3243 0,
3244 wpa_s->conf->p2p_go_he,
3245 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003246 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3247 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003249 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003250 wpa_msg_global(wpa_s, MSG_INFO,
3251 P2P_EVENT_INVITATION_ACCEPTED
3252 "sa=" MACSTR " go_dev_addr=" MACSTR
3253 " bssid=" MACSTR " unknown-network",
3254 MAC2STR(sa), MAC2STR(go_dev_addr),
3255 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003257 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003258 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003259 }
3260 return;
3261 }
3262
3263 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3264 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3265 " was rejected (status %u)", MAC2STR(sa), status);
3266 return;
3267 }
3268
3269 if (!s) {
3270 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003271 wpa_msg_global(wpa_s, MSG_INFO,
3272 P2P_EVENT_INVITATION_RECEIVED
3273 "sa=" MACSTR " go_dev_addr=" MACSTR
3274 " bssid=" MACSTR " unknown-network",
3275 MAC2STR(sa), MAC2STR(go_dev_addr),
3276 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003278 wpa_msg_global(wpa_s, MSG_INFO,
3279 P2P_EVENT_INVITATION_RECEIVED
3280 "sa=" MACSTR " go_dev_addr=" MACSTR
3281 " unknown-network",
3282 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003284 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3285 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 return;
3287 }
3288
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003289 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003290 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3291 "sa=" MACSTR " persistent=%d freq=%d",
3292 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003293 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003294 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3295 "sa=" MACSTR " persistent=%d",
3296 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003297 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003298 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3299 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300}
3301
3302
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003303static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3304 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003305 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003306{
3307 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003308 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003309
3310 if (ssid == NULL)
3311 return;
3312
3313 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003314 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003315 ETH_ALEN) == 0)
3316 break;
3317 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003318 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003319 if (ssid->mode != WPAS_MODE_P2P_GO &&
3320 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3321 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3322 "due to invitation result", ssid->id);
3323 wpas_notify_network_removed(wpa_s, ssid);
3324 wpa_config_remove_network(wpa_s->conf, ssid->id);
3325 return;
3326 }
3327 return; /* Peer not found in client list */
3328 }
3329
3330 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003331 "group %d client list%s",
3332 MAC2STR(peer), ssid->id,
3333 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003334 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3335 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3336 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003337 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003338 if (p2p_wpa_s->conf->update_config &&
3339 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003340 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003341}
3342
3343
3344static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3345 const u8 *peer)
3346{
3347 struct wpa_ssid *ssid;
3348
3349 wpa_s = wpa_s->global->p2p_invite_group;
3350 if (wpa_s == NULL)
3351 return; /* No known invitation group */
3352 ssid = wpa_s->current_ssid;
3353 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3354 !ssid->p2p_persistent_group)
3355 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003356 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003357 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003358 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003359}
3360
3361
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003362static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003363 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003364 const u8 *peer, int neg_freq,
3365 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366{
3367 struct wpa_supplicant *wpa_s = ctx;
3368 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003369 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370
3371 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003372 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3373 "status=%d " MACSTR,
3374 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003375 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003376 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3377 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378 }
3379 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3380
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003381 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3382 status, MAC2STR(peer));
3383 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003384 struct wpa_supplicant *group_if =
3385 wpa_s->global->p2p_invite_group;
3386
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003387 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3388 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003389
3390 /*
3391 * Invitation to an active group. If this is successful and we
3392 * are the GO, set the client wait to postpone some concurrent
3393 * operations and to allow provisioning and connection to happen
3394 * more quickly.
3395 */
3396 if (status == P2P_SC_SUCCESS &&
3397 group_if && group_if->current_ssid &&
3398 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3399 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3400#ifdef CONFIG_TESTING_OPTIONS
3401 if (group_if->p2p_go_csa_on_inv) {
3402 wpa_printf(MSG_DEBUG,
3403 "Testing: force P2P GO CSA after invitation");
3404 eloop_cancel_timeout(
3405 wpas_p2p_reconsider_moving_go,
3406 wpa_s, NULL);
3407 eloop_register_timeout(
3408 0, 50000,
3409 wpas_p2p_reconsider_moving_go,
3410 wpa_s, NULL);
3411 }
3412#endif /* CONFIG_TESTING_OPTIONS */
3413 }
3414 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003415 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003417 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3418 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3419 "invitation exchange to indicate readiness for "
3420 "re-invocation");
3421 }
3422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003423 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003424 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3425 ssid = wpa_config_get_network(
3426 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003427 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003428 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429 wpas_p2p_remove_pending_group_interface(wpa_s);
3430 return;
3431 }
3432
3433 ssid = wpa_config_get_network(wpa_s->conf,
3434 wpa_s->pending_invite_ssid_id);
3435 if (ssid == NULL) {
3436 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3437 "data matching with invitation");
3438 return;
3439 }
3440
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003441 /*
3442 * The peer could have missed our ctrl::ack frame for Invitation
3443 * Response and continue retransmitting the frame. To reduce the
3444 * likelihood of the peer not getting successful TX status for the
3445 * Invitation Response frame, wait a short time here before starting
3446 * the persistent group so that we will remain on the current channel to
3447 * acknowledge any possible retransmission from the peer.
3448 */
3449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3450 "starting persistent group");
3451 os_sleep(0, 50000);
3452
Dmitry Shmidt15907092014-03-25 10:42:57 -07003453 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003454 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003455 freq = neg_freq;
3456 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003457 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003458 freq = peer_oper_freq;
3459 else
3460 freq = 0;
3461
3462 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3463 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003465 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003466 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003467 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003468 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003469 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003470 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003471 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003472 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003473 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003474 ssid->mode == WPAS_MODE_P2P_GO ?
3475 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003476 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003477}
3478
3479
Dmitry Shmidt04949592012-07-19 12:16:46 -07003480static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3481 unsigned int freq)
3482{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003483 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3484 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003485 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003486}
3487
3488
3489static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3490{
3491 reg->channel[reg->channels] = chan;
3492 reg->channels++;
3493}
3494
3495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003497 struct p2p_channels *chan,
3498 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499{
3500 int i, cla = 0;
3501
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003502 wpa_s->global->p2p_24ghz_social_channels = 1;
3503
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003504 os_memset(cli_chan, 0, sizeof(*cli_chan));
3505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3507 "band");
3508
3509 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3510 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003511 chan->reg_class[cla].channels = 0;
3512 for (i = 0; i < 11; i++) {
3513 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3514 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3515 }
3516 if (chan->reg_class[cla].channels)
3517 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003518
3519 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3520 "band");
3521
3522 /* Operating class 115 - 5 GHz, channels 36-48 */
3523 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003524 chan->reg_class[cla].channels = 0;
3525 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3526 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3527 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3528 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3529 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3530 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3531 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3532 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3533 if (chan->reg_class[cla].channels)
3534 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535
3536 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3537 "band");
3538
3539 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3540 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003541 chan->reg_class[cla].channels = 0;
3542 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3543 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3544 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3545 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3546 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3547 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3548 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3549 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3550 if (chan->reg_class[cla].channels)
3551 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552
3553 chan->reg_classes = cla;
3554 return 0;
3555}
3556
3557
Dmitry Shmidt04949592012-07-19 12:16:46 -07003558static int has_channel(struct wpa_global *global,
3559 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560{
3561 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003562 unsigned int freq;
3563
3564 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3565 chan * 5;
3566 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003567 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568
3569 for (i = 0; i < mode->num_channels; i++) {
3570 if (mode->channels[i].chan == chan) {
3571 if (flags)
3572 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003573 if (mode->channels[i].flag &
3574 (HOSTAPD_CHAN_DISABLED |
3575 HOSTAPD_CHAN_RADAR))
3576 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003577 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3578 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003579 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 }
3581 }
3582
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003583 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584}
3585
3586
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003587static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3588 struct hostapd_hw_modes *mode,
3589 u8 channel)
3590{
Hai Shalom60840252021-02-19 19:02:11 -08003591 u8 center_channels[] = { 42, 58, 106, 122, 138, 155, 171 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003592 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003593
3594 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3595 return 0;
3596
3597 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3598 /*
3599 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3600 * so the center channel is 6 channels away from the start/end.
3601 */
3602 if (channel >= center_channels[i] - 6 &&
3603 channel <= center_channels[i] + 6)
3604 return center_channels[i];
3605
3606 return 0;
3607}
3608
3609
3610static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3611 struct hostapd_hw_modes *mode,
3612 u8 channel, u8 bw)
3613{
3614 u8 center_chan;
3615 int i, flags;
3616 enum chan_allowed res, ret = ALLOWED;
3617
3618 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3619 if (!center_chan)
3620 return NOT_ALLOWED;
3621 if (center_chan >= 58 && center_chan <= 138)
3622 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3623
3624 /* check all the channels are available */
3625 for (i = 0; i < 4; i++) {
3626 int adj_chan = center_chan - 6 + i * 4;
3627
3628 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3629 if (res == NOT_ALLOWED)
3630 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003631 if (res == NO_IR)
3632 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003633
3634 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3635 return NOT_ALLOWED;
3636 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3637 return NOT_ALLOWED;
3638 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3639 return NOT_ALLOWED;
3640 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3641 return NOT_ALLOWED;
3642 }
3643
3644 return ret;
3645}
3646
3647
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003648static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3649 struct hostapd_hw_modes *mode,
3650 u8 channel)
3651{
Hai Shalom60840252021-02-19 19:02:11 -08003652 u8 center_channels[] = { 50, 114, 163 };
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003653 unsigned int i;
3654
3655 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3656 return 0;
3657
3658 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3659 /*
3660 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3661 * so the center channel is 14 channels away from the start/end.
3662 */
3663 if (channel >= center_channels[i] - 14 &&
3664 channel <= center_channels[i] + 14)
3665 return center_channels[i];
3666
3667 return 0;
3668}
3669
3670
3671static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3672 struct hostapd_hw_modes *mode,
3673 u8 channel, u8 bw)
3674{
3675 u8 center_chan;
3676 int i, flags;
3677 enum chan_allowed res, ret = ALLOWED;
3678
3679 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3680 if (!center_chan)
3681 return NOT_ALLOWED;
3682 /* VHT 160 MHz uses DFS channels in most countries. */
3683
3684 /* Check all the channels are available */
3685 for (i = 0; i < 8; i++) {
3686 int adj_chan = center_chan - 14 + i * 4;
3687
3688 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3689 if (res == NOT_ALLOWED)
3690 return NOT_ALLOWED;
3691
3692 if (res == NO_IR)
3693 ret = NO_IR;
3694
3695 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3696 return NOT_ALLOWED;
3697 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3698 return NOT_ALLOWED;
3699 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3700 return NOT_ALLOWED;
3701 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3702 return NOT_ALLOWED;
3703 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3704 return NOT_ALLOWED;
3705 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3706 return NOT_ALLOWED;
3707 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3708 return NOT_ALLOWED;
3709 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3710 return NOT_ALLOWED;
3711 }
3712
3713 return ret;
3714}
3715
3716
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003717static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3718 struct hostapd_hw_modes *mode,
3719 u8 channel)
3720{
3721 struct ieee80211_edmg_config edmg;
3722
3723 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3724 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3725 return ALLOWED;
3726
3727 return NOT_ALLOWED;
3728}
3729
3730
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003731static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3732 struct hostapd_hw_modes *mode,
3733 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003734{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003735 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003736 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003737
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003738 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3739 if (bw == BW40MINUS) {
3740 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3741 return NOT_ALLOWED;
3742 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3743 } else if (bw == BW40PLUS) {
3744 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3745 return NOT_ALLOWED;
3746 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3747 } else if (bw == BW80) {
3748 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003749 } else if (bw == BW160) {
3750 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003751 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3752 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003753 }
3754
3755 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3756 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003757 if (res == NO_IR || res2 == NO_IR)
3758 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003759 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003760}
3761
3762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003764 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003765 struct p2p_channels *cli_chan,
3766 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003768 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003769 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003771 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3773 "of all supported channels; assume dualband "
3774 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003775 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003776 }
3777
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003778 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003780 for (op = 0; global_op_class[op].op_class; op++) {
3781 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003783 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784
Hai Shalom899fcc72020-10-19 14:38:18 -07003785 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003786 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003787 continue;
3788
Hai Shalomfdcde762020-04-02 11:19:20 -07003789 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3790 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 if (mode == NULL)
3792 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003793 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3794 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003796 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003797
3798 /* Check for non-continuous jump in channel index
3799 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003800 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003801 ch < 149 && ch + o->inc > 149)
3802 ch = 149;
3803
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003804 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3805 if (res == ALLOWED) {
3806 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003807 if (cla == P2P_MAX_REG_CLASSES)
3808 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003809 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3810 o->op_class);
3811 reg = &chan->reg_class[cla];
3812 cla++;
3813 reg->reg_class = o->op_class;
3814 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003815 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3816 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003817 reg->channel[reg->channels] = ch;
3818 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003819 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003820 wpa_s->conf->p2p_add_cli_chan) {
3821 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003822 if (cli_cla == P2P_MAX_REG_CLASSES)
3823 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003824 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3825 o->op_class);
3826 cli_reg = &cli_chan->reg_class[cli_cla];
3827 cli_cla++;
3828 cli_reg->reg_class = o->op_class;
3829 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003830 if (cli_reg->channels ==
3831 P2P_MAX_REG_CLASS_CHANNELS)
3832 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003833 cli_reg->channel[cli_reg->channels] = ch;
3834 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003836 }
3837 if (reg) {
3838 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3839 reg->channel, reg->channels);
3840 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003841 if (cli_reg) {
3842 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3843 cli_reg->channel, cli_reg->channels);
3844 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 }
3846
3847 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003848 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850 return 0;
3851}
3852
3853
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003854int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3855 struct hostapd_hw_modes *mode, u8 channel)
3856{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003857 int op;
3858 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003859
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003860 for (op = 0; global_op_class[op].op_class; op++) {
3861 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003862 u8 ch;
3863
Hai Shalom899fcc72020-10-19 14:38:18 -07003864 if (o->p2p == NO_P2P_SUPP ||
3865 (is_6ghz_op_class(o->op_class) &&
3866 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003867 continue;
3868
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003869 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3870 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003871 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3872 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003873 continue;
3874 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003875 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003876 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003877 }
3878 }
3879 return 0;
3880}
3881
3882
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003883int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3884 struct hostapd_hw_modes *mode, u8 channel)
3885{
3886 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3887 return 0;
3888
3889 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3890}
3891
3892
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003893int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3894 struct hostapd_hw_modes *mode, u8 channel)
3895{
3896 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3897 return 0;
3898 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3899}
3900
3901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3903 size_t buf_len)
3904{
3905 struct wpa_supplicant *wpa_s = ctx;
3906
3907 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3908 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3909 break;
3910 }
3911 if (wpa_s == NULL)
3912 return -1;
3913
3914 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3915}
3916
3917
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003918struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3919 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003920{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003921 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3922 struct wpa_ssid *s = wpa_s->current_ssid;
3923 if (s == NULL)
3924 continue;
3925 if (s->mode != WPAS_MODE_P2P_GO &&
3926 s->mode != WPAS_MODE_AP &&
3927 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3928 continue;
3929 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003930 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003931 continue;
3932 return wpa_s;
3933 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003934
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003935 return NULL;
3936
3937}
3938
3939
3940struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3941 const u8 *peer_dev_addr)
3942{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003943 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3944 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003945 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003946 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003947 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3948 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003949 }
3950
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003951 return NULL;
3952}
3953
3954
3955static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3956{
3957 struct wpa_supplicant *wpa_s = ctx;
3958
3959 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003960}
3961
3962
Dmitry Shmidt18463232014-01-24 12:29:41 -08003963static int wpas_is_concurrent_session_active(void *ctx)
3964{
3965 struct wpa_supplicant *wpa_s = ctx;
3966 struct wpa_supplicant *ifs;
3967
3968 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3969 if (ifs == wpa_s)
3970 continue;
3971 if (ifs->wpa_state > WPA_ASSOCIATED)
3972 return 1;
3973 }
3974 return 0;
3975}
3976
3977
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003978static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3979{
3980 struct wpa_supplicant *wpa_s = ctx;
3981 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3982}
3983
3984
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003985int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3986 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003987{
3988 struct wpa_interface iface;
3989 struct wpa_supplicant *p2pdev_wpa_s;
3990 char ifname[100];
3991 char force_name[100];
3992 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08003993 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003995 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3996 wpa_s->ifname);
3997 if (os_snprintf_error(sizeof(ifname), ret))
3998 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07003999 /* Cut length at the maximum size. Note that we don't need to ensure
4000 * collision free names here as the created interface is not a netdev.
4001 */
4002 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004003 force_name[0] = '\0';
4004 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004005
4006 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4007 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4008 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4009
4010 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004011 force_name, wpa_s->pending_interface_addr, NULL);
4012 if (ret < 0) {
4013 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4014 return ret;
4015 }
4016 os_strlcpy(wpa_s->pending_interface_name, ifname,
4017 sizeof(wpa_s->pending_interface_name));
4018
4019 os_memset(&iface, 0, sizeof(iface));
4020 iface.p2p_mgmt = 1;
4021 iface.ifname = wpa_s->pending_interface_name;
4022 iface.driver = wpa_s->driver->name;
4023 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004024
4025 /*
4026 * If a P2P Device configuration file was given, use it as the interface
4027 * configuration file (instead of using parent's configuration file.
4028 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004029 if (conf_p2p_dev) {
4030 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004031 iface.ctrl_interface = NULL;
4032 } else {
4033 iface.confname = wpa_s->confname;
4034 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4035 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004036
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004037 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004038 if (!p2pdev_wpa_s) {
4039 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4040 return -1;
4041 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004042
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004043 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004044 wpa_s->pending_interface_name[0] = '\0';
4045 return 0;
4046}
4047
4048
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004049static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4050 const u8 *noa, size_t noa_len)
4051{
4052 struct wpa_supplicant *wpa_s, *intf = ctx;
4053 char hex[100];
4054
4055 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4056 if (wpa_s->waiting_presence_resp)
4057 break;
4058 }
4059 if (!wpa_s) {
4060 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4061 return;
4062 }
4063 wpa_s->waiting_presence_resp = 0;
4064
4065 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4066 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4067 " status=%u noa=%s", MAC2STR(src), status, hex);
4068}
4069
4070
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004071static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4072 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004073 u8 *ret_ssid, size_t *ret_ssid_len,
4074 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004075{
4076 struct wpa_supplicant *wpa_s = ctx;
4077 struct wpa_ssid *s;
4078
4079 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4080 if (s) {
4081 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4082 *ret_ssid_len = s->ssid_len;
4083 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004084
4085 if (s->mode != WPAS_MODE_P2P_GO) {
4086 os_memset(intended_iface_addr, 0, ETH_ALEN);
4087 } else if (wpas_p2p_create_iface(wpa_s)) {
4088 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4089 return 0;
4090
4091 os_memcpy(intended_iface_addr,
4092 wpa_s->pending_interface_addr, ETH_ALEN);
4093 } else {
4094 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4095 ETH_ALEN);
4096 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004097 return 1;
4098 }
4099
4100 return 0;
4101}
4102
4103
4104static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004105 u8 *ssid, size_t *ssid_len, int *group_iface,
4106 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004107{
4108 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004109 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004110 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004111
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004112 /*
4113 * group_iface will be set to 1 only if a dedicated interface for P2P
4114 * role is required. First, we try to reuse an active GO. However,
4115 * if it is not present, we will try to reactivate an existing
4116 * persistent group and set group_iface to 1, so the caller will know
4117 * that the pending interface should be used.
4118 */
4119 *group_iface = 0;
4120
4121 if (freq)
4122 *freq = 0;
4123
4124 go = wpas_p2p_get_go_group(wpa_s);
4125 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004126 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004127 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004128 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004129 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4130 else
4131 return 0;
4132 } else {
4133 s = go->current_ssid;
4134 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4135 if (freq)
4136 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004137 }
4138
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004139 os_memcpy(ssid, s->ssid, s->ssid_len);
4140 *ssid_len = s->ssid_len;
4141
4142 return 1;
4143}
4144
4145
4146static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4147 const u8 *ssid, size_t ssid_len)
4148{
4149 struct wpa_supplicant *wpa_s = ctx;
4150 struct wpa_ssid *s;
4151 int save_config = 0;
4152 size_t i;
4153
4154 /* Start with our first choice of Persistent Groups */
4155 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4156 if (go && ssid && ssid_len &&
4157 s->ssid_len == ssid_len &&
4158 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4159 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4160 break;
4161
4162 /* Remove stale persistent group */
4163 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004164 wpa_dbg(wpa_s, MSG_DEBUG,
4165 "P2P: Remove stale persistent group id=%d",
4166 s->id);
4167 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004168 wpa_config_remove_network(wpa_s->conf, s->id);
4169 save_config = 1;
4170 continue;
4171 }
4172
4173 for (i = 0; i < s->num_p2p_clients; i++) {
4174 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4175 peer, ETH_ALEN) != 0)
4176 continue;
4177
4178 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4179 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4180 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4181 break;
4182 }
4183 s->num_p2p_clients--;
4184 save_config = 1;
4185 }
4186
4187 if (save_config)
4188 p2p_config_write(wpa_s);
4189
4190 /* Return TRUE if valid SSID remains */
4191 return s != NULL;
4192}
4193
4194
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004195static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4196 const u8 *feat_cap, size_t feat_cap_len)
4197{
4198 static const char pref[] = " feature_cap=";
4199 int ret;
4200
4201 buf[0] = '\0';
4202
4203 /*
4204 * We expect a feature capability to contain at least one byte to be
4205 * reported. The string buffer provided by the caller function is
4206 * expected to be big enough to contain all bytes of the attribute for
4207 * known specifications. This function truncates the reported bytes if
4208 * the feature capability data exceeds the string buffer size.
4209 */
4210 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4211 return;
4212
4213 os_memcpy(buf, pref, sizeof(pref));
4214 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4215 buf_len - sizeof(pref) + 1,
4216 feat_cap, feat_cap_len);
4217
4218 if (ret != (2 * (int) feat_cap_len))
4219 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4220}
4221
4222
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004223static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4224 const u8 *adv_mac, const u8 *ses_mac,
4225 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4226 u8 conncap, int passwd_id,
4227 const u8 *persist_ssid,
4228 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004229 int prov_start, const char *session_info,
4230 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004231 unsigned int freq,
4232 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004233{
4234 struct wpa_supplicant *wpa_s = ctx;
4235 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004236 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004237 int save_config = 0;
4238 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004239 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004240
4241 if (!dev)
4242 return;
4243
4244 os_memset(mac, 0, ETH_ALEN);
4245 if (!adv_mac)
4246 adv_mac = mac;
4247 if (!ses_mac)
4248 ses_mac = mac;
4249 if (!grp_mac)
4250 grp_mac = mac;
4251
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004252 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4253 feat_cap, feat_cap_len);
4254
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004255 if (prov_start) {
4256 if (session_info == NULL) {
4257 wpa_msg_global(wpa_s, MSG_INFO,
4258 P2P_EVENT_P2PS_PROVISION_START MACSTR
4259 " adv_id=%x conncap=%x"
4260 " adv_mac=" MACSTR
4261 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004262 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004263 MAC2STR(dev), adv_id, conncap,
4264 MAC2STR(adv_mac),
4265 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004266 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004267 } else {
4268 wpa_msg_global(wpa_s, MSG_INFO,
4269 P2P_EVENT_P2PS_PROVISION_START MACSTR
4270 " adv_id=%x conncap=%x"
4271 " adv_mac=" MACSTR
4272 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004273 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274 MAC2STR(dev), adv_id, conncap,
4275 MAC2STR(adv_mac),
4276 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004277 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004278 }
4279 return;
4280 }
4281
4282 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4283 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4284
4285 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4286 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4287 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4288
4289 if (persistent_go && !persistent_go->num_p2p_clients) {
4290 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004291 wpa_dbg(wpa_s, MSG_DEBUG,
4292 "P2P: Remove empty persistent group id=%d",
4293 persistent_go->id);
4294 wpas_notify_persistent_group_removed(wpa_s,
4295 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004296 wpa_config_remove_network(wpa_s->conf,
4297 persistent_go->id);
4298 }
4299
4300 wpa_msg_global(wpa_s, MSG_INFO,
4301 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4302 " status=%d"
4303 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004304 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004305 MAC2STR(dev), status,
4306 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004307 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004308 return;
4309 }
4310
4311 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004312 if (persist_ssid && persist_ssid_size)
4313 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4314 persist_ssid_size);
4315
4316 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4317 is_zero_ether_addr(grp_mac)) {
4318 wpa_dbg(wpa_s, MSG_ERROR,
4319 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4320 return;
4321 }
4322
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004323 for (;;) {
4324 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4325 if (!stale)
4326 break;
4327
4328 if (s && s->ssid_len == stale->ssid_len &&
4329 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4330 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4331 break;
4332
4333 /* Remove stale persistent group */
4334 if (stale->mode != WPAS_MODE_P2P_GO ||
4335 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004336 wpa_dbg(wpa_s, MSG_DEBUG,
4337 "P2P: Remove stale persistent group id=%d",
4338 stale->id);
4339 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004340 wpa_config_remove_network(wpa_s->conf, stale->id);
4341 } else {
4342 size_t i;
4343
4344 for (i = 0; i < stale->num_p2p_clients; i++) {
4345 if (os_memcmp(stale->p2p_client_list +
4346 i * ETH_ALEN,
4347 dev, ETH_ALEN) == 0) {
4348 os_memmove(stale->p2p_client_list +
4349 i * ETH_ALEN,
4350 stale->p2p_client_list +
4351 (i + 1) * ETH_ALEN,
4352 (stale->num_p2p_clients -
4353 i - 1) * ETH_ALEN);
4354 break;
4355 }
4356 }
4357 stale->num_p2p_clients--;
4358 }
4359 save_config = 1;
4360 }
4361
4362 if (save_config)
4363 p2p_config_write(wpa_s);
4364
4365 if (s) {
4366 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4367 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4368
4369 if (persistent_go && s != persistent_go &&
4370 !persistent_go->num_p2p_clients) {
4371 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004372 wpa_dbg(wpa_s, MSG_DEBUG,
4373 "P2P: Remove empty persistent group id=%d",
4374 persistent_go->id);
4375 wpas_notify_persistent_group_removed(wpa_s,
4376 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004377 wpa_config_remove_network(wpa_s->conf,
4378 persistent_go->id);
4379 /* Save config */
4380 }
4381
4382 wpa_msg_global(wpa_s, MSG_INFO,
4383 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4384 " status=%d"
4385 " adv_id=%x adv_mac=" MACSTR
4386 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004387 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004388 MAC2STR(dev), status,
4389 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004390 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004391 return;
4392 }
4393
Hai Shalom5f92bc92019-04-18 11:54:11 -07004394 wpa_s->global->pending_p2ps_group = 0;
4395 wpa_s->global->pending_p2ps_group_freq = 0;
4396
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004397 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004398 /*
4399 * We need to copy the interface name. Simply saving a
4400 * pointer isn't enough, since if we use pending_interface_name
4401 * it will be overwritten when the group is added.
4402 */
4403 char go_ifname[100];
4404
4405 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004406 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004407 if (!response_done) {
4408 wpa_s->global->pending_p2ps_group = 1;
4409 wpa_s->global->pending_p2ps_group_freq = freq;
4410 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004411
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004412 if (!wpas_p2p_create_iface(wpa_s))
4413 os_memcpy(go_ifname, wpa_s->ifname,
4414 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004415 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004416 os_memcpy(go_ifname,
4417 wpa_s->pending_interface_name,
4418 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004419
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004420 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004421 wpas_p2ps_prov_complete(
4422 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4423 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004424 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004425 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4426 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004427 return;
4428 }
4429
4430 /* If PD Resp complete, start up the GO */
4431 if (response_done && persistent_go) {
4432 wpas_p2p_group_add_persistent(
4433 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004434 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004435 persistent_go->mode ==
4436 WPAS_MODE_P2P_GO ?
4437 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004438 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004439 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004440 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004441 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004442 }
4443
4444 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004445 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4446 ETH_ALEN);
4447 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004448 }
4449 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004450 os_memcpy(go_ifname, go_wpa_s->ifname,
4451 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004452
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004453 if (is_zero_ether_addr(grp_mac)) {
4454 wpa_dbg(go_wpa_s, MSG_DEBUG,
4455 "P2P: Setting PIN-1 for ANY");
4456 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4457 "12345670", NULL, 0,
4458 0);
4459 } else {
4460 wpa_dbg(go_wpa_s, MSG_DEBUG,
4461 "P2P: Setting PIN-1 for " MACSTR,
4462 MAC2STR(grp_mac));
4463 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4464 "12345670", NULL, 0,
4465 0);
4466 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004467
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004468 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4469 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004470 }
4471
4472 wpa_msg_global(wpa_s, MSG_INFO,
4473 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4474 " status=%d conncap=%x"
4475 " adv_id=%x adv_mac=" MACSTR
4476 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004477 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004478 MAC2STR(dev), status, conncap,
4479 adv_id, MAC2STR(adv_mac),
4480 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004481 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004482 return;
4483 }
4484
4485 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4486 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4487
4488 if (persistent_go && !persistent_go->num_p2p_clients) {
4489 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004490 wpa_dbg(wpa_s, MSG_DEBUG,
4491 "P2P: Remove empty persistent group id=%d",
4492 persistent_go->id);
4493 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004494 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4495 }
4496
4497 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004498 char ssid_hex[32 * 2 + 1];
4499
4500 if (group_ssid)
4501 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4502 group_ssid, group_ssid_len);
4503 else
4504 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004505 wpa_msg_global(wpa_s, MSG_INFO,
4506 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4507 " status=%d conncap=%x"
4508 " adv_id=%x adv_mac=" MACSTR
4509 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004510 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004511 MAC2STR(dev), status, conncap,
4512 adv_id, MAC2STR(adv_mac),
4513 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004514 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4515 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004516 } else {
4517 wpa_msg_global(wpa_s, MSG_INFO,
4518 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4519 " status=%d conncap=%x"
4520 " adv_id=%x adv_mac=" MACSTR
4521 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004522 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004523 MAC2STR(dev), status, conncap,
4524 adv_id, MAC2STR(adv_mac),
4525 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004526 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004527 }
4528}
4529
4530
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004531static int _wpas_p2p_in_progress(void *ctx)
4532{
4533 struct wpa_supplicant *wpa_s = ctx;
4534 return wpas_p2p_in_progress(wpa_s);
4535}
4536
4537
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004538static int wpas_prov_disc_resp_cb(void *ctx)
4539{
4540 struct wpa_supplicant *wpa_s = ctx;
4541 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004542 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004543
4544 if (!wpa_s->global->pending_p2ps_group)
4545 return 0;
4546
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004547 freq = wpa_s->global->pending_p2ps_group_freq;
4548 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004549 wpa_s->global->pending_p2ps_group = 0;
4550
4551 if (wpas_p2p_get_go_group(wpa_s))
4552 return 0;
4553 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4554
4555 if (persistent_go) {
4556 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004557 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4558 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004559 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004560 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004561 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004562 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004563 }
4564
4565 return 1;
4566}
4567
4568
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004569static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4570 unsigned int *len,
4571 unsigned int *freq_list)
4572{
4573 struct wpa_supplicant *wpa_s = ctx;
4574
4575 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4576 WPA_IF_P2P_CLIENT, len, freq_list);
4577}
4578
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004579int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4580{
4581 u8 addr[ETH_ALEN] = {0};
4582
4583 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4584 return 0;
4585
Hai Shalom60840252021-02-19 19:02:11 -08004586 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4587 if (is_zero_ether_addr(
4588 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4589 !is_zero_ether_addr(wpa_s->own_addr)) {
4590 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4591 wpa_s->own_addr, ETH_ALEN);
4592 }
4593 return 0;
4594 }
4595
4596 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004597 if (random_mac_addr(addr) < 0) {
4598 wpa_msg(wpa_s, MSG_INFO,
4599 "Failed to generate random MAC address");
4600 return -EINVAL;
4601 }
4602
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004603 /* Store generated MAC address. */
4604 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4605 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004606 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004607 /* If there are existing saved groups, restore last MAC address.
4608 * if there is no last used MAC address, the last one is
4609 * factory MAC. */
4610 if (is_zero_ether_addr(
4611 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004612 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004613 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4614 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004615 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4616 }
4617
4618 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4619 wpa_msg(wpa_s, MSG_INFO,
4620 "Failed to set random MAC address");
4621 return -EINVAL;
4622 }
4623
4624 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4625 wpa_msg(wpa_s, MSG_INFO,
4626 "Could not update MAC address information");
4627 return -EINVAL;
4628 }
4629
4630 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4631 MAC2STR(addr));
4632
4633 return 0;
4634}
4635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636/**
4637 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4638 * @global: Pointer to global data from wpa_supplicant_init()
4639 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4640 * Returns: 0 on success, -1 on failure
4641 */
4642int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4643{
4644 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 int i;
4646
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004647 if (wpa_s->conf->p2p_disabled)
4648 return 0;
4649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4651 return 0;
4652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004653 if (global->p2p)
4654 return 0;
4655
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004656 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4657 wpa_msg(wpa_s, MSG_ERROR,
4658 "Failed to initialize P2P random MAC address.");
4659 return -1;
4660 }
4661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004664 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004665 p2p.p2p_scan = wpas_p2p_scan;
4666 p2p.send_action = wpas_send_action;
4667 p2p.send_action_done = wpas_send_action_done;
4668 p2p.go_neg_completed = wpas_go_neg_completed;
4669 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4670 p2p.dev_found = wpas_dev_found;
4671 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004672 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 p2p.start_listen = wpas_start_listen;
4674 p2p.stop_listen = wpas_stop_listen;
4675 p2p.send_probe_resp = wpas_send_probe_resp;
4676 p2p.sd_request = wpas_sd_request;
4677 p2p.sd_response = wpas_sd_response;
4678 p2p.prov_disc_req = wpas_prov_disc_req;
4679 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004680 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 p2p.invitation_process = wpas_invitation_process;
4682 p2p.invitation_received = wpas_invitation_received;
4683 p2p.invitation_result = wpas_invitation_result;
4684 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004685 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004686 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004687 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004688 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004689 p2p.get_persistent_group = wpas_get_persistent_group;
4690 p2p.get_go_info = wpas_get_go_info;
4691 p2p.remove_stale_groups = wpas_remove_stale_groups;
4692 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4693 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4694 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004695 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004696 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697
4698 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004699 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 p2p.dev_name = wpa_s->conf->device_name;
4701 p2p.manufacturer = wpa_s->conf->manufacturer;
4702 p2p.model_name = wpa_s->conf->model_name;
4703 p2p.model_number = wpa_s->conf->model_number;
4704 p2p.serial_number = wpa_s->conf->serial_number;
4705 if (wpa_s->wps) {
4706 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4707 p2p.config_methods = wpa_s->wps->config_methods;
4708 }
4709
Hai Shalom60840252021-02-19 19:02:11 -08004710 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4711 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004712 wpa_printf(MSG_ERROR,
4713 "P2P: Failed to configure supported channel list");
4714 return -1;
4715 }
4716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004717 if (wpa_s->conf->p2p_listen_reg_class &&
4718 wpa_s->conf->p2p_listen_channel) {
4719 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4720 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004721 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 /*
4724 * Pick one of the social channels randomly as the listen
4725 * channel.
4726 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004727 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004728 &p2p.channel,
4729 &global->p2p_go_avoid_freq,
4730 &global->p2p_disallow_freq) !=
4731 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004732 wpa_printf(MSG_INFO,
4733 "P2P: No social channels supported by the driver - do not enable P2P");
4734 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004735 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004736 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004738 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4739 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740
4741 if (wpa_s->conf->p2p_oper_reg_class &&
4742 wpa_s->conf->p2p_oper_channel) {
4743 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4744 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4745 p2p.cfg_op_channel = 1;
4746 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4747 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4748
4749 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004751 * Use random operation channel from 2.4 GHz band social
4752 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4753 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004754 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004755 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004756 &p2p.op_channel, NULL,
4757 NULL) != 0) {
4758 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004759 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004760 p2p.op_reg_class = 0;
4761 p2p.op_channel = 0;
4762 /* This will be overridden during group setup in
4763 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004765 p2p.cfg_op_channel = 0;
4766 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4767 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4768 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004769
4770 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4771 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4772 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4773 }
4774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4776 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4777 p2p.country[2] = 0x04;
4778 } else
4779 os_memcpy(p2p.country, "XX\x04", 3);
4780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4782 WPS_DEV_TYPE_LEN);
4783
4784 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4785 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4786 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4787
4788 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4789 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4790
4791 p2p.max_peers = 100;
4792
4793 if (wpa_s->conf->p2p_ssid_postfix) {
4794 p2p.ssid_postfix_len =
4795 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4796 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4797 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4798 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4799 p2p.ssid_postfix_len);
4800 }
4801
4802 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4803
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004804 p2p.max_listen = wpa_s->max_remain_on_chan;
4805
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004806 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4807 wpa_s->conf->p2p_passphrase_len <= 63)
4808 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4809 else
4810 p2p.passphrase_len = 8;
4811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812 global->p2p = p2p_init(&p2p);
4813 if (global->p2p == NULL)
4814 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004815 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816
4817 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4818 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4819 continue;
4820 p2p_add_wps_vendor_extension(
4821 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4822 }
4823
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004824 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 return 0;
4827}
4828
4829
4830/**
4831 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4832 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4833 *
4834 * This function deinitialize per-interface P2P data.
4835 */
4836void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4837{
4838 if (wpa_s->driver && wpa_s->drv_priv)
4839 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004840
4841 if (wpa_s->go_params) {
4842 /* Clear any stored provisioning info */
4843 p2p_clear_provisioning_info(
4844 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004845 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004846 }
4847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 os_free(wpa_s->go_params);
4849 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004850 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4852 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07004853 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4855 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4856 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004857 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07004858 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004859 wpas_p2p_listen_work_done(wpa_s);
4860 if (wpa_s->p2p_send_action_work) {
4861 os_free(wpa_s->p2p_send_action_work->ctx);
4862 radio_work_done(wpa_s->p2p_send_action_work);
4863 wpa_s->p2p_send_action_work = NULL;
4864 }
4865 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004867 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4868 wpa_s->p2p_oob_dev_pw = NULL;
4869
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004870 os_free(wpa_s->p2p_group_common_freqs);
4871 wpa_s->p2p_group_common_freqs = NULL;
4872 wpa_s->p2p_group_common_freqs_num = 0;
4873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 /* TODO: remove group interface from the driver if this wpa_s instance
4875 * is on top of a P2P group interface */
4876}
4877
4878
4879/**
4880 * wpas_p2p_deinit_global - Deinitialize global P2P module
4881 * @global: Pointer to global data from wpa_supplicant_init()
4882 *
4883 * This function deinitializes the global (per device) P2P module.
4884 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004885static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004886{
4887 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004889 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004890
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004891 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892
4893 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004894 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4895 wpa_s = wpa_s->next;
4896 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897 tmp = global->ifaces;
4898 while (tmp &&
4899 (tmp == wpa_s ||
4900 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4901 tmp = tmp->next;
4902 }
4903 if (tmp == NULL)
4904 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004905 /* Disconnect from the P2P group and deinit the interface */
4906 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907 }
4908
4909 /*
4910 * Deinit GO data on any possibly remaining interface (if main
4911 * interface is used as GO).
4912 */
4913 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4914 if (wpa_s->ap_iface)
4915 wpas_p2p_group_deinit(wpa_s);
4916 }
4917
4918 p2p_deinit(global->p2p);
4919 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004920 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921}
4922
4923
4924static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4925{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004926 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004927 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 if (wpa_s->drv_flags &
4929 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4930 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4931 return 1; /* P2P group requires a new interface in every case
4932 */
4933 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4934 return 0; /* driver does not support concurrent operations */
4935 if (wpa_s->global->ifaces->next)
4936 return 1; /* more that one interface already in use */
4937 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4938 return 1; /* this interface is already in use */
4939 return 0;
4940}
4941
4942
4943static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4944 const u8 *peer_addr,
4945 enum p2p_wps_method wps_method,
4946 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004947 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004948 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004950 if (persistent_group && wpa_s->conf->persistent_reconnect)
4951 persistent_group = 2;
4952
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004953 /*
4954 * Increase GO config timeout if HT40 is used since it takes some time
4955 * to scan channels for coex purposes before the BSS can be started.
4956 */
4957 p2p_set_config_timeout(wpa_s->global->p2p,
4958 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4961 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004962 persistent_group, ssid ? ssid->ssid : NULL,
4963 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004964 wpa_s->p2p_pd_before_go_neg, pref_freq,
4965 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4966 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967}
4968
4969
4970static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4971 const u8 *peer_addr,
4972 enum p2p_wps_method wps_method,
4973 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004974 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004975 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004977 if (persistent_group && wpa_s->conf->persistent_reconnect)
4978 persistent_group = 2;
4979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4981 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004982 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004983 ssid ? ssid->ssid_len : 0, pref_freq,
4984 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4985 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986}
4987
4988
4989static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4990{
4991 wpa_s->p2p_join_scan_count++;
4992 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4993 wpa_s->p2p_join_scan_count);
4994 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4995 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4996 " for join operationg - stop join attempt",
4997 MAC2STR(wpa_s->pending_join_iface_addr));
4998 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004999 if (wpa_s->p2p_auto_pd) {
5000 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005001 wpa_msg_global(wpa_s, MSG_INFO,
5002 P2P_EVENT_PROV_DISC_FAILURE
5003 " p2p_dev_addr=" MACSTR " status=N/A",
5004 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005005 return;
5006 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005007 if (wpa_s->p2p_fallback_to_go_neg) {
5008 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5009 "failed - fall back to GO Negotiation");
5010 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5011 P2P_EVENT_FALLBACK_TO_GO_NEG
5012 "reason=join-failed");
5013 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5014 return;
5015 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005016 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005017 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005018 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 }
5020}
5021
5022
Dmitry Shmidt04949592012-07-19 12:16:46 -07005023static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5024{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005025 int res;
5026 unsigned int num, i;
5027 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005028
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005029 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5030 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005031 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005032 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005033
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005034 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5035 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005036 if (!freqs)
5037 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005038
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005039 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5040 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005041
5042 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005043 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005044 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5045 freq);
5046 res = 0;
5047 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005048 }
5049 }
5050
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005051 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005052 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005053
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005054exit_free:
5055 os_free(freqs);
5056 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005057}
5058
5059
5060static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5061 const u8 *peer_dev_addr)
5062{
5063 struct wpa_bss *bss;
5064 int updated;
5065
5066 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5067 if (bss == NULL)
5068 return -1;
5069 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5070 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5071 "last scan");
5072 return 0;
5073 }
5074
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005075 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5076 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005077 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5078 "%ld.%06ld (%supdated in last scan)",
5079 bss->last_update.sec, bss->last_update.usec,
5080 updated ? "": "not ");
5081
5082 return updated;
5083}
5084
5085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5087 struct wpa_scan_results *scan_res)
5088{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005089 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090 int freq;
5091 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5094
5095 if (wpa_s->global->p2p_disabled)
5096 return;
5097
Dmitry Shmidt04949592012-07-19 12:16:46 -07005098 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5099 scan_res ? (int) scan_res->num : -1,
5100 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101
5102 if (scan_res)
5103 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5104
Dmitry Shmidt04949592012-07-19 12:16:46 -07005105 if (wpa_s->p2p_auto_pd) {
5106 int join = wpas_p2p_peer_go(wpa_s,
5107 wpa_s->pending_join_dev_addr);
5108 if (join == 0 &&
5109 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5110 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005111 bss = wpa_bss_get_bssid_latest(
5112 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005113 if (bss) {
5114 freq = bss->freq;
5115 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5116 "the peer " MACSTR " at %d MHz",
5117 wpa_s->auto_pd_scan_retry,
5118 MAC2STR(wpa_s->
5119 pending_join_dev_addr),
5120 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005121 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005122 return;
5123 }
5124 }
5125
5126 if (join < 0)
5127 join = 0;
5128
5129 wpa_s->p2p_auto_pd = 0;
5130 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5131 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5132 MAC2STR(wpa_s->pending_join_dev_addr), join);
5133 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005134 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005135 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005136 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005137 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005138 wpa_msg_global(wpa_s, MSG_INFO,
5139 P2P_EVENT_PROV_DISC_FAILURE
5140 " p2p_dev_addr=" MACSTR " status=N/A",
5141 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005142 }
5143 return;
5144 }
5145
5146 if (wpa_s->p2p_auto_join) {
5147 int join = wpas_p2p_peer_go(wpa_s,
5148 wpa_s->pending_join_dev_addr);
5149 if (join < 0) {
5150 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5151 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005152 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005153 P2P_EVENT_FALLBACK_TO_GO_NEG
5154 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005155 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5156 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5157 wpa_s->p2p_persistent_group, 0, 0, 0,
5158 wpa_s->p2p_go_intent,
5159 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005160 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005161 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005162 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005163 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005164 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005165 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005166 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005167 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005168 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005169 return;
5170 }
5171
5172 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5173 "try to join the group", join ? "" :
5174 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005175 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005176 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005177 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005178 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005179 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005180 }
5181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5183 wpa_s->pending_join_iface_addr);
5184 if (freq < 0 &&
5185 p2p_get_interface_addr(wpa_s->global->p2p,
5186 wpa_s->pending_join_dev_addr,
5187 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005188 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5189 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5191 "address for join from " MACSTR " to " MACSTR
5192 " based on newly discovered P2P peer entry",
5193 MAC2STR(wpa_s->pending_join_iface_addr),
5194 MAC2STR(iface_addr));
5195 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5196 ETH_ALEN);
5197
5198 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5199 wpa_s->pending_join_iface_addr);
5200 }
5201 if (freq >= 0) {
5202 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5203 "from P2P peer table: %d MHz", freq);
5204 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005205 if (wpa_s->p2p_join_ssid_len) {
5206 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5207 MACSTR " and SSID %s",
5208 MAC2STR(wpa_s->pending_join_iface_addr),
5209 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5210 wpa_s->p2p_join_ssid_len));
5211 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5212 wpa_s->p2p_join_ssid,
5213 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005214 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005215 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5216 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5217 bss = wpa_bss_get_bssid_latest(wpa_s,
5218 wpa_s->pending_join_iface_addr);
5219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005221 u8 dev_addr[ETH_ALEN];
5222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223 freq = bss->freq;
5224 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005225 "from BSS table: %d MHz (SSID %s)", freq,
5226 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005227 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005228 dev_addr) == 0 &&
5229 os_memcmp(wpa_s->pending_join_dev_addr,
5230 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5231 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5232 ETH_ALEN) != 0) {
5233 wpa_printf(MSG_DEBUG,
5234 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5235 MAC2STR(dev_addr),
5236 MAC2STR(wpa_s->pending_join_dev_addr));
5237 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5238 ETH_ALEN);
5239 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240 }
5241 if (freq > 0) {
5242 u16 method;
5243
Dmitry Shmidt04949592012-07-19 12:16:46 -07005244 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005245 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005246 P2P_EVENT_GROUP_FORMATION_FAILURE
5247 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005248 wpas_notify_p2p_group_formation_failure(
5249 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005250 return;
5251 }
5252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5254 "prior to joining an existing group (GO " MACSTR
5255 " freq=%u MHz)",
5256 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5257 wpa_s->pending_pd_before_join = 1;
5258
5259 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 case WPS_PIN_DISPLAY:
5261 method = WPS_CONFIG_KEYPAD;
5262 break;
5263 case WPS_PIN_KEYPAD:
5264 method = WPS_CONFIG_DISPLAY;
5265 break;
5266 case WPS_PBC:
5267 method = WPS_CONFIG_PUSHBUTTON;
5268 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005269 case WPS_P2PS:
5270 method = WPS_CONFIG_P2PS;
5271 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272 default:
5273 method = 0;
5274 break;
5275 }
5276
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005277 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5278 wpa_s->pending_join_dev_addr) ==
5279 method)) {
5280 /*
5281 * We have already performed provision discovery for
5282 * joining the group. Proceed directly to join
5283 * operation without duplicated provision discovery. */
5284 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5285 "with " MACSTR " already done - proceed to "
5286 "join",
5287 MAC2STR(wpa_s->pending_join_dev_addr));
5288 wpa_s->pending_pd_before_join = 0;
5289 goto start;
5290 }
5291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005293 wpa_s->pending_join_dev_addr,
5294 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005295 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5297 "Discovery Request before joining an "
5298 "existing group");
5299 wpa_s->pending_pd_before_join = 0;
5300 goto start;
5301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005302 return;
5303 }
5304
5305 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5306 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5307 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5308 wpas_p2p_check_join_scan_limit(wpa_s);
5309 return;
5310
5311start:
5312 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005313 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5314 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315}
5316
5317
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005318static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5319 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005320{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005321 int ret;
5322 struct wpa_driver_scan_params params;
5323 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005324 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005325 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005326 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005327
5328 os_memset(&params, 0, sizeof(params));
5329
5330 /* P2P Wildcard SSID */
5331 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005332 if (ssid && ssid_len) {
5333 params.ssids[0].ssid = ssid;
5334 params.ssids[0].ssid_len = ssid_len;
5335 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5336 wpa_s->p2p_join_ssid_len = ssid_len;
5337 } else {
5338 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5339 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5340 wpa_s->p2p_join_ssid_len = 0;
5341 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342
5343 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5345 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5346 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347 if (wps_ie == NULL) {
5348 wpas_p2p_scan_res_join(wpa_s, NULL);
5349 return;
5350 }
5351
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005352 if (!freq) {
5353 int oper_freq;
5354 /*
5355 * If freq is not provided, check the operating freq of the GO
5356 * and use a single channel scan on if possible.
5357 */
5358 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5359 wpa_s->pending_join_iface_addr);
5360 if (oper_freq > 0)
5361 freq = oper_freq;
5362 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363 if (freq > 0) {
5364 freqs[0] = freq;
5365 params.freqs = freqs;
Hai Shalom899fcc72020-10-19 14:38:18 -07005366 } else if (wpa_s->conf->p2p_6ghz_disable) {
5367 wpa_printf(MSG_DEBUG,
5368 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005369 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5370 0);
5371 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5372 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005373 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005375 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5376 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5377 if (ies == NULL) {
5378 wpabuf_free(wps_ie);
5379 wpas_p2p_scan_res_join(wpa_s, NULL);
5380 return;
5381 }
5382 wpabuf_put_buf(ies, wps_ie);
5383 wpabuf_free(wps_ie);
5384
5385 bands = wpas_get_bands(wpa_s, freqs);
5386 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5387
5388 params.p2p_probe = 1;
5389 params.extra_ies = wpabuf_head(ies);
5390 params.extra_ies_len = wpabuf_len(ies);
5391
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005392 if (wpa_s->clear_driver_scan_cache) {
5393 wpa_printf(MSG_DEBUG,
5394 "Request driver to clear scan cache due to local BSS flush");
5395 params.only_new_results = 1;
5396 }
5397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 /*
5399 * Run a scan to update BSS table and start Provision Discovery once
5400 * the new scan results become available.
5401 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005402 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalom899fcc72020-10-19 14:38:18 -07005403 if (wpa_s->conf->p2p_6ghz_disable && params.freqs != freqs)
5404 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005405 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005406 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005407 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005408 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005409 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411
5412 wpabuf_free(ies);
5413
5414 if (ret) {
5415 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5416 "try again later");
5417 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5418 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5419 wpas_p2p_check_join_scan_limit(wpa_s);
5420 }
5421}
5422
5423
Dmitry Shmidt04949592012-07-19 12:16:46 -07005424static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5425{
5426 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005427 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005428}
5429
5430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005432 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005433 int auto_join, int op_freq,
5434 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435{
5436 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005437 MACSTR " dev " MACSTR " op_freq=%d)%s",
5438 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005439 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005440 if (ssid && ssid_len) {
5441 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5442 wpa_ssid_txt(ssid, ssid_len));
5443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444
Dmitry Shmidt04949592012-07-19 12:16:46 -07005445 wpa_s->p2p_auto_pd = 0;
5446 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5448 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5449 wpa_s->pending_join_wps_method = wps_method;
5450
5451 /* Make sure we are not running find during connection establishment */
5452 wpas_p2p_stop_find(wpa_s);
5453
5454 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005455 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 return 0;
5457}
5458
5459
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005460static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5461 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462{
5463 struct wpa_supplicant *group;
5464 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005465 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466
5467 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5468 if (group == NULL)
5469 return -1;
5470 if (group != wpa_s) {
5471 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5472 sizeof(group->p2p_pin));
5473 group->p2p_wps_method = wpa_s->p2p_wps_method;
5474 }
5475
Hai Shalom74f70d42019-02-11 14:42:39 -08005476 /*
5477 * Need to mark the current interface for p2p_group_formation
5478 * when a separate group interface is not used. This is needed
5479 * to allow p2p_cancel stop a pending p2p_connect-join.
5480 * wpas_p2p_init_group_interface() addresses this for the case
5481 * where a separate group interface is used.
5482 */
5483 if (group == wpa_s->parent)
5484 wpa_s->global->p2p_group_formation = group;
5485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005487 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488
5489 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005490 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005491 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5492 ETH_ALEN);
5493 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005494 if (freq && ssid && ssid_len) {
5495 res.freq = freq;
5496 res.ssid_len = ssid_len;
5497 os_memcpy(res.ssid, ssid, ssid_len);
5498 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005499 if (ssid && ssid_len) {
5500 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5501 ssid, ssid_len);
5502 } else {
5503 bss = wpa_bss_get_bssid_latest(
5504 wpa_s, wpa_s->pending_join_iface_addr);
5505 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005506 if (bss) {
5507 res.freq = bss->freq;
5508 res.ssid_len = bss->ssid_len;
5509 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5510 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5511 bss->freq,
5512 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005513 } else if (ssid && ssid_len) {
5514 res.ssid_len = ssid_len;
5515 os_memcpy(res.ssid, ssid, ssid_len);
5516 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5517 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005518 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005519 }
5520
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005521 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5522 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5523 "starting client");
5524 wpa_drv_cancel_remain_on_channel(wpa_s);
5525 wpa_s->off_channel_freq = 0;
5526 wpa_s->roc_waiting_drv_freq = 0;
5527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528 wpas_start_wps_enrollee(group, &res);
5529
5530 /*
5531 * Allow a longer timeout for join-a-running-group than normal 15
5532 * second group formation timeout since the GO may not have authorized
5533 * our connection yet.
5534 */
5535 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5536 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5537 wpa_s, NULL);
5538
5539 return 0;
5540}
5541
5542
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005543static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005544 int *force_freq, int *pref_freq, int go,
5545 unsigned int *pref_freq_list,
5546 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005547{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005548 struct wpa_used_freq_data *freqs;
5549 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005550 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5551
5552 max_pref_freq = *num_pref_freq;
5553 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005554
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005555 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5556 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005557 if (!freqs)
5558 return -1;
5559
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005560 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5561 wpa_s->num_multichan_concurrent);
5562
5563 /*
5564 * It is possible that the total number of used frequencies is bigger
5565 * than the number of frequencies used for P2P, so get the system wide
5566 * number of unused frequencies.
5567 */
5568 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5569
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005570 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005571 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5572 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005573
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005574 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005575 int ret;
5576 if (go)
5577 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5578 else
5579 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5580 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005581 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005582 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5583 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005584 /*
5585 * If freq is a DFS channel and DFS is offloaded
5586 * to the driver, allow P2P GO to use it.
5587 */
5588 wpa_printf(MSG_DEBUG,
5589 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5590 freq);
5591 } else {
5592 wpa_printf(MSG_DEBUG,
5593 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5594 freq);
5595 res = -3;
5596 goto exit_free;
5597 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005598 }
5599
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005600 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005601 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005602 freq_in_use = 1;
5603 }
5604
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005605 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005606 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5607 freq);
5608 res = -2;
5609 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005610 }
5611 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5612 "requested channel (%u MHz)", freq);
5613 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005614 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005615 }
5616
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005617 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005618
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005619 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5620 enum wpa_driver_if_type iface_type;
5621
5622 if (go)
5623 iface_type = WPA_IF_P2P_GO;
5624 else
5625 iface_type = WPA_IF_P2P_CLIENT;
5626
5627 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5628 best_freq, go);
5629
5630 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5631 &max_pref_freq,
5632 pref_freq_list);
5633 if (!res && max_pref_freq > 0) {
5634 *num_pref_freq = max_pref_freq;
5635 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005636 while (i < *num_pref_freq &&
5637 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005638 pref_freq_list[i]) ||
5639 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005640 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005641 wpa_printf(MSG_DEBUG,
5642 "P2P: preferred_freq_list[%d]=%d is disallowed",
5643 i, pref_freq_list[i]);
5644 i++;
5645 }
5646 if (i != *num_pref_freq) {
5647 best_freq = pref_freq_list[i];
5648 wpa_printf(MSG_DEBUG,
5649 "P2P: Using preferred_freq_list[%d]=%d",
5650 i, best_freq);
5651 } else {
5652 wpa_printf(MSG_DEBUG,
5653 "P2P: All driver preferred frequencies are disallowed for P2P use");
5654 *num_pref_freq = 0;
5655 }
5656 } else {
5657 wpa_printf(MSG_DEBUG,
5658 "P2P: No preferred frequency list available");
5659 }
5660 }
5661
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005662 /* We have a candidate frequency to use */
5663 if (best_freq > 0) {
5664 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005665 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005666 best_freq);
5667 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005668 } else {
5669 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 -07005670 best_freq);
5671 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005672 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005673 } else if (num_unused > 0) {
5674 wpa_printf(MSG_DEBUG,
5675 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5676 *force_freq = 0;
5677 } else {
5678 wpa_printf(MSG_DEBUG,
5679 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5680 res = -2;
5681 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005682 }
5683
5684exit_ok:
5685 res = 0;
5686exit_free:
5687 os_free(freqs);
5688 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005689}
5690
5691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005692/**
5693 * wpas_p2p_connect - Request P2P Group Formation to be started
5694 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5695 * @peer_addr: Address of the peer P2P Device
5696 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5697 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005698 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005699 * @join: Whether to join an existing group (as a client) instead of starting
5700 * Group Owner negotiation; @peer_addr is BSSID in that case
5701 * @auth: Whether to only authorize the connection instead of doing that and
5702 * initiating Group Owner negotiation
5703 * @go_intent: GO Intent or -1 to use default
5704 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005705 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005706 * @persistent_id: Persistent group credentials to use for forcing GO
5707 * parameters or -1 to generate new values (SSID/passphrase)
5708 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5709 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005710 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005711 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005712 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005713 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005714 * @group_ssid: Specific Group SSID for join or %NULL if not set
5715 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5717 * failure, -2 on failure due to channel not currently available,
5718 * -3 if forced channel is not supported
5719 */
5720int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5721 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005722 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005723 int go_intent, int freq, unsigned int vht_center_freq2,
5724 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005725 unsigned int vht_chwidth, int he, int edmg,
5726 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005728 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005729 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005731 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005732 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005733 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005734
5735 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5736 return -1;
5737
Dmitry Shmidt04949592012-07-19 12:16:46 -07005738 if (persistent_id >= 0) {
5739 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5740 if (ssid == NULL || ssid->disabled != 2 ||
5741 ssid->mode != WPAS_MODE_P2P_GO)
5742 return -1;
5743 }
5744
Hai Shalom899fcc72020-10-19 14:38:18 -07005745 if (is_6ghz_freq(freq) && wpa_s->conf->p2p_6ghz_disable)
5746 return -2;
5747
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005748 os_free(wpa_s->global->add_psk);
5749 wpa_s->global->add_psk = NULL;
5750
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005751 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005752 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005753 wpa_s->global->pending_p2ps_group_freq = 0;
5754 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756 if (go_intent < 0)
5757 go_intent = wpa_s->conf->p2p_go_intent;
5758
5759 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005760 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761
5762 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005763 wpa_s->p2p_persistent_group = !!persistent_group;
5764 wpa_s->p2p_persistent_id = persistent_id;
5765 wpa_s->p2p_go_intent = go_intent;
5766 wpa_s->p2p_connect_freq = freq;
5767 wpa_s->p2p_fallback_to_go_neg = 0;
5768 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005769 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005770 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005771 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5772 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005773 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005774 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775
5776 if (pin)
5777 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5778 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005779 if (wps_generate_pin((unsigned int *) &ret) < 0)
5780 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005781 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5782 "%08d", ret);
5783 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5784 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005785 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5786 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005787 } else if (wps_method == WPS_P2PS) {
5788 /* Force the P2Ps default PIN to be used */
5789 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005790 } else
5791 wpa_s->p2p_pin[0] = '\0';
5792
Dmitry Shmidt04949592012-07-19 12:16:46 -07005793 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5795 if (auth) {
5796 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5797 "connect a running group from " MACSTR,
5798 MAC2STR(peer_addr));
5799 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5800 return ret;
5801 }
5802 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5803 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5804 iface_addr) < 0) {
5805 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5806 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5807 dev_addr);
5808 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005809 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005810 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005811 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5812 "%ld.%06ld",
5813 wpa_s->p2p_auto_started.sec,
5814 wpa_s->p2p_auto_started.usec);
5815 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005816 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005817 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005818 auto_join, freq,
5819 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820 return -1;
5821 return ret;
5822 }
5823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005824 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005825 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005826 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005827 if (res)
5828 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005829 wpas_p2p_set_own_freq_preference(wpa_s,
5830 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005831
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005832 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005834 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005836 if (wpa_s->create_p2p_iface) {
5837 /* Prepare to add a new interface for the group */
5838 iftype = WPA_IF_P2P_GROUP;
5839 if (go_intent == 15)
5840 iftype = WPA_IF_P2P_GO;
5841 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5842 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5843 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005844 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005845 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005847 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005848 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005849 if (wpa_s->p2p_mgmt)
5850 if_addr = wpa_s->parent->own_addr;
5851 else
5852 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005853 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5854 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855
5856 if (auth) {
5857 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005858 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005859 force_freq, persistent_group, ssid,
5860 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861 return -1;
5862 return ret;
5863 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005864
5865 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5866 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005867 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005868 if (wpa_s->create_p2p_iface)
5869 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005870 return -1;
5871 }
5872 return ret;
5873}
5874
5875
5876/**
5877 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5878 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5879 * @freq: Frequency of the channel in MHz
5880 * @duration: Duration of the stay on the channel in milliseconds
5881 *
5882 * This callback is called when the driver indicates that it has started the
5883 * requested remain-on-channel duration.
5884 */
5885void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5886 unsigned int freq, unsigned int duration)
5887{
5888 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5889 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005890 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)",
5891 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5892 wpa_s->roc_waiting_drv_freq, freq, duration);
5893 if (wpa_s->off_channel_freq &&
5894 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5896 wpa_s->pending_listen_duration);
5897 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005898 } else {
5899 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5900 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5901 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005902 }
5903}
5904
5905
Jithu Jance57cea1a2014-08-20 10:22:04 -07005906int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907{
5908 /* Limit maximum Listen state time based on driver limitation. */
5909 if (timeout > wpa_s->max_remain_on_chan)
5910 timeout = wpa_s->max_remain_on_chan;
5911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912 return p2p_listen(wpa_s->global->p2p, timeout);
5913}
5914
5915
5916/**
5917 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5918 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5919 * @freq: Frequency of the channel in MHz
5920 *
5921 * This callback is called when the driver indicates that a remain-on-channel
5922 * operation has been completed, i.e., the duration on the requested channel
5923 * has timed out.
5924 */
5925void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5926 unsigned int freq)
5927{
5928 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5929 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005930 wpa_s->global->p2p_long_listen,
5931 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005932 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5934 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005935 if (wpa_s->global->p2p_long_listen > 0)
5936 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5938 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005939 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005940 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005941 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005942 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005943 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005944 } else {
5945 /*
5946 * When listen duration is over, stop listen & update p2p_state
5947 * to IDLE.
5948 */
5949 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005950 }
5951}
5952
5953
5954/**
5955 * wpas_p2p_group_remove - Remove a P2P group
5956 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5957 * @ifname: Network interface name of the group interface or "*" to remove all
5958 * groups
5959 * Returns: 0 on success, -1 on failure
5960 *
5961 * This function is used to remove a P2P group. This can be used to disconnect
5962 * from a group in which the local end is a P2P Client or to end a P2P Group in
5963 * case the local end is the Group Owner. If a virtual network interface was
5964 * created for this group, that interface will be removed. Otherwise, only the
5965 * configured P2P group network will be removed from the interface.
5966 */
5967int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5968{
5969 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005970 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971
5972 if (os_strcmp(ifname, "*") == 0) {
5973 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07005974 bool calling_wpa_s_group_removed = false;
5975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976 wpa_s = global->ifaces;
5977 while (wpa_s) {
5978 prev = wpa_s;
5979 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005980 if (prev->p2p_group_interface !=
5981 NOT_P2P_GROUP_INTERFACE ||
5982 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07005983 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005984 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07005985 if (prev == calling_wpa_s)
5986 calling_wpa_s_group_removed = true;
5987 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005989
5990 if (!calling_wpa_s_group_removed &&
5991 (calling_wpa_s->p2p_group_interface !=
5992 NOT_P2P_GROUP_INTERFACE ||
5993 (calling_wpa_s->current_ssid &&
5994 calling_wpa_s->current_ssid->p2p_group))) {
5995 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
5996 wpas_p2p_disconnect_safely(calling_wpa_s,
5997 calling_wpa_s);
5998 }
5999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000 return 0;
6001 }
6002
6003 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6004 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6005 break;
6006 }
6007
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006008 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009}
6010
6011
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006012static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6013{
6014 unsigned int r;
6015
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006016 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6017 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
6018 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
6019 int res;
6020
6021 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6022 &size, pref_freq_list);
6023 if (!res && size > 0) {
6024 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006025 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006026 (!p2p_supported_freq(wpa_s->global->p2p,
6027 pref_freq_list[i]) ||
6028 wpas_p2p_disallowed_freq(wpa_s->global,
6029 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006030 wpa_printf(MSG_DEBUG,
6031 "P2P: preferred_freq_list[%d]=%d is disallowed",
6032 i, pref_freq_list[i]);
6033 i++;
6034 }
6035 if (i != size) {
6036 freq = pref_freq_list[i];
6037 wpa_printf(MSG_DEBUG,
6038 "P2P: Using preferred_freq_list[%d]=%d",
6039 i, freq);
6040 } else {
6041 wpa_printf(MSG_DEBUG,
6042 "P2P: All driver preferred frequencies are disallowed for P2P use");
6043 }
6044 } else {
6045 wpa_printf(MSG_DEBUG,
6046 "P2P: No preferred frequency list available");
6047 }
6048 }
6049
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006050 if (freq == 2) {
6051 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6052 "band");
6053 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006054 p2p_supported_freq_go(wpa_s->global->p2p,
6055 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006056 freq = wpa_s->best_24_freq;
6057 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6058 "channel: %d MHz", freq);
6059 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006060 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6061 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006062 freq = 2412 + (r % 3) * 25;
6063 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6064 "channel: %d MHz", freq);
6065 }
6066 }
6067
6068 if (freq == 5) {
6069 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6070 "band");
6071 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006072 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006073 wpa_s->best_5_freq)) {
6074 freq = wpa_s->best_5_freq;
6075 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6076 "channel: %d MHz", freq);
6077 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006078 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6079 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006080
6081 /*
6082 * most of 5G channels are DFS, only operating class 115 and 124
6083 * are available possibly, randomly pick a start to check them.
6084 */
6085 int possible_5g_freqs[] = {
6086 /* operating class 115 */
6087 5180, 5200, 5220, 5240,
6088 /* operating class 124 */
6089 5745, 5765, 5785, 5805,
6090 };
6091 int possible_5g_freqs_num =
6092 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6093
6094 int i;
6095 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6096 if (p2p_supported_freq_go(
6097 wpa_s->global->p2p,
6098 possible_5g_freqs[r % possible_5g_freqs_num])) {
6099 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6100 break;
6101 }
6102 }
6103
6104 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006105 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6106 "5 GHz channel for P2P group");
6107 return -1;
6108 }
6109 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6110 "channel: %d MHz", freq);
6111 }
6112 }
6113
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006114 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006115 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006116 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6117 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006118 /*
6119 * If freq is a DFS channel and DFS is offloaded to the
6120 * driver, allow P2P GO to use it.
6121 */
6122 wpa_printf(MSG_DEBUG, "P2P: "
6123 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6124 __func__, freq);
6125 return freq;
6126 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006127 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6128 "(%u MHz) is not supported for P2P uses",
6129 freq);
6130 return -1;
6131 }
6132
6133 return freq;
6134}
6135
6136
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006137static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6138 const struct p2p_channels *channels,
6139 int freq)
6140{
6141 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6142 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6143 freq_included(wpa_s, channels, freq))
6144 return 1;
6145 return 0;
6146}
6147
6148
6149static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6150 struct p2p_go_neg_results *params,
6151 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006152{
6153 unsigned int i, r;
6154
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006155 /* try all channels in operating class 115 */
6156 for (i = 0; i < 4; i++) {
6157 params->freq = 5180 + i * 20;
6158 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006159 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006160 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6161 goto out;
6162 }
6163
6164 /* try all channels in operating class 124 */
6165 for (i = 0; i < 4; i++) {
6166 params->freq = 5745 + i * 20;
6167 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006168 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006169 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6170 goto out;
6171 }
6172
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006173 /* try social channel class 180 channel 2 */
6174 params->freq = 58320 + 1 * 2160;
6175 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006176 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006177 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6178 goto out;
6179
6180 /* try all channels in reg. class 180 */
6181 for (i = 0; i < 4; i++) {
6182 params->freq = 58320 + i * 2160;
6183 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006184 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006185 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6186 goto out;
6187 }
6188
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006189 /* try some random selection of the social channels */
6190 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6191 return;
6192
6193 for (i = 0; i < 3; i++) {
6194 params->freq = 2412 + ((r + i) % 3) * 25;
6195 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6196 goto out;
6197 }
6198
6199 /* try all other channels in operating class 81 */
6200 for (i = 0; i < 11; i++) {
6201 params->freq = 2412 + i * 5;
6202
6203 /* skip social channels; covered in the previous loop */
6204 if (params->freq == 2412 ||
6205 params->freq == 2437 ||
6206 params->freq == 2462)
6207 continue;
6208
6209 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6210 goto out;
6211 }
6212
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006213 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006214 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006215 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006216out:
6217 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6218 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006219}
6220
6221
Roshan Pius3a1667e2018-07-03 15:17:14 -07006222static int wpas_same_band(int freq1, int freq2)
6223{
6224 enum hostapd_hw_mode mode1, mode2;
6225 u8 chan1, chan2;
6226
6227 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6228 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6229 if (mode1 == NUM_HOSTAPD_MODES)
6230 return 0;
6231 return mode1 == mode2;
6232}
6233
6234
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006235static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6236 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006237 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006238 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006239 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006240 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006241{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006242 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006243 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006244 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006245 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006246 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006247
6248 os_memset(params, 0, sizeof(*params));
6249 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006250 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006251 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006252 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006253 params->max_oper_chwidth = max_oper_chwidth;
6254 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006255 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006257 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6258 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006259 if (!freqs)
6260 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006261
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006262 num = get_shared_radio_freqs_data(wpa_s, freqs,
6263 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006264
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006265 if (wpa_s->current_ssid &&
6266 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6267 wpa_s->wpa_state == WPA_COMPLETED) {
6268 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6269 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006270
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006271 /*
6272 * If the frequency selection is done for an active P2P GO that
6273 * is not sharing a frequency, allow to select a new frequency
6274 * even if there are no unused frequencies as we are about to
6275 * move the P2P GO so its frequency can be re-used.
6276 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006277 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006278 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6279 freqs[i].flags == 0) {
6280 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006281 break;
6282 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006283 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006284 }
6285
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006286 /* Try to use EDMG channel */
6287 if (params->edmg) {
6288 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6289 goto success;
6290 params->edmg = 0;
6291 }
6292
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006293 /* try using the forced freq */
6294 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006295 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6296 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006297 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006298 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006299 freq);
6300 goto fail;
6301 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006302 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6303 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006304 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6305 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006306 /*
6307 * If freq is a DFS channel and DFS is offloaded
6308 * to the driver, allow P2P GO to use it.
6309 */
6310 wpa_printf(MSG_DEBUG,
6311 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6312 __func__, freq);
6313 } else {
6314 wpa_printf(MSG_DEBUG,
6315 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6316 freq);
6317 goto fail;
6318 }
6319 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006320
6321 for (i = 0; i < num; i++) {
6322 if (freqs[i].freq == freq) {
6323 wpa_printf(MSG_DEBUG,
6324 "P2P: forced freq (%d MHz) is also shared",
6325 freq);
6326 params->freq = freq;
6327 goto success;
6328 }
6329 }
6330
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006331 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006332 wpa_printf(MSG_DEBUG,
6333 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6334 freq);
6335 goto fail;
6336 }
6337
6338 wpa_printf(MSG_DEBUG,
6339 "P2P: force GO freq (%d MHz) on a free channel",
6340 freq);
6341 params->freq = freq;
6342 goto success;
6343 }
6344
6345 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006346 if (num &&
6347 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006348 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6349 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6350 wpa_printf(MSG_DEBUG,
6351 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006352 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006353 params->freq = cand;
6354 goto success;
6355 }
6356
6357 /* try using one of the shared freqs */
6358 for (i = 0; i < num; i++) {
6359 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6360 freqs[i].freq)) {
6361 wpa_printf(MSG_DEBUG,
6362 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006363 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006364 params->freq = freqs[i].freq;
6365 goto success;
6366 }
6367 }
6368 }
6369
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006370 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006371 wpa_printf(MSG_DEBUG,
6372 "P2P: Cannot force GO on any of the channels we are already using");
6373 goto fail;
6374 }
6375
6376 /* try using the setting from the configuration file */
6377 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6378 wpa_s->conf->p2p_oper_channel >= 1 &&
6379 wpa_s->conf->p2p_oper_channel <= 11 &&
6380 wpas_p2p_supported_freq_go(
6381 wpa_s, channels,
6382 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6383 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6384 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6385 "frequency %d MHz", params->freq);
6386 goto success;
6387 }
6388
6389 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6390 wpa_s->conf->p2p_oper_reg_class == 116 ||
6391 wpa_s->conf->p2p_oper_reg_class == 117 ||
6392 wpa_s->conf->p2p_oper_reg_class == 124 ||
6393 wpa_s->conf->p2p_oper_reg_class == 125 ||
6394 wpa_s->conf->p2p_oper_reg_class == 126 ||
6395 wpa_s->conf->p2p_oper_reg_class == 127) &&
6396 wpas_p2p_supported_freq_go(wpa_s, channels,
6397 5000 +
6398 5 * wpa_s->conf->p2p_oper_channel)) {
6399 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6400 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6401 "frequency %d MHz", params->freq);
6402 goto success;
6403 }
6404
6405 /* Try using best channels */
6406 if (wpa_s->conf->p2p_oper_channel == 0 &&
6407 wpa_s->best_overall_freq > 0 &&
6408 wpas_p2p_supported_freq_go(wpa_s, channels,
6409 wpa_s->best_overall_freq)) {
6410 params->freq = wpa_s->best_overall_freq;
6411 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6412 "channel %d MHz", params->freq);
6413 goto success;
6414 }
6415
6416 if (wpa_s->conf->p2p_oper_channel == 0 &&
6417 wpa_s->best_24_freq > 0 &&
6418 wpas_p2p_supported_freq_go(wpa_s, channels,
6419 wpa_s->best_24_freq)) {
6420 params->freq = wpa_s->best_24_freq;
6421 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6422 "channel %d MHz", params->freq);
6423 goto success;
6424 }
6425
6426 if (wpa_s->conf->p2p_oper_channel == 0 &&
6427 wpa_s->best_5_freq > 0 &&
6428 wpas_p2p_supported_freq_go(wpa_s, channels,
6429 wpa_s->best_5_freq)) {
6430 params->freq = wpa_s->best_5_freq;
6431 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6432 "channel %d MHz", params->freq);
6433 goto success;
6434 }
6435
6436 /* try using preferred channels */
6437 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6438 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6439 params->freq = cand;
6440 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6441 "channels", params->freq);
6442 goto success;
6443 }
6444
Roshan Pius3a1667e2018-07-03 15:17:14 -07006445 /* Try using a channel that allows VHT to be used with 80 MHz */
6446 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6447 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6448 enum hostapd_hw_mode mode;
6449 struct hostapd_hw_modes *hwmode;
6450 u8 chan;
6451
6452 cand = wpa_s->p2p_group_common_freqs[i];
6453 mode = ieee80211_freq_to_chan(cand, &chan);
6454 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006455 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006456 if (!hwmode ||
6457 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6458 BW80) != ALLOWED)
6459 continue;
6460 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6461 params->freq = cand;
6462 wpa_printf(MSG_DEBUG,
6463 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6464 params->freq);
6465 goto success;
6466 }
6467 }
6468 }
6469
6470 /* Try using a channel that allows HT to be used with 40 MHz on the same
6471 * band so that CSA can be used */
6472 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6473 wpa_s->p2p_group_common_freqs) {
6474 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6475 enum hostapd_hw_mode mode;
6476 struct hostapd_hw_modes *hwmode;
6477 u8 chan;
6478
6479 cand = wpa_s->p2p_group_common_freqs[i];
6480 mode = ieee80211_freq_to_chan(cand, &chan);
6481 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006482 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006483 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6484 cand) ||
6485 !hwmode ||
6486 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6487 BW40MINUS) != ALLOWED &&
6488 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6489 BW40PLUS) != ALLOWED))
6490 continue;
6491 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6492 params->freq = cand;
6493 wpa_printf(MSG_DEBUG,
6494 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6495 params->freq);
6496 goto success;
6497 }
6498 }
6499 }
6500
6501 /* Try using one of the group common freqs on the same band so that CSA
6502 * can be used */
6503 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6504 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6505 cand = wpa_s->p2p_group_common_freqs[i];
6506 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6507 cand))
6508 continue;
6509 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6510 params->freq = cand;
6511 wpa_printf(MSG_DEBUG,
6512 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6513 params->freq);
6514 goto success;
6515 }
6516 }
6517 }
6518
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006519 /* Try using one of the group common freqs */
6520 if (wpa_s->p2p_group_common_freqs) {
6521 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6522 cand = wpa_s->p2p_group_common_freqs[i];
6523 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6524 params->freq = cand;
6525 wpa_printf(MSG_DEBUG,
6526 "P2P: Use freq %d MHz common with the peer",
6527 params->freq);
6528 goto success;
6529 }
6530 }
6531 }
6532
6533 /* no preference, select some channel */
6534 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6535
6536 if (params->freq == 0) {
6537 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6538 goto fail;
6539 }
6540
6541success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006542 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006543 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006544fail:
6545 os_free(freqs);
6546 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006547}
6548
6549
6550static struct wpa_supplicant *
6551wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6552 int go)
6553{
6554 struct wpa_supplicant *group_wpa_s;
6555
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006556 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006557 if (wpa_s->p2p_mgmt) {
6558 /*
6559 * We may be called on the p2p_dev interface which
6560 * cannot be used for group operations, so always use
6561 * the primary interface.
6562 */
6563 wpa_s->parent->p2pdev = wpa_s;
6564 wpa_s = wpa_s->parent;
6565 }
6566 wpa_dbg(wpa_s, MSG_DEBUG,
6567 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006568 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006569 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006570 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006571 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006572 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573
6574 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006575 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006576 wpa_msg_global(wpa_s, MSG_ERROR,
6577 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006579 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006580 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6581 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006582 wpa_msg_global(wpa_s, MSG_ERROR,
6583 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584 wpas_p2p_remove_pending_group_interface(wpa_s);
6585 return NULL;
6586 }
6587
Roshan Pius3a1667e2018-07-03 15:17:14 -07006588 if (go && wpa_s->p2p_go_do_acs) {
6589 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6590 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6591 wpa_s->p2p_go_do_acs = 0;
6592 }
6593
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006594 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6595 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006596 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006597 return group_wpa_s;
6598}
6599
6600
6601/**
6602 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6603 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6604 * @persistent_group: Whether to create a persistent group
6605 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006606 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006607 * @ht40: Start GO with 40 MHz channel width
6608 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006609 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006610 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611 * Returns: 0 on success, -1 on failure
6612 *
6613 * This function creates a new P2P group with the local end as the Group Owner,
6614 * i.e., without using Group Owner Negotiation.
6615 */
6616int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006617 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006618 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006619{
6620 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006621
6622 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6623 return -1;
6624
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006625 os_free(wpa_s->global->add_psk);
6626 wpa_s->global->add_psk = NULL;
6627
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006628 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006629 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006630 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006631
Roshan Pius3a1667e2018-07-03 15:17:14 -07006632 if (!wpa_s->p2p_go_do_acs) {
6633 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6634 if (freq < 0)
6635 return -1;
6636 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006637
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006638 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006639 ht40, vht, max_oper_chwidth, he, edmg,
6640 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006641 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006643 p2p_go_params(wpa_s->global->p2p, &params);
6644 params.persistent_group = persistent_group;
6645
6646 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6647 if (wpa_s == NULL)
6648 return -1;
6649 wpas_start_wps_go(wpa_s, &params, 0);
6650
6651 return 0;
6652}
6653
6654
6655static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006656 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006657 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006658{
6659 struct wpa_ssid *ssid;
6660
6661 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6662 if (wpa_s == NULL)
6663 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006664 if (force_scan)
6665 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006666 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006667
6668 wpa_supplicant_ap_deinit(wpa_s);
6669
6670 ssid = wpa_config_add_network(wpa_s->conf);
6671 if (ssid == NULL)
6672 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006673 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006674 wpa_config_set_network_defaults(ssid);
6675 ssid->temporary = 1;
6676 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006677 ssid->pbss = params->pbss;
6678 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6679 WPA_CIPHER_CCMP;
6680 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6682 ssid->ssid = os_malloc(params->ssid_len);
6683 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006684 wpa_config_remove_network(wpa_s->conf, ssid->id);
6685 return -1;
6686 }
6687 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6688 ssid->ssid_len = params->ssid_len;
6689 ssid->p2p_group = 1;
6690 ssid->export_keys = 1;
6691 if (params->psk_set) {
6692 os_memcpy(ssid->psk, params->psk, 32);
6693 ssid->psk_set = 1;
6694 }
6695 if (params->passphrase)
6696 ssid->passphrase = os_strdup(params->passphrase);
6697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006698 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006699 wpa_s->p2p_in_invitation = 1;
6700 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006701 wpa_s->p2p_go_group_formation_completed = 0;
6702 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006703
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006704 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006705 NULL);
6706 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6707 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006708 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006709 wpa_supplicant_select_network(wpa_s, ssid);
6710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006711 return 0;
6712}
6713
6714
6715int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6716 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006717 int force_freq, int neg_freq,
6718 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006719 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006720 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006721 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006722 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006723{
6724 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006725 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006726
6727 if (ssid->disabled != 2 || ssid->ssid == NULL)
6728 return -1;
6729
6730 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6731 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6732 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6733 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006734 if (go == 0 &&
6735 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006736 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006737 /*
6738 * This can happen if Invitation Response frame was lost
6739 * and the peer (GO of a persistent group) tries to
6740 * invite us again. Reschedule the timeout to avoid
6741 * terminating the wait for the connection too early
6742 * since we now know that the peer is still trying to
6743 * invite us instead of having already started the GO.
6744 */
6745 wpa_printf(MSG_DEBUG,
6746 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6747 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6748 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006749 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751 return 0;
6752 }
6753
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006754 os_free(wpa_s->global->add_psk);
6755 wpa_s->global->add_psk = NULL;
6756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006757 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006758 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759
Dmitry Shmidt04949592012-07-19 12:16:46 -07006760 wpa_s->p2p_fallback_to_go_neg = 0;
6761
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006762 if (ssid->mode == WPAS_MODE_P2P_GO) {
6763 if (force_freq > 0) {
6764 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6765 if (freq < 0)
6766 return -1;
6767 } else {
6768 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6769 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006770 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006771 freq = 0;
6772 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006773 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006774 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006775 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006776 struct os_reltime now;
6777 struct wpa_bss *bss =
6778 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006779
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006780 os_get_reltime(&now);
6781 if (bss &&
6782 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006783 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006784 freq = bss->freq;
6785 else
6786 freq = 0;
6787 }
6788
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006789 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6790 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006791 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006792 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006793 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006794
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006795 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006796 ht40, vht, max_oper_chwidth, he, edmg,
6797 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006798 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006799
6800 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006801 params.psk_set = ssid->psk_set;
6802 if (params.psk_set)
6803 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006804 if (ssid->passphrase) {
6805 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6806 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6807 "persistent group");
6808 return -1;
6809 }
6810 os_strlcpy(params.passphrase, ssid->passphrase,
6811 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006813 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6814 params.ssid_len = ssid->ssid_len;
6815 params.persistent_group = 1;
6816
6817 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6818 if (wpa_s == NULL)
6819 return -1;
6820
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006821 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6822
Dmitry Shmidt56052862013-10-04 10:23:25 -07006823 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006824 wpas_start_wps_go(wpa_s, &params, 0);
6825
6826 return 0;
6827}
6828
6829
6830static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6831 struct wpabuf *proberesp_ies)
6832{
6833 struct wpa_supplicant *wpa_s = ctx;
6834 if (wpa_s->ap_iface) {
6835 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006836 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6837 wpabuf_free(beacon_ies);
6838 wpabuf_free(proberesp_ies);
6839 return;
6840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006841 if (beacon_ies) {
6842 wpabuf_free(hapd->p2p_beacon_ie);
6843 hapd->p2p_beacon_ie = beacon_ies;
6844 }
6845 wpabuf_free(hapd->p2p_probe_resp_ie);
6846 hapd->p2p_probe_resp_ie = proberesp_ies;
6847 } else {
6848 wpabuf_free(beacon_ies);
6849 wpabuf_free(proberesp_ies);
6850 }
6851 wpa_supplicant_ap_update_beacon(wpa_s);
6852}
6853
6854
6855static void wpas_p2p_idle_update(void *ctx, int idle)
6856{
6857 struct wpa_supplicant *wpa_s = ctx;
6858 if (!wpa_s->ap_iface)
6859 return;
6860 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006861 if (idle) {
6862 if (wpa_s->global->p2p_fail_on_wps_complete &&
6863 wpa_s->p2p_in_provisioning) {
6864 wpas_p2p_grpform_fail_after_wps(wpa_s);
6865 return;
6866 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006867 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006868 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006869 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6870}
6871
6872
6873struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006874 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006875{
6876 struct p2p_group *group;
6877 struct p2p_group_config *cfg;
6878
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006879 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6880 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006881 return NULL;
6882
6883 cfg = os_zalloc(sizeof(*cfg));
6884 if (cfg == NULL)
6885 return NULL;
6886
Dmitry Shmidt04949592012-07-19 12:16:46 -07006887 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006888 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006889 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006890 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006891 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6892 if (wpa_s->max_stations &&
6893 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6894 cfg->max_clients = wpa_s->max_stations;
6895 else
6896 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006897 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6898 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006899 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900 cfg->cb_ctx = wpa_s;
6901 cfg->ie_update = wpas_p2p_ie_update;
6902 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006903 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6904 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905
6906 group = p2p_group_init(wpa_s->global->p2p, cfg);
6907 if (group == NULL)
6908 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006909 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006910 p2p_group_notif_formation_done(group);
6911 wpa_s->p2p_group = group;
6912 return group;
6913}
6914
6915
6916void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6917 int registrar)
6918{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006919 struct wpa_ssid *ssid = wpa_s->current_ssid;
6920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006921 if (!wpa_s->p2p_in_provisioning) {
6922 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6923 "provisioning not in progress");
6924 return;
6925 }
6926
Dmitry Shmidt04949592012-07-19 12:16:46 -07006927 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6928 u8 go_dev_addr[ETH_ALEN];
6929 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6930 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6931 ssid->ssid_len);
6932 /* Clear any stored provisioning info */
6933 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6934 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006935
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006936 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006938 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006939 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6940 /*
6941 * Use a separate timeout for initial data connection to
6942 * complete to allow the group to be removed automatically if
6943 * something goes wrong in this step before the P2P group idle
6944 * timeout mechanism is taken into use.
6945 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006946 wpa_dbg(wpa_s, MSG_DEBUG,
6947 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6948 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006949 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6950 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006951 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006952 /* Complete group formation on successful data connection. */
6953 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006954 } else if (ssid) {
6955 /*
6956 * Use a separate timeout for initial data connection to
6957 * complete to allow the group to be removed automatically if
6958 * the client does not complete data connection successfully.
6959 */
6960 wpa_dbg(wpa_s, MSG_DEBUG,
6961 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6962 P2P_MAX_INITIAL_CONN_WAIT_GO);
6963 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6964 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006965 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006966 /*
6967 * Complete group formation on first successful data connection
6968 */
6969 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006971 if (wpa_s->global->p2p)
6972 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006973 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974}
6975
6976
Jouni Malinen75ecf522011-06-27 15:19:46 -07006977void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6978 struct wps_event_fail *fail)
6979{
6980 if (!wpa_s->p2p_in_provisioning) {
6981 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6982 "provisioning not in progress");
6983 return;
6984 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006985
6986 if (wpa_s->go_params) {
6987 p2p_clear_provisioning_info(
6988 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006989 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006990 }
6991
Jouni Malinen75ecf522011-06-27 15:19:46 -07006992 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006993
6994 if (wpa_s == wpa_s->global->p2p_group_formation) {
6995 /*
6996 * Allow some time for the failed WPS negotiation exchange to
6997 * complete, but remove the group since group formation cannot
6998 * succeed after provisioning failure.
6999 */
7000 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7001 wpa_s->global->p2p_fail_on_wps_complete = 1;
7002 eloop_deplete_timeout(0, 50000,
7003 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007004 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007005 }
7006}
7007
7008
7009int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7010{
7011 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7012 !wpa_s->p2p_in_provisioning)
7013 return 0;
7014
7015 wpas_p2p_grpform_fail_after_wps(wpa_s);
7016
7017 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007018}
7019
7020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007022 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007023 enum wpas_p2p_prov_disc_use use,
7024 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007025{
7026 u16 config_methods;
7027
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007028 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007029 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007030 wpa_s->p2p_fallback_to_go_neg = 0;
7031 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007032 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7033 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007034 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7035 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7036
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007037 wpa_printf(MSG_DEBUG,
7038 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7039 __func__, p2ps_prov->conncap,
7040 p2ps_prov->adv_id, p2ps_prov->conncap,
7041 p2ps_prov->status, p2ps_prov->info);
7042
7043 config_methods = 0;
7044 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007045 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007047 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007048 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7049 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007051 else {
7052 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007053 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007054 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007055 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007056
Dmitry Shmidt04949592012-07-19 12:16:46 -07007057 if (use == WPAS_P2P_PD_AUTO) {
7058 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7059 wpa_s->pending_pd_config_methods = config_methods;
7060 wpa_s->p2p_auto_pd = 1;
7061 wpa_s->p2p_auto_join = 0;
7062 wpa_s->pending_pd_before_join = 0;
7063 wpa_s->auto_pd_scan_retry = 0;
7064 wpas_p2p_stop_find(wpa_s);
7065 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007066 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007067 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7068 wpa_s->p2p_auto_started.sec,
7069 wpa_s->p2p_auto_started.usec);
7070 wpas_p2p_join_scan(wpa_s, NULL);
7071 return 0;
7072 }
7073
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007074 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7075 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007076 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007077 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007078
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007079 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007080 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007081 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082}
7083
7084
7085int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7086 char *end)
7087{
7088 return p2p_scan_result_text(ies, ies_len, buf, end);
7089}
7090
7091
7092static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7093{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007094 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007095 return;
7096
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007097 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007098 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007099 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7100 wpa_s, NULL);
7101 offchannel_send_action_done(wpa_s);
7102 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007104 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7105 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007106 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107}
7108
7109
7110int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7111 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007112 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007113 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007114 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115{
7116 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007117 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007118
Dmitry Shmidt04949592012-07-19 12:16:46 -07007119 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007120 wpa_s->p2p_in_provisioning) {
7121 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7122 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7123 " (P2P disabled)" : "",
7124 wpa_s->p2p_in_provisioning ?
7125 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007126 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007128
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007129 wpa_supplicant_cancel_sched_scan(wpa_s);
7130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007131 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007132 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007133 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007134}
7135
7136
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007137static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7138 struct wpa_scan_results *scan_res)
7139{
7140 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7141
7142 if (wpa_s->p2p_scan_work) {
7143 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7144 wpa_s->p2p_scan_work = NULL;
7145 radio_work_done(work);
7146 }
7147
7148 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7149 return;
7150
7151 /*
7152 * Indicate that results have been processed so that the P2P module can
7153 * continue pending tasks.
7154 */
Hai Shalom60840252021-02-19 19:02:11 -08007155 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007156}
7157
7158
7159static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007160{
7161 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007162 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007163 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7164 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165
7166 if (wpa_s->global->p2p)
7167 p2p_stop_find(wpa_s->global->p2p);
7168
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007169 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7170 wpa_printf(MSG_DEBUG,
7171 "P2P: Do not consider the scan results after stop_find");
7172 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7173 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007174}
7175
7176
7177void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7178{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007179 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007180 if (!wpa_s->global->pending_group_iface_for_p2ps)
7181 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007182}
7183
7184
7185static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7186{
7187 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007188 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007189}
7190
7191
7192int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7193{
7194 int res;
7195
7196 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7197 return -1;
7198
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007199 if (wpa_s->p2p_lo_started) {
7200 wpa_printf(MSG_DEBUG,
7201 "P2P: Cannot start P2P listen, it is offloaded");
7202 return -1;
7203 }
7204
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007205 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007206 wpas_p2p_clear_pending_action_tx(wpa_s);
7207
7208 if (timeout == 0) {
7209 /*
7210 * This is a request for unlimited Listen state. However, at
7211 * least for now, this is mapped to a Listen state for one
7212 * hour.
7213 */
7214 timeout = 3600;
7215 }
7216 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007217 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007218
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007219 /*
7220 * Stop previous find/listen operation to avoid trying to request a new
7221 * remain-on-channel operation while the driver is still running the
7222 * previous one.
7223 */
7224 if (wpa_s->global->p2p)
7225 p2p_stop_find(wpa_s->global->p2p);
7226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007227 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7228 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007229 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007230 eloop_register_timeout(timeout, 0,
7231 wpas_p2p_long_listen_timeout,
7232 wpa_s, NULL);
7233 }
7234
7235 return res;
7236}
7237
7238
7239int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7240 u8 *buf, size_t len, int p2p_group)
7241{
7242 struct wpabuf *p2p_ie;
7243 int ret;
7244
7245 if (wpa_s->global->p2p_disabled)
7246 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007247 /*
7248 * Advertize mandatory cross connection capability even on
7249 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7250 */
7251 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007252 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253 if (wpa_s->global->p2p == NULL)
7254 return -1;
7255 if (bss == NULL)
7256 return -1;
7257
7258 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7259 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7260 p2p_group, p2p_ie);
7261 wpabuf_free(p2p_ie);
7262
7263 return ret;
7264}
7265
7266
7267int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007268 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007269 const u8 *ie, size_t ie_len,
7270 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007271{
7272 if (wpa_s->global->p2p_disabled)
7273 return 0;
7274 if (wpa_s->global->p2p == NULL)
7275 return 0;
7276
Dmitry Shmidt04949592012-07-19 12:16:46 -07007277 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007278 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007279 case P2P_PREQ_NOT_P2P:
7280 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7281 ssi_signal);
7282 /* fall through */
7283 case P2P_PREQ_MALFORMED:
7284 case P2P_PREQ_NOT_LISTEN:
7285 case P2P_PREQ_NOT_PROCESSED:
7286 default: /* make gcc happy */
7287 return 0;
7288 case P2P_PREQ_PROCESSED:
7289 return 1;
7290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007291}
7292
7293
7294void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7295 const u8 *sa, const u8 *bssid,
7296 u8 category, const u8 *data, size_t len, int freq)
7297{
7298 if (wpa_s->global->p2p_disabled)
7299 return;
7300 if (wpa_s->global->p2p == NULL)
7301 return;
7302
7303 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7304 freq);
7305}
7306
7307
7308void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7309{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007310 unsigned int bands;
7311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312 if (wpa_s->global->p2p_disabled)
7313 return;
7314 if (wpa_s->global->p2p == NULL)
7315 return;
7316
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007317 bands = wpas_get_bands(wpa_s, NULL);
7318 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007319}
7320
7321
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007322static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323{
7324 p2p_group_deinit(wpa_s->p2p_group);
7325 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007326
7327 wpa_s->ap_configured_cb = NULL;
7328 wpa_s->ap_configured_cb_ctx = NULL;
7329 wpa_s->ap_configured_cb_data = NULL;
7330 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007331}
7332
7333
7334int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7335{
Hai Shalomfdcde762020-04-02 11:19:20 -07007336 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007338 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7339 return -1;
7340
7341 return p2p_reject(wpa_s->global->p2p, addr);
7342}
7343
7344
7345/* Invite to reinvoke a persistent group */
7346int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007347 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007348 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007349 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350{
7351 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007352 u8 *bssid = NULL;
7353 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007354 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007355 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007356 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007357
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007358 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007359 if (peer_addr)
7360 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7361 else
7362 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363
Jouni Malinen31be0a42012-08-31 21:20:51 +03007364 wpa_s->p2p_persistent_go_freq = freq;
7365 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007366 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007367 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007368 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7369 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007370 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007371 if (ssid->mode == WPAS_MODE_P2P_GO) {
7372 role = P2P_INVITE_ROLE_GO;
7373 if (peer_addr == NULL) {
7374 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7375 "address in invitation command");
7376 return -1;
7377 }
7378 if (wpas_p2p_create_iface(wpa_s)) {
7379 if (wpas_p2p_add_group_interface(wpa_s,
7380 WPA_IF_P2P_GO) < 0) {
7381 wpa_printf(MSG_ERROR, "P2P: Failed to "
7382 "allocate a new interface for the "
7383 "group");
7384 return -1;
7385 }
7386 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007387 } else if (wpa_s->p2p_mgmt)
7388 bssid = wpa_s->parent->own_addr;
7389 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007390 bssid = wpa_s->own_addr;
7391 } else {
7392 role = P2P_INVITE_ROLE_CLIENT;
7393 peer_addr = ssid->bssid;
7394 }
7395 wpa_s->pending_invite_ssid_id = ssid->id;
7396
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007397 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007398 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007399 role == P2P_INVITE_ROLE_GO,
7400 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007401 if (res)
7402 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007404 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7405 return -1;
7406
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007407 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7408
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007409 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7410 no_pref_freq_given && pref_freq > 0 &&
7411 wpa_s->num_multichan_concurrent > 1 &&
7412 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7413 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7414 pref_freq);
7415 pref_freq = 0;
7416 }
7417
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007418 /*
7419 * Stop any find/listen operations before invitation and possibly
7420 * connection establishment.
7421 */
7422 wpas_p2p_stop_find_oper(wpa_s);
7423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007424 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007425 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007426 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007427}
7428
7429
7430/* Invite to join an active group */
7431int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7432 const u8 *peer_addr, const u8 *go_dev_addr)
7433{
7434 struct wpa_global *global = wpa_s->global;
7435 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007436 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007437 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007438 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007439 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007440 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007441 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442
Jouni Malinen31be0a42012-08-31 21:20:51 +03007443 wpa_s->p2p_persistent_go_freq = 0;
7444 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007445 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007446 wpa_s->p2p_go_vht_center_freq2 = 0;
7447 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007448 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007450 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7451 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7452 break;
7453 }
7454 if (wpa_s == NULL) {
7455 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7456 return -1;
7457 }
7458
7459 ssid = wpa_s->current_ssid;
7460 if (ssid == NULL) {
7461 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7462 "invitation");
7463 return -1;
7464 }
7465
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007466 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007467 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007468 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007469 ssid->ssid, ssid->ssid_len);
7470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471 if (ssid->mode == WPAS_MODE_P2P_GO) {
7472 role = P2P_INVITE_ROLE_ACTIVE_GO;
7473 bssid = wpa_s->own_addr;
7474 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007475 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007476 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007477 } else {
7478 role = P2P_INVITE_ROLE_CLIENT;
7479 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7480 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7481 "invite to current group");
7482 return -1;
7483 }
7484 bssid = wpa_s->bssid;
7485 if (go_dev_addr == NULL &&
7486 !is_zero_ether_addr(wpa_s->go_dev_addr))
7487 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007488 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7489 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007490 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007491 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007493 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7494 return -1;
7495
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007496 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007497 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007498 role == P2P_INVITE_ROLE_ACTIVE_GO,
7499 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007500 if (res)
7501 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007502 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007504 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007505 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007506 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007507}
7508
7509
7510void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7511{
7512 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007513 u8 go_dev_addr[ETH_ALEN];
7514 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007515 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007516 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007517 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007518
Dmitry Shmidt04949592012-07-19 12:16:46 -07007519 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7520 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007521 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007522 }
7523
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007524 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007525 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526
7527 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007528 if (!wpa_s->p2p_go_group_formation_completed &&
7529 wpa_s->global->p2p_group_formation == wpa_s) {
7530 wpa_dbg(wpa_s, MSG_DEBUG,
7531 "P2P: Marking group formation completed on client on data connection");
7532 wpa_s->p2p_go_group_formation_completed = 1;
7533 wpa_s->global->p2p_group_formation = NULL;
7534 wpa_s->p2p_in_provisioning = 0;
7535 wpa_s->p2p_in_invitation = 0;
7536 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007538 os_memset(go_dev_addr, 0, ETH_ALEN);
7539 if (ssid->bssid_set)
7540 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7541 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7542 ssid->ssid_len);
7543 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7544
7545 if (wpa_s->global->p2p_group_formation == wpa_s)
7546 wpa_s->global->p2p_group_formation = NULL;
7547
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007548 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7549 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007550
7551 ip_addr[0] = '\0';
7552 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007553 int res;
7554
7555 res = os_snprintf(ip_addr, sizeof(ip_addr),
7556 " ip_addr=%u.%u.%u.%u "
7557 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7558 ip[0], ip[1], ip[2], ip[3],
7559 ip[4], ip[5], ip[6], ip[7],
7560 ip[8], ip[9], ip[10], ip[11]);
7561 if (os_snprintf_error(sizeof(ip_addr), res))
7562 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007563 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007564 }
7565
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007566 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7567 ssid->passphrase == NULL && ssid->psk_set ?
7568 ssid->psk : NULL,
7569 ssid->passphrase, go_dev_addr, persistent,
7570 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007571
7572 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007573 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7574 ssid, go_dev_addr);
7575
Hai Shalom5f92bc92019-04-18 11:54:11 -07007576 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577}
7578
7579
7580int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7581 u32 interval1, u32 duration2, u32 interval2)
7582{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007583 int ret;
7584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007585 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7586 return -1;
7587
7588 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7589 wpa_s->current_ssid == NULL ||
7590 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7591 return -1;
7592
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007593 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7594 wpa_s->own_addr, wpa_s->assoc_freq,
7595 duration1, interval1, duration2, interval2);
7596 if (ret == 0)
7597 wpa_s->waiting_presence_resp = 1;
7598
7599 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007600}
7601
7602
7603int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7604 unsigned int interval)
7605{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7607 return -1;
7608
7609 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7610}
7611
7612
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007613static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7614{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007615 if (wpa_s->current_ssid == NULL) {
7616 /*
7617 * current_ssid can be cleared when P2P client interface gets
7618 * disconnected, so assume this interface was used as P2P
7619 * client.
7620 */
7621 return 1;
7622 }
7623 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007624 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7625}
7626
7627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007628static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7629{
7630 struct wpa_supplicant *wpa_s = eloop_ctx;
7631
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007632 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007633 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7634 "disabled");
7635 return;
7636 }
7637
Dmitry Shmidt04949592012-07-19 12:16:46 -07007638 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7639 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007640 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007641}
7642
7643
7644static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7645{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007646 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647
Dmitry Shmidt04949592012-07-19 12:16:46 -07007648 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7649 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007651 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7652 return;
7653
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007654 timeout = wpa_s->conf->p2p_group_idle;
7655 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7656 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7657 timeout = P2P_MAX_CLIENT_IDLE;
7658
7659 if (timeout == 0)
7660 return;
7661
Dmitry Shmidt04949592012-07-19 12:16:46 -07007662 if (timeout < 0) {
7663 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7664 timeout = 0; /* special client mode no-timeout */
7665 else
7666 return;
7667 }
7668
7669 if (wpa_s->p2p_in_provisioning) {
7670 /*
7671 * Use the normal group formation timeout during the
7672 * provisioning phase to avoid terminating this process too
7673 * early due to group idle timeout.
7674 */
7675 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7676 "during provisioning");
7677 return;
7678 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307679
Dmitry Shmidt04949592012-07-19 12:16:46 -07007680 if (wpa_s->show_group_started) {
7681 /*
7682 * Use the normal group formation timeout between the end of
7683 * the provisioning phase and completion of 4-way handshake to
7684 * avoid terminating this process too early due to group idle
7685 * timeout.
7686 */
7687 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7688 "while waiting for initial 4-way handshake to "
7689 "complete");
7690 return;
7691 }
7692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007693 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007694 timeout);
7695 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7696 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007697}
7698
7699
Jouni Malinen2b89da82012-08-31 22:04:41 +03007700/* Returns 1 if the interface was removed */
7701int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7702 u16 reason_code, const u8 *ie, size_t ie_len,
7703 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007704{
7705 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007706 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007707
Dmitry Shmidt04949592012-07-19 12:16:46 -07007708 if (!locally_generated)
7709 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7710 ie_len);
7711
7712 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7713 wpa_s->current_ssid &&
7714 wpa_s->current_ssid->p2p_group &&
7715 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7716 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7717 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007718 if (wpas_p2p_group_delete(wpa_s,
7719 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7720 > 0)
7721 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007722 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007723
7724 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007725}
7726
7727
7728void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007729 u16 reason_code, const u8 *ie, size_t ie_len,
7730 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007731{
7732 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7733 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007734
Dmitry Shmidt04949592012-07-19 12:16:46 -07007735 if (!locally_generated)
7736 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7737 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007738}
7739
7740
7741void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7742{
7743 struct p2p_data *p2p = wpa_s->global->p2p;
7744
7745 if (p2p == NULL)
7746 return;
7747
7748 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7749 return;
7750
7751 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7752 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7753
7754 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7755 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7756
7757 if (wpa_s->wps &&
7758 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7759 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7760
7761 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7762 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7763
7764 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7765 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7766 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7767 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7768 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7769 }
7770
7771 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7772 p2p_set_sec_dev_types(p2p,
7773 (void *) wpa_s->conf->sec_device_type,
7774 wpa_s->conf->num_sec_device_types);
7775
7776 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7777 int i;
7778 p2p_remove_wps_vendor_extensions(p2p);
7779 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7780 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7781 continue;
7782 p2p_add_wps_vendor_extension(
7783 p2p, wpa_s->conf->wps_vendor_ext[i]);
7784 }
7785 }
7786
7787 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7788 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7789 char country[3];
7790 country[0] = wpa_s->conf->country[0];
7791 country[1] = wpa_s->conf->country[1];
7792 country[2] = 0x04;
7793 p2p_set_country(p2p, country);
7794 }
7795
7796 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7797 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7798 wpa_s->conf->p2p_ssid_postfix ?
7799 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7800 0);
7801 }
7802
7803 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7804 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007805
7806 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7807 u8 reg_class, channel;
7808 int ret;
7809 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007810 u8 channel_forced;
7811
Jouni Malinen75ecf522011-06-27 15:19:46 -07007812 if (wpa_s->conf->p2p_listen_reg_class &&
7813 wpa_s->conf->p2p_listen_channel) {
7814 reg_class = wpa_s->conf->p2p_listen_reg_class;
7815 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007816 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007817 } else {
7818 reg_class = 81;
7819 /*
7820 * Pick one of the social channels randomly as the
7821 * listen channel.
7822 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007823 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7824 channel = 1;
7825 else
7826 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007827 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007828 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007829 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7830 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007831 if (ret)
7832 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7833 "failed: %d", ret);
7834 }
7835 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7836 u8 op_reg_class, op_channel, cfg_op_channel;
7837 int ret = 0;
7838 unsigned int r;
7839 if (wpa_s->conf->p2p_oper_reg_class &&
7840 wpa_s->conf->p2p_oper_channel) {
7841 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7842 op_channel = wpa_s->conf->p2p_oper_channel;
7843 cfg_op_channel = 1;
7844 } else {
7845 op_reg_class = 81;
7846 /*
7847 * Use random operation channel from (1, 6, 11)
7848 *if no other preference is indicated.
7849 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007850 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7851 op_channel = 1;
7852 else
7853 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007854 cfg_op_channel = 0;
7855 }
7856 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7857 cfg_op_channel);
7858 if (ret)
7859 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7860 "failed: %d", ret);
7861 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007862
7863 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7864 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7865 wpa_s->conf->p2p_pref_chan) < 0) {
7866 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7867 "update failed");
7868 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007869
7870 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7871 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7872 "update failed");
7873 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007874 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007875
7876 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7877 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007878}
7879
7880
7881int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7882 int duration)
7883{
7884 if (!wpa_s->ap_iface)
7885 return -1;
7886 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7887 duration);
7888}
7889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007890
7891int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7892{
7893 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7894 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007895
7896 wpa_s->global->cross_connection = enabled;
7897 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7898
7899 if (!enabled) {
7900 struct wpa_supplicant *iface;
7901
7902 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7903 {
7904 if (iface->cross_connect_enabled == 0)
7905 continue;
7906
7907 iface->cross_connect_enabled = 0;
7908 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007909 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007910 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7911 iface->ifname,
7912 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007913 }
7914 }
7915
7916 return 0;
7917}
7918
7919
7920static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7921{
7922 struct wpa_supplicant *iface;
7923
7924 if (!uplink->global->cross_connection)
7925 return;
7926
7927 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7928 if (!iface->cross_connect_enabled)
7929 continue;
7930 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7931 0)
7932 continue;
7933 if (iface->ap_iface == NULL)
7934 continue;
7935 if (iface->cross_connect_in_use)
7936 continue;
7937
7938 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007939 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007940 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7941 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007942 }
7943}
7944
7945
7946static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7947{
7948 struct wpa_supplicant *iface;
7949
7950 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7951 if (!iface->cross_connect_enabled)
7952 continue;
7953 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7954 0)
7955 continue;
7956 if (!iface->cross_connect_in_use)
7957 continue;
7958
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007959 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007960 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7961 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007962 iface->cross_connect_in_use = 0;
7963 }
7964}
7965
7966
7967void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7968{
7969 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7970 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7971 wpa_s->cross_connect_disallowed)
7972 wpas_p2p_disable_cross_connect(wpa_s);
7973 else
7974 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007975 if (!wpa_s->ap_iface &&
7976 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7977 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007978}
7979
7980
7981void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7982{
7983 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007984 if (!wpa_s->ap_iface &&
7985 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7986 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007987 wpas_p2p_set_group_idle_timeout(wpa_s);
7988}
7989
7990
7991static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7992{
7993 struct wpa_supplicant *iface;
7994
7995 if (!wpa_s->global->cross_connection)
7996 return;
7997
7998 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7999 if (iface == wpa_s)
8000 continue;
8001 if (iface->drv_flags &
8002 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8003 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008004 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8005 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008006 continue;
8007
8008 wpa_s->cross_connect_enabled = 1;
8009 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8010 sizeof(wpa_s->cross_connect_uplink));
8011 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8012 "%s to %s whenever uplink is available",
8013 wpa_s->ifname, wpa_s->cross_connect_uplink);
8014
8015 if (iface->ap_iface || iface->current_ssid == NULL ||
8016 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8017 iface->cross_connect_disallowed ||
8018 iface->wpa_state != WPA_COMPLETED)
8019 break;
8020
8021 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008022 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008023 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8024 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008025 break;
8026 }
8027}
8028
8029
8030int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8031{
8032 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8033 !wpa_s->p2p_in_provisioning)
8034 return 0; /* not P2P client operation */
8035
8036 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8037 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008038 if (wpa_s != wpa_s->p2pdev)
8039 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008040 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008041 return 1;
8042}
8043
8044
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008045void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8046{
8047 struct wpa_supplicant *wpa_s = eloop_ctx;
8048 wpas_p2p_notif_pbc_overlap(wpa_s);
8049}
8050
8051
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008052void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8053 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008054{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008055 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008056 struct wpa_used_freq_data *freqs = NULL;
8057 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008058
8059 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8060 return;
8061
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008062 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8063 if (!freqs)
8064 return;
8065
8066 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008068 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008069 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008070 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8071 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008072 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8073 "channel list");
8074 return;
8075 }
8076
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008077 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008078
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008079 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008080
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008081 /*
8082 * The used frequencies map changed, so it is possible that a GO is
8083 * using a channel that is no longer valid for P2P use. It is also
8084 * possible that due to policy consideration, it would be preferable to
8085 * move it to a frequency already used by other station interfaces.
8086 */
8087 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8088
8089 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008090}
8091
8092
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008093static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8094 struct wpa_scan_results *scan_res)
8095{
8096 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8097}
8098
8099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008100int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8101{
8102 struct wpa_global *global = wpa_s->global;
8103 int found = 0;
8104 const u8 *peer;
8105
8106 if (global->p2p == NULL)
8107 return -1;
8108
8109 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8110
8111 if (wpa_s->pending_interface_name[0] &&
8112 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8113 found = 1;
8114
8115 peer = p2p_get_go_neg_peer(global->p2p);
8116 if (peer) {
8117 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8118 MACSTR, MAC2STR(peer));
8119 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008120 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008121 }
8122
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008123 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8124 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8125 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8126 found = 1;
8127 }
8128
8129 if (wpa_s->pending_pd_before_join) {
8130 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8131 wpa_s->pending_pd_before_join = 0;
8132 found = 1;
8133 }
8134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008135 wpas_p2p_stop_find(wpa_s);
8136
8137 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8138 if (wpa_s == global->p2p_group_formation &&
8139 (wpa_s->p2p_in_provisioning ||
8140 wpa_s->parent->pending_interface_type ==
8141 WPA_IF_P2P_CLIENT)) {
8142 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8143 "formation found - cancelling",
8144 wpa_s->ifname);
8145 found = 1;
8146 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008147 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008148 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008149 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008150 break;
8151 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008152 wpas_p2p_group_delete(wpa_s,
8153 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008154 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008155 } else if (wpa_s->p2p_in_invitation) {
8156 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8157 wpa_s->ifname);
8158 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008159 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008160 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008161 }
8162 }
8163
8164 if (!found) {
8165 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8166 return -1;
8167 }
8168
8169 return 0;
8170}
8171
8172
8173void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8174{
8175 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8176 return;
8177
8178 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8179 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008180 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008181}
8182
8183
8184void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8185 int freq_24, int freq_5, int freq_overall)
8186{
8187 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008188 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008189 return;
8190 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8191}
8192
8193
8194int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8195{
8196 u8 peer[ETH_ALEN];
8197 struct p2p_data *p2p = wpa_s->global->p2p;
8198
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008199 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008200 return -1;
8201
8202 if (hwaddr_aton(addr, peer))
8203 return -1;
8204
8205 return p2p_unauthorize(p2p, peer);
8206}
8207
8208
8209/**
8210 * wpas_p2p_disconnect - Disconnect from a P2P Group
8211 * @wpa_s: Pointer to wpa_supplicant data
8212 * Returns: 0 on success, -1 on failure
8213 *
8214 * This can be used to disconnect from a group in which the local end is a P2P
8215 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8216 * virtual network interface was created for this group, that interface will be
8217 * removed. Otherwise, only the configured P2P group network will be removed
8218 * from the interface.
8219 */
8220int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8221{
8222
8223 if (wpa_s == NULL)
8224 return -1;
8225
Jouni Malinen2b89da82012-08-31 22:04:41 +03008226 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8227 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008228}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008229
8230
8231int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8232{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008233 int ret;
8234
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008235 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8236 return 0;
8237
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008238 ret = p2p_in_progress(wpa_s->global->p2p);
8239 if (ret == 0) {
8240 /*
8241 * Check whether there is an ongoing WPS provisioning step (or
8242 * other parts of group formation) on another interface since
8243 * p2p_in_progress() does not report this to avoid issues for
8244 * scans during such provisioning step.
8245 */
8246 if (wpa_s->global->p2p_group_formation &&
8247 wpa_s->global->p2p_group_formation != wpa_s) {
8248 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8249 "in group formation",
8250 wpa_s->global->p2p_group_formation->ifname);
8251 ret = 1;
8252 }
8253 }
8254
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008255 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008256 struct os_reltime now;
8257 os_get_reltime(&now);
8258 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8259 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008260 /* Wait for the first client has expired */
8261 wpa_s->global->p2p_go_wait_client.sec = 0;
8262 } else {
8263 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8264 ret = 1;
8265 }
8266 }
8267
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008268 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008269}
8270
8271
8272void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8273 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008274{
8275 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8276 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008277 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008278 /**
8279 * Remove the network by scheduling the group formation
8280 * timeout to happen immediately. The teardown code
8281 * needs to be scheduled to run asynch later so that we
8282 * don't delete data from under ourselves unexpectedly.
8283 * Calling wpas_p2p_group_formation_timeout directly
8284 * causes a series of crashes in WPS failure scenarios.
8285 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008286 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8287 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008288 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008289 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008290 }
8291}
8292
8293
8294struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008295 const u8 *addr, const u8 *ssid,
8296 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008297{
8298 struct wpa_ssid *s;
8299 size_t i;
8300
8301 for (s = wpa_s->conf->ssid; s; s = s->next) {
8302 if (s->disabled != 2)
8303 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008304 if (ssid &&
8305 (ssid_len != s->ssid_len ||
8306 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8307 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008308 if (addr == NULL) {
8309 if (s->mode == WPAS_MODE_P2P_GO)
8310 return s;
8311 continue;
8312 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008313 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8314 return s; /* peer is GO in the persistent group */
8315 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8316 continue;
8317 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008318 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008319 addr, ETH_ALEN) == 0)
8320 return s; /* peer is P2P client in persistent
8321 * group */
8322 }
8323 }
8324
8325 return NULL;
8326}
8327
8328
8329void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8330 const u8 *addr)
8331{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008332 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008333 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008334 /*
8335 * This can happen if WPS provisioning step is not terminated
8336 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8337 * peer was able to connect, there is no need to time out group
8338 * formation after this, though. In addition, this is used with
8339 * the initial connection wait on the GO as a separate formation
8340 * timeout and as such, expected to be hit after the initial WPS
8341 * provisioning step.
8342 */
8343 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008344
8345 if (!wpa_s->p2p_go_group_formation_completed &&
8346 !wpa_s->group_formation_reported) {
8347 /*
8348 * GO has not yet notified group formation success since
8349 * the WPS step was not completed cleanly. Do that
8350 * notification now since the P2P Client was able to
8351 * connect and as such, must have received the
8352 * credential from the WPS step.
8353 */
8354 if (wpa_s->global->p2p)
8355 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008356 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008357 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008358 }
8359 if (!wpa_s->p2p_go_group_formation_completed) {
8360 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8361 wpa_s->p2p_go_group_formation_completed = 1;
8362 wpa_s->global->p2p_group_formation = NULL;
8363 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008364 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008365 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008366 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008367 if (addr == NULL)
8368 return;
8369 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8370}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008371
Dmitry Shmidt04949592012-07-19 12:16:46 -07008372
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008373static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8374 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008375{
8376 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008377 int ret = 0;
8378
Dmitry Shmidt04949592012-07-19 12:16:46 -07008379 if (wpa_s->global->p2p_group_formation)
8380 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008381 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008382 offchannel_send_action_done(wpa_s);
8383 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008384 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008385 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8386 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8387 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8388 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008389 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008390 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008391 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008392 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008393 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008394 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008395 wpa_s->p2p_go_he,
8396 wpa_s->p2p_go_edmg,
8397 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008398 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008399}
8400
8401
8402int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8403{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008404 int res;
8405
Dmitry Shmidt04949592012-07-19 12:16:46 -07008406 if (!wpa_s->p2p_fallback_to_go_neg ||
8407 wpa_s->p2p_in_provisioning <= 5)
8408 return 0;
8409
8410 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8411 return 0; /* peer operating as a GO */
8412
8413 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8414 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008415 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008416 "reason=GO-not-found");
8417 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008418
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008419 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008420}
8421
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008422
8423unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8424{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008425 struct wpa_supplicant *ifs;
8426
8427 if (wpa_s->wpa_state > WPA_SCANNING) {
8428 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8429 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008430 wpa_s->conf->p2p_search_delay);
8431 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008432 }
8433
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008434 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8435 radio_list) {
8436 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008437 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8438 "delay due to concurrent operation on "
8439 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008440 wpa_s->conf->p2p_search_delay,
8441 ifs->ifname);
8442 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008443 }
8444 }
8445
8446 return 0;
8447}
8448
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008449
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008450static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8451 struct wpa_ssid *s, const u8 *addr,
8452 int iface_addr)
8453{
8454 struct psk_list_entry *psk, *tmp;
8455 int changed = 0;
8456
8457 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8458 list) {
8459 if ((iface_addr && !psk->p2p &&
8460 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8461 (!iface_addr && psk->p2p &&
8462 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8463 wpa_dbg(wpa_s, MSG_DEBUG,
8464 "P2P: Remove persistent group PSK list entry for "
8465 MACSTR " p2p=%u",
8466 MAC2STR(psk->addr), psk->p2p);
8467 dl_list_del(&psk->list);
8468 os_free(psk);
8469 changed++;
8470 }
8471 }
8472
8473 return changed;
8474}
8475
8476
8477void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8478 const u8 *p2p_dev_addr,
8479 const u8 *psk, size_t psk_len)
8480{
8481 struct wpa_ssid *ssid = wpa_s->current_ssid;
8482 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008483 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008484
8485 if (psk_len != sizeof(p->psk))
8486 return;
8487
8488 if (p2p_dev_addr) {
8489 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8490 " p2p_dev_addr=" MACSTR,
8491 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8492 if (is_zero_ether_addr(p2p_dev_addr))
8493 p2p_dev_addr = NULL;
8494 } else {
8495 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8496 MAC2STR(mac_addr));
8497 }
8498
8499 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8500 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8501 /* To be added to persistent group once created */
8502 if (wpa_s->global->add_psk == NULL) {
8503 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8504 if (wpa_s->global->add_psk == NULL)
8505 return;
8506 }
8507 p = wpa_s->global->add_psk;
8508 if (p2p_dev_addr) {
8509 p->p2p = 1;
8510 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8511 } else {
8512 p->p2p = 0;
8513 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8514 }
8515 os_memcpy(p->psk, psk, psk_len);
8516 return;
8517 }
8518
8519 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8520 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8521 return;
8522 }
8523
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008524 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008525 ssid->ssid_len);
8526 if (!persistent) {
8527 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8528 return;
8529 }
8530
8531 p = os_zalloc(sizeof(*p));
8532 if (p == NULL)
8533 return;
8534 if (p2p_dev_addr) {
8535 p->p2p = 1;
8536 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8537 } else {
8538 p->p2p = 0;
8539 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8540 }
8541 os_memcpy(p->psk, psk, psk_len);
8542
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008543 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8544 (last = dl_list_last(&persistent->psk_list,
8545 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008546 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8547 MACSTR " (p2p=%u) to make room for a new one",
8548 MAC2STR(last->addr), last->p2p);
8549 dl_list_del(&last->list);
8550 os_free(last);
8551 }
8552
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008553 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008554 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8555 p2p_dev_addr == NULL);
8556 if (p2p_dev_addr) {
8557 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8558 MACSTR, MAC2STR(p2p_dev_addr));
8559 } else {
8560 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8561 MAC2STR(mac_addr));
8562 }
8563 dl_list_add(&persistent->psk_list, &p->list);
8564
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008565 if (wpa_s->p2pdev->conf->update_config &&
8566 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008567 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008568}
8569
8570
8571static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8572 struct wpa_ssid *s, const u8 *addr,
8573 int iface_addr)
8574{
8575 int res;
8576
8577 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008578 if (res > 0 && wpa_s->conf->update_config &&
8579 wpa_config_write(wpa_s->confname, wpa_s->conf))
8580 wpa_dbg(wpa_s, MSG_DEBUG,
8581 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008582}
8583
8584
8585static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8586 const u8 *peer, int iface_addr)
8587{
8588 struct hostapd_data *hapd;
8589 struct hostapd_wpa_psk *psk, *prev, *rem;
8590 struct sta_info *sta;
8591
8592 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8593 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8594 return;
8595
8596 /* Remove per-station PSK entry */
8597 hapd = wpa_s->ap_iface->bss[0];
8598 prev = NULL;
8599 psk = hapd->conf->ssid.wpa_psk;
8600 while (psk) {
8601 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8602 (!iface_addr &&
8603 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8604 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8605 MACSTR " iface_addr=%d",
8606 MAC2STR(peer), iface_addr);
8607 if (prev)
8608 prev->next = psk->next;
8609 else
8610 hapd->conf->ssid.wpa_psk = psk->next;
8611 rem = psk;
8612 psk = psk->next;
8613 os_free(rem);
8614 } else {
8615 prev = psk;
8616 psk = psk->next;
8617 }
8618 }
8619
8620 /* Disconnect from group */
8621 if (iface_addr)
8622 sta = ap_get_sta(hapd, peer);
8623 else
8624 sta = ap_get_sta_p2p(hapd, peer);
8625 if (sta) {
8626 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8627 " (iface_addr=%d) from group",
8628 MAC2STR(peer), iface_addr);
8629 hostapd_drv_sta_deauth(hapd, sta->addr,
8630 WLAN_REASON_DEAUTH_LEAVING);
8631 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8632 }
8633}
8634
8635
8636void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8637 int iface_addr)
8638{
8639 struct wpa_ssid *s;
8640 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008641 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008642
8643 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8644
8645 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008646 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008647 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8648 continue;
8649 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008650 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8651 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008652 }
8653
8654 /* Remove from any operating group */
8655 for (w = wpa_s->global->ifaces; w; w = w->next)
8656 wpas_p2p_remove_client_go(w, peer, iface_addr);
8657}
8658
8659
8660static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8661{
8662 struct wpa_supplicant *wpa_s = eloop_ctx;
8663 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8664}
8665
8666
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008667static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8668{
8669 struct wpa_supplicant *wpa_s = eloop_ctx;
8670
8671 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8672 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8673}
8674
8675
8676int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8677 struct wpa_ssid *ssid)
8678{
8679 struct wpa_supplicant *iface;
8680
8681 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8682 if (!iface->current_ssid ||
8683 iface->current_ssid->frequency == freq ||
8684 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8685 !iface->current_ssid->p2p_group))
8686 continue;
8687
8688 /* Remove the connection with least priority */
8689 if (!wpas_is_p2p_prioritized(iface)) {
8690 /* STA connection has priority over existing
8691 * P2P connection, so remove the interface. */
8692 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8693 eloop_register_timeout(0, 0,
8694 wpas_p2p_group_freq_conflict,
8695 iface, NULL);
8696 /* If connection in progress is P2P connection, do not
8697 * proceed for the connection. */
8698 if (wpa_s == iface)
8699 return -1;
8700 else
8701 return 0;
8702 } else {
8703 /* P2P connection has priority, disable the STA network
8704 */
8705 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8706 ssid);
8707 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8708 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8709 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8710 ETH_ALEN);
8711 /* If P2P connection is in progress, continue
8712 * connecting...*/
8713 if (wpa_s == iface)
8714 return 0;
8715 else
8716 return -1;
8717 }
8718 }
8719
8720 return 0;
8721}
8722
8723
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008724int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8725{
8726 struct wpa_ssid *ssid = wpa_s->current_ssid;
8727
8728 if (ssid == NULL || !ssid->p2p_group)
8729 return 0;
8730
8731 if (wpa_s->p2p_last_4way_hs_fail &&
8732 wpa_s->p2p_last_4way_hs_fail == ssid) {
8733 u8 go_dev_addr[ETH_ALEN];
8734 struct wpa_ssid *persistent;
8735
8736 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8737 ssid->ssid,
8738 ssid->ssid_len) <= 0) {
8739 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8740 goto disconnect;
8741 }
8742
8743 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8744 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008745 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008746 ssid->ssid,
8747 ssid->ssid_len);
8748 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8749 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8750 goto disconnect;
8751 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008752 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008753 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8754 persistent->id);
8755 disconnect:
8756 wpa_s->p2p_last_4way_hs_fail = NULL;
8757 /*
8758 * Remove the group from a timeout to avoid issues with caller
8759 * continuing to use the interface if this is on a P2P group
8760 * interface.
8761 */
8762 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8763 wpa_s, NULL);
8764 return 1;
8765 }
8766
8767 wpa_s->p2p_last_4way_hs_fail = ssid;
8768 return 0;
8769}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008770
8771
8772#ifdef CONFIG_WPS_NFC
8773
8774static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8775 struct wpabuf *p2p)
8776{
8777 struct wpabuf *ret;
8778 size_t wsc_len;
8779
8780 if (p2p == NULL) {
8781 wpabuf_free(wsc);
8782 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8783 return NULL;
8784 }
8785
8786 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8787 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8788 if (ret == NULL) {
8789 wpabuf_free(wsc);
8790 wpabuf_free(p2p);
8791 return NULL;
8792 }
8793
8794 wpabuf_put_be16(ret, wsc_len);
8795 if (wsc)
8796 wpabuf_put_buf(ret, wsc);
8797 wpabuf_put_be16(ret, wpabuf_len(p2p));
8798 wpabuf_put_buf(ret, p2p);
8799
8800 wpabuf_free(wsc);
8801 wpabuf_free(p2p);
8802 wpa_hexdump_buf(MSG_DEBUG,
8803 "P2P: Generated NFC connection handover message", ret);
8804
8805 if (ndef && ret) {
8806 struct wpabuf *tmp;
8807 tmp = ndef_build_p2p(ret);
8808 wpabuf_free(ret);
8809 if (tmp == NULL) {
8810 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8811 return NULL;
8812 }
8813 ret = tmp;
8814 }
8815
8816 return ret;
8817}
8818
8819
8820static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8821 struct wpa_ssid **ssid, u8 *go_dev_addr)
8822{
8823 struct wpa_supplicant *iface;
8824
8825 if (go_dev_addr)
8826 os_memset(go_dev_addr, 0, ETH_ALEN);
8827 if (ssid)
8828 *ssid = NULL;
8829 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8830 if (iface->wpa_state < WPA_ASSOCIATING ||
8831 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8832 !iface->current_ssid->p2p_group ||
8833 iface->current_ssid->mode != WPAS_MODE_INFRA)
8834 continue;
8835 if (ssid)
8836 *ssid = iface->current_ssid;
8837 if (go_dev_addr)
8838 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8839 return iface->assoc_freq;
8840 }
8841 return 0;
8842}
8843
8844
8845struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8846 int ndef)
8847{
8848 struct wpabuf *wsc, *p2p;
8849 struct wpa_ssid *ssid;
8850 u8 go_dev_addr[ETH_ALEN];
8851 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8852
8853 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8854 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8855 return NULL;
8856 }
8857
8858 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8859 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8860 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8862 return NULL;
8863 }
8864
8865 if (cli_freq == 0) {
8866 wsc = wps_build_nfc_handover_req_p2p(
8867 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8868 } else
8869 wsc = NULL;
8870 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8871 go_dev_addr, ssid ? ssid->ssid : NULL,
8872 ssid ? ssid->ssid_len : 0);
8873
8874 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8875}
8876
8877
8878struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8879 int ndef, int tag)
8880{
8881 struct wpabuf *wsc, *p2p;
8882 struct wpa_ssid *ssid;
8883 u8 go_dev_addr[ETH_ALEN];
8884 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8885
8886 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8887 return NULL;
8888
8889 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8890 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8891 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8892 return NULL;
8893
8894 if (cli_freq == 0) {
8895 wsc = wps_build_nfc_handover_sel_p2p(
8896 wpa_s->parent->wps,
8897 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8898 DEV_PW_NFC_CONNECTION_HANDOVER,
8899 wpa_s->conf->wps_nfc_dh_pubkey,
8900 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8901 } else
8902 wsc = NULL;
8903 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8904 go_dev_addr, ssid ? ssid->ssid : NULL,
8905 ssid ? ssid->ssid_len : 0);
8906
8907 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8908}
8909
8910
8911static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8912 struct p2p_nfc_params *params)
8913{
8914 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8915 "connection handover (freq=%d)",
8916 params->go_freq);
8917
8918 if (params->go_freq && params->go_ssid_len) {
8919 wpa_s->p2p_wps_method = WPS_NFC;
8920 wpa_s->pending_join_wps_method = WPS_NFC;
8921 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8922 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8923 ETH_ALEN);
8924 return wpas_p2p_join_start(wpa_s, params->go_freq,
8925 params->go_ssid,
8926 params->go_ssid_len);
8927 }
8928
8929 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8930 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008931 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008932 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008933 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008934 params->go_ssid_len ? params->go_ssid : NULL,
8935 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008936}
8937
8938
8939static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8940 struct p2p_nfc_params *params, int tag)
8941{
8942 int res, persistent;
8943 struct wpa_ssid *ssid;
8944
8945 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8946 "connection handover");
8947 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8948 ssid = wpa_s->current_ssid;
8949 if (ssid == NULL)
8950 continue;
8951 if (ssid->mode != WPAS_MODE_P2P_GO)
8952 continue;
8953 if (wpa_s->ap_iface == NULL)
8954 continue;
8955 break;
8956 }
8957 if (wpa_s == NULL) {
8958 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8959 return -1;
8960 }
8961
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008962 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008963 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008964 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008965 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8966 return -1;
8967 }
8968 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008969 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8970 wpa_s->p2pdev->p2p_oob_dev_pw,
8971 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8972 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008973 if (res)
8974 return res;
8975
8976 if (!tag) {
8977 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8978 return 0;
8979 }
8980
8981 if (!params->peer ||
8982 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8983 return 0;
8984
8985 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8986 " to join", MAC2STR(params->peer->p2p_device_addr));
8987
8988 wpa_s->global->p2p_invite_group = wpa_s;
8989 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008990 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008991 params->peer->p2p_device_addr,
8992 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008993 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008994
8995 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8996 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8997 ssid->ssid, ssid->ssid_len, ssid->frequency,
8998 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008999 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009000}
9001
9002
9003static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9004 struct p2p_nfc_params *params,
9005 int forced_freq)
9006{
9007 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9008 "connection handover");
9009 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9010 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009011 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009012 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009013 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9014 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009015}
9016
9017
9018static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9019 struct p2p_nfc_params *params,
9020 int forced_freq)
9021{
9022 int res;
9023
9024 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9025 "connection handover");
9026 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9027 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009028 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009029 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009030 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9031 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009032 if (res)
9033 return res;
9034
9035 res = wpas_p2p_listen(wpa_s, 60);
9036 if (res) {
9037 p2p_unauthorize(wpa_s->global->p2p,
9038 params->peer->p2p_device_addr);
9039 }
9040
9041 return res;
9042}
9043
9044
9045static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9046 const struct wpabuf *data,
9047 int sel, int tag, int forced_freq)
9048{
9049 const u8 *pos, *end;
9050 u16 len, id;
9051 struct p2p_nfc_params params;
9052 int res;
9053
9054 os_memset(&params, 0, sizeof(params));
9055 params.sel = sel;
9056
9057 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9058
9059 pos = wpabuf_head(data);
9060 end = pos + wpabuf_len(data);
9061
9062 if (end - pos < 2) {
9063 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9064 "attributes");
9065 return -1;
9066 }
9067 len = WPA_GET_BE16(pos);
9068 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009069 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009070 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9071 "attributes");
9072 return -1;
9073 }
9074 params.wsc_attr = pos;
9075 params.wsc_len = len;
9076 pos += len;
9077
9078 if (end - pos < 2) {
9079 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9080 "attributes");
9081 return -1;
9082 }
9083 len = WPA_GET_BE16(pos);
9084 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009085 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009086 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9087 "attributes");
9088 return -1;
9089 }
9090 params.p2p_attr = pos;
9091 params.p2p_len = len;
9092 pos += len;
9093
9094 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9095 params.wsc_attr, params.wsc_len);
9096 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9097 params.p2p_attr, params.p2p_len);
9098 if (pos < end) {
9099 wpa_hexdump(MSG_DEBUG,
9100 "P2P: Ignored extra data after P2P attributes",
9101 pos, end - pos);
9102 }
9103
9104 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9105 if (res)
9106 return res;
9107
9108 if (params.next_step == NO_ACTION)
9109 return 0;
9110
9111 if (params.next_step == BOTH_GO) {
9112 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9113 MAC2STR(params.peer->p2p_device_addr));
9114 return 0;
9115 }
9116
9117 if (params.next_step == PEER_CLIENT) {
9118 if (!is_zero_ether_addr(params.go_dev_addr)) {
9119 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9120 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9121 " ssid=\"%s\"",
9122 MAC2STR(params.peer->p2p_device_addr),
9123 params.go_freq,
9124 MAC2STR(params.go_dev_addr),
9125 wpa_ssid_txt(params.go_ssid,
9126 params.go_ssid_len));
9127 } else {
9128 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9129 "peer=" MACSTR " freq=%d",
9130 MAC2STR(params.peer->p2p_device_addr),
9131 params.go_freq);
9132 }
9133 return 0;
9134 }
9135
9136 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9137 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9138 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9139 return 0;
9140 }
9141
9142 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9143 wpa_s->p2p_oob_dev_pw = NULL;
9144
9145 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9146 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9147 "received");
9148 return -1;
9149 }
9150
9151 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9152 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9153 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9154 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9155 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9156 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9157 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9158
9159 if (tag) {
9160 if (id < 0x10) {
9161 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9162 "peer OOB Device Password Id %u", id);
9163 return -1;
9164 }
9165 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9166 "Device Password Id %u", id);
9167 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9168 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9169 params.oob_dev_pw_len -
9170 WPS_OOB_PUBKEY_HASH_LEN - 2);
9171 wpa_s->p2p_oob_dev_pw_id = id;
9172 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9173 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9174 params.oob_dev_pw_len -
9175 WPS_OOB_PUBKEY_HASH_LEN - 2);
9176 if (wpa_s->p2p_oob_dev_pw == NULL)
9177 return -1;
9178
9179 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9180 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9181 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9182 return -1;
9183 } else {
9184 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9185 "without Device Password");
9186 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9187 }
9188
9189 switch (params.next_step) {
9190 case NO_ACTION:
9191 case BOTH_GO:
9192 case PEER_CLIENT:
9193 /* already covered above */
9194 return 0;
9195 case JOIN_GROUP:
9196 return wpas_p2p_nfc_join_group(wpa_s, &params);
9197 case AUTH_JOIN:
9198 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9199 case INIT_GO_NEG:
9200 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9201 case RESP_GO_NEG:
9202 /* TODO: use own OOB Dev Pw */
9203 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9204 }
9205
9206 return -1;
9207}
9208
9209
9210int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9211 const struct wpabuf *data, int forced_freq)
9212{
9213 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9214 return -1;
9215
9216 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9217}
9218
9219
9220int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9221 const struct wpabuf *req,
9222 const struct wpabuf *sel, int forced_freq)
9223{
9224 struct wpabuf *tmp;
9225 int ret;
9226
9227 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9228 return -1;
9229
9230 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9231
9232 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9233 wpabuf_head(req), wpabuf_len(req));
9234 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9235 wpabuf_head(sel), wpabuf_len(sel));
9236 if (forced_freq)
9237 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9238 tmp = ndef_parse_p2p(init ? sel : req);
9239 if (tmp == NULL) {
9240 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9241 return -1;
9242 }
9243
9244 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9245 forced_freq);
9246 wpabuf_free(tmp);
9247
9248 return ret;
9249}
9250
9251
9252int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9253{
9254 const u8 *if_addr;
9255 int go_intent = wpa_s->conf->p2p_go_intent;
9256 struct wpa_supplicant *iface;
9257
9258 if (wpa_s->global->p2p == NULL)
9259 return -1;
9260
9261 if (!enabled) {
9262 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9263 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9264 {
9265 if (!iface->ap_iface)
9266 continue;
9267 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9268 }
9269 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9270 0, NULL);
9271 if (wpa_s->p2p_nfc_tag_enabled)
9272 wpas_p2p_remove_pending_group_interface(wpa_s);
9273 wpa_s->p2p_nfc_tag_enabled = 0;
9274 return 0;
9275 }
9276
9277 if (wpa_s->global->p2p_disabled)
9278 return -1;
9279
9280 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9281 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9282 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9283 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9284 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9285 "to allow static handover cases");
9286 return -1;
9287 }
9288
9289 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9290
9291 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9292 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9293 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9294 if (wpa_s->p2p_oob_dev_pw == NULL)
9295 return -1;
9296 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9297
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009298 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9299 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9300 /*
9301 * P2P Group Interface present and the command came on group
9302 * interface, so enable the token for the current interface.
9303 */
9304 wpa_s->create_p2p_iface = 0;
9305 } else {
9306 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9307 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009308
9309 if (wpa_s->create_p2p_iface) {
9310 enum wpa_driver_if_type iftype;
9311 /* Prepare to add a new interface for the group */
9312 iftype = WPA_IF_P2P_GROUP;
9313 if (go_intent == 15)
9314 iftype = WPA_IF_P2P_GO;
9315 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9316 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9317 "interface for the group");
9318 return -1;
9319 }
9320
9321 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009322 } else if (wpa_s->p2p_mgmt)
9323 if_addr = wpa_s->parent->own_addr;
9324 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009325 if_addr = wpa_s->own_addr;
9326
9327 wpa_s->p2p_nfc_tag_enabled = enabled;
9328
9329 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9330 struct hostapd_data *hapd;
9331 if (iface->ap_iface == NULL)
9332 continue;
9333 hapd = iface->ap_iface->bss[0];
9334 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9335 hapd->conf->wps_nfc_dh_pubkey =
9336 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9337 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9338 hapd->conf->wps_nfc_dh_privkey =
9339 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9340 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9341 hapd->conf->wps_nfc_dev_pw =
9342 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9343 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9344
9345 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9346 wpa_dbg(iface, MSG_DEBUG,
9347 "P2P: Failed to enable NFC Tag for GO");
9348 }
9349 }
9350 p2p_set_authorized_oob_dev_pw_id(
9351 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9352 if_addr);
9353
9354 return 0;
9355}
9356
9357#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009358
9359
9360static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9361 struct wpa_used_freq_data *freqs,
9362 unsigned int num)
9363{
9364 u8 curr_chan, cand, chan;
9365 unsigned int i;
9366
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009367 /*
9368 * If possible, optimize the Listen channel to be a channel that is
9369 * already used by one of the other interfaces.
9370 */
9371 if (!wpa_s->conf->p2p_optimize_listen_chan)
9372 return;
9373
9374 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9375 return;
9376
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009377 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9378 for (i = 0, cand = 0; i < num; i++) {
9379 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9380 if (curr_chan == chan) {
9381 cand = 0;
9382 break;
9383 }
9384
9385 if (chan == 1 || chan == 6 || chan == 11)
9386 cand = chan;
9387 }
9388
9389 if (cand) {
9390 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009391 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009392 cand);
9393 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9394 }
9395}
9396
9397
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009398static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009399{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009400 struct hostapd_config *conf;
9401 struct p2p_go_neg_results params;
9402 struct csa_settings csa_settings;
9403 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9404 int old_freq = current_ssid->frequency;
9405 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009407 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9408 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9409 return -1;
9410 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009411
9412 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009413 * TODO: This function may not always work correctly. For example,
9414 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009415 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009416 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9417 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009418 wpa_dbg(wpa_s, MSG_DEBUG,
9419 "P2P CSA: Failed to select new frequency for GO");
9420 return -1;
9421 }
9422
9423 if (current_ssid->frequency == params.freq) {
9424 wpa_dbg(wpa_s, MSG_DEBUG,
9425 "P2P CSA: Selected same frequency - not moving GO");
9426 return 0;
9427 }
9428
9429 conf = hostapd_config_defaults();
9430 if (!conf) {
9431 wpa_dbg(wpa_s, MSG_DEBUG,
9432 "P2P CSA: Failed to allocate default config");
9433 return -1;
9434 }
9435
9436 current_ssid->frequency = params.freq;
9437 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9438 wpa_dbg(wpa_s, MSG_DEBUG,
9439 "P2P CSA: Failed to create new GO config");
9440 ret = -1;
9441 goto out;
9442 }
9443
9444 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9445 wpa_dbg(wpa_s, MSG_DEBUG,
9446 "P2P CSA: CSA to a different band is not supported");
9447 ret = -1;
9448 goto out;
9449 }
9450
9451 os_memset(&csa_settings, 0, sizeof(csa_settings));
9452 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9453 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9454 csa_settings.freq_params.freq = params.freq;
9455 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9456 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9457 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9458
9459 if (conf->ieee80211ac) {
9460 int freq1 = 0, freq2 = 0;
9461 u8 chan, opclass;
9462
9463 if (ieee80211_freq_to_channel_ext(params.freq,
9464 conf->secondary_channel,
9465 conf->vht_oper_chwidth,
9466 &opclass, &chan) ==
9467 NUM_HOSTAPD_MODES) {
9468 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9469 ret = -1;
9470 goto out;
9471 }
9472
9473 if (conf->vht_oper_centr_freq_seg0_idx)
9474 freq1 = ieee80211_chan_to_freq(
9475 NULL, opclass,
9476 conf->vht_oper_centr_freq_seg0_idx);
9477
9478 if (conf->vht_oper_centr_freq_seg1_idx)
9479 freq2 = ieee80211_chan_to_freq(
9480 NULL, opclass,
9481 conf->vht_oper_centr_freq_seg1_idx);
9482
9483 if (freq1 < 0 || freq2 < 0) {
9484 wpa_dbg(wpa_s, MSG_DEBUG,
9485 "P2P CSA: Selected invalid VHT center freqs");
9486 ret = -1;
9487 goto out;
9488 }
9489
9490 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9491 csa_settings.freq_params.center_freq1 = freq1;
9492 csa_settings.freq_params.center_freq2 = freq2;
9493
9494 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009495 case CHANWIDTH_80MHZ:
9496 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009497 csa_settings.freq_params.bandwidth = 80;
9498 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009499 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009500 csa_settings.freq_params.bandwidth = 160;
9501 break;
9502 }
9503 }
9504
9505 ret = ap_switch_channel(wpa_s, &csa_settings);
9506out:
9507 current_ssid->frequency = old_freq;
9508 hostapd_config_free(conf);
9509 return ret;
9510}
9511
9512
9513static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9514{
9515 struct p2p_go_neg_results params;
9516 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009517 void (*ap_configured_cb)(void *ctx, void *data);
9518 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009519
9520 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9521
9522 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9523 current_ssid->frequency);
9524
9525 /* Stop the AP functionality */
9526 /* TODO: Should do this in a way that does not indicated to possible
9527 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009528 /* If this action occurs before a group is started, the callback should
9529 * be preserved, or GROUP-STARTED event would be lost. If this action
9530 * occurs after a group is started, these pointers are all NULL and
9531 * harmless. */
9532 ap_configured_cb = wpa_s->ap_configured_cb;
9533 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9534 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009535 wpa_supplicant_ap_deinit(wpa_s);
9536
9537 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009538 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9539 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009540 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9541 wpas_p2p_group_delete(wpa_s,
9542 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9543 return;
9544 }
9545 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9546 params.freq);
9547
9548 if (params.freq &&
9549 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9550 wpa_printf(MSG_DEBUG,
9551 "P2P: Selected freq (%u MHz) is not valid for P2P",
9552 params.freq);
9553 wpas_p2p_group_delete(wpa_s,
9554 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9555 return;
9556 }
9557
Hai Shalom899fcc72020-10-19 14:38:18 -07009558 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009559 wpa_s->ap_configured_cb = ap_configured_cb;
9560 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9561 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009562
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009563 /* Update the frequency */
9564 current_ssid->frequency = params.freq;
9565 wpa_s->connect_without_scan = current_ssid;
9566 wpa_s->reassociate = 1;
9567 wpa_s->disconnected = 0;
9568 wpa_supplicant_req_scan(wpa_s, 0, 0);
9569}
9570
9571
9572static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9573{
9574 struct wpa_supplicant *wpa_s = eloop_ctx;
9575
9576 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009577 return;
9578
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009579 wpas_p2p_go_update_common_freqs(wpa_s);
9580
9581 /* Do not move GO in the middle of a CSA */
9582 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9583 wpa_printf(MSG_DEBUG,
9584 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009585 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009586 }
9587
9588 /*
9589 * First, try a channel switch flow. If it is not supported or fails,
9590 * take down the GO and bring it up again.
9591 */
9592 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9593 wpas_p2p_move_go_no_csa(wpa_s);
9594}
9595
9596
9597static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9598{
9599 struct wpa_supplicant *wpa_s = eloop_ctx;
9600 struct wpa_used_freq_data *freqs = NULL;
9601 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009602
9603 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9604 if (!freqs)
9605 return;
9606
9607 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9608
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009609 /* Previous attempt to move a GO was not possible -- try again. */
9610 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9611 WPAS_P2P_CHANNEL_UPDATE_ANY);
9612
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009613 os_free(freqs);
9614}
9615
9616
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009617/*
9618 * Consider moving a GO from its currently used frequency:
9619 * 1. It is possible that due to regulatory consideration the frequency
9620 * can no longer be used and there is a need to evacuate the GO.
9621 * 2. It is possible that due to MCC considerations, it would be preferable
9622 * to move the GO to a channel that is currently used by some other
9623 * station interface.
9624 *
9625 * In case a frequency that became invalid is once again valid, cancel a
9626 * previously initiated GO frequency change.
9627 */
9628static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9629 struct wpa_used_freq_data *freqs,
9630 unsigned int num)
9631{
9632 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9633 unsigned int timeout;
9634 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009635 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009636
9637 wpas_p2p_go_update_common_freqs(wpa_s);
9638
9639 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009640 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009641 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009642 for (i = 0, invalid_freq = 0; i < num; i++) {
9643 if (freqs[i].freq == freq) {
9644 flags = freqs[i].flags;
9645
9646 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009647 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9648 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009649 wpa_dbg(wpa_s, MSG_DEBUG,
9650 "P2P: Freq=%d MHz no longer valid for GO",
9651 freq);
9652 invalid_freq = 1;
9653 }
9654 } else if (freqs[i].flags == 0) {
9655 /* Freq is not used by any other station interface */
9656 continue;
9657 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009658 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009659 /* Freq is not valid for P2P use cases */
9660 continue;
9661 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9662 P2P_GO_FREQ_MOVE_SCM) {
9663 policy_move = 1;
9664 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9665 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9666 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9667 policy_move = 1;
9668 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9669 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9670 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9671 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9672 policy_move = 1;
9673 } else if ((wpa_s->drv_flags &
9674 WPA_DRIVER_FLAGS_AP_CSA) &&
9675 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9676 u8 chan;
9677
9678 /*
9679 * We do not support CSA between bands, so move
9680 * GO only within the same band.
9681 */
9682 if (wpa_s->ap_iface->current_mode->mode ==
9683 ieee80211_freq_to_chan(freqs[i].freq,
9684 &chan))
9685 policy_move = 1;
9686 }
9687 }
9688 }
9689
9690 wpa_dbg(wpa_s, MSG_DEBUG,
9691 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9692 invalid_freq, policy_move, flags);
9693
9694 /*
9695 * The channel is valid, or we are going to have a policy move, so
9696 * cancel timeout.
9697 */
9698 if (!invalid_freq || policy_move) {
9699 wpa_dbg(wpa_s, MSG_DEBUG,
9700 "P2P: Cancel a GO move from freq=%d MHz", freq);
9701 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9702
9703 if (wpas_p2p_in_progress(wpa_s)) {
9704 wpa_dbg(wpa_s, MSG_DEBUG,
9705 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9706 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9707 wpa_s, NULL);
9708 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9709 wpas_p2p_reconsider_moving_go,
9710 wpa_s, NULL);
9711 return;
9712 }
9713 }
9714
9715 if (!invalid_freq && (!policy_move || flags != 0)) {
9716 wpa_dbg(wpa_s, MSG_DEBUG,
9717 "P2P: Not initiating a GO frequency change");
9718 return;
9719 }
9720
9721 /*
9722 * Do not consider moving GO if it is in the middle of a CSA. When the
9723 * CSA is finished this flow should be retriggered.
9724 */
9725 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9726 wpa_dbg(wpa_s, MSG_DEBUG,
9727 "P2P: Not initiating a GO frequency change - CSA is in progress");
9728 return;
9729 }
9730
9731 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9732 timeout = P2P_GO_FREQ_CHANGE_TIME;
9733 else
9734 timeout = 0;
9735
9736 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9737 freq, timeout);
9738 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9739 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9740}
9741
9742
9743static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9744 struct wpa_used_freq_data *freqs,
9745 unsigned int num,
9746 enum wpas_p2p_channel_update_trig trig)
9747{
9748 struct wpa_supplicant *ifs;
9749
9750 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9751 NULL);
9752
9753 /*
9754 * Travers all the radio interfaces, and for each GO interface, check
9755 * if there is a need to move the GO from the frequency it is using,
9756 * or in case the frequency is valid again, cancel the evacuation flow.
9757 */
9758 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9759 radio_list) {
9760 if (ifs->current_ssid == NULL ||
9761 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9762 continue;
9763
9764 /*
9765 * The GO was just started or completed channel switch, no need
9766 * to move it.
9767 */
9768 if (wpa_s == ifs &&
9769 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9770 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9771 wpa_dbg(wpa_s, MSG_DEBUG,
9772 "P2P: GO move - schedule re-consideration");
9773 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9774 wpas_p2p_reconsider_moving_go,
9775 wpa_s, NULL);
9776 continue;
9777 }
9778
9779 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9780 }
9781}
9782
9783
9784void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9785{
9786 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9787 return;
9788
9789 wpas_p2p_update_channel_list(wpa_s,
9790 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9791}
9792
9793
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009794void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9795{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009796 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9797 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9798 "the management interface is being removed");
9799 wpas_p2p_deinit_global(wpa_s->global);
9800 }
9801}
9802
9803
9804void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9805{
9806 if (wpa_s->ap_iface->bss)
9807 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9808 wpas_p2p_group_deinit(wpa_s);
9809}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009810
9811
9812int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9813 unsigned int period, unsigned int interval,
9814 unsigned int count)
9815{
9816 struct p2p_data *p2p = wpa_s->global->p2p;
9817 u8 *device_types;
9818 size_t dev_types_len;
9819 struct wpabuf *buf;
9820 int ret;
9821
9822 if (wpa_s->p2p_lo_started) {
9823 wpa_dbg(wpa_s, MSG_DEBUG,
9824 "P2P Listen offload is already started");
9825 return 0;
9826 }
9827
9828 if (wpa_s->global->p2p == NULL ||
9829 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9830 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9831 return -1;
9832 }
9833
9834 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9835 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9836 freq);
9837 return -1;
9838 }
9839
9840 /* Get device type */
9841 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9842 WPS_DEV_TYPE_LEN;
9843 device_types = os_malloc(dev_types_len);
9844 if (!device_types)
9845 return -1;
9846 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9847 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9848 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9849
9850 /* Get Probe Response IE(s) */
9851 buf = p2p_build_probe_resp_template(p2p, freq);
9852 if (!buf) {
9853 os_free(device_types);
9854 return -1;
9855 }
9856
9857 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9858 device_types, dev_types_len,
9859 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9860 if (ret < 0)
9861 wpa_dbg(wpa_s, MSG_DEBUG,
9862 "P2P: Failed to start P2P listen offload");
9863
9864 os_free(device_types);
9865 wpabuf_free(buf);
9866
9867 if (ret == 0) {
9868 wpa_s->p2p_lo_started = 1;
9869
9870 /* Stop current P2P listen if any */
9871 wpas_stop_listen(wpa_s);
9872 }
9873
9874 return ret;
9875}
9876
9877
9878int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9879{
9880 int ret;
9881
9882 if (!wpa_s->p2p_lo_started)
9883 return 0;
9884
9885 ret = wpa_drv_p2p_lo_stop(wpa_s);
9886 if (ret < 0)
9887 wpa_dbg(wpa_s, MSG_DEBUG,
9888 "P2P: Failed to stop P2P listen offload");
9889
9890 wpa_s->p2p_lo_started = 0;
9891 return ret;
9892}