blob: a7e7587b734dbe339c5aea762ba11ef5b987cdfb [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;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002498 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002499 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002500#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 struct wpa_supplicant *wpa_s = ctx;
2502 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002503 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002504 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002505
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002506#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002507 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2508 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002509 if (wfd_dev_info_hex) {
2510 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2511 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2512 // Only used for notification, so not handling error.
2513 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2514 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002515
2516 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2517 WFD_SUBELEM_R2_DEVICE_INFO);
2518 if (wfd_r2_dev_info_hex) {
2519 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2520 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2521 // Only used for notification, so not handling error.
2522 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2523 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002524#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002525
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002526 if (info->p2ps_instance) {
2527 char str[256];
2528 const u8 *buf = wpabuf_head(info->p2ps_instance);
2529 size_t len = wpabuf_len(info->p2ps_instance);
2530
2531 while (len) {
2532 u32 id;
2533 u16 methods;
2534 u8 str_len;
2535
2536 if (len < 4 + 2 + 1)
2537 break;
2538 id = WPA_GET_LE32(buf);
2539 buf += sizeof(u32);
2540 methods = WPA_GET_BE16(buf);
2541 buf += sizeof(u16);
2542 str_len = *buf++;
2543 if (str_len > len - 4 - 2 - 1)
2544 break;
2545 os_memcpy(str, buf, str_len);
2546 str[str_len] = '\0';
2547 buf += str_len;
2548 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2549
2550 wpa_msg_global(wpa_s, MSG_INFO,
2551 P2P_EVENT_DEVICE_FOUND MACSTR
2552 " p2p_dev_addr=" MACSTR
2553 " pri_dev_type=%s name='%s'"
2554 " config_methods=0x%x"
2555 " dev_capab=0x%x"
2556 " group_capab=0x%x"
2557 " adv_id=%x asp_svc=%s%s",
2558 MAC2STR(addr),
2559 MAC2STR(info->p2p_device_addr),
2560 wps_dev_type_bin2str(
2561 info->pri_dev_type,
2562 devtype, sizeof(devtype)),
2563 info->device_name, methods,
2564 info->dev_capab, info->group_capab,
2565 id, str,
2566 info->vendor_elems ?
2567 " vendor_elems=1" : "");
2568 }
2569 goto done;
2570 }
2571
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002572 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2573 " p2p_dev_addr=" MACSTR
2574 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002575 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002576 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2577 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2578 sizeof(devtype)),
2579 info->device_name, info->config_methods,
2580 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002581 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002582 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002583 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2584 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002585 info->vendor_elems ? " vendor_elems=1" : "",
2586 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002588done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002589 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002590 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002591#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002592
Roshan Piusfd2fd662017-01-23 13:41:57 -08002593 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002594 wfd_dev_info_len, wfd_r2_dev_info,
2595 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002596 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597}
2598
2599
2600static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2601{
2602 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002603
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002604 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2605 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2608}
2609
2610
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002611static void wpas_find_stopped(void *ctx)
2612{
2613 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002614
2615 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2616 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2617
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002618 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002619 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002620}
2621
2622
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002623struct wpas_p2p_listen_work {
2624 unsigned int freq;
2625 unsigned int duration;
2626 struct wpabuf *probe_resp_ie;
2627};
2628
2629
2630static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2631{
2632 if (lwork == NULL)
2633 return;
2634 wpabuf_free(lwork->probe_resp_ie);
2635 os_free(lwork);
2636}
2637
2638
2639static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2640{
2641 struct wpas_p2p_listen_work *lwork;
2642
2643 if (!wpa_s->p2p_listen_work)
2644 return;
2645
2646 lwork = wpa_s->p2p_listen_work->ctx;
2647 wpas_p2p_listen_work_free(lwork);
2648 radio_work_done(wpa_s->p2p_listen_work);
2649 wpa_s->p2p_listen_work = NULL;
2650}
2651
2652
2653static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2654{
2655 struct wpa_supplicant *wpa_s = work->wpa_s;
2656 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002657 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002658
2659 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002660 if (work->started) {
2661 wpa_s->p2p_listen_work = NULL;
2662 wpas_stop_listen(wpa_s);
2663 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002664 wpas_p2p_listen_work_free(lwork);
2665 return;
2666 }
2667
2668 wpa_s->p2p_listen_work = work;
2669
2670 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2671
2672 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2673 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2674 "report received Probe Request frames");
2675 wpas_p2p_listen_work_done(wpa_s);
2676 return;
2677 }
2678
2679 wpa_s->pending_listen_freq = lwork->freq;
2680 wpa_s->pending_listen_duration = lwork->duration;
2681
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002682 duration = lwork->duration;
2683#ifdef CONFIG_TESTING_OPTIONS
2684 if (wpa_s->extra_roc_dur) {
2685 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2686 duration, duration + wpa_s->extra_roc_dur);
2687 duration += wpa_s->extra_roc_dur;
2688 }
2689#endif /* CONFIG_TESTING_OPTIONS */
2690
2691 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002692 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2693 "to remain on channel (%u MHz) for Listen "
2694 "state", lwork->freq);
2695 wpas_p2p_listen_work_done(wpa_s);
2696 wpa_s->pending_listen_freq = 0;
2697 return;
2698 }
2699 wpa_s->off_channel_freq = 0;
2700 wpa_s->roc_waiting_drv_freq = lwork->freq;
2701}
2702
2703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704static int wpas_start_listen(void *ctx, unsigned int freq,
2705 unsigned int duration,
2706 const struct wpabuf *probe_resp_ie)
2707{
2708 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002709 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002711 if (wpa_s->p2p_listen_work) {
2712 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713 return -1;
2714 }
2715
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002716 lwork = os_zalloc(sizeof(*lwork));
2717 if (lwork == NULL)
2718 return -1;
2719 lwork->freq = freq;
2720 lwork->duration = duration;
2721 if (probe_resp_ie) {
2722 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2723 if (lwork->probe_resp_ie == NULL) {
2724 wpas_p2p_listen_work_free(lwork);
2725 return -1;
2726 }
2727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002729 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2730 lwork) < 0) {
2731 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732 return -1;
2733 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734
2735 return 0;
2736}
2737
2738
2739static void wpas_stop_listen(void *ctx)
2740{
2741 struct wpa_supplicant *wpa_s = ctx;
2742 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2743 wpa_drv_cancel_remain_on_channel(wpa_s);
2744 wpa_s->off_channel_freq = 0;
2745 wpa_s->roc_waiting_drv_freq = 0;
2746 }
2747 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002748
2749 /*
2750 * Don't cancel Probe Request RX reporting for a connected P2P Client
2751 * handling Probe Request frames.
2752 */
2753 if (!wpa_s->p2p_cli_probe)
2754 wpa_drv_probe_req_report(wpa_s, 0);
2755
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002756 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757}
2758
2759
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002760static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2761 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762{
2763 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002764 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002765 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766}
2767
2768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2770 const u8 *peer, const char *params,
2771 unsigned int generated_pin)
2772{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002773 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2774 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775}
2776
2777
2778static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2779 const u8 *peer, const char *params)
2780{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002781 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2782 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783}
2784
2785
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002786static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2787 const u8 *dev_addr, const u8 *pri_dev_type,
2788 const char *dev_name, u16 supp_config_methods,
2789 u8 dev_capab, u8 group_capab, const u8 *group_id,
2790 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791{
2792 struct wpa_supplicant *wpa_s = ctx;
2793 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002794 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 u8 empty_dev_type[8];
2796 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002797 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002798 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002799
2800 if (group_id) {
2801 for (group = wpa_s->global->ifaces; group; group = group->next)
2802 {
2803 struct wpa_ssid *s = group->current_ssid;
2804 if (s != NULL &&
2805 s->mode == WPAS_MODE_P2P_GO &&
2806 group_id_len - ETH_ALEN == s->ssid_len &&
2807 os_memcmp(group_id + ETH_ALEN, s->ssid,
2808 s->ssid_len) == 0)
2809 break;
2810 }
2811 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812
2813 if (pri_dev_type == NULL) {
2814 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2815 pri_dev_type = empty_dev_type;
2816 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002817 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2818 " pri_dev_type=%s name='%s' config_methods=0x%x "
2819 "dev_capab=0x%x group_capab=0x%x%s%s",
2820 MAC2STR(dev_addr),
2821 wps_dev_type_bin2str(pri_dev_type, devtype,
2822 sizeof(devtype)),
2823 dev_name, supp_config_methods, dev_capab, group_capab,
2824 group ? " group=" : "",
2825 group ? group->ifname : "");
2826 if (os_snprintf_error(sizeof(params), res))
2827 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002831 if (wps_generate_pin(&generated_pin) < 0) {
2832 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2833 wpas_notify_p2p_provision_discovery(
2834 wpa_s, peer, 0 /* response */,
2835 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2836 return;
2837 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838 wpas_prov_disc_local_display(wpa_s, peer, params,
2839 generated_pin);
2840 } else if (config_methods & WPS_CONFIG_KEYPAD)
2841 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2842 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002843 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2844 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002845
2846 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2847 P2P_PROV_DISC_SUCCESS,
2848 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849}
2850
2851
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002852static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853{
2854 struct wpa_supplicant *wpa_s = ctx;
2855 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002856 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002858 if (wpa_s->pending_pd_before_join &&
2859 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2860 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2861 wpa_s->pending_pd_before_join = 0;
2862 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2863 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002864 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002865 return;
2866 }
2867
Dmitry Shmidt04949592012-07-19 12:16:46 -07002868 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002869 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2870 int res;
2871
2872 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2873 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2874 if (os_snprintf_error(sizeof(params), res))
2875 params[sizeof(params) - 1] = '\0';
2876 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002877 params[0] = '\0';
2878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002880 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002882 if (wps_generate_pin(&generated_pin) < 0) {
2883 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2884 wpas_notify_p2p_provision_discovery(
2885 wpa_s, peer, 0 /* response */,
2886 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2887 return;
2888 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002889 wpas_prov_disc_local_display(wpa_s, peer, params,
2890 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002892 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2893 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894
Jouni Malinen75ecf522011-06-27 15:19:46 -07002895 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2896 P2P_PROV_DISC_SUCCESS,
2897 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898}
2899
2900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002901static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002902 enum p2p_prov_disc_status status,
2903 u32 adv_id, const u8 *adv_mac,
2904 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002905{
2906 struct wpa_supplicant *wpa_s = ctx;
2907
Dmitry Shmidt04949592012-07-19 12:16:46 -07002908 if (wpa_s->p2p_fallback_to_go_neg) {
2909 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2910 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002911 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002912 P2P_EVENT_FALLBACK_TO_GO_NEG
2913 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002914 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2915 return;
2916 }
2917
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002918 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002919 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002920 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2921 "join-existing-group operation (no ACK for PD "
2922 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002923 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002924 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002925 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002926
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002927 if (adv_id && adv_mac && deferred_session_resp) {
2928 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2929 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2930 " deferred_session_resp='%s'",
2931 MAC2STR(peer), status, adv_id,
2932 deferred_session_resp);
2933 } else if (adv_id && adv_mac) {
2934 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2935 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2936 MAC2STR(peer), status, adv_id);
2937 } else {
2938 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2939 " p2p_dev_addr=" MACSTR " status=%d",
2940 MAC2STR(peer), status);
2941 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002942
Jouni Malinen75ecf522011-06-27 15:19:46 -07002943 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2944 status, 0, 0);
2945}
2946
2947
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002948static int freq_included(struct wpa_supplicant *wpa_s,
2949 const struct p2p_channels *channels,
2950 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002951{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002952 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2953 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2954 return 1;
2955 return 0;
2956}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002957
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002958
2959static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2960{
2961 unsigned int num = P2P_MAX_CHANNELS;
2962 int *common_freqs;
2963 int ret;
2964
2965 p2p_go_dump_common_freqs(wpa_s);
2966 common_freqs = os_calloc(num, sizeof(int));
2967 if (!common_freqs)
2968 return;
2969
2970 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2971 if (ret < 0) {
2972 wpa_dbg(wpa_s, MSG_DEBUG,
2973 "P2P: Failed to get group common freqs");
2974 os_free(common_freqs);
2975 return;
2976 }
2977
2978 os_free(wpa_s->p2p_group_common_freqs);
2979 wpa_s->p2p_group_common_freqs = common_freqs;
2980 wpa_s->p2p_group_common_freqs_num = num;
2981 p2p_go_dump_common_freqs(wpa_s);
2982}
2983
2984
2985/*
2986 * Check if the given frequency is one of the possible operating frequencies
2987 * set after the completion of the GO Negotiation.
2988 */
2989static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2990{
2991 unsigned int i;
2992
2993 p2p_go_dump_common_freqs(wpa_s);
2994
2995 /* assume no restrictions */
2996 if (!wpa_s->p2p_group_common_freqs_num)
2997 return 1;
2998
2999 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3000 if (wpa_s->p2p_group_common_freqs[i] == freq)
3001 return 1;
3002 }
3003 return 0;
3004}
3005
3006
3007static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3008 struct sta_info *sta, void *ctx)
3009{
3010 int *ecsa_support = ctx;
3011
3012 *ecsa_support &= sta->ecsa_supported;
3013
3014 return 0;
3015}
3016
3017
3018/* Check if all the peers support eCSA */
3019static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3020{
3021 int ecsa_support = 1;
3022
3023 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3024 &ecsa_support);
3025
3026 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003027}
3028
3029
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003030/**
3031 * Pick the best frequency to use from all the currently used frequencies.
3032 */
3033static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3034 struct wpa_used_freq_data *freqs,
3035 unsigned int num)
3036{
3037 unsigned int i, c;
3038
3039 /* find a candidate freq that is supported by P2P */
3040 for (c = 0; c < num; c++)
3041 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3042 break;
3043
3044 if (c == num)
3045 return 0;
3046
3047 /* once we have a candidate, try to find a 'better' one */
3048 for (i = c + 1; i < num; i++) {
3049 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3050 continue;
3051
3052 /*
3053 * 1. Infrastructure station interfaces have higher preference.
3054 * 2. P2P Clients have higher preference.
3055 * 3. All others.
3056 */
3057 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3058 c = i;
3059 break;
3060 }
3061
3062 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3063 c = i;
3064 }
3065 return freqs[c].freq;
3066}
3067
3068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3070 const u8 *go_dev_addr, const u8 *ssid,
3071 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003072 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003073 const struct p2p_channels *channels,
3074 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075{
3076 struct wpa_supplicant *wpa_s = ctx;
3077 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003078 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003080 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081
3082 if (!persistent_group) {
3083 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003084 " to join an active group (SSID: %s)",
3085 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3087 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3088 == 0 ||
3089 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3090 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3091 "authorized invitation");
3092 goto accept_inv;
3093 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003094
3095#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003096 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3097 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003098 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003099 wpa_s->p2p_wps_method = WPS_NFC;
3100 wpa_s->pending_join_wps_method = WPS_NFC;
3101 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003102 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003103 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003104 bssid, ETH_ALEN);
3105 goto accept_inv;
3106 }
3107#endif /* CONFIG_WPS_NFC */
3108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 /*
3110 * Do not accept the invitation automatically; notify user and
3111 * request approval.
3112 */
3113 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3114 }
3115
3116 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3117 if (grp) {
3118 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3119 "running persistent group");
3120 if (*go)
3121 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3122 goto accept_inv;
3123 }
3124
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003125 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3126 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3127 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3128 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003129 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003130 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003131 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3132
3133 for (s = wpa_s->conf->ssid; s; s = s->next) {
3134 if (s->disabled == 2 &&
3135 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3136 s->ssid_len == ssid_len &&
3137 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3138 break;
3139 }
3140
3141 if (!s) {
3142 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3143 " requested reinvocation of an unknown group",
3144 MAC2STR(sa));
3145 return P2P_SC_FAIL_UNKNOWN_GROUP;
3146 }
3147
3148 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3149 *go = 1;
3150 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3151 wpa_printf(MSG_DEBUG, "P2P: The only available "
3152 "interface is already in use - reject "
3153 "invitation");
3154 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3155 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003156 if (wpa_s->p2p_mgmt)
3157 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3158 ETH_ALEN);
3159 else
3160 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 } else if (s->mode == WPAS_MODE_P2P_GO) {
3162 *go = 1;
3163 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3164 {
3165 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3166 "interface address for the group");
3167 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3168 }
3169 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3170 ETH_ALEN);
3171 }
3172
3173accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003174 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3175
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003176 best_freq = 0;
3177 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3178 sizeof(struct wpa_used_freq_data));
3179 if (freqs) {
3180 int num_channels = wpa_s->num_multichan_concurrent;
3181 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3182 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3183 os_free(freqs);
3184 }
3185
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003186 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003187 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003188 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003189 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003190
3191 if (wpa_s->num_multichan_concurrent < 2 ||
3192 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3193 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 -07003194 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003195 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196 }
3197
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003198 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3199 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003200 if (*go == 0) {
3201 /* We are the client */
3202 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3203 "running a GO but we are capable of MCC, "
3204 "figure out the best channel to use");
3205 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003206 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003207 /* We are the GO, and *force_freq is not in the
3208 * intersection */
3209 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3210 "in intersection but we are capable of MCC, "
3211 "figure out the best channel to use",
3212 *force_freq);
3213 *force_freq = 0;
3214 }
3215 }
3216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217 return P2P_SC_SUCCESS;
3218}
3219
3220
3221static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3222 const u8 *ssid, size_t ssid_len,
3223 const u8 *go_dev_addr, u8 status,
3224 int op_freq)
3225{
3226 struct wpa_supplicant *wpa_s = ctx;
3227 struct wpa_ssid *s;
3228
3229 for (s = wpa_s->conf->ssid; s; s = s->next) {
3230 if (s->disabled == 2 &&
3231 s->ssid_len == ssid_len &&
3232 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3233 break;
3234 }
3235
3236 if (status == P2P_SC_SUCCESS) {
3237 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003238 " was accepted; op_freq=%d MHz, SSID=%s",
3239 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003241 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003242 if (go) {
3243 wpa_msg_global(wpa_s, MSG_INFO,
3244 P2P_EVENT_INVITATION_ACCEPTED
3245 "sa=" MACSTR
3246 " persistent=%d freq=%d",
3247 MAC2STR(sa), s->id, op_freq);
3248 } else {
3249 wpa_msg_global(wpa_s, MSG_INFO,
3250 P2P_EVENT_INVITATION_ACCEPTED
3251 "sa=" MACSTR
3252 " persistent=%d",
3253 MAC2STR(sa), s->id);
3254 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003256 wpa_s, s, go, 0, op_freq, 0,
3257 wpa_s->conf->p2p_go_ht40,
3258 wpa_s->conf->p2p_go_vht,
3259 0,
3260 wpa_s->conf->p2p_go_he,
3261 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003262 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3263 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003265 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003266 wpa_msg_global(wpa_s, MSG_INFO,
3267 P2P_EVENT_INVITATION_ACCEPTED
3268 "sa=" MACSTR " go_dev_addr=" MACSTR
3269 " bssid=" MACSTR " unknown-network",
3270 MAC2STR(sa), MAC2STR(go_dev_addr),
3271 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003273 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003274 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 }
3276 return;
3277 }
3278
3279 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3280 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3281 " was rejected (status %u)", MAC2STR(sa), status);
3282 return;
3283 }
3284
3285 if (!s) {
3286 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003287 wpa_msg_global(wpa_s, MSG_INFO,
3288 P2P_EVENT_INVITATION_RECEIVED
3289 "sa=" MACSTR " go_dev_addr=" MACSTR
3290 " bssid=" MACSTR " unknown-network",
3291 MAC2STR(sa), MAC2STR(go_dev_addr),
3292 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003294 wpa_msg_global(wpa_s, MSG_INFO,
3295 P2P_EVENT_INVITATION_RECEIVED
3296 "sa=" MACSTR " go_dev_addr=" MACSTR
3297 " unknown-network",
3298 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003300 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3301 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302 return;
3303 }
3304
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003305 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003306 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3307 "sa=" MACSTR " persistent=%d freq=%d",
3308 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003309 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003310 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3311 "sa=" MACSTR " persistent=%d",
3312 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003313 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003314 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3315 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316}
3317
3318
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003319static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3320 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003321 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003322{
3323 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003324 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003325
3326 if (ssid == NULL)
3327 return;
3328
3329 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003330 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003331 ETH_ALEN) == 0)
3332 break;
3333 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003334 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003335 if (ssid->mode != WPAS_MODE_P2P_GO &&
3336 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3337 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3338 "due to invitation result", ssid->id);
3339 wpas_notify_network_removed(wpa_s, ssid);
3340 wpa_config_remove_network(wpa_s->conf, ssid->id);
3341 return;
3342 }
3343 return; /* Peer not found in client list */
3344 }
3345
3346 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003347 "group %d client list%s",
3348 MAC2STR(peer), ssid->id,
3349 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003350 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3351 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3352 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003353 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003354 if (p2p_wpa_s->conf->update_config &&
3355 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003356 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003357}
3358
3359
3360static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3361 const u8 *peer)
3362{
3363 struct wpa_ssid *ssid;
3364
3365 wpa_s = wpa_s->global->p2p_invite_group;
3366 if (wpa_s == NULL)
3367 return; /* No known invitation group */
3368 ssid = wpa_s->current_ssid;
3369 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3370 !ssid->p2p_persistent_group)
3371 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003372 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003373 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003374 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003375}
3376
3377
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003378static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003379 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003380 const u8 *peer, int neg_freq,
3381 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382{
3383 struct wpa_supplicant *wpa_s = ctx;
3384 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003385 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386
3387 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003388 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3389 "status=%d " MACSTR,
3390 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003392 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3393 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 }
3395 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3396
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003397 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3398 status, MAC2STR(peer));
3399 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003400 struct wpa_supplicant *group_if =
3401 wpa_s->global->p2p_invite_group;
3402
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003403 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3404 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003405
3406 /*
3407 * Invitation to an active group. If this is successful and we
3408 * are the GO, set the client wait to postpone some concurrent
3409 * operations and to allow provisioning and connection to happen
3410 * more quickly.
3411 */
3412 if (status == P2P_SC_SUCCESS &&
3413 group_if && group_if->current_ssid &&
3414 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3415 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3416#ifdef CONFIG_TESTING_OPTIONS
3417 if (group_if->p2p_go_csa_on_inv) {
3418 wpa_printf(MSG_DEBUG,
3419 "Testing: force P2P GO CSA after invitation");
3420 eloop_cancel_timeout(
3421 wpas_p2p_reconsider_moving_go,
3422 wpa_s, NULL);
3423 eloop_register_timeout(
3424 0, 50000,
3425 wpas_p2p_reconsider_moving_go,
3426 wpa_s, NULL);
3427 }
3428#endif /* CONFIG_TESTING_OPTIONS */
3429 }
3430 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003433 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3434 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3435 "invitation exchange to indicate readiness for "
3436 "re-invocation");
3437 }
3438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003440 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3441 ssid = wpa_config_get_network(
3442 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003443 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003445 wpas_p2p_remove_pending_group_interface(wpa_s);
3446 return;
3447 }
3448
3449 ssid = wpa_config_get_network(wpa_s->conf,
3450 wpa_s->pending_invite_ssid_id);
3451 if (ssid == NULL) {
3452 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3453 "data matching with invitation");
3454 return;
3455 }
3456
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003457 /*
3458 * The peer could have missed our ctrl::ack frame for Invitation
3459 * Response and continue retransmitting the frame. To reduce the
3460 * likelihood of the peer not getting successful TX status for the
3461 * Invitation Response frame, wait a short time here before starting
3462 * the persistent group so that we will remain on the current channel to
3463 * acknowledge any possible retransmission from the peer.
3464 */
3465 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3466 "starting persistent group");
3467 os_sleep(0, 50000);
3468
Dmitry Shmidt15907092014-03-25 10:42:57 -07003469 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003470 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003471 freq = neg_freq;
3472 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003473 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003474 freq = peer_oper_freq;
3475 else
3476 freq = 0;
3477
3478 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3479 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003480 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003481 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003482 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003483 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003484 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003485 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003486 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003487 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003488 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003489 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003490 ssid->mode == WPAS_MODE_P2P_GO ?
3491 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003492 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493}
3494
3495
Dmitry Shmidt04949592012-07-19 12:16:46 -07003496static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3497 unsigned int freq)
3498{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003499 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3500 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003501 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003502}
3503
3504
3505static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3506{
3507 reg->channel[reg->channels] = chan;
3508 reg->channels++;
3509}
3510
3511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003513 struct p2p_channels *chan,
3514 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515{
3516 int i, cla = 0;
3517
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003518 wpa_s->global->p2p_24ghz_social_channels = 1;
3519
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003520 os_memset(cli_chan, 0, sizeof(*cli_chan));
3521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003522 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3523 "band");
3524
3525 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3526 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003527 chan->reg_class[cla].channels = 0;
3528 for (i = 0; i < 11; i++) {
3529 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3530 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3531 }
3532 if (chan->reg_class[cla].channels)
3533 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534
3535 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3536 "band");
3537
3538 /* Operating class 115 - 5 GHz, channels 36-48 */
3539 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003540 chan->reg_class[cla].channels = 0;
3541 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3542 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3543 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3544 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3545 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3546 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3547 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3548 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3549 if (chan->reg_class[cla].channels)
3550 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551
3552 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3553 "band");
3554
3555 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3556 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003557 chan->reg_class[cla].channels = 0;
3558 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3559 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3560 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3561 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3562 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3563 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3564 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3565 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3566 if (chan->reg_class[cla].channels)
3567 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568
3569 chan->reg_classes = cla;
3570 return 0;
3571}
3572
3573
Dmitry Shmidt04949592012-07-19 12:16:46 -07003574static int has_channel(struct wpa_global *global,
3575 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576{
3577 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003578 unsigned int freq;
3579
3580 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3581 chan * 5;
3582 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003583 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584
3585 for (i = 0; i < mode->num_channels; i++) {
3586 if (mode->channels[i].chan == chan) {
3587 if (flags)
3588 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003589 if (mode->channels[i].flag &
3590 (HOSTAPD_CHAN_DISABLED |
3591 HOSTAPD_CHAN_RADAR))
3592 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003593 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3594 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003595 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596 }
3597 }
3598
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003599 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600}
3601
3602
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003603static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3604 struct hostapd_hw_modes *mode,
3605 u8 channel)
3606{
Hai Shalom60840252021-02-19 19:02:11 -08003607 u8 center_channels[] = { 42, 58, 106, 122, 138, 155, 171 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003608 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003609
3610 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3611 return 0;
3612
3613 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3614 /*
3615 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3616 * so the center channel is 6 channels away from the start/end.
3617 */
3618 if (channel >= center_channels[i] - 6 &&
3619 channel <= center_channels[i] + 6)
3620 return center_channels[i];
3621
3622 return 0;
3623}
3624
3625
3626static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3627 struct hostapd_hw_modes *mode,
3628 u8 channel, u8 bw)
3629{
3630 u8 center_chan;
3631 int i, flags;
3632 enum chan_allowed res, ret = ALLOWED;
3633
3634 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3635 if (!center_chan)
3636 return NOT_ALLOWED;
3637 if (center_chan >= 58 && center_chan <= 138)
3638 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3639
3640 /* check all the channels are available */
3641 for (i = 0; i < 4; i++) {
3642 int adj_chan = center_chan - 6 + i * 4;
3643
3644 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3645 if (res == NOT_ALLOWED)
3646 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003647 if (res == NO_IR)
3648 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003649
3650 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3651 return NOT_ALLOWED;
3652 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3653 return NOT_ALLOWED;
3654 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3655 return NOT_ALLOWED;
3656 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3657 return NOT_ALLOWED;
3658 }
3659
3660 return ret;
3661}
3662
3663
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003664static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3665 struct hostapd_hw_modes *mode,
3666 u8 channel)
3667{
Hai Shalom60840252021-02-19 19:02:11 -08003668 u8 center_channels[] = { 50, 114, 163 };
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003669 unsigned int i;
3670
3671 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3672 return 0;
3673
3674 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3675 /*
3676 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3677 * so the center channel is 14 channels away from the start/end.
3678 */
3679 if (channel >= center_channels[i] - 14 &&
3680 channel <= center_channels[i] + 14)
3681 return center_channels[i];
3682
3683 return 0;
3684}
3685
3686
3687static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3688 struct hostapd_hw_modes *mode,
3689 u8 channel, u8 bw)
3690{
3691 u8 center_chan;
3692 int i, flags;
3693 enum chan_allowed res, ret = ALLOWED;
3694
3695 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3696 if (!center_chan)
3697 return NOT_ALLOWED;
3698 /* VHT 160 MHz uses DFS channels in most countries. */
3699
3700 /* Check all the channels are available */
3701 for (i = 0; i < 8; i++) {
3702 int adj_chan = center_chan - 14 + i * 4;
3703
3704 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3705 if (res == NOT_ALLOWED)
3706 return NOT_ALLOWED;
3707
3708 if (res == NO_IR)
3709 ret = NO_IR;
3710
3711 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3712 return NOT_ALLOWED;
3713 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3714 return NOT_ALLOWED;
3715 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3716 return NOT_ALLOWED;
3717 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3718 return NOT_ALLOWED;
3719 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3720 return NOT_ALLOWED;
3721 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3722 return NOT_ALLOWED;
3723 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3724 return NOT_ALLOWED;
3725 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3726 return NOT_ALLOWED;
3727 }
3728
3729 return ret;
3730}
3731
3732
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003733static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3734 struct hostapd_hw_modes *mode,
3735 u8 channel)
3736{
3737 struct ieee80211_edmg_config edmg;
3738
3739 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3740 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3741 return ALLOWED;
3742
3743 return NOT_ALLOWED;
3744}
3745
3746
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003747static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3748 struct hostapd_hw_modes *mode,
3749 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003750{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003751 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003752 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003753
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003754 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3755 if (bw == BW40MINUS) {
3756 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3757 return NOT_ALLOWED;
3758 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3759 } else if (bw == BW40PLUS) {
3760 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3761 return NOT_ALLOWED;
3762 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3763 } else if (bw == BW80) {
3764 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003765 } else if (bw == BW160) {
3766 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003767 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3768 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003769 }
3770
3771 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3772 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003773 if (res == NO_IR || res2 == NO_IR)
3774 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003775 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003776}
3777
3778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003780 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003781 struct p2p_channels *cli_chan,
3782 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003784 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003785 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003787 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3789 "of all supported channels; assume dualband "
3790 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003791 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 }
3793
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003794 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003796 for (op = 0; global_op_class[op].op_class; op++) {
3797 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003799 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800
Hai Shalom899fcc72020-10-19 14:38:18 -07003801 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003802 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003803 continue;
3804
Hai Shalomfdcde762020-04-02 11:19:20 -07003805 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3806 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003807 if (mode == NULL)
3808 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003809 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3810 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003812 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003813
3814 /* Check for non-continuous jump in channel index
3815 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003816 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003817 ch < 149 && ch + o->inc > 149)
3818 ch = 149;
3819
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003820 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3821 if (res == ALLOWED) {
3822 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003823 if (cla == P2P_MAX_REG_CLASSES)
3824 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003825 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3826 o->op_class);
3827 reg = &chan->reg_class[cla];
3828 cla++;
3829 reg->reg_class = o->op_class;
3830 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003831 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3832 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003833 reg->channel[reg->channels] = ch;
3834 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003835 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003836 wpa_s->conf->p2p_add_cli_chan) {
3837 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003838 if (cli_cla == P2P_MAX_REG_CLASSES)
3839 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003840 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3841 o->op_class);
3842 cli_reg = &cli_chan->reg_class[cli_cla];
3843 cli_cla++;
3844 cli_reg->reg_class = o->op_class;
3845 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003846 if (cli_reg->channels ==
3847 P2P_MAX_REG_CLASS_CHANNELS)
3848 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003849 cli_reg->channel[cli_reg->channels] = ch;
3850 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 }
3853 if (reg) {
3854 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3855 reg->channel, reg->channels);
3856 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003857 if (cli_reg) {
3858 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3859 cli_reg->channel, cli_reg->channels);
3860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 }
3862
3863 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003864 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 return 0;
3867}
3868
3869
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003870int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3871 struct hostapd_hw_modes *mode, u8 channel)
3872{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003873 int op;
3874 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003875
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003876 for (op = 0; global_op_class[op].op_class; op++) {
3877 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003878 u8 ch;
3879
Hai Shalom899fcc72020-10-19 14:38:18 -07003880 if (o->p2p == NO_P2P_SUPP ||
3881 (is_6ghz_op_class(o->op_class) &&
3882 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003883 continue;
3884
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003885 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3886 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003887 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3888 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003889 continue;
3890 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003891 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003892 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003893 }
3894 }
3895 return 0;
3896}
3897
3898
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003899int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3900 struct hostapd_hw_modes *mode, u8 channel)
3901{
3902 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3903 return 0;
3904
3905 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3906}
3907
3908
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003909int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3910 struct hostapd_hw_modes *mode, u8 channel)
3911{
3912 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3913 return 0;
3914 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3915}
3916
3917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3919 size_t buf_len)
3920{
3921 struct wpa_supplicant *wpa_s = ctx;
3922
3923 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3924 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3925 break;
3926 }
3927 if (wpa_s == NULL)
3928 return -1;
3929
3930 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3931}
3932
3933
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003934struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3935 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003936{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003937 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3938 struct wpa_ssid *s = wpa_s->current_ssid;
3939 if (s == NULL)
3940 continue;
3941 if (s->mode != WPAS_MODE_P2P_GO &&
3942 s->mode != WPAS_MODE_AP &&
3943 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3944 continue;
3945 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003946 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003947 continue;
3948 return wpa_s;
3949 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003950
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003951 return NULL;
3952
3953}
3954
3955
3956struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3957 const u8 *peer_dev_addr)
3958{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003959 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3960 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003961 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003962 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003963 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3964 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003965 }
3966
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003967 return NULL;
3968}
3969
3970
3971static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3972{
3973 struct wpa_supplicant *wpa_s = ctx;
3974
3975 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003976}
3977
3978
Dmitry Shmidt18463232014-01-24 12:29:41 -08003979static int wpas_is_concurrent_session_active(void *ctx)
3980{
3981 struct wpa_supplicant *wpa_s = ctx;
3982 struct wpa_supplicant *ifs;
3983
3984 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3985 if (ifs == wpa_s)
3986 continue;
3987 if (ifs->wpa_state > WPA_ASSOCIATED)
3988 return 1;
3989 }
3990 return 0;
3991}
3992
3993
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003994static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3995{
3996 struct wpa_supplicant *wpa_s = ctx;
3997 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3998}
3999
4000
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004001int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4002 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004003{
4004 struct wpa_interface iface;
4005 struct wpa_supplicant *p2pdev_wpa_s;
4006 char ifname[100];
4007 char force_name[100];
4008 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004009 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004010
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004011 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4012 wpa_s->ifname);
4013 if (os_snprintf_error(sizeof(ifname), ret))
4014 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004015 /* Cut length at the maximum size. Note that we don't need to ensure
4016 * collision free names here as the created interface is not a netdev.
4017 */
4018 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004019 force_name[0] = '\0';
4020 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004021
4022 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4023 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4024 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4025
4026 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004027 force_name, wpa_s->pending_interface_addr, NULL);
4028 if (ret < 0) {
4029 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4030 return ret;
4031 }
4032 os_strlcpy(wpa_s->pending_interface_name, ifname,
4033 sizeof(wpa_s->pending_interface_name));
4034
4035 os_memset(&iface, 0, sizeof(iface));
4036 iface.p2p_mgmt = 1;
4037 iface.ifname = wpa_s->pending_interface_name;
4038 iface.driver = wpa_s->driver->name;
4039 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004040
4041 /*
4042 * If a P2P Device configuration file was given, use it as the interface
4043 * configuration file (instead of using parent's configuration file.
4044 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004045 if (conf_p2p_dev) {
4046 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004047 iface.ctrl_interface = NULL;
4048 } else {
4049 iface.confname = wpa_s->confname;
4050 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4051 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004052
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004053 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004054 if (!p2pdev_wpa_s) {
4055 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4056 return -1;
4057 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004058
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004059 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004060 wpa_s->pending_interface_name[0] = '\0';
4061 return 0;
4062}
4063
4064
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004065static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4066 const u8 *noa, size_t noa_len)
4067{
4068 struct wpa_supplicant *wpa_s, *intf = ctx;
4069 char hex[100];
4070
4071 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4072 if (wpa_s->waiting_presence_resp)
4073 break;
4074 }
4075 if (!wpa_s) {
4076 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4077 return;
4078 }
4079 wpa_s->waiting_presence_resp = 0;
4080
4081 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4082 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4083 " status=%u noa=%s", MAC2STR(src), status, hex);
4084}
4085
4086
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004087static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4088 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004089 u8 *ret_ssid, size_t *ret_ssid_len,
4090 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004091{
4092 struct wpa_supplicant *wpa_s = ctx;
4093 struct wpa_ssid *s;
4094
4095 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4096 if (s) {
4097 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4098 *ret_ssid_len = s->ssid_len;
4099 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004100
4101 if (s->mode != WPAS_MODE_P2P_GO) {
4102 os_memset(intended_iface_addr, 0, ETH_ALEN);
4103 } else if (wpas_p2p_create_iface(wpa_s)) {
4104 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4105 return 0;
4106
4107 os_memcpy(intended_iface_addr,
4108 wpa_s->pending_interface_addr, ETH_ALEN);
4109 } else {
4110 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4111 ETH_ALEN);
4112 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004113 return 1;
4114 }
4115
4116 return 0;
4117}
4118
4119
4120static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004121 u8 *ssid, size_t *ssid_len, int *group_iface,
4122 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004123{
4124 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004125 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004126 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004127
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004128 /*
4129 * group_iface will be set to 1 only if a dedicated interface for P2P
4130 * role is required. First, we try to reuse an active GO. However,
4131 * if it is not present, we will try to reactivate an existing
4132 * persistent group and set group_iface to 1, so the caller will know
4133 * that the pending interface should be used.
4134 */
4135 *group_iface = 0;
4136
4137 if (freq)
4138 *freq = 0;
4139
4140 go = wpas_p2p_get_go_group(wpa_s);
4141 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004142 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004143 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004144 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004145 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4146 else
4147 return 0;
4148 } else {
4149 s = go->current_ssid;
4150 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4151 if (freq)
4152 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004153 }
4154
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004155 os_memcpy(ssid, s->ssid, s->ssid_len);
4156 *ssid_len = s->ssid_len;
4157
4158 return 1;
4159}
4160
4161
4162static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4163 const u8 *ssid, size_t ssid_len)
4164{
4165 struct wpa_supplicant *wpa_s = ctx;
4166 struct wpa_ssid *s;
4167 int save_config = 0;
4168 size_t i;
4169
4170 /* Start with our first choice of Persistent Groups */
4171 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4172 if (go && ssid && ssid_len &&
4173 s->ssid_len == ssid_len &&
4174 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4175 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4176 break;
4177
4178 /* Remove stale persistent group */
4179 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004180 wpa_dbg(wpa_s, MSG_DEBUG,
4181 "P2P: Remove stale persistent group id=%d",
4182 s->id);
4183 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004184 wpa_config_remove_network(wpa_s->conf, s->id);
4185 save_config = 1;
4186 continue;
4187 }
4188
4189 for (i = 0; i < s->num_p2p_clients; i++) {
4190 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4191 peer, ETH_ALEN) != 0)
4192 continue;
4193
4194 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4195 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4196 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4197 break;
4198 }
4199 s->num_p2p_clients--;
4200 save_config = 1;
4201 }
4202
4203 if (save_config)
4204 p2p_config_write(wpa_s);
4205
4206 /* Return TRUE if valid SSID remains */
4207 return s != NULL;
4208}
4209
4210
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004211static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4212 const u8 *feat_cap, size_t feat_cap_len)
4213{
4214 static const char pref[] = " feature_cap=";
4215 int ret;
4216
4217 buf[0] = '\0';
4218
4219 /*
4220 * We expect a feature capability to contain at least one byte to be
4221 * reported. The string buffer provided by the caller function is
4222 * expected to be big enough to contain all bytes of the attribute for
4223 * known specifications. This function truncates the reported bytes if
4224 * the feature capability data exceeds the string buffer size.
4225 */
4226 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4227 return;
4228
4229 os_memcpy(buf, pref, sizeof(pref));
4230 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4231 buf_len - sizeof(pref) + 1,
4232 feat_cap, feat_cap_len);
4233
4234 if (ret != (2 * (int) feat_cap_len))
4235 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4236}
4237
4238
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004239static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4240 const u8 *adv_mac, const u8 *ses_mac,
4241 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4242 u8 conncap, int passwd_id,
4243 const u8 *persist_ssid,
4244 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004245 int prov_start, const char *session_info,
4246 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004247 unsigned int freq,
4248 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004249{
4250 struct wpa_supplicant *wpa_s = ctx;
4251 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004252 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004253 int save_config = 0;
4254 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004255 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004256
4257 if (!dev)
4258 return;
4259
4260 os_memset(mac, 0, ETH_ALEN);
4261 if (!adv_mac)
4262 adv_mac = mac;
4263 if (!ses_mac)
4264 ses_mac = mac;
4265 if (!grp_mac)
4266 grp_mac = mac;
4267
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004268 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4269 feat_cap, feat_cap_len);
4270
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004271 if (prov_start) {
4272 if (session_info == NULL) {
4273 wpa_msg_global(wpa_s, MSG_INFO,
4274 P2P_EVENT_P2PS_PROVISION_START MACSTR
4275 " adv_id=%x conncap=%x"
4276 " adv_mac=" MACSTR
4277 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004278 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004279 MAC2STR(dev), adv_id, conncap,
4280 MAC2STR(adv_mac),
4281 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004282 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004283 } else {
4284 wpa_msg_global(wpa_s, MSG_INFO,
4285 P2P_EVENT_P2PS_PROVISION_START MACSTR
4286 " adv_id=%x conncap=%x"
4287 " adv_mac=" MACSTR
4288 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004289 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004290 MAC2STR(dev), adv_id, conncap,
4291 MAC2STR(adv_mac),
4292 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004293 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004294 }
4295 return;
4296 }
4297
4298 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4299 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4300
4301 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4302 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4303 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4304
4305 if (persistent_go && !persistent_go->num_p2p_clients) {
4306 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004307 wpa_dbg(wpa_s, MSG_DEBUG,
4308 "P2P: Remove empty persistent group id=%d",
4309 persistent_go->id);
4310 wpas_notify_persistent_group_removed(wpa_s,
4311 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004312 wpa_config_remove_network(wpa_s->conf,
4313 persistent_go->id);
4314 }
4315
4316 wpa_msg_global(wpa_s, MSG_INFO,
4317 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4318 " status=%d"
4319 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004320 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004321 MAC2STR(dev), status,
4322 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004323 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004324 return;
4325 }
4326
4327 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004328 if (persist_ssid && persist_ssid_size)
4329 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4330 persist_ssid_size);
4331
4332 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4333 is_zero_ether_addr(grp_mac)) {
4334 wpa_dbg(wpa_s, MSG_ERROR,
4335 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4336 return;
4337 }
4338
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004339 for (;;) {
4340 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4341 if (!stale)
4342 break;
4343
4344 if (s && s->ssid_len == stale->ssid_len &&
4345 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4346 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4347 break;
4348
4349 /* Remove stale persistent group */
4350 if (stale->mode != WPAS_MODE_P2P_GO ||
4351 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004352 wpa_dbg(wpa_s, MSG_DEBUG,
4353 "P2P: Remove stale persistent group id=%d",
4354 stale->id);
4355 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004356 wpa_config_remove_network(wpa_s->conf, stale->id);
4357 } else {
4358 size_t i;
4359
4360 for (i = 0; i < stale->num_p2p_clients; i++) {
4361 if (os_memcmp(stale->p2p_client_list +
4362 i * ETH_ALEN,
4363 dev, ETH_ALEN) == 0) {
4364 os_memmove(stale->p2p_client_list +
4365 i * ETH_ALEN,
4366 stale->p2p_client_list +
4367 (i + 1) * ETH_ALEN,
4368 (stale->num_p2p_clients -
4369 i - 1) * ETH_ALEN);
4370 break;
4371 }
4372 }
4373 stale->num_p2p_clients--;
4374 }
4375 save_config = 1;
4376 }
4377
4378 if (save_config)
4379 p2p_config_write(wpa_s);
4380
4381 if (s) {
4382 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4383 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4384
4385 if (persistent_go && s != persistent_go &&
4386 !persistent_go->num_p2p_clients) {
4387 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004388 wpa_dbg(wpa_s, MSG_DEBUG,
4389 "P2P: Remove empty persistent group id=%d",
4390 persistent_go->id);
4391 wpas_notify_persistent_group_removed(wpa_s,
4392 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004393 wpa_config_remove_network(wpa_s->conf,
4394 persistent_go->id);
4395 /* Save config */
4396 }
4397
4398 wpa_msg_global(wpa_s, MSG_INFO,
4399 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4400 " status=%d"
4401 " adv_id=%x adv_mac=" MACSTR
4402 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004403 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004404 MAC2STR(dev), status,
4405 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004406 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004407 return;
4408 }
4409
Hai Shalom5f92bc92019-04-18 11:54:11 -07004410 wpa_s->global->pending_p2ps_group = 0;
4411 wpa_s->global->pending_p2ps_group_freq = 0;
4412
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004413 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004414 /*
4415 * We need to copy the interface name. Simply saving a
4416 * pointer isn't enough, since if we use pending_interface_name
4417 * it will be overwritten when the group is added.
4418 */
4419 char go_ifname[100];
4420
4421 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004422 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004423 if (!response_done) {
4424 wpa_s->global->pending_p2ps_group = 1;
4425 wpa_s->global->pending_p2ps_group_freq = freq;
4426 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004427
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004428 if (!wpas_p2p_create_iface(wpa_s))
4429 os_memcpy(go_ifname, wpa_s->ifname,
4430 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004431 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004432 os_memcpy(go_ifname,
4433 wpa_s->pending_interface_name,
4434 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004435
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004436 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004437 wpas_p2ps_prov_complete(
4438 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4439 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004440 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004441 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4442 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004443 return;
4444 }
4445
4446 /* If PD Resp complete, start up the GO */
4447 if (response_done && persistent_go) {
4448 wpas_p2p_group_add_persistent(
4449 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004450 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004451 persistent_go->mode ==
4452 WPAS_MODE_P2P_GO ?
4453 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004454 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004455 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004456 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004457 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004458 }
4459
4460 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004461 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4462 ETH_ALEN);
4463 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004464 }
4465 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004466 os_memcpy(go_ifname, go_wpa_s->ifname,
4467 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004468
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004469 if (is_zero_ether_addr(grp_mac)) {
4470 wpa_dbg(go_wpa_s, MSG_DEBUG,
4471 "P2P: Setting PIN-1 for ANY");
4472 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4473 "12345670", NULL, 0,
4474 0);
4475 } else {
4476 wpa_dbg(go_wpa_s, MSG_DEBUG,
4477 "P2P: Setting PIN-1 for " MACSTR,
4478 MAC2STR(grp_mac));
4479 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4480 "12345670", NULL, 0,
4481 0);
4482 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004483
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004484 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4485 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004486 }
4487
4488 wpa_msg_global(wpa_s, MSG_INFO,
4489 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4490 " status=%d conncap=%x"
4491 " adv_id=%x adv_mac=" MACSTR
4492 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004493 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004494 MAC2STR(dev), status, conncap,
4495 adv_id, MAC2STR(adv_mac),
4496 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004497 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004498 return;
4499 }
4500
4501 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4502 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4503
4504 if (persistent_go && !persistent_go->num_p2p_clients) {
4505 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004506 wpa_dbg(wpa_s, MSG_DEBUG,
4507 "P2P: Remove empty persistent group id=%d",
4508 persistent_go->id);
4509 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004510 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4511 }
4512
4513 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004514 char ssid_hex[32 * 2 + 1];
4515
4516 if (group_ssid)
4517 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4518 group_ssid, group_ssid_len);
4519 else
4520 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004521 wpa_msg_global(wpa_s, MSG_INFO,
4522 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4523 " status=%d conncap=%x"
4524 " adv_id=%x adv_mac=" MACSTR
4525 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004526 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004527 MAC2STR(dev), status, conncap,
4528 adv_id, MAC2STR(adv_mac),
4529 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004530 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4531 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004532 } else {
4533 wpa_msg_global(wpa_s, MSG_INFO,
4534 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4535 " status=%d conncap=%x"
4536 " adv_id=%x adv_mac=" MACSTR
4537 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004538 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004539 MAC2STR(dev), status, conncap,
4540 adv_id, MAC2STR(adv_mac),
4541 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004542 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004543 }
4544}
4545
4546
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004547static int _wpas_p2p_in_progress(void *ctx)
4548{
4549 struct wpa_supplicant *wpa_s = ctx;
4550 return wpas_p2p_in_progress(wpa_s);
4551}
4552
4553
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004554static int wpas_prov_disc_resp_cb(void *ctx)
4555{
4556 struct wpa_supplicant *wpa_s = ctx;
4557 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004558 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004559
4560 if (!wpa_s->global->pending_p2ps_group)
4561 return 0;
4562
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004563 freq = wpa_s->global->pending_p2ps_group_freq;
4564 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004565 wpa_s->global->pending_p2ps_group = 0;
4566
4567 if (wpas_p2p_get_go_group(wpa_s))
4568 return 0;
4569 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4570
4571 if (persistent_go) {
4572 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004573 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4574 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004575 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004576 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004577 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004578 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004579 }
4580
4581 return 1;
4582}
4583
4584
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004585static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4586 unsigned int *len,
4587 unsigned int *freq_list)
4588{
4589 struct wpa_supplicant *wpa_s = ctx;
4590
4591 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4592 WPA_IF_P2P_CLIENT, len, freq_list);
4593}
4594
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004595int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4596{
4597 u8 addr[ETH_ALEN] = {0};
4598
4599 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4600 return 0;
4601
Hai Shalom60840252021-02-19 19:02:11 -08004602 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4603 if (is_zero_ether_addr(
4604 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4605 !is_zero_ether_addr(wpa_s->own_addr)) {
4606 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4607 wpa_s->own_addr, ETH_ALEN);
4608 }
4609 return 0;
4610 }
4611
4612 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004613 if (random_mac_addr(addr) < 0) {
4614 wpa_msg(wpa_s, MSG_INFO,
4615 "Failed to generate random MAC address");
4616 return -EINVAL;
4617 }
4618
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004619 /* Store generated MAC address. */
4620 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4621 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004622 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004623 /* If there are existing saved groups, restore last MAC address.
4624 * if there is no last used MAC address, the last one is
4625 * factory MAC. */
4626 if (is_zero_ether_addr(
4627 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004628 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004629 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4630 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004631 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4632 }
4633
4634 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4635 wpa_msg(wpa_s, MSG_INFO,
4636 "Failed to set random MAC address");
4637 return -EINVAL;
4638 }
4639
4640 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4641 wpa_msg(wpa_s, MSG_INFO,
4642 "Could not update MAC address information");
4643 return -EINVAL;
4644 }
4645
4646 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4647 MAC2STR(addr));
4648
4649 return 0;
4650}
4651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652/**
4653 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4654 * @global: Pointer to global data from wpa_supplicant_init()
4655 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4656 * Returns: 0 on success, -1 on failure
4657 */
4658int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4659{
4660 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661 int i;
4662
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004663 if (wpa_s->conf->p2p_disabled)
4664 return 0;
4665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4667 return 0;
4668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669 if (global->p2p)
4670 return 0;
4671
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004672 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4673 wpa_msg(wpa_s, MSG_ERROR,
4674 "Failed to initialize P2P random MAC address.");
4675 return -1;
4676 }
4677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004679 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004680 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 p2p.p2p_scan = wpas_p2p_scan;
4682 p2p.send_action = wpas_send_action;
4683 p2p.send_action_done = wpas_send_action_done;
4684 p2p.go_neg_completed = wpas_go_neg_completed;
4685 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4686 p2p.dev_found = wpas_dev_found;
4687 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004688 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689 p2p.start_listen = wpas_start_listen;
4690 p2p.stop_listen = wpas_stop_listen;
4691 p2p.send_probe_resp = wpas_send_probe_resp;
4692 p2p.sd_request = wpas_sd_request;
4693 p2p.sd_response = wpas_sd_response;
4694 p2p.prov_disc_req = wpas_prov_disc_req;
4695 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004696 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697 p2p.invitation_process = wpas_invitation_process;
4698 p2p.invitation_received = wpas_invitation_received;
4699 p2p.invitation_result = wpas_invitation_result;
4700 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004701 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004702 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004703 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004704 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004705 p2p.get_persistent_group = wpas_get_persistent_group;
4706 p2p.get_go_info = wpas_get_go_info;
4707 p2p.remove_stale_groups = wpas_remove_stale_groups;
4708 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4709 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4710 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004711 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004712 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713
4714 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004715 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 p2p.dev_name = wpa_s->conf->device_name;
4717 p2p.manufacturer = wpa_s->conf->manufacturer;
4718 p2p.model_name = wpa_s->conf->model_name;
4719 p2p.model_number = wpa_s->conf->model_number;
4720 p2p.serial_number = wpa_s->conf->serial_number;
4721 if (wpa_s->wps) {
4722 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4723 p2p.config_methods = wpa_s->wps->config_methods;
4724 }
4725
Hai Shalom60840252021-02-19 19:02:11 -08004726 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4727 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004728 wpa_printf(MSG_ERROR,
4729 "P2P: Failed to configure supported channel list");
4730 return -1;
4731 }
4732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 if (wpa_s->conf->p2p_listen_reg_class &&
4734 wpa_s->conf->p2p_listen_channel) {
4735 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4736 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004737 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739 /*
4740 * Pick one of the social channels randomly as the listen
4741 * channel.
4742 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004743 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004744 &p2p.channel,
4745 &global->p2p_go_avoid_freq,
4746 &global->p2p_disallow_freq) !=
4747 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004748 wpa_printf(MSG_INFO,
4749 "P2P: No social channels supported by the driver - do not enable P2P");
4750 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004751 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004752 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004753 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004754 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4755 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756
4757 if (wpa_s->conf->p2p_oper_reg_class &&
4758 wpa_s->conf->p2p_oper_channel) {
4759 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4760 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4761 p2p.cfg_op_channel = 1;
4762 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4763 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4764
4765 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004767 * Use random operation channel from 2.4 GHz band social
4768 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4769 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004771 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004772 &p2p.op_channel, NULL,
4773 NULL) != 0) {
4774 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004775 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004776 p2p.op_reg_class = 0;
4777 p2p.op_channel = 0;
4778 /* This will be overridden during group setup in
4779 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004780 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 p2p.cfg_op_channel = 0;
4782 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4783 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4784 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004785
4786 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4787 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4788 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4789 }
4790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004791 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4792 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4793 p2p.country[2] = 0x04;
4794 } else
4795 os_memcpy(p2p.country, "XX\x04", 3);
4796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4798 WPS_DEV_TYPE_LEN);
4799
4800 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4801 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4802 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4803
4804 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4805 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4806
4807 p2p.max_peers = 100;
4808
4809 if (wpa_s->conf->p2p_ssid_postfix) {
4810 p2p.ssid_postfix_len =
4811 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4812 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4813 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4814 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4815 p2p.ssid_postfix_len);
4816 }
4817
4818 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4819
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004820 p2p.max_listen = wpa_s->max_remain_on_chan;
4821
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004822 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4823 wpa_s->conf->p2p_passphrase_len <= 63)
4824 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4825 else
4826 p2p.passphrase_len = 8;
4827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828 global->p2p = p2p_init(&p2p);
4829 if (global->p2p == NULL)
4830 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004831 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832
4833 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4834 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4835 continue;
4836 p2p_add_wps_vendor_extension(
4837 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4838 }
4839
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004840 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842 return 0;
4843}
4844
4845
4846/**
4847 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4848 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4849 *
4850 * This function deinitialize per-interface P2P data.
4851 */
4852void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4853{
4854 if (wpa_s->driver && wpa_s->drv_priv)
4855 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004856
4857 if (wpa_s->go_params) {
4858 /* Clear any stored provisioning info */
4859 p2p_clear_provisioning_info(
4860 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004861 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004862 }
4863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 os_free(wpa_s->go_params);
4865 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004866 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4868 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07004869 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4871 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4872 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004873 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07004874 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004875 wpas_p2p_listen_work_done(wpa_s);
4876 if (wpa_s->p2p_send_action_work) {
4877 os_free(wpa_s->p2p_send_action_work->ctx);
4878 radio_work_done(wpa_s->p2p_send_action_work);
4879 wpa_s->p2p_send_action_work = NULL;
4880 }
4881 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004883 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4884 wpa_s->p2p_oob_dev_pw = NULL;
4885
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004886 os_free(wpa_s->p2p_group_common_freqs);
4887 wpa_s->p2p_group_common_freqs = NULL;
4888 wpa_s->p2p_group_common_freqs_num = 0;
4889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890 /* TODO: remove group interface from the driver if this wpa_s instance
4891 * is on top of a P2P group interface */
4892}
4893
4894
4895/**
4896 * wpas_p2p_deinit_global - Deinitialize global P2P module
4897 * @global: Pointer to global data from wpa_supplicant_init()
4898 *
4899 * This function deinitializes the global (per device) P2P module.
4900 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004901static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902{
4903 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004905 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004906
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004907 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908
4909 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4911 wpa_s = wpa_s->next;
4912 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913 tmp = global->ifaces;
4914 while (tmp &&
4915 (tmp == wpa_s ||
4916 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4917 tmp = tmp->next;
4918 }
4919 if (tmp == NULL)
4920 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004921 /* Disconnect from the P2P group and deinit the interface */
4922 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923 }
4924
4925 /*
4926 * Deinit GO data on any possibly remaining interface (if main
4927 * interface is used as GO).
4928 */
4929 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4930 if (wpa_s->ap_iface)
4931 wpas_p2p_group_deinit(wpa_s);
4932 }
4933
4934 p2p_deinit(global->p2p);
4935 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004936 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937}
4938
4939
4940static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4941{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004942 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004943 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 if (wpa_s->drv_flags &
4945 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4946 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4947 return 1; /* P2P group requires a new interface in every case
4948 */
4949 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4950 return 0; /* driver does not support concurrent operations */
4951 if (wpa_s->global->ifaces->next)
4952 return 1; /* more that one interface already in use */
4953 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4954 return 1; /* this interface is already in use */
4955 return 0;
4956}
4957
4958
4959static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4960 const u8 *peer_addr,
4961 enum p2p_wps_method wps_method,
4962 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004963 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004964 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004966 if (persistent_group && wpa_s->conf->persistent_reconnect)
4967 persistent_group = 2;
4968
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004969 /*
4970 * Increase GO config timeout if HT40 is used since it takes some time
4971 * to scan channels for coex purposes before the BSS can be started.
4972 */
4973 p2p_set_config_timeout(wpa_s->global->p2p,
4974 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4977 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004978 persistent_group, ssid ? ssid->ssid : NULL,
4979 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004980 wpa_s->p2p_pd_before_go_neg, pref_freq,
4981 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4982 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004983}
4984
4985
4986static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4987 const u8 *peer_addr,
4988 enum p2p_wps_method wps_method,
4989 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004990 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004991 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004993 if (persistent_group && wpa_s->conf->persistent_reconnect)
4994 persistent_group = 2;
4995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4997 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004998 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004999 ssid ? ssid->ssid_len : 0, pref_freq,
5000 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5001 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002}
5003
5004
5005static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5006{
5007 wpa_s->p2p_join_scan_count++;
5008 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5009 wpa_s->p2p_join_scan_count);
5010 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5011 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5012 " for join operationg - stop join attempt",
5013 MAC2STR(wpa_s->pending_join_iface_addr));
5014 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005015 if (wpa_s->p2p_auto_pd) {
5016 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005017 wpa_msg_global(wpa_s, MSG_INFO,
5018 P2P_EVENT_PROV_DISC_FAILURE
5019 " p2p_dev_addr=" MACSTR " status=N/A",
5020 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005021 return;
5022 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005023 if (wpa_s->p2p_fallback_to_go_neg) {
5024 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5025 "failed - fall back to GO Negotiation");
5026 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5027 P2P_EVENT_FALLBACK_TO_GO_NEG
5028 "reason=join-failed");
5029 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5030 return;
5031 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005032 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005033 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005034 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035 }
5036}
5037
5038
Dmitry Shmidt04949592012-07-19 12:16:46 -07005039static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5040{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005041 int res;
5042 unsigned int num, i;
5043 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005044
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005045 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5046 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005047 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005048 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005049
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005050 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5051 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005052 if (!freqs)
5053 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005054
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005055 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5056 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005057
5058 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005059 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005060 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5061 freq);
5062 res = 0;
5063 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005064 }
5065 }
5066
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005067 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005068 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005069
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005070exit_free:
5071 os_free(freqs);
5072 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005073}
5074
5075
5076static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5077 const u8 *peer_dev_addr)
5078{
5079 struct wpa_bss *bss;
5080 int updated;
5081
5082 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5083 if (bss == NULL)
5084 return -1;
5085 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5086 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5087 "last scan");
5088 return 0;
5089 }
5090
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005091 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5092 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005093 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5094 "%ld.%06ld (%supdated in last scan)",
5095 bss->last_update.sec, bss->last_update.usec,
5096 updated ? "": "not ");
5097
5098 return updated;
5099}
5100
5101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5103 struct wpa_scan_results *scan_res)
5104{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005105 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 int freq;
5107 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5110
5111 if (wpa_s->global->p2p_disabled)
5112 return;
5113
Dmitry Shmidt04949592012-07-19 12:16:46 -07005114 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5115 scan_res ? (int) scan_res->num : -1,
5116 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117
5118 if (scan_res)
5119 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5120
Dmitry Shmidt04949592012-07-19 12:16:46 -07005121 if (wpa_s->p2p_auto_pd) {
5122 int join = wpas_p2p_peer_go(wpa_s,
5123 wpa_s->pending_join_dev_addr);
5124 if (join == 0 &&
5125 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5126 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005127 bss = wpa_bss_get_bssid_latest(
5128 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005129 if (bss) {
5130 freq = bss->freq;
5131 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5132 "the peer " MACSTR " at %d MHz",
5133 wpa_s->auto_pd_scan_retry,
5134 MAC2STR(wpa_s->
5135 pending_join_dev_addr),
5136 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005137 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005138 return;
5139 }
5140 }
5141
5142 if (join < 0)
5143 join = 0;
5144
5145 wpa_s->p2p_auto_pd = 0;
5146 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5147 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5148 MAC2STR(wpa_s->pending_join_dev_addr), join);
5149 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005150 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005151 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005152 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005153 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005154 wpa_msg_global(wpa_s, MSG_INFO,
5155 P2P_EVENT_PROV_DISC_FAILURE
5156 " p2p_dev_addr=" MACSTR " status=N/A",
5157 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005158 }
5159 return;
5160 }
5161
5162 if (wpa_s->p2p_auto_join) {
5163 int join = wpas_p2p_peer_go(wpa_s,
5164 wpa_s->pending_join_dev_addr);
5165 if (join < 0) {
5166 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5167 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005168 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005169 P2P_EVENT_FALLBACK_TO_GO_NEG
5170 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005171 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5172 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5173 wpa_s->p2p_persistent_group, 0, 0, 0,
5174 wpa_s->p2p_go_intent,
5175 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005176 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005177 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005178 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005179 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005180 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005181 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005182 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005183 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005184 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005185 return;
5186 }
5187
5188 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5189 "try to join the group", join ? "" :
5190 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005191 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005192 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005193 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005194 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005195 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005196 }
5197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005198 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5199 wpa_s->pending_join_iface_addr);
5200 if (freq < 0 &&
5201 p2p_get_interface_addr(wpa_s->global->p2p,
5202 wpa_s->pending_join_dev_addr,
5203 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005204 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5205 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5207 "address for join from " MACSTR " to " MACSTR
5208 " based on newly discovered P2P peer entry",
5209 MAC2STR(wpa_s->pending_join_iface_addr),
5210 MAC2STR(iface_addr));
5211 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5212 ETH_ALEN);
5213
5214 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5215 wpa_s->pending_join_iface_addr);
5216 }
5217 if (freq >= 0) {
5218 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5219 "from P2P peer table: %d MHz", freq);
5220 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005221 if (wpa_s->p2p_join_ssid_len) {
5222 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5223 MACSTR " and SSID %s",
5224 MAC2STR(wpa_s->pending_join_iface_addr),
5225 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5226 wpa_s->p2p_join_ssid_len));
5227 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5228 wpa_s->p2p_join_ssid,
5229 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005230 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005231 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5232 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5233 bss = wpa_bss_get_bssid_latest(wpa_s,
5234 wpa_s->pending_join_iface_addr);
5235 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005236 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005237 u8 dev_addr[ETH_ALEN];
5238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239 freq = bss->freq;
5240 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005241 "from BSS table: %d MHz (SSID %s)", freq,
5242 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005243 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005244 dev_addr) == 0 &&
5245 os_memcmp(wpa_s->pending_join_dev_addr,
5246 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5247 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5248 ETH_ALEN) != 0) {
5249 wpa_printf(MSG_DEBUG,
5250 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5251 MAC2STR(dev_addr),
5252 MAC2STR(wpa_s->pending_join_dev_addr));
5253 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5254 ETH_ALEN);
5255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256 }
5257 if (freq > 0) {
5258 u16 method;
5259
Dmitry Shmidt04949592012-07-19 12:16:46 -07005260 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005261 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005262 P2P_EVENT_GROUP_FORMATION_FAILURE
5263 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005264 wpas_notify_p2p_group_formation_failure(
5265 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005266 return;
5267 }
5268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005269 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5270 "prior to joining an existing group (GO " MACSTR
5271 " freq=%u MHz)",
5272 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5273 wpa_s->pending_pd_before_join = 1;
5274
5275 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005276 case WPS_PIN_DISPLAY:
5277 method = WPS_CONFIG_KEYPAD;
5278 break;
5279 case WPS_PIN_KEYPAD:
5280 method = WPS_CONFIG_DISPLAY;
5281 break;
5282 case WPS_PBC:
5283 method = WPS_CONFIG_PUSHBUTTON;
5284 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005285 case WPS_P2PS:
5286 method = WPS_CONFIG_P2PS;
5287 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288 default:
5289 method = 0;
5290 break;
5291 }
5292
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005293 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5294 wpa_s->pending_join_dev_addr) ==
5295 method)) {
5296 /*
5297 * We have already performed provision discovery for
5298 * joining the group. Proceed directly to join
5299 * operation without duplicated provision discovery. */
5300 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5301 "with " MACSTR " already done - proceed to "
5302 "join",
5303 MAC2STR(wpa_s->pending_join_dev_addr));
5304 wpa_s->pending_pd_before_join = 0;
5305 goto start;
5306 }
5307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005308 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005309 wpa_s->pending_join_dev_addr,
5310 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005311 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5313 "Discovery Request before joining an "
5314 "existing group");
5315 wpa_s->pending_pd_before_join = 0;
5316 goto start;
5317 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318 return;
5319 }
5320
5321 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5322 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5323 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5324 wpas_p2p_check_join_scan_limit(wpa_s);
5325 return;
5326
5327start:
5328 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005329 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5330 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005331}
5332
5333
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005334static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5335 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337 int ret;
5338 struct wpa_driver_scan_params params;
5339 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005340 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005341 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005342 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343
5344 os_memset(&params, 0, sizeof(params));
5345
5346 /* P2P Wildcard SSID */
5347 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005348 if (ssid && ssid_len) {
5349 params.ssids[0].ssid = ssid;
5350 params.ssids[0].ssid_len = ssid_len;
5351 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5352 wpa_s->p2p_join_ssid_len = ssid_len;
5353 } else {
5354 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5355 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5356 wpa_s->p2p_join_ssid_len = 0;
5357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005358
5359 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005360 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5361 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5362 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363 if (wps_ie == NULL) {
5364 wpas_p2p_scan_res_join(wpa_s, NULL);
5365 return;
5366 }
5367
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005368 if (!freq) {
5369 int oper_freq;
5370 /*
5371 * If freq is not provided, check the operating freq of the GO
5372 * and use a single channel scan on if possible.
5373 */
5374 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5375 wpa_s->pending_join_iface_addr);
5376 if (oper_freq > 0)
5377 freq = oper_freq;
5378 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005379 if (freq > 0) {
5380 freqs[0] = freq;
5381 params.freqs = freqs;
Hai Shalom899fcc72020-10-19 14:38:18 -07005382 } else if (wpa_s->conf->p2p_6ghz_disable) {
5383 wpa_printf(MSG_DEBUG,
5384 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005385 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5386 0);
5387 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5388 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005389 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005391 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5392 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5393 if (ies == NULL) {
5394 wpabuf_free(wps_ie);
5395 wpas_p2p_scan_res_join(wpa_s, NULL);
5396 return;
5397 }
5398 wpabuf_put_buf(ies, wps_ie);
5399 wpabuf_free(wps_ie);
5400
5401 bands = wpas_get_bands(wpa_s, freqs);
5402 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5403
5404 params.p2p_probe = 1;
5405 params.extra_ies = wpabuf_head(ies);
5406 params.extra_ies_len = wpabuf_len(ies);
5407
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005408 if (wpa_s->clear_driver_scan_cache) {
5409 wpa_printf(MSG_DEBUG,
5410 "Request driver to clear scan cache due to local BSS flush");
5411 params.only_new_results = 1;
5412 }
5413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414 /*
5415 * Run a scan to update BSS table and start Provision Discovery once
5416 * the new scan results become available.
5417 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005418 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalom899fcc72020-10-19 14:38:18 -07005419 if (wpa_s->conf->p2p_6ghz_disable && params.freqs != freqs)
5420 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005421 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005422 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005423 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005424 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005425 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005426 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005427
5428 wpabuf_free(ies);
5429
5430 if (ret) {
5431 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5432 "try again later");
5433 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5434 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5435 wpas_p2p_check_join_scan_limit(wpa_s);
5436 }
5437}
5438
5439
Dmitry Shmidt04949592012-07-19 12:16:46 -07005440static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5441{
5442 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005443 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005444}
5445
5446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005448 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005449 int auto_join, int op_freq,
5450 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451{
5452 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005453 MACSTR " dev " MACSTR " op_freq=%d)%s",
5454 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005455 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005456 if (ssid && ssid_len) {
5457 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5458 wpa_ssid_txt(ssid, ssid_len));
5459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460
Dmitry Shmidt04949592012-07-19 12:16:46 -07005461 wpa_s->p2p_auto_pd = 0;
5462 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5464 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5465 wpa_s->pending_join_wps_method = wps_method;
5466
5467 /* Make sure we are not running find during connection establishment */
5468 wpas_p2p_stop_find(wpa_s);
5469
5470 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005471 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 return 0;
5473}
5474
5475
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005476static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5477 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005478{
5479 struct wpa_supplicant *group;
5480 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005481 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482
5483 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5484 if (group == NULL)
5485 return -1;
5486 if (group != wpa_s) {
5487 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5488 sizeof(group->p2p_pin));
5489 group->p2p_wps_method = wpa_s->p2p_wps_method;
5490 }
5491
Hai Shalom74f70d42019-02-11 14:42:39 -08005492 /*
5493 * Need to mark the current interface for p2p_group_formation
5494 * when a separate group interface is not used. This is needed
5495 * to allow p2p_cancel stop a pending p2p_connect-join.
5496 * wpas_p2p_init_group_interface() addresses this for the case
5497 * where a separate group interface is used.
5498 */
5499 if (group == wpa_s->parent)
5500 wpa_s->global->p2p_group_formation = group;
5501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005503 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504
5505 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005506 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5508 ETH_ALEN);
5509 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005510 if (freq && ssid && ssid_len) {
5511 res.freq = freq;
5512 res.ssid_len = ssid_len;
5513 os_memcpy(res.ssid, ssid, ssid_len);
5514 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005515 if (ssid && ssid_len) {
5516 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5517 ssid, ssid_len);
5518 } else {
5519 bss = wpa_bss_get_bssid_latest(
5520 wpa_s, wpa_s->pending_join_iface_addr);
5521 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005522 if (bss) {
5523 res.freq = bss->freq;
5524 res.ssid_len = bss->ssid_len;
5525 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5526 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5527 bss->freq,
5528 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005529 } else if (ssid && ssid_len) {
5530 res.ssid_len = ssid_len;
5531 os_memcpy(res.ssid, ssid, ssid_len);
5532 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5533 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005534 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005535 }
5536
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005537 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5538 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5539 "starting client");
5540 wpa_drv_cancel_remain_on_channel(wpa_s);
5541 wpa_s->off_channel_freq = 0;
5542 wpa_s->roc_waiting_drv_freq = 0;
5543 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005544 wpas_start_wps_enrollee(group, &res);
5545
5546 /*
5547 * Allow a longer timeout for join-a-running-group than normal 15
5548 * second group formation timeout since the GO may not have authorized
5549 * our connection yet.
5550 */
5551 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5552 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5553 wpa_s, NULL);
5554
5555 return 0;
5556}
5557
5558
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005559static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005560 int *force_freq, int *pref_freq, int go,
5561 unsigned int *pref_freq_list,
5562 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005563{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005564 struct wpa_used_freq_data *freqs;
5565 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005566 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5567
5568 max_pref_freq = *num_pref_freq;
5569 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005570
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005571 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5572 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005573 if (!freqs)
5574 return -1;
5575
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005576 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5577 wpa_s->num_multichan_concurrent);
5578
5579 /*
5580 * It is possible that the total number of used frequencies is bigger
5581 * than the number of frequencies used for P2P, so get the system wide
5582 * number of unused frequencies.
5583 */
5584 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5585
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005586 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005587 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5588 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005589
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005590 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005591 int ret;
5592 if (go)
5593 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5594 else
5595 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5596 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005597 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005598 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5599 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005600 /*
5601 * If freq is a DFS channel and DFS is offloaded
5602 * to the driver, allow P2P GO to use it.
5603 */
5604 wpa_printf(MSG_DEBUG,
5605 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5606 freq);
5607 } else {
5608 wpa_printf(MSG_DEBUG,
5609 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5610 freq);
5611 res = -3;
5612 goto exit_free;
5613 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005614 }
5615
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005616 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005617 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005618 freq_in_use = 1;
5619 }
5620
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005621 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005622 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5623 freq);
5624 res = -2;
5625 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005626 }
5627 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5628 "requested channel (%u MHz)", freq);
5629 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005630 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005631 }
5632
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005633 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005634
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005635 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5636 enum wpa_driver_if_type iface_type;
5637
5638 if (go)
5639 iface_type = WPA_IF_P2P_GO;
5640 else
5641 iface_type = WPA_IF_P2P_CLIENT;
5642
5643 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5644 best_freq, go);
5645
5646 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5647 &max_pref_freq,
5648 pref_freq_list);
5649 if (!res && max_pref_freq > 0) {
5650 *num_pref_freq = max_pref_freq;
5651 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005652 while (i < *num_pref_freq &&
5653 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005654 pref_freq_list[i]) ||
5655 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005656 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005657 wpa_printf(MSG_DEBUG,
5658 "P2P: preferred_freq_list[%d]=%d is disallowed",
5659 i, pref_freq_list[i]);
5660 i++;
5661 }
5662 if (i != *num_pref_freq) {
5663 best_freq = pref_freq_list[i];
5664 wpa_printf(MSG_DEBUG,
5665 "P2P: Using preferred_freq_list[%d]=%d",
5666 i, best_freq);
5667 } else {
5668 wpa_printf(MSG_DEBUG,
5669 "P2P: All driver preferred frequencies are disallowed for P2P use");
5670 *num_pref_freq = 0;
5671 }
5672 } else {
5673 wpa_printf(MSG_DEBUG,
5674 "P2P: No preferred frequency list available");
5675 }
5676 }
5677
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005678 /* We have a candidate frequency to use */
5679 if (best_freq > 0) {
5680 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005681 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005682 best_freq);
5683 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005684 } else {
5685 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 -07005686 best_freq);
5687 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005688 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005689 } else if (num_unused > 0) {
5690 wpa_printf(MSG_DEBUG,
5691 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5692 *force_freq = 0;
5693 } else {
5694 wpa_printf(MSG_DEBUG,
5695 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5696 res = -2;
5697 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005698 }
5699
5700exit_ok:
5701 res = 0;
5702exit_free:
5703 os_free(freqs);
5704 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005705}
5706
5707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708/**
5709 * wpas_p2p_connect - Request P2P Group Formation to be started
5710 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5711 * @peer_addr: Address of the peer P2P Device
5712 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5713 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005714 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715 * @join: Whether to join an existing group (as a client) instead of starting
5716 * Group Owner negotiation; @peer_addr is BSSID in that case
5717 * @auth: Whether to only authorize the connection instead of doing that and
5718 * initiating Group Owner negotiation
5719 * @go_intent: GO Intent or -1 to use default
5720 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005721 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005722 * @persistent_id: Persistent group credentials to use for forcing GO
5723 * parameters or -1 to generate new values (SSID/passphrase)
5724 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5725 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005726 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005727 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005728 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005729 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005730 * @group_ssid: Specific Group SSID for join or %NULL if not set
5731 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005732 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5733 * failure, -2 on failure due to channel not currently available,
5734 * -3 if forced channel is not supported
5735 */
5736int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5737 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005738 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005739 int go_intent, int freq, unsigned int vht_center_freq2,
5740 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005741 unsigned int vht_chwidth, int he, int edmg,
5742 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005744 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005745 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005747 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005748 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005749 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005750
5751 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5752 return -1;
5753
Dmitry Shmidt04949592012-07-19 12:16:46 -07005754 if (persistent_id >= 0) {
5755 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5756 if (ssid == NULL || ssid->disabled != 2 ||
5757 ssid->mode != WPAS_MODE_P2P_GO)
5758 return -1;
5759 }
5760
Hai Shalom899fcc72020-10-19 14:38:18 -07005761 if (is_6ghz_freq(freq) && wpa_s->conf->p2p_6ghz_disable)
5762 return -2;
5763
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005764 os_free(wpa_s->global->add_psk);
5765 wpa_s->global->add_psk = NULL;
5766
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005767 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005768 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005769 wpa_s->global->pending_p2ps_group_freq = 0;
5770 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772 if (go_intent < 0)
5773 go_intent = wpa_s->conf->p2p_go_intent;
5774
5775 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005776 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005777
5778 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005779 wpa_s->p2p_persistent_group = !!persistent_group;
5780 wpa_s->p2p_persistent_id = persistent_id;
5781 wpa_s->p2p_go_intent = go_intent;
5782 wpa_s->p2p_connect_freq = freq;
5783 wpa_s->p2p_fallback_to_go_neg = 0;
5784 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005785 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005786 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005787 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5788 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005789 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005790 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791
5792 if (pin)
5793 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5794 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005795 if (wps_generate_pin((unsigned int *) &ret) < 0)
5796 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005797 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5798 "%08d", ret);
5799 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5800 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005801 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5802 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005803 } else if (wps_method == WPS_P2PS) {
5804 /* Force the P2Ps default PIN to be used */
5805 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005806 } else
5807 wpa_s->p2p_pin[0] = '\0';
5808
Dmitry Shmidt04949592012-07-19 12:16:46 -07005809 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005810 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5811 if (auth) {
5812 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5813 "connect a running group from " MACSTR,
5814 MAC2STR(peer_addr));
5815 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5816 return ret;
5817 }
5818 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5819 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5820 iface_addr) < 0) {
5821 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5822 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5823 dev_addr);
5824 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005825 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005826 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005827 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5828 "%ld.%06ld",
5829 wpa_s->p2p_auto_started.sec,
5830 wpa_s->p2p_auto_started.usec);
5831 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005832 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005833 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005834 auto_join, freq,
5835 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 return -1;
5837 return ret;
5838 }
5839
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005840 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005841 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005842 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005843 if (res)
5844 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005845 wpas_p2p_set_own_freq_preference(wpa_s,
5846 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005848 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5851
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005852 if (wpa_s->create_p2p_iface) {
5853 /* Prepare to add a new interface for the group */
5854 iftype = WPA_IF_P2P_GROUP;
5855 if (go_intent == 15)
5856 iftype = WPA_IF_P2P_GO;
5857 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5858 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5859 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005860 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005861 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005863 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005864 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005865 if (wpa_s->p2p_mgmt)
5866 if_addr = wpa_s->parent->own_addr;
5867 else
5868 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005869 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5870 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871
5872 if (auth) {
5873 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005874 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005875 force_freq, persistent_group, ssid,
5876 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005877 return -1;
5878 return ret;
5879 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005880
5881 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5882 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005883 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005884 if (wpa_s->create_p2p_iface)
5885 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 return -1;
5887 }
5888 return ret;
5889}
5890
5891
5892/**
5893 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5894 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5895 * @freq: Frequency of the channel in MHz
5896 * @duration: Duration of the stay on the channel in milliseconds
5897 *
5898 * This callback is called when the driver indicates that it has started the
5899 * requested remain-on-channel duration.
5900 */
5901void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5902 unsigned int freq, unsigned int duration)
5903{
5904 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5905 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005906 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)",
5907 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5908 wpa_s->roc_waiting_drv_freq, freq, duration);
5909 if (wpa_s->off_channel_freq &&
5910 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005911 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5912 wpa_s->pending_listen_duration);
5913 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005914 } else {
5915 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5916 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5917 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918 }
5919}
5920
5921
Jithu Jance57cea1a2014-08-20 10:22:04 -07005922int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923{
5924 /* Limit maximum Listen state time based on driver limitation. */
5925 if (timeout > wpa_s->max_remain_on_chan)
5926 timeout = wpa_s->max_remain_on_chan;
5927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005928 return p2p_listen(wpa_s->global->p2p, timeout);
5929}
5930
5931
5932/**
5933 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5934 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5935 * @freq: Frequency of the channel in MHz
5936 *
5937 * This callback is called when the driver indicates that a remain-on-channel
5938 * operation has been completed, i.e., the duration on the requested channel
5939 * has timed out.
5940 */
5941void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5942 unsigned int freq)
5943{
5944 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5945 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005946 wpa_s->global->p2p_long_listen,
5947 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005948 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005949 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5950 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005951 if (wpa_s->global->p2p_long_listen > 0)
5952 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005953 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5954 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005955 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005957 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005958 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005959 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005960 } else {
5961 /*
5962 * When listen duration is over, stop listen & update p2p_state
5963 * to IDLE.
5964 */
5965 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005966 }
5967}
5968
5969
5970/**
5971 * wpas_p2p_group_remove - Remove a P2P group
5972 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5973 * @ifname: Network interface name of the group interface or "*" to remove all
5974 * groups
5975 * Returns: 0 on success, -1 on failure
5976 *
5977 * This function is used to remove a P2P group. This can be used to disconnect
5978 * from a group in which the local end is a P2P Client or to end a P2P Group in
5979 * case the local end is the Group Owner. If a virtual network interface was
5980 * created for this group, that interface will be removed. Otherwise, only the
5981 * configured P2P group network will be removed from the interface.
5982 */
5983int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5984{
5985 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005986 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005987
5988 if (os_strcmp(ifname, "*") == 0) {
5989 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07005990 bool calling_wpa_s_group_removed = false;
5991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992 wpa_s = global->ifaces;
5993 while (wpa_s) {
5994 prev = wpa_s;
5995 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005996 if (prev->p2p_group_interface !=
5997 NOT_P2P_GROUP_INTERFACE ||
5998 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07005999 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006000 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006001 if (prev == calling_wpa_s)
6002 calling_wpa_s_group_removed = true;
6003 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006004 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006005
6006 if (!calling_wpa_s_group_removed &&
6007 (calling_wpa_s->p2p_group_interface !=
6008 NOT_P2P_GROUP_INTERFACE ||
6009 (calling_wpa_s->current_ssid &&
6010 calling_wpa_s->current_ssid->p2p_group))) {
6011 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6012 wpas_p2p_disconnect_safely(calling_wpa_s,
6013 calling_wpa_s);
6014 }
6015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006016 return 0;
6017 }
6018
6019 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6020 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6021 break;
6022 }
6023
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006024 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006025}
6026
6027
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006028static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6029{
6030 unsigned int r;
6031
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006032 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6033 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
6034 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
6035 int res;
6036
6037 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6038 &size, pref_freq_list);
6039 if (!res && size > 0) {
6040 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006041 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006042 (!p2p_supported_freq(wpa_s->global->p2p,
6043 pref_freq_list[i]) ||
6044 wpas_p2p_disallowed_freq(wpa_s->global,
6045 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006046 wpa_printf(MSG_DEBUG,
6047 "P2P: preferred_freq_list[%d]=%d is disallowed",
6048 i, pref_freq_list[i]);
6049 i++;
6050 }
6051 if (i != size) {
6052 freq = pref_freq_list[i];
6053 wpa_printf(MSG_DEBUG,
6054 "P2P: Using preferred_freq_list[%d]=%d",
6055 i, freq);
6056 } else {
6057 wpa_printf(MSG_DEBUG,
6058 "P2P: All driver preferred frequencies are disallowed for P2P use");
6059 }
6060 } else {
6061 wpa_printf(MSG_DEBUG,
6062 "P2P: No preferred frequency list available");
6063 }
6064 }
6065
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006066 if (freq == 2) {
6067 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6068 "band");
6069 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006070 p2p_supported_freq_go(wpa_s->global->p2p,
6071 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006072 freq = wpa_s->best_24_freq;
6073 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6074 "channel: %d MHz", freq);
6075 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006076 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6077 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006078 freq = 2412 + (r % 3) * 25;
6079 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6080 "channel: %d MHz", freq);
6081 }
6082 }
6083
6084 if (freq == 5) {
6085 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6086 "band");
6087 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006088 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006089 wpa_s->best_5_freq)) {
6090 freq = wpa_s->best_5_freq;
6091 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6092 "channel: %d MHz", freq);
6093 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006094 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6095 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006096
6097 /*
6098 * most of 5G channels are DFS, only operating class 115 and 124
6099 * are available possibly, randomly pick a start to check them.
6100 */
6101 int possible_5g_freqs[] = {
6102 /* operating class 115 */
6103 5180, 5200, 5220, 5240,
6104 /* operating class 124 */
6105 5745, 5765, 5785, 5805,
6106 };
6107 int possible_5g_freqs_num =
6108 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6109
6110 int i;
6111 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6112 if (p2p_supported_freq_go(
6113 wpa_s->global->p2p,
6114 possible_5g_freqs[r % possible_5g_freqs_num])) {
6115 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6116 break;
6117 }
6118 }
6119
6120 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006121 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6122 "5 GHz channel for P2P group");
6123 return -1;
6124 }
6125 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6126 "channel: %d MHz", freq);
6127 }
6128 }
6129
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006130 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006131 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006132 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6133 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006134 /*
6135 * If freq is a DFS channel and DFS is offloaded to the
6136 * driver, allow P2P GO to use it.
6137 */
6138 wpa_printf(MSG_DEBUG, "P2P: "
6139 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6140 __func__, freq);
6141 return freq;
6142 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006143 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6144 "(%u MHz) is not supported for P2P uses",
6145 freq);
6146 return -1;
6147 }
6148
6149 return freq;
6150}
6151
6152
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006153static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6154 const struct p2p_channels *channels,
6155 int freq)
6156{
6157 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6158 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6159 freq_included(wpa_s, channels, freq))
6160 return 1;
6161 return 0;
6162}
6163
6164
6165static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6166 struct p2p_go_neg_results *params,
6167 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006168{
6169 unsigned int i, r;
6170
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006171 /* try all channels in operating class 115 */
6172 for (i = 0; i < 4; i++) {
6173 params->freq = 5180 + i * 20;
6174 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006175 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006176 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6177 goto out;
6178 }
6179
6180 /* try all channels in operating class 124 */
6181 for (i = 0; i < 4; i++) {
6182 params->freq = 5745 + i * 20;
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 Shmidt203eadb2015-03-05 14:16:04 -08006185 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6186 goto out;
6187 }
6188
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006189 /* try social channel class 180 channel 2 */
6190 params->freq = 58320 + 1 * 2160;
6191 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006192 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006193 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6194 goto out;
6195
6196 /* try all channels in reg. class 180 */
6197 for (i = 0; i < 4; i++) {
6198 params->freq = 58320 + i * 2160;
6199 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006200 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006201 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6202 goto out;
6203 }
6204
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006205 /* try some random selection of the social channels */
6206 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6207 return;
6208
6209 for (i = 0; i < 3; i++) {
6210 params->freq = 2412 + ((r + i) % 3) * 25;
6211 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6212 goto out;
6213 }
6214
6215 /* try all other channels in operating class 81 */
6216 for (i = 0; i < 11; i++) {
6217 params->freq = 2412 + i * 5;
6218
6219 /* skip social channels; covered in the previous loop */
6220 if (params->freq == 2412 ||
6221 params->freq == 2437 ||
6222 params->freq == 2462)
6223 continue;
6224
6225 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6226 goto out;
6227 }
6228
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006229 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006230 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006231 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006232out:
6233 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6234 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006235}
6236
6237
Roshan Pius3a1667e2018-07-03 15:17:14 -07006238static int wpas_same_band(int freq1, int freq2)
6239{
6240 enum hostapd_hw_mode mode1, mode2;
6241 u8 chan1, chan2;
6242
6243 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6244 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6245 if (mode1 == NUM_HOSTAPD_MODES)
6246 return 0;
6247 return mode1 == mode2;
6248}
6249
6250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006251static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6252 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006253 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006254 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006255 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006256 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006258 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006259 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006260 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006261 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006262 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006263
6264 os_memset(params, 0, sizeof(*params));
6265 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006266 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006267 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006268 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006269 params->max_oper_chwidth = max_oper_chwidth;
6270 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006271 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006273 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6274 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006275 if (!freqs)
6276 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006278 num = get_shared_radio_freqs_data(wpa_s, freqs,
6279 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006280
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006281 if (wpa_s->current_ssid &&
6282 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6283 wpa_s->wpa_state == WPA_COMPLETED) {
6284 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6285 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006286
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006287 /*
6288 * If the frequency selection is done for an active P2P GO that
6289 * is not sharing a frequency, allow to select a new frequency
6290 * even if there are no unused frequencies as we are about to
6291 * move the P2P GO so its frequency can be re-used.
6292 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006293 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006294 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6295 freqs[i].flags == 0) {
6296 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006297 break;
6298 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006299 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006300 }
6301
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006302 /* Try to use EDMG channel */
6303 if (params->edmg) {
6304 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6305 goto success;
6306 params->edmg = 0;
6307 }
6308
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006309 /* try using the forced freq */
6310 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006311 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6312 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006313 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006314 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006315 freq);
6316 goto fail;
6317 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006318 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6319 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006320 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6321 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006322 /*
6323 * If freq is a DFS channel and DFS is offloaded
6324 * to the driver, allow P2P GO to use it.
6325 */
6326 wpa_printf(MSG_DEBUG,
6327 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6328 __func__, freq);
6329 } else {
6330 wpa_printf(MSG_DEBUG,
6331 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6332 freq);
6333 goto fail;
6334 }
6335 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006336
6337 for (i = 0; i < num; i++) {
6338 if (freqs[i].freq == freq) {
6339 wpa_printf(MSG_DEBUG,
6340 "P2P: forced freq (%d MHz) is also shared",
6341 freq);
6342 params->freq = freq;
6343 goto success;
6344 }
6345 }
6346
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006347 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006348 wpa_printf(MSG_DEBUG,
6349 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6350 freq);
6351 goto fail;
6352 }
6353
6354 wpa_printf(MSG_DEBUG,
6355 "P2P: force GO freq (%d MHz) on a free channel",
6356 freq);
6357 params->freq = freq;
6358 goto success;
6359 }
6360
6361 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006362 if (num &&
6363 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006364 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6365 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6366 wpa_printf(MSG_DEBUG,
6367 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006368 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006369 params->freq = cand;
6370 goto success;
6371 }
6372
6373 /* try using one of the shared freqs */
6374 for (i = 0; i < num; i++) {
6375 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6376 freqs[i].freq)) {
6377 wpa_printf(MSG_DEBUG,
6378 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006379 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006380 params->freq = freqs[i].freq;
6381 goto success;
6382 }
6383 }
6384 }
6385
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006386 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006387 wpa_printf(MSG_DEBUG,
6388 "P2P: Cannot force GO on any of the channels we are already using");
6389 goto fail;
6390 }
6391
6392 /* try using the setting from the configuration file */
6393 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6394 wpa_s->conf->p2p_oper_channel >= 1 &&
6395 wpa_s->conf->p2p_oper_channel <= 11 &&
6396 wpas_p2p_supported_freq_go(
6397 wpa_s, channels,
6398 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6399 params->freq = 2407 + 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 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6406 wpa_s->conf->p2p_oper_reg_class == 116 ||
6407 wpa_s->conf->p2p_oper_reg_class == 117 ||
6408 wpa_s->conf->p2p_oper_reg_class == 124 ||
6409 wpa_s->conf->p2p_oper_reg_class == 125 ||
6410 wpa_s->conf->p2p_oper_reg_class == 126 ||
6411 wpa_s->conf->p2p_oper_reg_class == 127) &&
6412 wpas_p2p_supported_freq_go(wpa_s, channels,
6413 5000 +
6414 5 * wpa_s->conf->p2p_oper_channel)) {
6415 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6416 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6417 "frequency %d MHz", params->freq);
6418 goto success;
6419 }
6420
6421 /* Try using best channels */
6422 if (wpa_s->conf->p2p_oper_channel == 0 &&
6423 wpa_s->best_overall_freq > 0 &&
6424 wpas_p2p_supported_freq_go(wpa_s, channels,
6425 wpa_s->best_overall_freq)) {
6426 params->freq = wpa_s->best_overall_freq;
6427 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6428 "channel %d MHz", params->freq);
6429 goto success;
6430 }
6431
6432 if (wpa_s->conf->p2p_oper_channel == 0 &&
6433 wpa_s->best_24_freq > 0 &&
6434 wpas_p2p_supported_freq_go(wpa_s, channels,
6435 wpa_s->best_24_freq)) {
6436 params->freq = wpa_s->best_24_freq;
6437 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6438 "channel %d MHz", params->freq);
6439 goto success;
6440 }
6441
6442 if (wpa_s->conf->p2p_oper_channel == 0 &&
6443 wpa_s->best_5_freq > 0 &&
6444 wpas_p2p_supported_freq_go(wpa_s, channels,
6445 wpa_s->best_5_freq)) {
6446 params->freq = wpa_s->best_5_freq;
6447 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6448 "channel %d MHz", params->freq);
6449 goto success;
6450 }
6451
6452 /* try using preferred channels */
6453 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6454 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6455 params->freq = cand;
6456 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6457 "channels", params->freq);
6458 goto success;
6459 }
6460
Roshan Pius3a1667e2018-07-03 15:17:14 -07006461 /* Try using a channel that allows VHT to be used with 80 MHz */
6462 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6463 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6464 enum hostapd_hw_mode mode;
6465 struct hostapd_hw_modes *hwmode;
6466 u8 chan;
6467
6468 cand = wpa_s->p2p_group_common_freqs[i];
6469 mode = ieee80211_freq_to_chan(cand, &chan);
6470 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006471 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006472 if (!hwmode ||
6473 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6474 BW80) != ALLOWED)
6475 continue;
6476 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6477 params->freq = cand;
6478 wpa_printf(MSG_DEBUG,
6479 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6480 params->freq);
6481 goto success;
6482 }
6483 }
6484 }
6485
6486 /* Try using a channel that allows HT to be used with 40 MHz on the same
6487 * band so that CSA can be used */
6488 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6489 wpa_s->p2p_group_common_freqs) {
6490 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6491 enum hostapd_hw_mode mode;
6492 struct hostapd_hw_modes *hwmode;
6493 u8 chan;
6494
6495 cand = wpa_s->p2p_group_common_freqs[i];
6496 mode = ieee80211_freq_to_chan(cand, &chan);
6497 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006498 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006499 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6500 cand) ||
6501 !hwmode ||
6502 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6503 BW40MINUS) != ALLOWED &&
6504 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6505 BW40PLUS) != ALLOWED))
6506 continue;
6507 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6508 params->freq = cand;
6509 wpa_printf(MSG_DEBUG,
6510 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6511 params->freq);
6512 goto success;
6513 }
6514 }
6515 }
6516
6517 /* Try using one of the group common freqs on the same band so that CSA
6518 * can be used */
6519 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6520 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6521 cand = wpa_s->p2p_group_common_freqs[i];
6522 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6523 cand))
6524 continue;
6525 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6526 params->freq = cand;
6527 wpa_printf(MSG_DEBUG,
6528 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6529 params->freq);
6530 goto success;
6531 }
6532 }
6533 }
6534
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006535 /* Try using one of the group common freqs */
6536 if (wpa_s->p2p_group_common_freqs) {
6537 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6538 cand = wpa_s->p2p_group_common_freqs[i];
6539 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6540 params->freq = cand;
6541 wpa_printf(MSG_DEBUG,
6542 "P2P: Use freq %d MHz common with the peer",
6543 params->freq);
6544 goto success;
6545 }
6546 }
6547 }
6548
6549 /* no preference, select some channel */
6550 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6551
6552 if (params->freq == 0) {
6553 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6554 goto fail;
6555 }
6556
6557success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006558 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006559 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006560fail:
6561 os_free(freqs);
6562 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006563}
6564
6565
6566static struct wpa_supplicant *
6567wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6568 int go)
6569{
6570 struct wpa_supplicant *group_wpa_s;
6571
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006572 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006573 if (wpa_s->p2p_mgmt) {
6574 /*
6575 * We may be called on the p2p_dev interface which
6576 * cannot be used for group operations, so always use
6577 * the primary interface.
6578 */
6579 wpa_s->parent->p2pdev = wpa_s;
6580 wpa_s = wpa_s->parent;
6581 }
6582 wpa_dbg(wpa_s, MSG_DEBUG,
6583 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006584 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006585 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006586 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006588 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006589
6590 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006591 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006592 wpa_msg_global(wpa_s, MSG_ERROR,
6593 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006594 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006595 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6597 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006598 wpa_msg_global(wpa_s, MSG_ERROR,
6599 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006600 wpas_p2p_remove_pending_group_interface(wpa_s);
6601 return NULL;
6602 }
6603
Roshan Pius3a1667e2018-07-03 15:17:14 -07006604 if (go && wpa_s->p2p_go_do_acs) {
6605 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6606 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6607 wpa_s->p2p_go_do_acs = 0;
6608 }
6609
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006610 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6611 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006612 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006613 return group_wpa_s;
6614}
6615
6616
6617/**
6618 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6619 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6620 * @persistent_group: Whether to create a persistent group
6621 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006622 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006623 * @ht40: Start GO with 40 MHz channel width
6624 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006625 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006626 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006627 * Returns: 0 on success, -1 on failure
6628 *
6629 * This function creates a new P2P group with the local end as the Group Owner,
6630 * i.e., without using Group Owner Negotiation.
6631 */
6632int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006633 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006634 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635{
6636 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006637
6638 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6639 return -1;
6640
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006641 os_free(wpa_s->global->add_psk);
6642 wpa_s->global->add_psk = NULL;
6643
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006644 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006645 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006646 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006647
Roshan Pius3a1667e2018-07-03 15:17:14 -07006648 if (!wpa_s->p2p_go_do_acs) {
6649 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6650 if (freq < 0)
6651 return -1;
6652 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006653
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006654 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006655 ht40, vht, max_oper_chwidth, he, edmg,
6656 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006657 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006659 p2p_go_params(wpa_s->global->p2p, &params);
6660 params.persistent_group = persistent_group;
6661
6662 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6663 if (wpa_s == NULL)
6664 return -1;
6665 wpas_start_wps_go(wpa_s, &params, 0);
6666
6667 return 0;
6668}
6669
6670
6671static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006672 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006673 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006674{
6675 struct wpa_ssid *ssid;
6676
6677 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6678 if (wpa_s == NULL)
6679 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006680 if (force_scan)
6681 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006682 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006683
6684 wpa_supplicant_ap_deinit(wpa_s);
6685
6686 ssid = wpa_config_add_network(wpa_s->conf);
6687 if (ssid == NULL)
6688 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006689 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 wpa_config_set_network_defaults(ssid);
6691 ssid->temporary = 1;
6692 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006693 ssid->pbss = params->pbss;
6694 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6695 WPA_CIPHER_CCMP;
6696 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6698 ssid->ssid = os_malloc(params->ssid_len);
6699 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006700 wpa_config_remove_network(wpa_s->conf, ssid->id);
6701 return -1;
6702 }
6703 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6704 ssid->ssid_len = params->ssid_len;
6705 ssid->p2p_group = 1;
6706 ssid->export_keys = 1;
6707 if (params->psk_set) {
6708 os_memcpy(ssid->psk, params->psk, 32);
6709 ssid->psk_set = 1;
6710 }
6711 if (params->passphrase)
6712 ssid->passphrase = os_strdup(params->passphrase);
6713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006714 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006715 wpa_s->p2p_in_invitation = 1;
6716 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006717 wpa_s->p2p_go_group_formation_completed = 0;
6718 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006719
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006720 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006721 NULL);
6722 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6723 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006724 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006725 wpa_supplicant_select_network(wpa_s, ssid);
6726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727 return 0;
6728}
6729
6730
6731int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6732 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006733 int force_freq, int neg_freq,
6734 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006735 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006736 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006737 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006738 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006739{
6740 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006741 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006742
6743 if (ssid->disabled != 2 || ssid->ssid == NULL)
6744 return -1;
6745
6746 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6747 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6748 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6749 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006750 if (go == 0 &&
6751 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006752 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006753 /*
6754 * This can happen if Invitation Response frame was lost
6755 * and the peer (GO of a persistent group) tries to
6756 * invite us again. Reschedule the timeout to avoid
6757 * terminating the wait for the connection too early
6758 * since we now know that the peer is still trying to
6759 * invite us instead of having already started the GO.
6760 */
6761 wpa_printf(MSG_DEBUG,
6762 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6763 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6764 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006765 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006766 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767 return 0;
6768 }
6769
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006770 os_free(wpa_s->global->add_psk);
6771 wpa_s->global->add_psk = NULL;
6772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006773 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006774 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006775
Dmitry Shmidt04949592012-07-19 12:16:46 -07006776 wpa_s->p2p_fallback_to_go_neg = 0;
6777
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006778 if (ssid->mode == WPAS_MODE_P2P_GO) {
6779 if (force_freq > 0) {
6780 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6781 if (freq < 0)
6782 return -1;
6783 } else {
6784 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6785 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006786 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006787 freq = 0;
6788 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006789 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006790 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006791 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006792 struct os_reltime now;
6793 struct wpa_bss *bss =
6794 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006795
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006796 os_get_reltime(&now);
6797 if (bss &&
6798 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006799 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006800 freq = bss->freq;
6801 else
6802 freq = 0;
6803 }
6804
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006805 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6806 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006807 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006808 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006809 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006810
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006811 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006812 ht40, vht, max_oper_chwidth, he, edmg,
6813 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006814 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006815
6816 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006817 params.psk_set = ssid->psk_set;
6818 if (params.psk_set)
6819 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006820 if (ssid->passphrase) {
6821 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6822 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6823 "persistent group");
6824 return -1;
6825 }
6826 os_strlcpy(params.passphrase, ssid->passphrase,
6827 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006828 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006829 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6830 params.ssid_len = ssid->ssid_len;
6831 params.persistent_group = 1;
6832
6833 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6834 if (wpa_s == NULL)
6835 return -1;
6836
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006837 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6838
Dmitry Shmidt56052862013-10-04 10:23:25 -07006839 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840 wpas_start_wps_go(wpa_s, &params, 0);
6841
6842 return 0;
6843}
6844
6845
6846static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6847 struct wpabuf *proberesp_ies)
6848{
6849 struct wpa_supplicant *wpa_s = ctx;
6850 if (wpa_s->ap_iface) {
6851 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006852 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6853 wpabuf_free(beacon_ies);
6854 wpabuf_free(proberesp_ies);
6855 return;
6856 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857 if (beacon_ies) {
6858 wpabuf_free(hapd->p2p_beacon_ie);
6859 hapd->p2p_beacon_ie = beacon_ies;
6860 }
6861 wpabuf_free(hapd->p2p_probe_resp_ie);
6862 hapd->p2p_probe_resp_ie = proberesp_ies;
6863 } else {
6864 wpabuf_free(beacon_ies);
6865 wpabuf_free(proberesp_ies);
6866 }
6867 wpa_supplicant_ap_update_beacon(wpa_s);
6868}
6869
6870
6871static void wpas_p2p_idle_update(void *ctx, int idle)
6872{
6873 struct wpa_supplicant *wpa_s = ctx;
6874 if (!wpa_s->ap_iface)
6875 return;
6876 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006877 if (idle) {
6878 if (wpa_s->global->p2p_fail_on_wps_complete &&
6879 wpa_s->p2p_in_provisioning) {
6880 wpas_p2p_grpform_fail_after_wps(wpa_s);
6881 return;
6882 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006883 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006884 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006885 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6886}
6887
6888
6889struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006890 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006891{
6892 struct p2p_group *group;
6893 struct p2p_group_config *cfg;
6894
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006895 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6896 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006897 return NULL;
6898
6899 cfg = os_zalloc(sizeof(*cfg));
6900 if (cfg == NULL)
6901 return NULL;
6902
Dmitry Shmidt04949592012-07-19 12:16:46 -07006903 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006904 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006905 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006906 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6908 if (wpa_s->max_stations &&
6909 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6910 cfg->max_clients = wpa_s->max_stations;
6911 else
6912 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006913 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6914 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006915 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006916 cfg->cb_ctx = wpa_s;
6917 cfg->ie_update = wpas_p2p_ie_update;
6918 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006919 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6920 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006921
6922 group = p2p_group_init(wpa_s->global->p2p, cfg);
6923 if (group == NULL)
6924 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006925 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926 p2p_group_notif_formation_done(group);
6927 wpa_s->p2p_group = group;
6928 return group;
6929}
6930
6931
6932void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6933 int registrar)
6934{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006935 struct wpa_ssid *ssid = wpa_s->current_ssid;
6936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937 if (!wpa_s->p2p_in_provisioning) {
6938 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6939 "provisioning not in progress");
6940 return;
6941 }
6942
Dmitry Shmidt04949592012-07-19 12:16:46 -07006943 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6944 u8 go_dev_addr[ETH_ALEN];
6945 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6946 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6947 ssid->ssid_len);
6948 /* Clear any stored provisioning info */
6949 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6950 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006951
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006952 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006953 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006954 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006955 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6956 /*
6957 * Use a separate timeout for initial data connection to
6958 * complete to allow the group to be removed automatically if
6959 * something goes wrong in this step before the P2P group idle
6960 * timeout mechanism is taken into use.
6961 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006962 wpa_dbg(wpa_s, MSG_DEBUG,
6963 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6964 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006965 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6966 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006967 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006968 /* Complete group formation on successful data connection. */
6969 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006970 } else if (ssid) {
6971 /*
6972 * Use a separate timeout for initial data connection to
6973 * complete to allow the group to be removed automatically if
6974 * the client does not complete data connection successfully.
6975 */
6976 wpa_dbg(wpa_s, MSG_DEBUG,
6977 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6978 P2P_MAX_INITIAL_CONN_WAIT_GO);
6979 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6980 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006981 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006982 /*
6983 * Complete group formation on first successful data connection
6984 */
6985 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006987 if (wpa_s->global->p2p)
6988 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006989 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006990}
6991
6992
Jouni Malinen75ecf522011-06-27 15:19:46 -07006993void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6994 struct wps_event_fail *fail)
6995{
6996 if (!wpa_s->p2p_in_provisioning) {
6997 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6998 "provisioning not in progress");
6999 return;
7000 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007001
7002 if (wpa_s->go_params) {
7003 p2p_clear_provisioning_info(
7004 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007005 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007006 }
7007
Jouni Malinen75ecf522011-06-27 15:19:46 -07007008 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007009
7010 if (wpa_s == wpa_s->global->p2p_group_formation) {
7011 /*
7012 * Allow some time for the failed WPS negotiation exchange to
7013 * complete, but remove the group since group formation cannot
7014 * succeed after provisioning failure.
7015 */
7016 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7017 wpa_s->global->p2p_fail_on_wps_complete = 1;
7018 eloop_deplete_timeout(0, 50000,
7019 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007020 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007021 }
7022}
7023
7024
7025int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7026{
7027 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7028 !wpa_s->p2p_in_provisioning)
7029 return 0;
7030
7031 wpas_p2p_grpform_fail_after_wps(wpa_s);
7032
7033 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007034}
7035
7036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007037int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007038 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007039 enum wpas_p2p_prov_disc_use use,
7040 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007041{
7042 u16 config_methods;
7043
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007044 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007045 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007046 wpa_s->p2p_fallback_to_go_neg = 0;
7047 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007048 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7049 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007050 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7051 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7052
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007053 wpa_printf(MSG_DEBUG,
7054 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7055 __func__, p2ps_prov->conncap,
7056 p2ps_prov->adv_id, p2ps_prov->conncap,
7057 p2ps_prov->status, p2ps_prov->info);
7058
7059 config_methods = 0;
7060 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007061 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007062 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007063 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007064 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7065 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007066 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007067 else {
7068 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007069 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007072
Dmitry Shmidt04949592012-07-19 12:16:46 -07007073 if (use == WPAS_P2P_PD_AUTO) {
7074 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7075 wpa_s->pending_pd_config_methods = config_methods;
7076 wpa_s->p2p_auto_pd = 1;
7077 wpa_s->p2p_auto_join = 0;
7078 wpa_s->pending_pd_before_join = 0;
7079 wpa_s->auto_pd_scan_retry = 0;
7080 wpas_p2p_stop_find(wpa_s);
7081 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007082 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007083 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7084 wpa_s->p2p_auto_started.sec,
7085 wpa_s->p2p_auto_started.usec);
7086 wpas_p2p_join_scan(wpa_s, NULL);
7087 return 0;
7088 }
7089
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007090 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7091 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007092 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007095 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007096 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007097 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007098}
7099
7100
7101int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7102 char *end)
7103{
7104 return p2p_scan_result_text(ies, ies_len, buf, end);
7105}
7106
7107
7108static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7109{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007110 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007111 return;
7112
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007113 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007114 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007115 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7116 wpa_s, NULL);
7117 offchannel_send_action_done(wpa_s);
7118 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7121 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007122 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007123}
7124
7125
7126int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7127 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007128 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007129 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007130 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007131{
7132 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007133 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007134
Dmitry Shmidt04949592012-07-19 12:16:46 -07007135 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007136 wpa_s->p2p_in_provisioning) {
7137 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7138 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7139 " (P2P disabled)" : "",
7140 wpa_s->p2p_in_provisioning ?
7141 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007142 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007144
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007145 wpa_supplicant_cancel_sched_scan(wpa_s);
7146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007147 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007148 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007149 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007150}
7151
7152
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007153static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7154 struct wpa_scan_results *scan_res)
7155{
7156 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7157
7158 if (wpa_s->p2p_scan_work) {
7159 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7160 wpa_s->p2p_scan_work = NULL;
7161 radio_work_done(work);
7162 }
7163
7164 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7165 return;
7166
7167 /*
7168 * Indicate that results have been processed so that the P2P module can
7169 * continue pending tasks.
7170 */
Hai Shalom60840252021-02-19 19:02:11 -08007171 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007172}
7173
7174
7175static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007176{
7177 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007178 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007179 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7180 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007181
7182 if (wpa_s->global->p2p)
7183 p2p_stop_find(wpa_s->global->p2p);
7184
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007185 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7186 wpa_printf(MSG_DEBUG,
7187 "P2P: Do not consider the scan results after stop_find");
7188 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7189 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007190}
7191
7192
7193void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7194{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007195 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007196 if (!wpa_s->global->pending_group_iface_for_p2ps)
7197 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007198}
7199
7200
7201static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7202{
7203 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007204 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007205}
7206
7207
7208int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7209{
7210 int res;
7211
7212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7213 return -1;
7214
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007215 if (wpa_s->p2p_lo_started) {
7216 wpa_printf(MSG_DEBUG,
7217 "P2P: Cannot start P2P listen, it is offloaded");
7218 return -1;
7219 }
7220
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007221 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007222 wpas_p2p_clear_pending_action_tx(wpa_s);
7223
7224 if (timeout == 0) {
7225 /*
7226 * This is a request for unlimited Listen state. However, at
7227 * least for now, this is mapped to a Listen state for one
7228 * hour.
7229 */
7230 timeout = 3600;
7231 }
7232 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007233 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007234
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007235 /*
7236 * Stop previous find/listen operation to avoid trying to request a new
7237 * remain-on-channel operation while the driver is still running the
7238 * previous one.
7239 */
7240 if (wpa_s->global->p2p)
7241 p2p_stop_find(wpa_s->global->p2p);
7242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007243 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7244 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007245 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007246 eloop_register_timeout(timeout, 0,
7247 wpas_p2p_long_listen_timeout,
7248 wpa_s, NULL);
7249 }
7250
7251 return res;
7252}
7253
7254
7255int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7256 u8 *buf, size_t len, int p2p_group)
7257{
7258 struct wpabuf *p2p_ie;
7259 int ret;
7260
7261 if (wpa_s->global->p2p_disabled)
7262 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007263 /*
7264 * Advertize mandatory cross connection capability even on
7265 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7266 */
7267 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007268 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007269 if (wpa_s->global->p2p == NULL)
7270 return -1;
7271 if (bss == NULL)
7272 return -1;
7273
7274 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7275 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7276 p2p_group, p2p_ie);
7277 wpabuf_free(p2p_ie);
7278
7279 return ret;
7280}
7281
7282
7283int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007284 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007285 const u8 *ie, size_t ie_len,
7286 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007287{
7288 if (wpa_s->global->p2p_disabled)
7289 return 0;
7290 if (wpa_s->global->p2p == NULL)
7291 return 0;
7292
Dmitry Shmidt04949592012-07-19 12:16:46 -07007293 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007294 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007295 case P2P_PREQ_NOT_P2P:
7296 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7297 ssi_signal);
7298 /* fall through */
7299 case P2P_PREQ_MALFORMED:
7300 case P2P_PREQ_NOT_LISTEN:
7301 case P2P_PREQ_NOT_PROCESSED:
7302 default: /* make gcc happy */
7303 return 0;
7304 case P2P_PREQ_PROCESSED:
7305 return 1;
7306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007307}
7308
7309
7310void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7311 const u8 *sa, const u8 *bssid,
7312 u8 category, const u8 *data, size_t len, int freq)
7313{
7314 if (wpa_s->global->p2p_disabled)
7315 return;
7316 if (wpa_s->global->p2p == NULL)
7317 return;
7318
7319 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7320 freq);
7321}
7322
7323
7324void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7325{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007326 unsigned int bands;
7327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007328 if (wpa_s->global->p2p_disabled)
7329 return;
7330 if (wpa_s->global->p2p == NULL)
7331 return;
7332
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007333 bands = wpas_get_bands(wpa_s, NULL);
7334 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007335}
7336
7337
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007338static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007339{
7340 p2p_group_deinit(wpa_s->p2p_group);
7341 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007342
7343 wpa_s->ap_configured_cb = NULL;
7344 wpa_s->ap_configured_cb_ctx = NULL;
7345 wpa_s->ap_configured_cb_data = NULL;
7346 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007347}
7348
7349
7350int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7351{
Hai Shalomfdcde762020-04-02 11:19:20 -07007352 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007354 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7355 return -1;
7356
7357 return p2p_reject(wpa_s->global->p2p, addr);
7358}
7359
7360
7361/* Invite to reinvoke a persistent group */
7362int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007363 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007364 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007365 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007366{
7367 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007368 u8 *bssid = NULL;
7369 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007370 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007371 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007372 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007373
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007374 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007375 if (peer_addr)
7376 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7377 else
7378 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007379
Jouni Malinen31be0a42012-08-31 21:20:51 +03007380 wpa_s->p2p_persistent_go_freq = freq;
7381 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007382 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007383 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007384 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7385 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007386 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007387 if (ssid->mode == WPAS_MODE_P2P_GO) {
7388 role = P2P_INVITE_ROLE_GO;
7389 if (peer_addr == NULL) {
7390 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7391 "address in invitation command");
7392 return -1;
7393 }
7394 if (wpas_p2p_create_iface(wpa_s)) {
7395 if (wpas_p2p_add_group_interface(wpa_s,
7396 WPA_IF_P2P_GO) < 0) {
7397 wpa_printf(MSG_ERROR, "P2P: Failed to "
7398 "allocate a new interface for the "
7399 "group");
7400 return -1;
7401 }
7402 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007403 } else if (wpa_s->p2p_mgmt)
7404 bssid = wpa_s->parent->own_addr;
7405 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007406 bssid = wpa_s->own_addr;
7407 } else {
7408 role = P2P_INVITE_ROLE_CLIENT;
7409 peer_addr = ssid->bssid;
7410 }
7411 wpa_s->pending_invite_ssid_id = ssid->id;
7412
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007413 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007414 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007415 role == P2P_INVITE_ROLE_GO,
7416 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007417 if (res)
7418 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7421 return -1;
7422
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007423 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7424
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007425 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7426 no_pref_freq_given && pref_freq > 0 &&
7427 wpa_s->num_multichan_concurrent > 1 &&
7428 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7429 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7430 pref_freq);
7431 pref_freq = 0;
7432 }
7433
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007434 /*
7435 * Stop any find/listen operations before invitation and possibly
7436 * connection establishment.
7437 */
7438 wpas_p2p_stop_find_oper(wpa_s);
7439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007440 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007441 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007442 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007443}
7444
7445
7446/* Invite to join an active group */
7447int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7448 const u8 *peer_addr, const u8 *go_dev_addr)
7449{
7450 struct wpa_global *global = wpa_s->global;
7451 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007452 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007453 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007454 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007455 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007456 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007457 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458
Jouni Malinen31be0a42012-08-31 21:20:51 +03007459 wpa_s->p2p_persistent_go_freq = 0;
7460 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007461 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007462 wpa_s->p2p_go_vht_center_freq2 = 0;
7463 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007464 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7467 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7468 break;
7469 }
7470 if (wpa_s == NULL) {
7471 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7472 return -1;
7473 }
7474
7475 ssid = wpa_s->current_ssid;
7476 if (ssid == NULL) {
7477 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7478 "invitation");
7479 return -1;
7480 }
7481
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007482 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007483 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007484 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007485 ssid->ssid, ssid->ssid_len);
7486
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007487 if (ssid->mode == WPAS_MODE_P2P_GO) {
7488 role = P2P_INVITE_ROLE_ACTIVE_GO;
7489 bssid = wpa_s->own_addr;
7490 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007491 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007492 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007493 } else {
7494 role = P2P_INVITE_ROLE_CLIENT;
7495 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7496 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7497 "invite to current group");
7498 return -1;
7499 }
7500 bssid = wpa_s->bssid;
7501 if (go_dev_addr == NULL &&
7502 !is_zero_ether_addr(wpa_s->go_dev_addr))
7503 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007504 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7505 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007506 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007507 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007509 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7510 return -1;
7511
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007512 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007513 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007514 role == P2P_INVITE_ROLE_ACTIVE_GO,
7515 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007516 if (res)
7517 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007518 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007520 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007521 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007522 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007523}
7524
7525
7526void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7527{
7528 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007529 u8 go_dev_addr[ETH_ALEN];
7530 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007531 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007532 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007533 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534
Dmitry Shmidt04949592012-07-19 12:16:46 -07007535 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7536 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007537 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007538 }
7539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007540 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007541 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007542
7543 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007544 if (!wpa_s->p2p_go_group_formation_completed &&
7545 wpa_s->global->p2p_group_formation == wpa_s) {
7546 wpa_dbg(wpa_s, MSG_DEBUG,
7547 "P2P: Marking group formation completed on client on data connection");
7548 wpa_s->p2p_go_group_formation_completed = 1;
7549 wpa_s->global->p2p_group_formation = NULL;
7550 wpa_s->p2p_in_provisioning = 0;
7551 wpa_s->p2p_in_invitation = 0;
7552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554 os_memset(go_dev_addr, 0, ETH_ALEN);
7555 if (ssid->bssid_set)
7556 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7557 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7558 ssid->ssid_len);
7559 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7560
7561 if (wpa_s->global->p2p_group_formation == wpa_s)
7562 wpa_s->global->p2p_group_formation = NULL;
7563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007564 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7565 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007566
7567 ip_addr[0] = '\0';
7568 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007569 int res;
7570
7571 res = os_snprintf(ip_addr, sizeof(ip_addr),
7572 " ip_addr=%u.%u.%u.%u "
7573 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7574 ip[0], ip[1], ip[2], ip[3],
7575 ip[4], ip[5], ip[6], ip[7],
7576 ip[8], ip[9], ip[10], ip[11]);
7577 if (os_snprintf_error(sizeof(ip_addr), res))
7578 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007579 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007580 }
7581
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007582 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7583 ssid->passphrase == NULL && ssid->psk_set ?
7584 ssid->psk : NULL,
7585 ssid->passphrase, go_dev_addr, persistent,
7586 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007587
7588 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007589 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7590 ssid, go_dev_addr);
7591
Hai Shalom5f92bc92019-04-18 11:54:11 -07007592 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007593}
7594
7595
7596int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7597 u32 interval1, u32 duration2, u32 interval2)
7598{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007599 int ret;
7600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007601 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7602 return -1;
7603
7604 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7605 wpa_s->current_ssid == NULL ||
7606 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7607 return -1;
7608
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007609 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7610 wpa_s->own_addr, wpa_s->assoc_freq,
7611 duration1, interval1, duration2, interval2);
7612 if (ret == 0)
7613 wpa_s->waiting_presence_resp = 1;
7614
7615 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007616}
7617
7618
7619int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7620 unsigned int interval)
7621{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007622 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7623 return -1;
7624
7625 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7626}
7627
7628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007629static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7630{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007631 if (wpa_s->current_ssid == NULL) {
7632 /*
7633 * current_ssid can be cleared when P2P client interface gets
7634 * disconnected, so assume this interface was used as P2P
7635 * client.
7636 */
7637 return 1;
7638 }
7639 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007640 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7641}
7642
7643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7645{
7646 struct wpa_supplicant *wpa_s = eloop_ctx;
7647
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007648 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007649 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7650 "disabled");
7651 return;
7652 }
7653
Dmitry Shmidt04949592012-07-19 12:16:46 -07007654 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7655 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007656 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007657}
7658
7659
7660static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7661{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007662 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663
Dmitry Shmidt04949592012-07-19 12:16:46 -07007664 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7665 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007667 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7668 return;
7669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007670 timeout = wpa_s->conf->p2p_group_idle;
7671 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7672 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7673 timeout = P2P_MAX_CLIENT_IDLE;
7674
7675 if (timeout == 0)
7676 return;
7677
Dmitry Shmidt04949592012-07-19 12:16:46 -07007678 if (timeout < 0) {
7679 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7680 timeout = 0; /* special client mode no-timeout */
7681 else
7682 return;
7683 }
7684
7685 if (wpa_s->p2p_in_provisioning) {
7686 /*
7687 * Use the normal group formation timeout during the
7688 * provisioning phase to avoid terminating this process too
7689 * early due to group idle timeout.
7690 */
7691 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7692 "during provisioning");
7693 return;
7694 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307695
Dmitry Shmidt04949592012-07-19 12:16:46 -07007696 if (wpa_s->show_group_started) {
7697 /*
7698 * Use the normal group formation timeout between the end of
7699 * the provisioning phase and completion of 4-way handshake to
7700 * avoid terminating this process too early due to group idle
7701 * timeout.
7702 */
7703 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7704 "while waiting for initial 4-way handshake to "
7705 "complete");
7706 return;
7707 }
7708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007709 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007710 timeout);
7711 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7712 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007713}
7714
7715
Jouni Malinen2b89da82012-08-31 22:04:41 +03007716/* Returns 1 if the interface was removed */
7717int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7718 u16 reason_code, const u8 *ie, size_t ie_len,
7719 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007720{
7721 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007722 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723
Dmitry Shmidt04949592012-07-19 12:16:46 -07007724 if (!locally_generated)
7725 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7726 ie_len);
7727
7728 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7729 wpa_s->current_ssid &&
7730 wpa_s->current_ssid->p2p_group &&
7731 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7732 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7733 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007734 if (wpas_p2p_group_delete(wpa_s,
7735 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7736 > 0)
7737 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007738 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007739
7740 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007741}
7742
7743
7744void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007745 u16 reason_code, const u8 *ie, size_t ie_len,
7746 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007747{
7748 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7749 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007750
Dmitry Shmidt04949592012-07-19 12:16:46 -07007751 if (!locally_generated)
7752 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7753 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007754}
7755
7756
7757void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7758{
7759 struct p2p_data *p2p = wpa_s->global->p2p;
7760
7761 if (p2p == NULL)
7762 return;
7763
7764 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7765 return;
7766
7767 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7768 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7769
7770 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7771 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7772
7773 if (wpa_s->wps &&
7774 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7775 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7776
7777 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7778 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7779
7780 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7781 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7782 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7783 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7784 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7785 }
7786
7787 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7788 p2p_set_sec_dev_types(p2p,
7789 (void *) wpa_s->conf->sec_device_type,
7790 wpa_s->conf->num_sec_device_types);
7791
7792 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7793 int i;
7794 p2p_remove_wps_vendor_extensions(p2p);
7795 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7796 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7797 continue;
7798 p2p_add_wps_vendor_extension(
7799 p2p, wpa_s->conf->wps_vendor_ext[i]);
7800 }
7801 }
7802
7803 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7804 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7805 char country[3];
7806 country[0] = wpa_s->conf->country[0];
7807 country[1] = wpa_s->conf->country[1];
7808 country[2] = 0x04;
7809 p2p_set_country(p2p, country);
7810 }
7811
7812 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7813 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7814 wpa_s->conf->p2p_ssid_postfix ?
7815 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7816 0);
7817 }
7818
7819 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7820 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007821
7822 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7823 u8 reg_class, channel;
7824 int ret;
7825 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007826 u8 channel_forced;
7827
Jouni Malinen75ecf522011-06-27 15:19:46 -07007828 if (wpa_s->conf->p2p_listen_reg_class &&
7829 wpa_s->conf->p2p_listen_channel) {
7830 reg_class = wpa_s->conf->p2p_listen_reg_class;
7831 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007832 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007833 } else {
7834 reg_class = 81;
7835 /*
7836 * Pick one of the social channels randomly as the
7837 * listen channel.
7838 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007839 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7840 channel = 1;
7841 else
7842 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007843 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007844 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007845 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7846 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007847 if (ret)
7848 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7849 "failed: %d", ret);
7850 }
7851 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7852 u8 op_reg_class, op_channel, cfg_op_channel;
7853 int ret = 0;
7854 unsigned int r;
7855 if (wpa_s->conf->p2p_oper_reg_class &&
7856 wpa_s->conf->p2p_oper_channel) {
7857 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7858 op_channel = wpa_s->conf->p2p_oper_channel;
7859 cfg_op_channel = 1;
7860 } else {
7861 op_reg_class = 81;
7862 /*
7863 * Use random operation channel from (1, 6, 11)
7864 *if no other preference is indicated.
7865 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007866 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7867 op_channel = 1;
7868 else
7869 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007870 cfg_op_channel = 0;
7871 }
7872 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7873 cfg_op_channel);
7874 if (ret)
7875 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7876 "failed: %d", ret);
7877 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007878
7879 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7880 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7881 wpa_s->conf->p2p_pref_chan) < 0) {
7882 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7883 "update failed");
7884 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007885
7886 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7887 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7888 "update failed");
7889 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007890 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007891
7892 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7893 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007894}
7895
7896
7897int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7898 int duration)
7899{
7900 if (!wpa_s->ap_iface)
7901 return -1;
7902 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7903 duration);
7904}
7905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007906
7907int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7908{
7909 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7910 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911
7912 wpa_s->global->cross_connection = enabled;
7913 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7914
7915 if (!enabled) {
7916 struct wpa_supplicant *iface;
7917
7918 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7919 {
7920 if (iface->cross_connect_enabled == 0)
7921 continue;
7922
7923 iface->cross_connect_enabled = 0;
7924 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007925 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007926 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7927 iface->ifname,
7928 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007929 }
7930 }
7931
7932 return 0;
7933}
7934
7935
7936static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7937{
7938 struct wpa_supplicant *iface;
7939
7940 if (!uplink->global->cross_connection)
7941 return;
7942
7943 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7944 if (!iface->cross_connect_enabled)
7945 continue;
7946 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7947 0)
7948 continue;
7949 if (iface->ap_iface == NULL)
7950 continue;
7951 if (iface->cross_connect_in_use)
7952 continue;
7953
7954 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007955 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007956 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7957 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007958 }
7959}
7960
7961
7962static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7963{
7964 struct wpa_supplicant *iface;
7965
7966 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7967 if (!iface->cross_connect_enabled)
7968 continue;
7969 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7970 0)
7971 continue;
7972 if (!iface->cross_connect_in_use)
7973 continue;
7974
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007975 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007976 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7977 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007978 iface->cross_connect_in_use = 0;
7979 }
7980}
7981
7982
7983void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7984{
7985 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7986 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7987 wpa_s->cross_connect_disallowed)
7988 wpas_p2p_disable_cross_connect(wpa_s);
7989 else
7990 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007991 if (!wpa_s->ap_iface &&
7992 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7993 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007994}
7995
7996
7997void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7998{
7999 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008000 if (!wpa_s->ap_iface &&
8001 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8002 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003 wpas_p2p_set_group_idle_timeout(wpa_s);
8004}
8005
8006
8007static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8008{
8009 struct wpa_supplicant *iface;
8010
8011 if (!wpa_s->global->cross_connection)
8012 return;
8013
8014 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8015 if (iface == wpa_s)
8016 continue;
8017 if (iface->drv_flags &
8018 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8019 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008020 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8021 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022 continue;
8023
8024 wpa_s->cross_connect_enabled = 1;
8025 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8026 sizeof(wpa_s->cross_connect_uplink));
8027 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8028 "%s to %s whenever uplink is available",
8029 wpa_s->ifname, wpa_s->cross_connect_uplink);
8030
8031 if (iface->ap_iface || iface->current_ssid == NULL ||
8032 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8033 iface->cross_connect_disallowed ||
8034 iface->wpa_state != WPA_COMPLETED)
8035 break;
8036
8037 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008038 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008039 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8040 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008041 break;
8042 }
8043}
8044
8045
8046int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8047{
8048 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8049 !wpa_s->p2p_in_provisioning)
8050 return 0; /* not P2P client operation */
8051
8052 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8053 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008054 if (wpa_s != wpa_s->p2pdev)
8055 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008056 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008057 return 1;
8058}
8059
8060
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008061void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8062{
8063 struct wpa_supplicant *wpa_s = eloop_ctx;
8064 wpas_p2p_notif_pbc_overlap(wpa_s);
8065}
8066
8067
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008068void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8069 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008070{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008071 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008072 struct wpa_used_freq_data *freqs = NULL;
8073 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008074
8075 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8076 return;
8077
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008078 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8079 if (!freqs)
8080 return;
8081
8082 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008084 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008085 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008086 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8087 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008088 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8089 "channel list");
8090 return;
8091 }
8092
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008093 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008094
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008095 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008096
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008097 /*
8098 * The used frequencies map changed, so it is possible that a GO is
8099 * using a channel that is no longer valid for P2P use. It is also
8100 * possible that due to policy consideration, it would be preferable to
8101 * move it to a frequency already used by other station interfaces.
8102 */
8103 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8104
8105 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008106}
8107
8108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008109static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8110 struct wpa_scan_results *scan_res)
8111{
8112 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8113}
8114
8115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008116int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8117{
8118 struct wpa_global *global = wpa_s->global;
8119 int found = 0;
8120 const u8 *peer;
8121
8122 if (global->p2p == NULL)
8123 return -1;
8124
8125 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8126
8127 if (wpa_s->pending_interface_name[0] &&
8128 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8129 found = 1;
8130
8131 peer = p2p_get_go_neg_peer(global->p2p);
8132 if (peer) {
8133 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8134 MACSTR, MAC2STR(peer));
8135 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008136 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008137 }
8138
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008139 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8140 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8141 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8142 found = 1;
8143 }
8144
8145 if (wpa_s->pending_pd_before_join) {
8146 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8147 wpa_s->pending_pd_before_join = 0;
8148 found = 1;
8149 }
8150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008151 wpas_p2p_stop_find(wpa_s);
8152
8153 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8154 if (wpa_s == global->p2p_group_formation &&
8155 (wpa_s->p2p_in_provisioning ||
8156 wpa_s->parent->pending_interface_type ==
8157 WPA_IF_P2P_CLIENT)) {
8158 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8159 "formation found - cancelling",
8160 wpa_s->ifname);
8161 found = 1;
8162 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008163 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008164 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008165 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008166 break;
8167 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008168 wpas_p2p_group_delete(wpa_s,
8169 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008170 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008171 } else if (wpa_s->p2p_in_invitation) {
8172 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8173 wpa_s->ifname);
8174 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008175 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008176 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008177 }
8178 }
8179
8180 if (!found) {
8181 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8182 return -1;
8183 }
8184
8185 return 0;
8186}
8187
8188
8189void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8190{
8191 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8192 return;
8193
8194 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8195 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008196 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008197}
8198
8199
8200void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8201 int freq_24, int freq_5, int freq_overall)
8202{
8203 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008204 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008205 return;
8206 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8207}
8208
8209
8210int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8211{
8212 u8 peer[ETH_ALEN];
8213 struct p2p_data *p2p = wpa_s->global->p2p;
8214
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008215 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008216 return -1;
8217
8218 if (hwaddr_aton(addr, peer))
8219 return -1;
8220
8221 return p2p_unauthorize(p2p, peer);
8222}
8223
8224
8225/**
8226 * wpas_p2p_disconnect - Disconnect from a P2P Group
8227 * @wpa_s: Pointer to wpa_supplicant data
8228 * Returns: 0 on success, -1 on failure
8229 *
8230 * This can be used to disconnect from a group in which the local end is a P2P
8231 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8232 * virtual network interface was created for this group, that interface will be
8233 * removed. Otherwise, only the configured P2P group network will be removed
8234 * from the interface.
8235 */
8236int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8237{
8238
8239 if (wpa_s == NULL)
8240 return -1;
8241
Jouni Malinen2b89da82012-08-31 22:04:41 +03008242 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8243 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008244}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008245
8246
8247int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8248{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008249 int ret;
8250
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008251 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8252 return 0;
8253
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008254 ret = p2p_in_progress(wpa_s->global->p2p);
8255 if (ret == 0) {
8256 /*
8257 * Check whether there is an ongoing WPS provisioning step (or
8258 * other parts of group formation) on another interface since
8259 * p2p_in_progress() does not report this to avoid issues for
8260 * scans during such provisioning step.
8261 */
8262 if (wpa_s->global->p2p_group_formation &&
8263 wpa_s->global->p2p_group_formation != wpa_s) {
8264 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8265 "in group formation",
8266 wpa_s->global->p2p_group_formation->ifname);
8267 ret = 1;
8268 }
8269 }
8270
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008271 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008272 struct os_reltime now;
8273 os_get_reltime(&now);
8274 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8275 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008276 /* Wait for the first client has expired */
8277 wpa_s->global->p2p_go_wait_client.sec = 0;
8278 } else {
8279 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8280 ret = 1;
8281 }
8282 }
8283
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008284 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008285}
8286
8287
8288void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8289 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008290{
8291 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8292 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008293 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008294 /**
8295 * Remove the network by scheduling the group formation
8296 * timeout to happen immediately. The teardown code
8297 * needs to be scheduled to run asynch later so that we
8298 * don't delete data from under ourselves unexpectedly.
8299 * Calling wpas_p2p_group_formation_timeout directly
8300 * causes a series of crashes in WPS failure scenarios.
8301 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008302 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8303 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008304 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008305 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008306 }
8307}
8308
8309
8310struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008311 const u8 *addr, const u8 *ssid,
8312 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008313{
8314 struct wpa_ssid *s;
8315 size_t i;
8316
8317 for (s = wpa_s->conf->ssid; s; s = s->next) {
8318 if (s->disabled != 2)
8319 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008320 if (ssid &&
8321 (ssid_len != s->ssid_len ||
8322 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8323 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008324 if (addr == NULL) {
8325 if (s->mode == WPAS_MODE_P2P_GO)
8326 return s;
8327 continue;
8328 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008329 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8330 return s; /* peer is GO in the persistent group */
8331 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8332 continue;
8333 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008334 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008335 addr, ETH_ALEN) == 0)
8336 return s; /* peer is P2P client in persistent
8337 * group */
8338 }
8339 }
8340
8341 return NULL;
8342}
8343
8344
8345void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8346 const u8 *addr)
8347{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008348 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008349 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008350 /*
8351 * This can happen if WPS provisioning step is not terminated
8352 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8353 * peer was able to connect, there is no need to time out group
8354 * formation after this, though. In addition, this is used with
8355 * the initial connection wait on the GO as a separate formation
8356 * timeout and as such, expected to be hit after the initial WPS
8357 * provisioning step.
8358 */
8359 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008360
8361 if (!wpa_s->p2p_go_group_formation_completed &&
8362 !wpa_s->group_formation_reported) {
8363 /*
8364 * GO has not yet notified group formation success since
8365 * the WPS step was not completed cleanly. Do that
8366 * notification now since the P2P Client was able to
8367 * connect and as such, must have received the
8368 * credential from the WPS step.
8369 */
8370 if (wpa_s->global->p2p)
8371 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008372 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008373 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008374 }
8375 if (!wpa_s->p2p_go_group_formation_completed) {
8376 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8377 wpa_s->p2p_go_group_formation_completed = 1;
8378 wpa_s->global->p2p_group_formation = NULL;
8379 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008380 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008381 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008382 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008383 if (addr == NULL)
8384 return;
8385 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8386}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008387
Dmitry Shmidt04949592012-07-19 12:16:46 -07008388
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008389static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8390 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008391{
8392 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008393 int ret = 0;
8394
Dmitry Shmidt04949592012-07-19 12:16:46 -07008395 if (wpa_s->global->p2p_group_formation)
8396 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008397 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008398 offchannel_send_action_done(wpa_s);
8399 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008400 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008401 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8402 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8403 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8404 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008405 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008406 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008407 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008408 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008409 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008410 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008411 wpa_s->p2p_go_he,
8412 wpa_s->p2p_go_edmg,
8413 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008414 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008415}
8416
8417
8418int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8419{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008420 int res;
8421
Dmitry Shmidt04949592012-07-19 12:16:46 -07008422 if (!wpa_s->p2p_fallback_to_go_neg ||
8423 wpa_s->p2p_in_provisioning <= 5)
8424 return 0;
8425
8426 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8427 return 0; /* peer operating as a GO */
8428
8429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8430 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008431 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008432 "reason=GO-not-found");
8433 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008434
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008435 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008436}
8437
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008438
8439unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8440{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008441 struct wpa_supplicant *ifs;
8442
8443 if (wpa_s->wpa_state > WPA_SCANNING) {
8444 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8445 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008446 wpa_s->conf->p2p_search_delay);
8447 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008448 }
8449
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008450 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8451 radio_list) {
8452 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008453 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8454 "delay due to concurrent operation on "
8455 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008456 wpa_s->conf->p2p_search_delay,
8457 ifs->ifname);
8458 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008459 }
8460 }
8461
8462 return 0;
8463}
8464
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008465
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008466static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8467 struct wpa_ssid *s, const u8 *addr,
8468 int iface_addr)
8469{
8470 struct psk_list_entry *psk, *tmp;
8471 int changed = 0;
8472
8473 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8474 list) {
8475 if ((iface_addr && !psk->p2p &&
8476 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8477 (!iface_addr && psk->p2p &&
8478 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8479 wpa_dbg(wpa_s, MSG_DEBUG,
8480 "P2P: Remove persistent group PSK list entry for "
8481 MACSTR " p2p=%u",
8482 MAC2STR(psk->addr), psk->p2p);
8483 dl_list_del(&psk->list);
8484 os_free(psk);
8485 changed++;
8486 }
8487 }
8488
8489 return changed;
8490}
8491
8492
8493void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8494 const u8 *p2p_dev_addr,
8495 const u8 *psk, size_t psk_len)
8496{
8497 struct wpa_ssid *ssid = wpa_s->current_ssid;
8498 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008499 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008500
8501 if (psk_len != sizeof(p->psk))
8502 return;
8503
8504 if (p2p_dev_addr) {
8505 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8506 " p2p_dev_addr=" MACSTR,
8507 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8508 if (is_zero_ether_addr(p2p_dev_addr))
8509 p2p_dev_addr = NULL;
8510 } else {
8511 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8512 MAC2STR(mac_addr));
8513 }
8514
8515 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8516 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8517 /* To be added to persistent group once created */
8518 if (wpa_s->global->add_psk == NULL) {
8519 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8520 if (wpa_s->global->add_psk == NULL)
8521 return;
8522 }
8523 p = wpa_s->global->add_psk;
8524 if (p2p_dev_addr) {
8525 p->p2p = 1;
8526 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8527 } else {
8528 p->p2p = 0;
8529 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8530 }
8531 os_memcpy(p->psk, psk, psk_len);
8532 return;
8533 }
8534
8535 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8536 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8537 return;
8538 }
8539
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008540 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008541 ssid->ssid_len);
8542 if (!persistent) {
8543 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8544 return;
8545 }
8546
8547 p = os_zalloc(sizeof(*p));
8548 if (p == NULL)
8549 return;
8550 if (p2p_dev_addr) {
8551 p->p2p = 1;
8552 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8553 } else {
8554 p->p2p = 0;
8555 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8556 }
8557 os_memcpy(p->psk, psk, psk_len);
8558
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008559 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8560 (last = dl_list_last(&persistent->psk_list,
8561 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8563 MACSTR " (p2p=%u) to make room for a new one",
8564 MAC2STR(last->addr), last->p2p);
8565 dl_list_del(&last->list);
8566 os_free(last);
8567 }
8568
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008569 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008570 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8571 p2p_dev_addr == NULL);
8572 if (p2p_dev_addr) {
8573 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8574 MACSTR, MAC2STR(p2p_dev_addr));
8575 } else {
8576 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8577 MAC2STR(mac_addr));
8578 }
8579 dl_list_add(&persistent->psk_list, &p->list);
8580
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008581 if (wpa_s->p2pdev->conf->update_config &&
8582 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008583 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008584}
8585
8586
8587static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8588 struct wpa_ssid *s, const u8 *addr,
8589 int iface_addr)
8590{
8591 int res;
8592
8593 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008594 if (res > 0 && wpa_s->conf->update_config &&
8595 wpa_config_write(wpa_s->confname, wpa_s->conf))
8596 wpa_dbg(wpa_s, MSG_DEBUG,
8597 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008598}
8599
8600
8601static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8602 const u8 *peer, int iface_addr)
8603{
8604 struct hostapd_data *hapd;
8605 struct hostapd_wpa_psk *psk, *prev, *rem;
8606 struct sta_info *sta;
8607
8608 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8609 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8610 return;
8611
8612 /* Remove per-station PSK entry */
8613 hapd = wpa_s->ap_iface->bss[0];
8614 prev = NULL;
8615 psk = hapd->conf->ssid.wpa_psk;
8616 while (psk) {
8617 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8618 (!iface_addr &&
8619 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8620 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8621 MACSTR " iface_addr=%d",
8622 MAC2STR(peer), iface_addr);
8623 if (prev)
8624 prev->next = psk->next;
8625 else
8626 hapd->conf->ssid.wpa_psk = psk->next;
8627 rem = psk;
8628 psk = psk->next;
8629 os_free(rem);
8630 } else {
8631 prev = psk;
8632 psk = psk->next;
8633 }
8634 }
8635
8636 /* Disconnect from group */
8637 if (iface_addr)
8638 sta = ap_get_sta(hapd, peer);
8639 else
8640 sta = ap_get_sta_p2p(hapd, peer);
8641 if (sta) {
8642 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8643 " (iface_addr=%d) from group",
8644 MAC2STR(peer), iface_addr);
8645 hostapd_drv_sta_deauth(hapd, sta->addr,
8646 WLAN_REASON_DEAUTH_LEAVING);
8647 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8648 }
8649}
8650
8651
8652void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8653 int iface_addr)
8654{
8655 struct wpa_ssid *s;
8656 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008657 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008658
8659 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8660
8661 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008662 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008663 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8664 continue;
8665 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008666 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8667 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008668 }
8669
8670 /* Remove from any operating group */
8671 for (w = wpa_s->global->ifaces; w; w = w->next)
8672 wpas_p2p_remove_client_go(w, peer, iface_addr);
8673}
8674
8675
8676static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8677{
8678 struct wpa_supplicant *wpa_s = eloop_ctx;
8679 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8680}
8681
8682
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008683static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8684{
8685 struct wpa_supplicant *wpa_s = eloop_ctx;
8686
8687 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8688 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8689}
8690
8691
8692int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8693 struct wpa_ssid *ssid)
8694{
8695 struct wpa_supplicant *iface;
8696
8697 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8698 if (!iface->current_ssid ||
8699 iface->current_ssid->frequency == freq ||
8700 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8701 !iface->current_ssid->p2p_group))
8702 continue;
8703
8704 /* Remove the connection with least priority */
8705 if (!wpas_is_p2p_prioritized(iface)) {
8706 /* STA connection has priority over existing
8707 * P2P connection, so remove the interface. */
8708 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8709 eloop_register_timeout(0, 0,
8710 wpas_p2p_group_freq_conflict,
8711 iface, NULL);
8712 /* If connection in progress is P2P connection, do not
8713 * proceed for the connection. */
8714 if (wpa_s == iface)
8715 return -1;
8716 else
8717 return 0;
8718 } else {
8719 /* P2P connection has priority, disable the STA network
8720 */
8721 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8722 ssid);
8723 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8724 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8725 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8726 ETH_ALEN);
8727 /* If P2P connection is in progress, continue
8728 * connecting...*/
8729 if (wpa_s == iface)
8730 return 0;
8731 else
8732 return -1;
8733 }
8734 }
8735
8736 return 0;
8737}
8738
8739
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008740int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8741{
8742 struct wpa_ssid *ssid = wpa_s->current_ssid;
8743
8744 if (ssid == NULL || !ssid->p2p_group)
8745 return 0;
8746
8747 if (wpa_s->p2p_last_4way_hs_fail &&
8748 wpa_s->p2p_last_4way_hs_fail == ssid) {
8749 u8 go_dev_addr[ETH_ALEN];
8750 struct wpa_ssid *persistent;
8751
8752 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8753 ssid->ssid,
8754 ssid->ssid_len) <= 0) {
8755 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8756 goto disconnect;
8757 }
8758
8759 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8760 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008761 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008762 ssid->ssid,
8763 ssid->ssid_len);
8764 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8765 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8766 goto disconnect;
8767 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008768 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008769 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8770 persistent->id);
8771 disconnect:
8772 wpa_s->p2p_last_4way_hs_fail = NULL;
8773 /*
8774 * Remove the group from a timeout to avoid issues with caller
8775 * continuing to use the interface if this is on a P2P group
8776 * interface.
8777 */
8778 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8779 wpa_s, NULL);
8780 return 1;
8781 }
8782
8783 wpa_s->p2p_last_4way_hs_fail = ssid;
8784 return 0;
8785}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008786
8787
8788#ifdef CONFIG_WPS_NFC
8789
8790static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8791 struct wpabuf *p2p)
8792{
8793 struct wpabuf *ret;
8794 size_t wsc_len;
8795
8796 if (p2p == NULL) {
8797 wpabuf_free(wsc);
8798 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8799 return NULL;
8800 }
8801
8802 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8803 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8804 if (ret == NULL) {
8805 wpabuf_free(wsc);
8806 wpabuf_free(p2p);
8807 return NULL;
8808 }
8809
8810 wpabuf_put_be16(ret, wsc_len);
8811 if (wsc)
8812 wpabuf_put_buf(ret, wsc);
8813 wpabuf_put_be16(ret, wpabuf_len(p2p));
8814 wpabuf_put_buf(ret, p2p);
8815
8816 wpabuf_free(wsc);
8817 wpabuf_free(p2p);
8818 wpa_hexdump_buf(MSG_DEBUG,
8819 "P2P: Generated NFC connection handover message", ret);
8820
8821 if (ndef && ret) {
8822 struct wpabuf *tmp;
8823 tmp = ndef_build_p2p(ret);
8824 wpabuf_free(ret);
8825 if (tmp == NULL) {
8826 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8827 return NULL;
8828 }
8829 ret = tmp;
8830 }
8831
8832 return ret;
8833}
8834
8835
8836static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8837 struct wpa_ssid **ssid, u8 *go_dev_addr)
8838{
8839 struct wpa_supplicant *iface;
8840
8841 if (go_dev_addr)
8842 os_memset(go_dev_addr, 0, ETH_ALEN);
8843 if (ssid)
8844 *ssid = NULL;
8845 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8846 if (iface->wpa_state < WPA_ASSOCIATING ||
8847 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8848 !iface->current_ssid->p2p_group ||
8849 iface->current_ssid->mode != WPAS_MODE_INFRA)
8850 continue;
8851 if (ssid)
8852 *ssid = iface->current_ssid;
8853 if (go_dev_addr)
8854 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8855 return iface->assoc_freq;
8856 }
8857 return 0;
8858}
8859
8860
8861struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8862 int ndef)
8863{
8864 struct wpabuf *wsc, *p2p;
8865 struct wpa_ssid *ssid;
8866 u8 go_dev_addr[ETH_ALEN];
8867 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8868
8869 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8870 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8871 return NULL;
8872 }
8873
8874 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8875 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8876 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8877 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8878 return NULL;
8879 }
8880
8881 if (cli_freq == 0) {
8882 wsc = wps_build_nfc_handover_req_p2p(
8883 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8884 } else
8885 wsc = NULL;
8886 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8887 go_dev_addr, ssid ? ssid->ssid : NULL,
8888 ssid ? ssid->ssid_len : 0);
8889
8890 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8891}
8892
8893
8894struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8895 int ndef, int tag)
8896{
8897 struct wpabuf *wsc, *p2p;
8898 struct wpa_ssid *ssid;
8899 u8 go_dev_addr[ETH_ALEN];
8900 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8901
8902 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8903 return NULL;
8904
8905 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8906 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8907 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8908 return NULL;
8909
8910 if (cli_freq == 0) {
8911 wsc = wps_build_nfc_handover_sel_p2p(
8912 wpa_s->parent->wps,
8913 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8914 DEV_PW_NFC_CONNECTION_HANDOVER,
8915 wpa_s->conf->wps_nfc_dh_pubkey,
8916 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8917 } else
8918 wsc = NULL;
8919 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8920 go_dev_addr, ssid ? ssid->ssid : NULL,
8921 ssid ? ssid->ssid_len : 0);
8922
8923 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8924}
8925
8926
8927static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8928 struct p2p_nfc_params *params)
8929{
8930 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8931 "connection handover (freq=%d)",
8932 params->go_freq);
8933
8934 if (params->go_freq && params->go_ssid_len) {
8935 wpa_s->p2p_wps_method = WPS_NFC;
8936 wpa_s->pending_join_wps_method = WPS_NFC;
8937 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8938 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8939 ETH_ALEN);
8940 return wpas_p2p_join_start(wpa_s, params->go_freq,
8941 params->go_ssid,
8942 params->go_ssid_len);
8943 }
8944
8945 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8946 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008947 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008948 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008949 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008950 params->go_ssid_len ? params->go_ssid : NULL,
8951 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008952}
8953
8954
8955static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8956 struct p2p_nfc_params *params, int tag)
8957{
8958 int res, persistent;
8959 struct wpa_ssid *ssid;
8960
8961 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8962 "connection handover");
8963 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8964 ssid = wpa_s->current_ssid;
8965 if (ssid == NULL)
8966 continue;
8967 if (ssid->mode != WPAS_MODE_P2P_GO)
8968 continue;
8969 if (wpa_s->ap_iface == NULL)
8970 continue;
8971 break;
8972 }
8973 if (wpa_s == NULL) {
8974 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8975 return -1;
8976 }
8977
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008978 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008979 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008980 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008981 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8982 return -1;
8983 }
8984 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008985 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8986 wpa_s->p2pdev->p2p_oob_dev_pw,
8987 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8988 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008989 if (res)
8990 return res;
8991
8992 if (!tag) {
8993 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8994 return 0;
8995 }
8996
8997 if (!params->peer ||
8998 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8999 return 0;
9000
9001 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9002 " to join", MAC2STR(params->peer->p2p_device_addr));
9003
9004 wpa_s->global->p2p_invite_group = wpa_s;
9005 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009006 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009007 params->peer->p2p_device_addr,
9008 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009009 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009010
9011 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9012 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9013 ssid->ssid, ssid->ssid_len, ssid->frequency,
9014 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009015 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009016}
9017
9018
9019static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9020 struct p2p_nfc_params *params,
9021 int forced_freq)
9022{
9023 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9024 "connection handover");
9025 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9026 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009027 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009028 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009029 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9030 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009031}
9032
9033
9034static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9035 struct p2p_nfc_params *params,
9036 int forced_freq)
9037{
9038 int res;
9039
9040 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9041 "connection handover");
9042 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9043 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009044 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009045 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009046 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9047 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009048 if (res)
9049 return res;
9050
9051 res = wpas_p2p_listen(wpa_s, 60);
9052 if (res) {
9053 p2p_unauthorize(wpa_s->global->p2p,
9054 params->peer->p2p_device_addr);
9055 }
9056
9057 return res;
9058}
9059
9060
9061static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9062 const struct wpabuf *data,
9063 int sel, int tag, int forced_freq)
9064{
9065 const u8 *pos, *end;
9066 u16 len, id;
9067 struct p2p_nfc_params params;
9068 int res;
9069
9070 os_memset(&params, 0, sizeof(params));
9071 params.sel = sel;
9072
9073 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9074
9075 pos = wpabuf_head(data);
9076 end = pos + wpabuf_len(data);
9077
9078 if (end - pos < 2) {
9079 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
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 WSC "
9087 "attributes");
9088 return -1;
9089 }
9090 params.wsc_attr = pos;
9091 params.wsc_len = len;
9092 pos += len;
9093
9094 if (end - pos < 2) {
9095 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9096 "attributes");
9097 return -1;
9098 }
9099 len = WPA_GET_BE16(pos);
9100 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009101 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009102 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9103 "attributes");
9104 return -1;
9105 }
9106 params.p2p_attr = pos;
9107 params.p2p_len = len;
9108 pos += len;
9109
9110 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9111 params.wsc_attr, params.wsc_len);
9112 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9113 params.p2p_attr, params.p2p_len);
9114 if (pos < end) {
9115 wpa_hexdump(MSG_DEBUG,
9116 "P2P: Ignored extra data after P2P attributes",
9117 pos, end - pos);
9118 }
9119
9120 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9121 if (res)
9122 return res;
9123
9124 if (params.next_step == NO_ACTION)
9125 return 0;
9126
9127 if (params.next_step == BOTH_GO) {
9128 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9129 MAC2STR(params.peer->p2p_device_addr));
9130 return 0;
9131 }
9132
9133 if (params.next_step == PEER_CLIENT) {
9134 if (!is_zero_ether_addr(params.go_dev_addr)) {
9135 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9136 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9137 " ssid=\"%s\"",
9138 MAC2STR(params.peer->p2p_device_addr),
9139 params.go_freq,
9140 MAC2STR(params.go_dev_addr),
9141 wpa_ssid_txt(params.go_ssid,
9142 params.go_ssid_len));
9143 } else {
9144 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9145 "peer=" MACSTR " freq=%d",
9146 MAC2STR(params.peer->p2p_device_addr),
9147 params.go_freq);
9148 }
9149 return 0;
9150 }
9151
9152 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9153 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9154 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9155 return 0;
9156 }
9157
9158 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9159 wpa_s->p2p_oob_dev_pw = NULL;
9160
9161 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9162 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9163 "received");
9164 return -1;
9165 }
9166
9167 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9168 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9169 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9170 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9171 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9172 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9173 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9174
9175 if (tag) {
9176 if (id < 0x10) {
9177 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9178 "peer OOB Device Password Id %u", id);
9179 return -1;
9180 }
9181 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9182 "Device Password Id %u", id);
9183 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9184 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9185 params.oob_dev_pw_len -
9186 WPS_OOB_PUBKEY_HASH_LEN - 2);
9187 wpa_s->p2p_oob_dev_pw_id = id;
9188 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9189 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9190 params.oob_dev_pw_len -
9191 WPS_OOB_PUBKEY_HASH_LEN - 2);
9192 if (wpa_s->p2p_oob_dev_pw == NULL)
9193 return -1;
9194
9195 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9196 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9197 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9198 return -1;
9199 } else {
9200 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9201 "without Device Password");
9202 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9203 }
9204
9205 switch (params.next_step) {
9206 case NO_ACTION:
9207 case BOTH_GO:
9208 case PEER_CLIENT:
9209 /* already covered above */
9210 return 0;
9211 case JOIN_GROUP:
9212 return wpas_p2p_nfc_join_group(wpa_s, &params);
9213 case AUTH_JOIN:
9214 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9215 case INIT_GO_NEG:
9216 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9217 case RESP_GO_NEG:
9218 /* TODO: use own OOB Dev Pw */
9219 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9220 }
9221
9222 return -1;
9223}
9224
9225
9226int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9227 const struct wpabuf *data, int forced_freq)
9228{
9229 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9230 return -1;
9231
9232 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9233}
9234
9235
9236int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9237 const struct wpabuf *req,
9238 const struct wpabuf *sel, int forced_freq)
9239{
9240 struct wpabuf *tmp;
9241 int ret;
9242
9243 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9244 return -1;
9245
9246 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9247
9248 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9249 wpabuf_head(req), wpabuf_len(req));
9250 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9251 wpabuf_head(sel), wpabuf_len(sel));
9252 if (forced_freq)
9253 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9254 tmp = ndef_parse_p2p(init ? sel : req);
9255 if (tmp == NULL) {
9256 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9257 return -1;
9258 }
9259
9260 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9261 forced_freq);
9262 wpabuf_free(tmp);
9263
9264 return ret;
9265}
9266
9267
9268int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9269{
9270 const u8 *if_addr;
9271 int go_intent = wpa_s->conf->p2p_go_intent;
9272 struct wpa_supplicant *iface;
9273
9274 if (wpa_s->global->p2p == NULL)
9275 return -1;
9276
9277 if (!enabled) {
9278 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9279 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9280 {
9281 if (!iface->ap_iface)
9282 continue;
9283 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9284 }
9285 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9286 0, NULL);
9287 if (wpa_s->p2p_nfc_tag_enabled)
9288 wpas_p2p_remove_pending_group_interface(wpa_s);
9289 wpa_s->p2p_nfc_tag_enabled = 0;
9290 return 0;
9291 }
9292
9293 if (wpa_s->global->p2p_disabled)
9294 return -1;
9295
9296 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9297 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9298 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9299 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9300 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9301 "to allow static handover cases");
9302 return -1;
9303 }
9304
9305 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9306
9307 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9308 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9309 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9310 if (wpa_s->p2p_oob_dev_pw == NULL)
9311 return -1;
9312 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9313
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009314 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9315 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9316 /*
9317 * P2P Group Interface present and the command came on group
9318 * interface, so enable the token for the current interface.
9319 */
9320 wpa_s->create_p2p_iface = 0;
9321 } else {
9322 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9323 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009324
9325 if (wpa_s->create_p2p_iface) {
9326 enum wpa_driver_if_type iftype;
9327 /* Prepare to add a new interface for the group */
9328 iftype = WPA_IF_P2P_GROUP;
9329 if (go_intent == 15)
9330 iftype = WPA_IF_P2P_GO;
9331 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9332 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9333 "interface for the group");
9334 return -1;
9335 }
9336
9337 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009338 } else if (wpa_s->p2p_mgmt)
9339 if_addr = wpa_s->parent->own_addr;
9340 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009341 if_addr = wpa_s->own_addr;
9342
9343 wpa_s->p2p_nfc_tag_enabled = enabled;
9344
9345 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9346 struct hostapd_data *hapd;
9347 if (iface->ap_iface == NULL)
9348 continue;
9349 hapd = iface->ap_iface->bss[0];
9350 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9351 hapd->conf->wps_nfc_dh_pubkey =
9352 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9353 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9354 hapd->conf->wps_nfc_dh_privkey =
9355 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9356 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9357 hapd->conf->wps_nfc_dev_pw =
9358 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9359 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9360
9361 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9362 wpa_dbg(iface, MSG_DEBUG,
9363 "P2P: Failed to enable NFC Tag for GO");
9364 }
9365 }
9366 p2p_set_authorized_oob_dev_pw_id(
9367 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9368 if_addr);
9369
9370 return 0;
9371}
9372
9373#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009374
9375
9376static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9377 struct wpa_used_freq_data *freqs,
9378 unsigned int num)
9379{
9380 u8 curr_chan, cand, chan;
9381 unsigned int i;
9382
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009383 /*
9384 * If possible, optimize the Listen channel to be a channel that is
9385 * already used by one of the other interfaces.
9386 */
9387 if (!wpa_s->conf->p2p_optimize_listen_chan)
9388 return;
9389
9390 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9391 return;
9392
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009393 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9394 for (i = 0, cand = 0; i < num; i++) {
9395 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9396 if (curr_chan == chan) {
9397 cand = 0;
9398 break;
9399 }
9400
9401 if (chan == 1 || chan == 6 || chan == 11)
9402 cand = chan;
9403 }
9404
9405 if (cand) {
9406 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009407 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009408 cand);
9409 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9410 }
9411}
9412
9413
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009414static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009415{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009416 struct hostapd_config *conf;
9417 struct p2p_go_neg_results params;
9418 struct csa_settings csa_settings;
9419 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9420 int old_freq = current_ssid->frequency;
9421 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009422
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009423 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9424 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9425 return -1;
9426 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009427
9428 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009429 * TODO: This function may not always work correctly. For example,
9430 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009431 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009432 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9433 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009434 wpa_dbg(wpa_s, MSG_DEBUG,
9435 "P2P CSA: Failed to select new frequency for GO");
9436 return -1;
9437 }
9438
9439 if (current_ssid->frequency == params.freq) {
9440 wpa_dbg(wpa_s, MSG_DEBUG,
9441 "P2P CSA: Selected same frequency - not moving GO");
9442 return 0;
9443 }
9444
9445 conf = hostapd_config_defaults();
9446 if (!conf) {
9447 wpa_dbg(wpa_s, MSG_DEBUG,
9448 "P2P CSA: Failed to allocate default config");
9449 return -1;
9450 }
9451
9452 current_ssid->frequency = params.freq;
9453 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9454 wpa_dbg(wpa_s, MSG_DEBUG,
9455 "P2P CSA: Failed to create new GO config");
9456 ret = -1;
9457 goto out;
9458 }
9459
9460 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9461 wpa_dbg(wpa_s, MSG_DEBUG,
9462 "P2P CSA: CSA to a different band is not supported");
9463 ret = -1;
9464 goto out;
9465 }
9466
9467 os_memset(&csa_settings, 0, sizeof(csa_settings));
9468 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9469 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9470 csa_settings.freq_params.freq = params.freq;
9471 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9472 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9473 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9474
9475 if (conf->ieee80211ac) {
9476 int freq1 = 0, freq2 = 0;
9477 u8 chan, opclass;
9478
9479 if (ieee80211_freq_to_channel_ext(params.freq,
9480 conf->secondary_channel,
9481 conf->vht_oper_chwidth,
9482 &opclass, &chan) ==
9483 NUM_HOSTAPD_MODES) {
9484 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9485 ret = -1;
9486 goto out;
9487 }
9488
9489 if (conf->vht_oper_centr_freq_seg0_idx)
9490 freq1 = ieee80211_chan_to_freq(
9491 NULL, opclass,
9492 conf->vht_oper_centr_freq_seg0_idx);
9493
9494 if (conf->vht_oper_centr_freq_seg1_idx)
9495 freq2 = ieee80211_chan_to_freq(
9496 NULL, opclass,
9497 conf->vht_oper_centr_freq_seg1_idx);
9498
9499 if (freq1 < 0 || freq2 < 0) {
9500 wpa_dbg(wpa_s, MSG_DEBUG,
9501 "P2P CSA: Selected invalid VHT center freqs");
9502 ret = -1;
9503 goto out;
9504 }
9505
9506 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9507 csa_settings.freq_params.center_freq1 = freq1;
9508 csa_settings.freq_params.center_freq2 = freq2;
9509
9510 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009511 case CHANWIDTH_80MHZ:
9512 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009513 csa_settings.freq_params.bandwidth = 80;
9514 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009515 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009516 csa_settings.freq_params.bandwidth = 160;
9517 break;
9518 }
9519 }
9520
9521 ret = ap_switch_channel(wpa_s, &csa_settings);
9522out:
9523 current_ssid->frequency = old_freq;
9524 hostapd_config_free(conf);
9525 return ret;
9526}
9527
9528
9529static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9530{
9531 struct p2p_go_neg_results params;
9532 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009533 void (*ap_configured_cb)(void *ctx, void *data);
9534 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009535
9536 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9537
9538 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9539 current_ssid->frequency);
9540
9541 /* Stop the AP functionality */
9542 /* TODO: Should do this in a way that does not indicated to possible
9543 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009544 /* If this action occurs before a group is started, the callback should
9545 * be preserved, or GROUP-STARTED event would be lost. If this action
9546 * occurs after a group is started, these pointers are all NULL and
9547 * harmless. */
9548 ap_configured_cb = wpa_s->ap_configured_cb;
9549 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9550 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009551 wpa_supplicant_ap_deinit(wpa_s);
9552
9553 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009554 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9555 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009556 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9557 wpas_p2p_group_delete(wpa_s,
9558 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9559 return;
9560 }
9561 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9562 params.freq);
9563
9564 if (params.freq &&
9565 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9566 wpa_printf(MSG_DEBUG,
9567 "P2P: Selected freq (%u MHz) is not valid for P2P",
9568 params.freq);
9569 wpas_p2p_group_delete(wpa_s,
9570 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9571 return;
9572 }
9573
Hai Shalom899fcc72020-10-19 14:38:18 -07009574 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009575 wpa_s->ap_configured_cb = ap_configured_cb;
9576 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9577 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009578
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009579 /* Update the frequency */
9580 current_ssid->frequency = params.freq;
9581 wpa_s->connect_without_scan = current_ssid;
9582 wpa_s->reassociate = 1;
9583 wpa_s->disconnected = 0;
9584 wpa_supplicant_req_scan(wpa_s, 0, 0);
9585}
9586
9587
9588static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9589{
9590 struct wpa_supplicant *wpa_s = eloop_ctx;
9591
9592 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009593 return;
9594
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009595 wpas_p2p_go_update_common_freqs(wpa_s);
9596
9597 /* Do not move GO in the middle of a CSA */
9598 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9599 wpa_printf(MSG_DEBUG,
9600 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009601 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009602 }
9603
9604 /*
9605 * First, try a channel switch flow. If it is not supported or fails,
9606 * take down the GO and bring it up again.
9607 */
9608 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9609 wpas_p2p_move_go_no_csa(wpa_s);
9610}
9611
9612
9613static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9614{
9615 struct wpa_supplicant *wpa_s = eloop_ctx;
9616 struct wpa_used_freq_data *freqs = NULL;
9617 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009618
9619 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9620 if (!freqs)
9621 return;
9622
9623 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9624
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009625 /* Previous attempt to move a GO was not possible -- try again. */
9626 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9627 WPAS_P2P_CHANNEL_UPDATE_ANY);
9628
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009629 os_free(freqs);
9630}
9631
9632
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009633/*
9634 * Consider moving a GO from its currently used frequency:
9635 * 1. It is possible that due to regulatory consideration the frequency
9636 * can no longer be used and there is a need to evacuate the GO.
9637 * 2. It is possible that due to MCC considerations, it would be preferable
9638 * to move the GO to a channel that is currently used by some other
9639 * station interface.
9640 *
9641 * In case a frequency that became invalid is once again valid, cancel a
9642 * previously initiated GO frequency change.
9643 */
9644static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9645 struct wpa_used_freq_data *freqs,
9646 unsigned int num)
9647{
9648 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9649 unsigned int timeout;
9650 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009651 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009652
9653 wpas_p2p_go_update_common_freqs(wpa_s);
9654
9655 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009656 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009657 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009658 for (i = 0, invalid_freq = 0; i < num; i++) {
9659 if (freqs[i].freq == freq) {
9660 flags = freqs[i].flags;
9661
9662 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009663 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9664 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009665 wpa_dbg(wpa_s, MSG_DEBUG,
9666 "P2P: Freq=%d MHz no longer valid for GO",
9667 freq);
9668 invalid_freq = 1;
9669 }
9670 } else if (freqs[i].flags == 0) {
9671 /* Freq is not used by any other station interface */
9672 continue;
9673 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009674 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009675 /* Freq is not valid for P2P use cases */
9676 continue;
9677 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9678 P2P_GO_FREQ_MOVE_SCM) {
9679 policy_move = 1;
9680 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9681 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9682 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9683 policy_move = 1;
9684 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9685 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9686 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9687 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9688 policy_move = 1;
9689 } else if ((wpa_s->drv_flags &
9690 WPA_DRIVER_FLAGS_AP_CSA) &&
9691 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9692 u8 chan;
9693
9694 /*
9695 * We do not support CSA between bands, so move
9696 * GO only within the same band.
9697 */
9698 if (wpa_s->ap_iface->current_mode->mode ==
9699 ieee80211_freq_to_chan(freqs[i].freq,
9700 &chan))
9701 policy_move = 1;
9702 }
9703 }
9704 }
9705
9706 wpa_dbg(wpa_s, MSG_DEBUG,
9707 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9708 invalid_freq, policy_move, flags);
9709
9710 /*
9711 * The channel is valid, or we are going to have a policy move, so
9712 * cancel timeout.
9713 */
9714 if (!invalid_freq || policy_move) {
9715 wpa_dbg(wpa_s, MSG_DEBUG,
9716 "P2P: Cancel a GO move from freq=%d MHz", freq);
9717 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9718
9719 if (wpas_p2p_in_progress(wpa_s)) {
9720 wpa_dbg(wpa_s, MSG_DEBUG,
9721 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9722 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9723 wpa_s, NULL);
9724 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9725 wpas_p2p_reconsider_moving_go,
9726 wpa_s, NULL);
9727 return;
9728 }
9729 }
9730
9731 if (!invalid_freq && (!policy_move || flags != 0)) {
9732 wpa_dbg(wpa_s, MSG_DEBUG,
9733 "P2P: Not initiating a GO frequency change");
9734 return;
9735 }
9736
9737 /*
9738 * Do not consider moving GO if it is in the middle of a CSA. When the
9739 * CSA is finished this flow should be retriggered.
9740 */
9741 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9742 wpa_dbg(wpa_s, MSG_DEBUG,
9743 "P2P: Not initiating a GO frequency change - CSA is in progress");
9744 return;
9745 }
9746
9747 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9748 timeout = P2P_GO_FREQ_CHANGE_TIME;
9749 else
9750 timeout = 0;
9751
9752 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9753 freq, timeout);
9754 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9755 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9756}
9757
9758
9759static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9760 struct wpa_used_freq_data *freqs,
9761 unsigned int num,
9762 enum wpas_p2p_channel_update_trig trig)
9763{
9764 struct wpa_supplicant *ifs;
9765
9766 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9767 NULL);
9768
9769 /*
9770 * Travers all the radio interfaces, and for each GO interface, check
9771 * if there is a need to move the GO from the frequency it is using,
9772 * or in case the frequency is valid again, cancel the evacuation flow.
9773 */
9774 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9775 radio_list) {
9776 if (ifs->current_ssid == NULL ||
9777 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9778 continue;
9779
9780 /*
9781 * The GO was just started or completed channel switch, no need
9782 * to move it.
9783 */
9784 if (wpa_s == ifs &&
9785 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9786 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9787 wpa_dbg(wpa_s, MSG_DEBUG,
9788 "P2P: GO move - schedule re-consideration");
9789 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9790 wpas_p2p_reconsider_moving_go,
9791 wpa_s, NULL);
9792 continue;
9793 }
9794
9795 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9796 }
9797}
9798
9799
9800void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9801{
9802 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9803 return;
9804
9805 wpas_p2p_update_channel_list(wpa_s,
9806 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9807}
9808
9809
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009810void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9811{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009812 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9813 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9814 "the management interface is being removed");
9815 wpas_p2p_deinit_global(wpa_s->global);
9816 }
9817}
9818
9819
9820void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9821{
9822 if (wpa_s->ap_iface->bss)
9823 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9824 wpas_p2p_group_deinit(wpa_s);
9825}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009826
9827
9828int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9829 unsigned int period, unsigned int interval,
9830 unsigned int count)
9831{
9832 struct p2p_data *p2p = wpa_s->global->p2p;
9833 u8 *device_types;
9834 size_t dev_types_len;
9835 struct wpabuf *buf;
9836 int ret;
9837
9838 if (wpa_s->p2p_lo_started) {
9839 wpa_dbg(wpa_s, MSG_DEBUG,
9840 "P2P Listen offload is already started");
9841 return 0;
9842 }
9843
9844 if (wpa_s->global->p2p == NULL ||
9845 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9846 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9847 return -1;
9848 }
9849
9850 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9851 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9852 freq);
9853 return -1;
9854 }
9855
9856 /* Get device type */
9857 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9858 WPS_DEV_TYPE_LEN;
9859 device_types = os_malloc(dev_types_len);
9860 if (!device_types)
9861 return -1;
9862 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9863 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9864 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9865
9866 /* Get Probe Response IE(s) */
9867 buf = p2p_build_probe_resp_template(p2p, freq);
9868 if (!buf) {
9869 os_free(device_types);
9870 return -1;
9871 }
9872
9873 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9874 device_types, dev_types_len,
9875 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9876 if (ret < 0)
9877 wpa_dbg(wpa_s, MSG_DEBUG,
9878 "P2P: Failed to start P2P listen offload");
9879
9880 os_free(device_types);
9881 wpabuf_free(buf);
9882
9883 if (ret == 0) {
9884 wpa_s->p2p_lo_started = 1;
9885
9886 /* Stop current P2P listen if any */
9887 wpas_stop_listen(wpa_s);
9888 }
9889
9890 return ret;
9891}
9892
9893
9894int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9895{
9896 int ret;
9897
9898 if (!wpa_s->p2p_lo_started)
9899 return 0;
9900
9901 ret = wpa_drv_p2p_lo_stop(wpa_s);
9902 if (ret < 0)
9903 wpa_dbg(wpa_s, MSG_DEBUG,
9904 "P2P: Failed to stop P2P listen offload");
9905
9906 wpa_s->p2p_lo_started = 0;
9907 return ret;
9908}