blob: f3bb56a158d1d9aac5639c3de231f6eda1acf2e8 [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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
247{
248 size_t i;
249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800250 if (wpa_s->p2p_scan_work) {
251 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
252 wpa_s->p2p_scan_work = NULL;
253 radio_work_done(work);
254 }
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
257 return;
258
259 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
260 (int) scan_res->num);
261
262 for (i = 0; i < scan_res->num; i++) {
263 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700265 const u8 *ies;
266 size_t ies_len;
267
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700271
272 ies = (const u8 *) (bss + 1);
273 ies_len = bss->ie_len;
274 if (bss->beacon_ie_len > 0 &&
275 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
276 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
277 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
278 MACSTR, MAC2STR(bss->bssid));
279 ies = ies + ies_len;
280 ies_len = bss->beacon_ie_len;
281 }
282
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800285 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700286 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 break;
288 }
289
290 p2p_scan_res_handled(wpa_s->global->p2p);
291}
292
293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
295{
296 struct wpa_supplicant *wpa_s = work->wpa_s;
297 struct wpa_driver_scan_params *params = work->ctx;
298 int ret;
299
300 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800301 if (!work->started) {
302 wpa_scan_free_params(params);
303 return;
304 }
305
306 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 return;
308 }
309
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800310 if (wpa_s->clear_driver_scan_cache) {
311 wpa_printf(MSG_DEBUG,
312 "Request driver to clear scan cache due to local BSS flush");
313 params->only_new_results = 1;
314 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800315 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800316 if (ret == 0)
317 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 wpa_scan_free_params(params);
319 work->ctx = NULL;
320 if (ret) {
321 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800322 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800323 return;
324 }
325
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800326 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 os_get_reltime(&wpa_s->scan_trigger_time);
328 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
329 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800330 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 wpa_s->p2p_scan_work = work;
332}
333
334
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800335static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
336 int freq)
337{
338 if (wpa_s->global->p2p_24ghz_social_channels &&
339 (freq == 2412 || freq == 2437 || freq == 2462)) {
340 /*
341 * Search all social channels regardless of whether these have
342 * been disabled for P2P operating channel use to avoid missing
343 * peers.
344 */
345 return 1;
346 }
347 return p2p_supported_freq(wpa_s->global->p2p, freq);
348}
349
350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
352 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354{
355 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800356 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700358 unsigned int num_channels = 0;
359 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700361 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800362 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
365 return -1;
366
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 if (wpa_s->p2p_scan_work) {
368 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
369 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700370 }
371
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 params = os_zalloc(sizeof(*params));
373 if (params == NULL)
374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800377 params->num_ssids = 1;
378 n = os_malloc(P2P_WILDCARD_SSID_LEN);
379 if (n == NULL)
380 goto fail;
381 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
382 params->ssids[0].ssid = n;
383 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384
385 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700386 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
387 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 num_req_dev_types, req_dev_types);
389 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 switch (type) {
393 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700394 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
395 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800396 if (params->freqs == NULL)
397 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700398 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800399 if (wpas_p2p_search_social_channel(
400 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700401 params->freqs[num_channels++] =
402 social_channels_freq[i];
403 }
404 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 break;
406 case P2P_SCAN_FULL:
407 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800408 case P2P_SCAN_SPECIFIC:
409 params->freqs = os_calloc(2, sizeof(int));
410 if (params->freqs == NULL)
411 goto fail;
412 params->freqs[0] = freq;
413 params->freqs[1] = 0;
414 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700416 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
417 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800418 if (params->freqs == NULL)
419 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700420 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800421 if (wpas_p2p_search_social_channel(
422 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700423 params->freqs[num_channels++] =
424 social_channels_freq[i];
425 }
426 if (p2p_supported_freq(wpa_s->global->p2p, freq))
427 params->freqs[num_channels++] = freq;
428 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 break;
430 }
431
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800432 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
433 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
434 if (ies == NULL) {
435 wpabuf_free(wps_ie);
436 goto fail;
437 }
438 wpabuf_put_buf(ies, wps_ie);
439 wpabuf_free(wps_ie);
440
441 bands = wpas_get_bands(wpa_s, params->freqs);
442 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
443
444 params->p2p_probe = 1;
445 n = os_malloc(wpabuf_len(ies));
446 if (n == NULL) {
447 wpabuf_free(ies);
448 goto fail;
449 }
450 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
451 params->extra_ies = n;
452 params->extra_ies_len = wpabuf_len(ies);
453 wpabuf_free(ies);
454
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800455 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800456 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
457 params) < 0)
458 goto fail;
459 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800461fail:
462 wpa_scan_free_params(params);
463 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464}
465
466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
468{
469 switch (p2p_group_interface) {
470 case P2P_GROUP_INTERFACE_PENDING:
471 return WPA_IF_P2P_GROUP;
472 case P2P_GROUP_INTERFACE_GO:
473 return WPA_IF_P2P_GO;
474 case P2P_GROUP_INTERFACE_CLIENT:
475 return WPA_IF_P2P_CLIENT;
476 }
477
478 return WPA_IF_P2P_GROUP;
479}
480
481
482static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
483 const u8 *ssid,
484 size_t ssid_len, int *go)
485{
486 struct wpa_ssid *s;
487
488 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
489 for (s = wpa_s->conf->ssid; s; s = s->next) {
490 if (s->disabled != 0 || !s->p2p_group ||
491 s->ssid_len != ssid_len ||
492 os_memcmp(ssid, s->ssid, ssid_len) != 0)
493 continue;
494 if (s->mode == WPAS_MODE_P2P_GO &&
495 s != wpa_s->current_ssid)
496 continue;
497 if (go)
498 *go = s->mode == WPAS_MODE_P2P_GO;
499 return wpa_s;
500 }
501 }
502
503 return NULL;
504}
505
506
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800507static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
508{
509 struct wpa_supplicant *wpa_s = eloop_ctx;
510 wpa_printf(MSG_DEBUG,
511 "P2P: Complete previously requested removal of %s",
512 wpa_s->ifname);
513 wpas_p2p_disconnect(wpa_s);
514}
515
516
517static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
518 struct wpa_supplicant *calling_wpa_s)
519{
520 if (calling_wpa_s == wpa_s && wpa_s &&
521 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
522 /*
523 * The calling wpa_s instance is going to be removed. Do that
524 * from an eloop callback to keep the instance available until
525 * the caller has returned. This my be needed, e.g., to provide
526 * control interface responses on the per-interface socket.
527 */
528 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
529 wpa_s, NULL) < 0)
530 return -1;
531 return 0;
532 }
533
534 return wpas_p2p_disconnect(wpa_s);
535}
536
537
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800538/* Determine total number of clients in active groups where we are the GO */
539static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
540{
541 unsigned int count = 0;
542 struct wpa_ssid *s;
543
544 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
545 for (s = wpa_s->conf->ssid; s; s = s->next) {
546 wpa_printf(MSG_DEBUG,
547 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
548 wpa_s, s, s->disabled, s->p2p_group,
549 s->mode);
550 if (!s->disabled && s->p2p_group &&
551 s->mode == WPAS_MODE_P2P_GO) {
552 count += p2p_get_group_num_members(
553 wpa_s->p2p_group);
554 }
555 }
556 }
557
558 return count;
559}
560
561
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800562static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
563{
564 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
565 !wpa_s->current_ssid->disabled &&
566 wpa_s->current_ssid->p2p_group &&
567 wpa_s->current_ssid->p2p_persistent_group;
568}
569
570
571static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
572{
573 return p2p_is_active_persistent_group(wpa_s) &&
574 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
575}
576
577
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800578/* Find an interface for a P2P group where we are the GO */
579static struct wpa_supplicant *
580wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
581{
582 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800583
584 if (!wpa_s)
585 return NULL;
586
587 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800588 if (!p2p_is_active_persistent_go(wpa_s))
589 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800590
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800591 /* Prefer a group with connected clients */
592 if (p2p_get_group_num_members(wpa_s->p2p_group))
593 return wpa_s;
594 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800595 }
596
597 /* No group with connected clients, so pick the one without (if any) */
598 return save;
599}
600
601
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800602static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800603{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800604 return p2p_is_active_persistent_group(wpa_s) &&
605 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
606}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800608
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800609/* Find an interface for a P2P group where we are the P2P Client */
610static struct wpa_supplicant *
611wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
612{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800613 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800614 if (p2p_is_active_persistent_cli(wpa_s))
615 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800616 }
617
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800619}
620
621
622/* Find a persistent group where we are the GO */
623static struct wpa_ssid *
624wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
625{
626 struct wpa_ssid *s;
627
628 for (s = wpa_s->conf->ssid; s; s = s->next) {
629 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
630 return s;
631 }
632
633 return NULL;
634}
635
636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800637static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
638 unsigned int *force_freq,
639 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800640{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800641 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800642 struct wpa_ssid *s;
643 u8 conncap = P2PS_SETUP_NONE;
644 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800645 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800646 struct wpa_ssid *persistent_go;
647 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800649
650 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
651
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800652 if (force_freq)
653 *force_freq = 0;
654 if (pref_freq)
655 *pref_freq = 0;
656
657 size = P2P_MAX_PREF_CHANNELS;
658 if (force_freq && pref_freq &&
659 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
660 (int *) pref_freq, 0, pref_freq_list, &size))
661 wpas_p2p_set_own_freq_preference(wpa_s,
662 *force_freq ? *force_freq :
663 *pref_freq);
664
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665 /*
666 * For non-concurrent capable devices:
667 * If persistent_go, then no new.
668 * If GO, then no client.
669 * If client, then no GO.
670 */
671 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 if (go_wpa_s)
673 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800674 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800675 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
676 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678 wpa_printf(MSG_DEBUG,
679 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
680 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681
682 /* If not concurrent, restrict our choices */
683 if (p2p_no_group_iface) {
684 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800687 return P2PS_SETUP_NONE;
688
689 if (go_wpa_s) {
690 if (role == P2PS_SETUP_CLIENT ||
691 incoming == P2PS_SETUP_GROUP_OWNER ||
692 p2p_client_limit_reached(go_wpa_s->p2p_group))
693 return P2PS_SETUP_NONE;
694
695 return P2PS_SETUP_GROUP_OWNER;
696 }
697
698 if (persistent_go) {
699 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
700 if (!incoming)
701 return P2PS_SETUP_GROUP_OWNER |
702 P2PS_SETUP_CLIENT;
703 if (incoming == P2PS_SETUP_NEW) {
704 u8 r;
705
706 if (os_get_random(&r, sizeof(r)) < 0 ||
707 (r & 1))
708 return P2PS_SETUP_CLIENT;
709 return P2PS_SETUP_GROUP_OWNER;
710 }
711 }
712 }
713 }
714
715 /* If a required role has been specified, handle it here */
716 if (role && role != P2PS_SETUP_NEW) {
717 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800718 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
719 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
720 /*
721 * Peer has an active GO, so if the role allows it and
722 * we do not have any active roles, become client.
723 */
724 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
725 !cli_wpa_s)
726 return P2PS_SETUP_CLIENT;
727
728 /* fall through */
729
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800730 case P2PS_SETUP_NONE:
731 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732 conncap = role;
733 goto grp_owner;
734
735 case P2PS_SETUP_GROUP_OWNER:
736 /*
737 * Must be a complimentary role - cannot be a client to
738 * more than one peer.
739 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800740 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800741 return P2PS_SETUP_NONE;
742
743 return P2PS_SETUP_CLIENT;
744
745 case P2PS_SETUP_CLIENT:
746 /* Must be a complimentary role */
747 if (incoming != role) {
748 conncap = P2PS_SETUP_GROUP_OWNER;
749 goto grp_owner;
750 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700751 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800752
753 default:
754 return P2PS_SETUP_NONE;
755 }
756 }
757
758 /*
759 * For now, we only will support ownership of one group, and being a
760 * client of one group. Therefore, if we have either an existing GO
761 * group, or an existing client group, we will not do a new GO
762 * negotiation, but rather try to re-use the existing groups.
763 */
764 switch (incoming) {
765 case P2PS_SETUP_NONE:
766 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800767 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800768 conncap = P2PS_SETUP_GROUP_OWNER;
769 else if (!owned_members)
770 conncap = P2PS_SETUP_NEW;
771 else if (incoming == P2PS_SETUP_NONE)
772 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
773 else
774 conncap = P2PS_SETUP_CLIENT;
775 break;
776
777 case P2PS_SETUP_CLIENT:
778 conncap = P2PS_SETUP_GROUP_OWNER;
779 break;
780
781 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800782 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800783 conncap = P2PS_SETUP_CLIENT;
784 break;
785
786 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
787 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800788 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800789 conncap = P2PS_SETUP_GROUP_OWNER;
790 else {
791 u8 r;
792
793 if (os_get_random(&r, sizeof(r)) < 0 ||
794 (r & 1))
795 conncap = P2PS_SETUP_CLIENT;
796 else
797 conncap = P2PS_SETUP_GROUP_OWNER;
798 }
799 break;
800
801 default:
802 return P2PS_SETUP_NONE;
803 }
804
805grp_owner:
806 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
807 (!incoming && (conncap & P2PS_SETUP_NEW))) {
808 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
809 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800810
811 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800812 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800814 wpa_s->p2p_mgmt ?
815 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800816 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800817 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800818 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800819 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800820 wpa_printf(MSG_ERROR,
821 "P2P: Failed to allocate a new interface for the group");
822 return P2PS_SETUP_NONE;
823 }
824 wpa_s->global->pending_group_iface_for_p2ps = 1;
825 p2p_set_intended_addr(wpa_s->global->p2p,
826 wpa_s->pending_interface_addr);
827 }
828 }
829
830 return conncap;
831}
832
833
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700834static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
835 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836{
837 struct wpa_ssid *ssid;
838 char *gtype;
839 const char *reason;
840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 ssid = wpa_s->current_ssid;
842 if (ssid == NULL) {
843 /*
844 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700845 * pending P2P group interface waiting for provisioning or a
846 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 */
848 ssid = wpa_s->conf->ssid;
849 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700850 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 break;
852 ssid = ssid->next;
853 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300854 if (ssid == NULL &&
855 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
856 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700857 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
858 "not found");
859 return -1;
860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 }
862 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
863 gtype = "GO";
864 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
865 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
866 wpa_s->reassociate = 0;
867 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 gtype = "client";
869 } else
870 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700871
872 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
873 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
874
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800875 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700876 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800877 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
878 wpa_s, NULL)) {
879 wpa_printf(MSG_DEBUG,
880 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
881 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
882 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
883 wpa_s, NULL);
884 }
885 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 if (wpa_s->cross_connect_in_use) {
888 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800889 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700890 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
891 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700893 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 case P2P_GROUP_REMOVAL_REQUESTED:
895 reason = " reason=REQUESTED";
896 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
898 reason = " reason=FORMATION_FAILED";
899 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
901 reason = " reason=IDLE";
902 break;
903 case P2P_GROUP_REMOVAL_UNAVAILABLE:
904 reason = " reason=UNAVAILABLE";
905 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
907 reason = " reason=GO_ENDING_SESSION";
908 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700909 case P2P_GROUP_REMOVAL_PSK_FAILURE:
910 reason = " reason=PSK_FAILURE";
911 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800912 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
913 reason = " reason=FREQ_CONFLICT";
914 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 default:
916 reason = "";
917 break;
918 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800920 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700921 P2P_EVENT_GROUP_REMOVED "%s %s%s",
922 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800925 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
926 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
928 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800929 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800930 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800931 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
932 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700933 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800934 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700935 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700936
Dmitry Shmidt15907092014-03-25 10:42:57 -0700937 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800938 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
939 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700940
Dmitry Shmidt96571392013-10-14 12:54:46 -0700941 /*
942 * Make sure wait for the first client does not remain active after the
943 * group has been removed.
944 */
945 wpa_s->global->p2p_go_wait_client.sec = 0;
946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
948 struct wpa_global *global;
949 char *ifname;
950 enum wpa_driver_if_type type;
951 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
952 wpa_s->ifname);
953 global = wpa_s->global;
954 ifname = os_strdup(wpa_s->ifname);
955 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800956 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700957 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 wpa_s = global->ifaces;
959 if (wpa_s && ifname)
960 wpa_drv_if_remove(wpa_s, type, ifname);
961 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300962 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 }
964
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800965 /*
966 * The primary interface was used for P2P group operations, so
967 * need to reset its p2pdev.
968 */
969 wpa_s->p2pdev = wpa_s->parent;
970
Dmitry Shmidt96571392013-10-14 12:54:46 -0700971 if (!wpa_s->p2p_go_group_formation_completed) {
972 wpa_s->global->p2p_group_formation = NULL;
973 wpa_s->p2p_in_provisioning = 0;
974 }
975
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800976 wpa_s->show_group_started = 0;
977 os_free(wpa_s->go_params);
978 wpa_s->go_params = NULL;
979
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800980 os_free(wpa_s->p2p_group_common_freqs);
981 wpa_s->p2p_group_common_freqs = NULL;
982 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700983 wpa_s->p2p_go_do_acs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800984
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800985 wpa_s->waiting_presence_resp = 0;
986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
988 if (ssid && (ssid->p2p_group ||
989 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
990 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
991 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700992 if (ssid == wpa_s->current_ssid) {
993 wpa_sm_set_config(wpa_s->wpa, NULL);
994 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700996 }
997 /*
998 * Networks objects created during any P2P activities are not
999 * exposed out as they might/will confuse certain non-P2P aware
1000 * applications since these network objects won't behave like
1001 * regular ones.
1002 *
1003 * Likewise, we don't send out network removed signals for such
1004 * network objects.
1005 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 wpa_config_remove_network(wpa_s->conf, id);
1007 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001008 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 } else {
1010 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1011 "found");
1012 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001013 if (wpa_s->ap_iface)
1014 wpa_supplicant_ap_deinit(wpa_s);
1015 else
1016 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001017
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001018 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1019
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001020 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021}
1022
1023
1024static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1025 u8 *go_dev_addr,
1026 const u8 *ssid, size_t ssid_len)
1027{
1028 struct wpa_bss *bss;
1029 const u8 *bssid;
1030 struct wpabuf *p2p;
1031 u8 group_capab;
1032 const u8 *addr;
1033
1034 if (wpa_s->go_params)
1035 bssid = wpa_s->go_params->peer_interface_addr;
1036 else
1037 bssid = wpa_s->bssid;
1038
1039 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001040 if (bss == NULL && wpa_s->go_params &&
1041 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1042 bss = wpa_bss_get_p2p_dev_addr(
1043 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 if (bss == NULL) {
1045 u8 iface_addr[ETH_ALEN];
1046 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1047 iface_addr) == 0)
1048 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1049 }
1050 if (bss == NULL) {
1051 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1052 "group is persistent - BSS " MACSTR " not found",
1053 MAC2STR(bssid));
1054 return 0;
1055 }
1056
1057 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001058 if (p2p == NULL)
1059 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1060 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 if (p2p == NULL) {
1062 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1063 "group is persistent - BSS " MACSTR
1064 " did not include P2P IE", MAC2STR(bssid));
1065 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1066 (u8 *) (bss + 1), bss->ie_len);
1067 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1068 ((u8 *) bss + 1) + bss->ie_len,
1069 bss->beacon_ie_len);
1070 return 0;
1071 }
1072
1073 group_capab = p2p_get_group_capab(p2p);
1074 addr = p2p_get_go_dev_addr(p2p);
1075 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1076 "group_capab=0x%x", group_capab);
1077 if (addr) {
1078 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1079 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1080 MAC2STR(addr));
1081 } else
1082 os_memset(go_dev_addr, 0, ETH_ALEN);
1083 wpabuf_free(p2p);
1084
1085 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1086 "go_dev_addr=" MACSTR,
1087 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1088
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001089 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090}
1091
1092
Jouni Malinen75ecf522011-06-27 15:19:46 -07001093static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1094 struct wpa_ssid *ssid,
1095 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096{
1097 struct wpa_ssid *s;
1098 int changed = 0;
1099
1100 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1101 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1102 for (s = wpa_s->conf->ssid; s; s = s->next) {
1103 if (s->disabled == 2 &&
1104 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1105 s->ssid_len == ssid->ssid_len &&
1106 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1107 break;
1108 }
1109
1110 if (s) {
1111 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1112 "entry");
1113 if (ssid->passphrase && !s->passphrase)
1114 changed = 1;
1115 else if (ssid->passphrase && s->passphrase &&
1116 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1117 changed = 1;
1118 } else {
1119 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1120 "entry");
1121 changed = 1;
1122 s = wpa_config_add_network(wpa_s->conf);
1123 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001124 return -1;
1125
1126 /*
1127 * Instead of network_added we emit persistent_group_added
1128 * notification. Also to keep the defense checks in
1129 * persistent_group obj registration method, we set the
1130 * relevant flags in s to designate it as a persistent group.
1131 */
1132 s->p2p_group = 1;
1133 s->p2p_persistent_group = 1;
1134 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 wpa_config_set_network_defaults(s);
1136 }
1137
1138 s->p2p_group = 1;
1139 s->p2p_persistent_group = 1;
1140 s->disabled = 2;
1141 s->bssid_set = 1;
1142 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1143 s->mode = ssid->mode;
1144 s->auth_alg = WPA_AUTH_ALG_OPEN;
1145 s->key_mgmt = WPA_KEY_MGMT_PSK;
1146 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001147 s->pbss = ssid->pbss;
1148 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 s->export_keys = 1;
1150 if (ssid->passphrase) {
1151 os_free(s->passphrase);
1152 s->passphrase = os_strdup(ssid->passphrase);
1153 }
1154 if (ssid->psk_set) {
1155 s->psk_set = 1;
1156 os_memcpy(s->psk, ssid->psk, 32);
1157 }
1158 if (s->passphrase && !s->psk_set)
1159 wpa_config_update_psk(s);
1160 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1161 os_free(s->ssid);
1162 s->ssid = os_malloc(ssid->ssid_len);
1163 }
1164 if (s->ssid) {
1165 s->ssid_len = ssid->ssid_len;
1166 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1167 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001168 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1169 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1170 wpa_s->global->add_psk = NULL;
1171 changed = 1;
1172 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 if (changed && wpa_s->conf->update_config &&
1175 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1176 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1177 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001178
1179 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180}
1181
1182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1184 const u8 *addr)
1185{
1186 struct wpa_ssid *ssid, *s;
1187 u8 *n;
1188 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001189 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001190 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001191
1192 ssid = wpa_s->current_ssid;
1193 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1194 !ssid->p2p_persistent_group)
1195 return;
1196
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001197 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1199 continue;
1200
1201 if (s->ssid_len == ssid->ssid_len &&
1202 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1203 break;
1204 }
1205
1206 if (s == NULL)
1207 return;
1208
1209 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001210 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001211 ETH_ALEN) != 0)
1212 continue;
1213
1214 if (i == s->num_p2p_clients - 1)
1215 return; /* already the most recent entry */
1216
1217 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001218 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1219 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1220 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001221 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001222 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1223 ETH_ALEN);
1224 os_memset(s->p2p_client_list +
1225 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1226 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001227 found = 1;
1228 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001229 }
1230
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001231 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1232 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001233 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001234 if (n == NULL)
1235 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001236 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1237 ETH_ALEN);
1238 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1239 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240 s->p2p_client_list = n;
1241 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001242 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001243 /* Not enough room for an additional entry - drop the oldest
1244 * entry */
1245 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001246 s->p2p_client_list + 2 * ETH_ALEN,
1247 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001248 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001249 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001250 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001251 os_memset(s->p2p_client_list +
1252 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1253 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001254 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001255
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001256 if (p2p_wpa_s->conf->update_config &&
1257 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001258 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259}
1260
1261
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001262static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1263 int go, struct wpa_ssid *ssid, int freq,
1264 const u8 *psk, const char *passphrase,
1265 const u8 *go_dev_addr, int persistent,
1266 const char *extra)
1267{
1268 const char *ssid_txt;
1269 char psk_txt[65];
1270
1271 if (psk)
1272 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1273 else
1274 psk_txt[0] = '\0';
1275
1276 if (ssid)
1277 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1278 else
1279 ssid_txt = "";
1280
1281 if (passphrase && passphrase[0] == '\0')
1282 passphrase = NULL;
1283
1284 /*
1285 * Include PSK/passphrase only in the control interface message and
1286 * leave it out from the debug log entry.
1287 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001288 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001289 P2P_EVENT_GROUP_STARTED
1290 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1291 MACSTR "%s%s",
1292 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1293 psk ? " psk=" : "", psk_txt,
1294 passphrase ? " passphrase=\"" : "",
1295 passphrase ? passphrase : "",
1296 passphrase ? "\"" : "",
1297 MAC2STR(go_dev_addr),
1298 persistent ? " [PERSISTENT]" : "", extra);
1299 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1300 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1301 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1302 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1303 extra);
1304}
1305
1306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001308 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309{
1310 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 int client;
1312 int persistent;
1313 u8 go_dev_addr[ETH_ALEN];
1314
1315 /*
1316 * This callback is likely called for the main interface. Update wpa_s
1317 * to use the group interface if a new interface was created for the
1318 * group.
1319 */
1320 if (wpa_s->global->p2p_group_formation)
1321 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001322 if (wpa_s->p2p_go_group_formation_completed) {
1323 wpa_s->global->p2p_group_formation = NULL;
1324 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001325 } else if (wpa_s->p2p_in_provisioning && !success) {
1326 wpa_msg(wpa_s, MSG_DEBUG,
1327 "P2P: Stop provisioning state due to failure");
1328 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001329 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001330 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001331 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332
1333 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001334 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001335 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001336 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001337 if (already_deleted)
1338 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001339 wpas_p2p_group_delete(wpa_s,
1340 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 return;
1342 }
1343
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001344 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001345 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346
1347 ssid = wpa_s->current_ssid;
1348 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1349 ssid->mode = WPAS_MODE_P2P_GO;
1350 p2p_group_notif_formation_done(wpa_s->p2p_group);
1351 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1352 }
1353
1354 persistent = 0;
1355 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 client = ssid->mode == WPAS_MODE_INFRA;
1357 if (ssid->mode == WPAS_MODE_P2P_GO) {
1358 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001359 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1360 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 } else
1362 persistent = wpas_p2p_persistent_group(wpa_s,
1363 go_dev_addr,
1364 ssid->ssid,
1365 ssid->ssid_len);
1366 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 client = wpa_s->p2p_group_interface ==
1368 P2P_GROUP_INTERFACE_CLIENT;
1369 os_memset(go_dev_addr, 0, ETH_ALEN);
1370 }
1371
1372 wpa_s->show_group_started = 0;
1373 if (client) {
1374 /*
1375 * Indicate event only after successfully completed 4-way
1376 * handshake, i.e., when the interface is ready for data
1377 * packets.
1378 */
1379 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001381 wpas_p2p_group_started(wpa_s, 1, ssid,
1382 ssid ? ssid->frequency : 0,
1383 ssid && ssid->passphrase == NULL &&
1384 ssid->psk_set ? ssid->psk : NULL,
1385 ssid ? ssid->passphrase : NULL,
1386 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 wpas_p2p_cross_connect_setup(wpa_s);
1388 wpas_p2p_set_group_idle_timeout(wpa_s);
1389 }
1390
1391 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001392 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1393 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001394 else {
1395 os_free(wpa_s->global->add_psk);
1396 wpa_s->global->add_psk = NULL;
1397 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001398
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001399 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001400 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001401 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001402 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403}
1404
1405
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001406struct send_action_work {
1407 unsigned int freq;
1408 u8 dst[ETH_ALEN];
1409 u8 src[ETH_ALEN];
1410 u8 bssid[ETH_ALEN];
1411 size_t len;
1412 unsigned int wait_time;
1413 u8 buf[0];
1414};
1415
1416
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001417static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1418{
1419 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1420
1421 wpa_printf(MSG_DEBUG,
1422 "P2P: Free Action frame radio work @%p (freq=%u dst="
1423 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1424 wpa_s->p2p_send_action_work, awork->freq,
1425 MAC2STR(awork->dst), MAC2STR(awork->src),
1426 MAC2STR(awork->bssid), awork->wait_time);
1427 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1428 awork->buf, awork->len);
1429 os_free(awork);
1430 wpa_s->p2p_send_action_work->ctx = NULL;
1431 radio_work_done(wpa_s->p2p_send_action_work);
1432 wpa_s->p2p_send_action_work = NULL;
1433}
1434
1435
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001436static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1437 void *timeout_ctx)
1438{
1439 struct wpa_supplicant *wpa_s = eloop_ctx;
1440
1441 if (!wpa_s->p2p_send_action_work)
1442 return;
1443
1444 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001445 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001446}
1447
1448
Dmitry Shmidt03658832014-08-13 11:03:49 -07001449static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001451 if (wpa_s->p2p_send_action_work) {
1452 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001453
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001454 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001455 wpa_printf(MSG_DEBUG,
1456 "P2P: Clear Action TX work @%p (wait_time=%u)",
1457 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001458 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001459 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001460 } else {
1461 /*
1462 * In theory, this should not be needed, but number of
1463 * places in the P2P code is still using non-zero wait
1464 * time for the last Action frame in the sequence and
1465 * some of these do not call send_action_done().
1466 */
1467 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1468 wpa_s, NULL);
1469 eloop_register_timeout(
1470 0, awork->wait_time * 1000,
1471 wpas_p2p_send_action_work_timeout,
1472 wpa_s, NULL);
1473 }
1474 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001475}
1476
1477
1478static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1479 unsigned int freq,
1480 const u8 *dst, const u8 *src,
1481 const u8 *bssid,
1482 const u8 *data, size_t data_len,
1483 enum offchannel_send_action_result
1484 result)
1485{
1486 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1487
1488 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1491 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 switch (result) {
1494 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1495 res = P2P_SEND_ACTION_SUCCESS;
1496 break;
1497 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1498 res = P2P_SEND_ACTION_NO_ACK;
1499 break;
1500 case OFFCHANNEL_SEND_ACTION_FAILED:
1501 res = P2P_SEND_ACTION_FAILED;
1502 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 }
1504
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001505 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001507 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1508 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001509 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001510 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1511 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001513 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1514 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001515 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001516 P2P_EVENT_FALLBACK_TO_GO_NEG
1517 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001518 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1519 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520 }
1521}
1522
1523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1525{
1526 struct wpa_supplicant *wpa_s = work->wpa_s;
1527 struct send_action_work *awork = work->ctx;
1528
1529 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001530 if (work->started) {
1531 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1532 wpa_s, NULL);
1533 wpa_s->p2p_send_action_work = NULL;
1534 offchannel_send_action_done(wpa_s);
1535 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001536 os_free(awork);
1537 return;
1538 }
1539
1540 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1541 awork->bssid, awork->buf, awork->len,
1542 awork->wait_time,
1543 wpas_p2p_send_action_tx_status, 1) < 0) {
1544 os_free(awork);
1545 radio_work_done(work);
1546 return;
1547 }
1548 wpa_s->p2p_send_action_work = work;
1549}
1550
1551
1552static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1553 unsigned int freq, const u8 *dst,
1554 const u8 *src, const u8 *bssid, const u8 *buf,
1555 size_t len, unsigned int wait_time)
1556{
1557 struct send_action_work *awork;
1558
Hai Shalom81f62d82019-07-22 12:10:00 -07001559 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001560 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1561 return -1;
1562 }
1563
1564 awork = os_zalloc(sizeof(*awork) + len);
1565 if (awork == NULL)
1566 return -1;
1567
1568 awork->freq = freq;
1569 os_memcpy(awork->dst, dst, ETH_ALEN);
1570 os_memcpy(awork->src, src, ETH_ALEN);
1571 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1572 awork->len = len;
1573 awork->wait_time = wait_time;
1574 os_memcpy(awork->buf, buf, len);
1575
Hai Shalom81f62d82019-07-22 12:10:00 -07001576 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001577 wpas_send_action_cb, awork) < 0) {
1578 os_free(awork);
1579 return -1;
1580 }
1581
1582 return 0;
1583}
1584
1585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1587 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001588 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589{
1590 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001591 int listen_freq = -1, send_freq = -1;
1592
Hai Shalom021b0b52019-04-10 11:17:58 -07001593 if (scheduled)
1594 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001595 if (wpa_s->p2p_listen_work)
1596 listen_freq = wpa_s->p2p_listen_work->freq;
1597 if (wpa_s->p2p_send_action_work)
1598 send_freq = wpa_s->p2p_send_action_work->freq;
1599 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001600 int res;
1601
1602 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1603 listen_freq, send_freq, freq);
1604 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1605 len, wait_time);
1606 if (res == 0 && scheduled)
1607 *scheduled = 1;
1608 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001609 }
1610
1611 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1613 wait_time,
1614 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615}
1616
1617
1618static void wpas_send_action_done(void *ctx)
1619{
1620 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001621
1622 if (wpa_s->p2p_send_action_work) {
1623 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1624 wpa_s, NULL);
1625 os_free(wpa_s->p2p_send_action_work->ctx);
1626 radio_work_done(wpa_s->p2p_send_action_work);
1627 wpa_s->p2p_send_action_work = NULL;
1628 }
1629
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001630 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631}
1632
1633
1634static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1635 struct p2p_go_neg_results *params)
1636{
1637 if (wpa_s->go_params == NULL) {
1638 wpa_s->go_params = os_malloc(sizeof(*params));
1639 if (wpa_s->go_params == NULL)
1640 return -1;
1641 }
1642 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1643 return 0;
1644}
1645
1646
1647static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1648 struct p2p_go_neg_results *res)
1649{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001650 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1652 " dev_addr " MACSTR " wps_method %d",
1653 MAC2STR(res->peer_interface_addr),
1654 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1656 res->ssid, res->ssid_len);
1657 wpa_supplicant_ap_deinit(wpa_s);
1658 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001659 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001660 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001661#ifdef CONFIG_WPS_NFC
1662 } else if (res->wps_method == WPS_NFC) {
1663 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1664 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001665 wpa_s->p2pdev->p2p_oob_dev_pw,
1666 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1667 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001668 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001669 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001670 NULL,
1671 NULL, 0, 0);
1672#endif /* CONFIG_WPS_NFC */
1673 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001675 if (wpa_s->p2p_wps_method == WPS_P2PS)
1676 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1678 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1679 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1680 wpa_s->p2p_pin, 1, dev_pw_id);
1681 }
1682}
1683
1684
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001685static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1686 struct wpa_ssid *ssid)
1687{
1688 struct wpa_ssid *persistent;
1689 struct psk_list_entry *psk;
1690 struct hostapd_data *hapd;
1691
1692 if (!wpa_s->ap_iface)
1693 return;
1694
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001695 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001696 ssid->ssid_len);
1697 if (persistent == NULL)
1698 return;
1699
1700 hapd = wpa_s->ap_iface->bss[0];
1701
1702 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1703 list) {
1704 struct hostapd_wpa_psk *hpsk;
1705
1706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1707 MACSTR " psk=%d",
1708 MAC2STR(psk->addr), psk->p2p);
1709 hpsk = os_zalloc(sizeof(*hpsk));
1710 if (hpsk == NULL)
1711 break;
1712 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1713 if (psk->p2p)
1714 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1715 else
1716 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1717 hpsk->next = hapd->conf->ssid.wpa_psk;
1718 hapd->conf->ssid.wpa_psk = hpsk;
1719 }
1720}
1721
1722
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001723static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1724{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001725 char buf[20 + P2P_MAX_CHANNELS * 6];
1726 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001727 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001728 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001729
Roshan Pius3a1667e2018-07-03 15:17:14 -07001730 pos = buf;
1731 end = pos + sizeof(buf);
1732 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1733 res = os_snprintf(pos, end - pos, " %d",
1734 wpa_s->p2p_group_common_freqs[i]);
1735 if (os_snprintf_error(end - pos, res))
1736 break;
1737 pos += res;
1738 }
1739 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001740
Roshan Pius3a1667e2018-07-03 15:17:14 -07001741 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001742}
1743
1744
1745static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1746 struct p2p_go_neg_results *params)
1747{
1748 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1749
1750 wpa_s->p2p_group_common_freqs_num = 0;
1751 os_free(wpa_s->p2p_group_common_freqs);
1752 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1753 if (!wpa_s->p2p_group_common_freqs)
1754 return;
1755
1756 for (i = 0; i < len; i++) {
1757 if (!wpa_s->go_params->freq_list[i])
1758 break;
1759 wpa_s->p2p_group_common_freqs[i] =
1760 wpa_s->go_params->freq_list[i];
1761 }
1762 wpa_s->p2p_group_common_freqs_num = i;
1763}
1764
1765
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001766static void p2p_config_write(struct wpa_supplicant *wpa_s)
1767{
1768#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001769 if (wpa_s->p2pdev->conf->update_config &&
1770 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001771 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1772#endif /* CONFIG_NO_CONFIG_WRITE */
1773}
1774
1775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776static void p2p_go_configured(void *ctx, void *data)
1777{
1778 struct wpa_supplicant *wpa_s = ctx;
1779 struct p2p_go_neg_results *params = data;
1780 struct wpa_ssid *ssid;
1781
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001782 wpa_s->ap_configured_cb = NULL;
1783 wpa_s->ap_configured_cb_ctx = NULL;
1784 wpa_s->ap_configured_cb_data = NULL;
1785 if (!wpa_s->go_params) {
1786 wpa_printf(MSG_ERROR,
1787 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1788 return;
1789 }
1790
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001791 p2p_go_save_group_common_freqs(wpa_s, params);
1792 p2p_go_dump_common_freqs(wpa_s);
1793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 ssid = wpa_s->current_ssid;
1795 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1796 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1797 if (wpa_s->global->p2p_group_formation == wpa_s)
1798 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001799 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1800 params->passphrase[0] == '\0' ?
1801 params->psk : NULL,
1802 params->passphrase,
1803 wpa_s->global->p2p_dev_addr,
1804 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001805 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001806
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001807 if (wpa_s->p2pdev->p2ps_method_config_any) {
1808 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001809 wpa_dbg(wpa_s, MSG_DEBUG,
1810 "P2PS: Setting default PIN for ANY");
1811 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1812 "12345670", NULL, 0,
1813 0);
1814 } else {
1815 wpa_dbg(wpa_s, MSG_DEBUG,
1816 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001817 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001818 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001819 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001820 "12345670", NULL, 0, 0);
1821 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001822 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001823 }
1824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001825 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001826 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001827 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001828 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001829 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001830 wpas_p2p_add_psk_list(wpa_s, ssid);
1831 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001832
1833 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001834 params->persistent_group, 0,
1835 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 wpas_p2p_cross_connect_setup(wpa_s);
1837 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001838
1839 if (wpa_s->p2p_first_connection_timeout) {
1840 wpa_dbg(wpa_s, MSG_DEBUG,
1841 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1842 wpa_s->p2p_first_connection_timeout);
1843 wpa_s->p2p_go_group_formation_completed = 0;
1844 wpa_s->global->p2p_group_formation = wpa_s;
1845 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001846 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001847 eloop_register_timeout(
1848 wpa_s->p2p_first_connection_timeout, 0,
1849 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001850 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001851 }
1852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 return;
1854 }
1855
1856 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1857 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1858 params->peer_interface_addr)) {
1859 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1860 "filtering");
1861 return;
1862 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001863 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001865 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001866#ifdef CONFIG_WPS_NFC
1867 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001868 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001869 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001870 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001871 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1872 return;
1873 }
1874 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001875 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1876 wpa_s->p2pdev->p2p_oob_dev_pw,
1877 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1878 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001879#endif /* CONFIG_WPS_NFC */
1880 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001882 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 os_free(wpa_s->go_params);
1884 wpa_s->go_params = NULL;
1885}
1886
1887
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001888/**
1889 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1890 * @freq: Frequency (MHz) to convert
1891 * @op_class: Buffer for returning operating class
1892 * @op_edmg_channel: Buffer for returning channel number
1893 * Returns: 0 on success, -1 on failure
1894 *
1895 * This can be used to find the highest channel bonding which includes the
1896 * specified frequency.
1897 */
1898static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1899 unsigned int freq,
1900 u8 *op_class, u8 *op_edmg_channel)
1901{
1902 struct hostapd_hw_modes *hwmode;
1903 struct ieee80211_edmg_config edmg;
1904 unsigned int i;
1905 enum chan_width chanwidth[] = {
1906 CHAN_WIDTH_8640,
1907 CHAN_WIDTH_6480,
1908 CHAN_WIDTH_4320,
1909 };
1910
1911 if (!wpa_s->hw.modes)
1912 return -1;
1913
1914 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07001915 HOSTAPD_MODE_IEEE80211AD, 0);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001916 if (!hwmode) {
1917 wpa_printf(MSG_ERROR,
1918 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1919 return -1;
1920 }
1921
1922 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1923 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1924 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1925 op_class,
1926 op_edmg_channel) < 0)
1927 continue;
1928
1929 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1930 if (edmg.channels &&
1931 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1932 wpa_printf(MSG_DEBUG,
1933 "Freq %u to EDMG channel %u at opclass %u",
1934 freq, *op_edmg_channel, *op_class);
1935 return 0;
1936 }
1937 }
1938
1939 return -1;
1940}
1941
1942
1943int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
1944 struct p2p_go_neg_results *params)
1945{
1946 u8 op_channel, op_class;
1947 int freq;
1948
1949 /* Try social channel as primary channel frequency */
1950 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
1951
1952 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
1953 &op_channel) == 0) {
1954 wpa_printf(MSG_DEBUG,
1955 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
1956 freq, op_channel, op_class);
1957 params->freq = freq;
1958 return 0;
1959 }
1960
1961 return -1;
1962}
1963
1964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1966 struct p2p_go_neg_results *params,
1967 int group_formation)
1968{
1969 struct wpa_ssid *ssid;
1970
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001971 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1972 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1973 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1974 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977
1978 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001979 if (ssid == NULL) {
1980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001982 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001984 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001985 wpa_s->p2p_go_group_formation_completed = 0;
1986 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001987 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 wpa_config_set_network_defaults(ssid);
1990 ssid->temporary = 1;
1991 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001992 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1994 WPAS_MODE_P2P_GO;
1995 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001996 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001997 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001998 ssid->max_oper_chwidth = params->max_oper_chwidth;
1999 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002000 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002001 if (params->edmg) {
2002 u8 op_channel, op_class;
2003
2004 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2005 &op_class, &op_channel)) {
2006 ssid->edmg_channel = op_channel;
2007 ssid->enable_edmg = params->edmg;
2008 } else {
2009 wpa_dbg(wpa_s, MSG_DEBUG,
2010 "P2P: Could not match EDMG channel, freq %d, for GO",
2011 params->freq);
2012 }
2013 }
2014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 ssid->ssid = os_zalloc(params->ssid_len + 1);
2016 if (ssid->ssid) {
2017 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2018 ssid->ssid_len = params->ssid_len;
2019 }
2020 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2021 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2022 ssid->proto = WPA_PROTO_RSN;
2023 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 ssid->group_cipher = WPA_CIPHER_CCMP;
2025 if (params->freq > 56160) {
2026 /*
2027 * Enable GCMP instead of CCMP as pairwise_cipher and
2028 * group_cipher in 60 GHz.
2029 */
2030 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2031 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002032 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2033 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002034 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002035 if (os_strlen(params->passphrase) > 0) {
2036 ssid->passphrase = os_strdup(params->passphrase);
2037 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002038 wpa_msg_global(wpa_s, MSG_ERROR,
2039 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002040 wpa_config_remove_network(wpa_s->conf, ssid->id);
2041 return;
2042 }
2043 } else
2044 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002045 ssid->psk_set = params->psk_set;
2046 if (ssid->psk_set)
2047 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002048 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002049 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002050 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051
2052 wpa_s->ap_configured_cb = p2p_go_configured;
2053 wpa_s->ap_configured_cb_ctx = wpa_s;
2054 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002055 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002056 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057 wpa_s->reassociate = 1;
2058 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2060 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 wpa_supplicant_req_scan(wpa_s, 0, 0);
2062}
2063
2064
2065static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2066 const struct wpa_supplicant *src)
2067{
2068 struct wpa_config *d;
2069 const struct wpa_config *s;
2070
2071 d = dst->conf;
2072 s = src->conf;
2073
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002074#define C(n) \
2075do { \
2076 if (s->n && !d->n) \
2077 d->n = os_strdup(s->n); \
2078} while (0)
2079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 C(device_name);
2081 C(manufacturer);
2082 C(model_name);
2083 C(model_number);
2084 C(serial_number);
2085 C(config_methods);
2086#undef C
2087
2088 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2089 os_memcpy(d->sec_device_type, s->sec_device_type,
2090 sizeof(d->sec_device_type));
2091 d->num_sec_device_types = s->num_sec_device_types;
2092
2093 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002094 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002097 d->max_num_sta = s->max_num_sta;
2098 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002099 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002100 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002101 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002102 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002103 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002104 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002105 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002106
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002107 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2108 !d->wps_nfc_pw_from_config) {
2109 wpabuf_free(d->wps_nfc_dh_privkey);
2110 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002111 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2112 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2113 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002114 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002115 d->go_interworking = s->go_interworking;
2116 d->go_access_network_type = s->go_access_network_type;
2117 d->go_internet = s->go_internet;
2118 d->go_venue_group = s->go_venue_group;
2119 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120}
2121
2122
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002123static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2124 char *ifname, size_t len)
2125{
2126 char *ifname_ptr = wpa_s->ifname;
2127
2128 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2129 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2130 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2131 }
2132
2133 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2134 if (os_strlen(ifname) >= IFNAMSIZ &&
2135 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002136 int res;
2137
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002138 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002139 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2140 if (os_snprintf_error(len, res) && len)
2141 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002142 }
2143}
2144
2145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2147 enum wpa_driver_if_type type)
2148{
2149 char ifname[120], force_ifname[120];
2150
2151 if (wpa_s->pending_interface_name[0]) {
2152 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2153 "- skip creation of a new one");
2154 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2155 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2156 "unknown?! ifname='%s'",
2157 wpa_s->pending_interface_name);
2158 return -1;
2159 }
2160 return 0;
2161 }
2162
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002163 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 force_ifname[0] = '\0';
2165
2166 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2167 ifname);
2168 wpa_s->p2p_group_idx++;
2169
2170 wpa_s->pending_interface_type = type;
2171 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2172 wpa_s->pending_interface_addr, NULL) < 0) {
2173 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2174 "interface");
2175 return -1;
2176 }
2177
Jimmy Chen36c21992018-11-29 16:46:43 +08002178 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002179 random_mac_addr(wpa_s->pending_interface_addr);
2180 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2181 " for the group",
2182 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002183 }
2184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 if (force_ifname[0]) {
2186 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2187 force_ifname);
2188 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2189 sizeof(wpa_s->pending_interface_name));
2190 } else
2191 os_strlcpy(wpa_s->pending_interface_name, ifname,
2192 sizeof(wpa_s->pending_interface_name));
2193 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2194 MACSTR, wpa_s->pending_interface_name,
2195 MAC2STR(wpa_s->pending_interface_addr));
2196
2197 return 0;
2198}
2199
2200
2201static void wpas_p2p_remove_pending_group_interface(
2202 struct wpa_supplicant *wpa_s)
2203{
2204 if (!wpa_s->pending_interface_name[0] ||
2205 is_zero_ether_addr(wpa_s->pending_interface_addr))
2206 return; /* No pending virtual interface */
2207
2208 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2209 wpa_s->pending_interface_name);
2210 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2211 wpa_s->pending_interface_name);
2212 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2213 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002214 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215}
2216
2217
2218static struct wpa_supplicant *
2219wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2220{
2221 struct wpa_interface iface;
2222 struct wpa_supplicant *group_wpa_s;
2223
2224 if (!wpa_s->pending_interface_name[0]) {
2225 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2226 if (!wpas_p2p_create_iface(wpa_s))
2227 return NULL;
2228 /*
2229 * Something has forced us to remove the pending interface; try
2230 * to create a new one and hope for the best that we will get
2231 * the same local address.
2232 */
2233 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2234 WPA_IF_P2P_CLIENT) < 0)
2235 return NULL;
2236 }
2237
2238 os_memset(&iface, 0, sizeof(iface));
2239 iface.ifname = wpa_s->pending_interface_name;
2240 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002241 if (wpa_s->conf->ctrl_interface == NULL &&
2242 wpa_s->parent != wpa_s &&
2243 wpa_s->p2p_mgmt &&
2244 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2245 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2246 else
2247 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002249 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 if (group_wpa_s == NULL) {
2251 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2252 "wpa_supplicant interface");
2253 return NULL;
2254 }
2255 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2257 P2P_GROUP_INTERFACE_CLIENT;
2258 wpa_s->global->p2p_group_formation = group_wpa_s;
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 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2262
Jimmy Chen36c21992018-11-29 16:46:43 +08002263 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002264 if (wpa_drv_set_mac_addr(group_wpa_s,
2265 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002266 wpa_msg(group_wpa_s, MSG_INFO,
2267 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002268 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2269 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002270 return NULL;
2271 }
2272
2273 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2274 wpa_msg(group_wpa_s, MSG_INFO,
2275 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002276 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2277 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002278 return NULL;
2279 }
2280
Hai Shalom74f70d42019-02-11 14:42:39 -08002281 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2282 " for the group",
2283 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002284 }
2285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 return group_wpa_s;
2287}
2288
2289
2290static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2291 void *timeout_ctx)
2292{
2293 struct wpa_supplicant *wpa_s = eloop_ctx;
2294 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002295 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002296}
2297
2298
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002299static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2300 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002301{
2302 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002303 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304 if (wpa_s->global->p2p)
2305 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002306 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307}
2308
2309
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002310static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2311{
2312 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2313 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002314 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002315 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002316 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002317 wpa_s->global->p2p_fail_on_wps_complete = 0;
2318}
2319
2320
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002321void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2322{
2323 if (wpa_s->global->p2p_group_formation != wpa_s)
2324 return;
2325 /* Speed up group formation timeout since this cannot succeed */
2326 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002327 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002328 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002329 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002330}
2331
2332
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002333static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334{
2335 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002336 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337
2338 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2339 wpa_drv_cancel_remain_on_channel(wpa_s);
2340 wpa_s->off_channel_freq = 0;
2341 wpa_s->roc_waiting_drv_freq = 0;
2342 }
2343
2344 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002345 wpa_msg_global(wpa_s, MSG_INFO,
2346 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2347 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002348 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349 wpas_p2p_remove_pending_group_interface(wpa_s);
2350 return;
2351 }
2352
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002353 if (!res->role_go) {
2354 /* Inform driver of the operating channel of GO. */
2355 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2356 }
2357
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002358 if (wpa_s->p2p_go_ht40)
2359 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002360 if (wpa_s->p2p_go_vht)
2361 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002362 if (wpa_s->p2p_go_he)
2363 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002364 if (wpa_s->p2p_go_edmg)
2365 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002366 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2367 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002368
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002369 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2370 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2371 " wps_method=%s",
2372 res->role_go ? "GO" : "client", res->freq, res->ht40,
2373 MAC2STR(res->peer_device_addr),
2374 MAC2STR(res->peer_interface_addr),
2375 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377
Dmitry Shmidt04949592012-07-19 12:16:46 -07002378 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2379 struct wpa_ssid *ssid;
2380 ssid = wpa_config_get_network(wpa_s->conf,
2381 wpa_s->p2p_persistent_id);
2382 if (ssid && ssid->disabled == 2 &&
2383 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2384 size_t len = os_strlen(ssid->passphrase);
2385 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2386 "on requested persistent group");
2387 os_memcpy(res->passphrase, ssid->passphrase, len);
2388 res->passphrase[len] = '\0';
2389 }
2390 }
2391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002393 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2395 if (group_wpa_s == NULL) {
2396 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002397 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2398 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002399 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400 return;
2401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2403 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002405 group_wpa_s = wpa_s->parent;
2406 wpa_s->global->p2p_group_formation = group_wpa_s;
2407 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002408 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002411 group_wpa_s->p2p_in_provisioning = 1;
2412 group_wpa_s->p2pdev = wpa_s;
2413 if (group_wpa_s != wpa_s) {
2414 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2415 sizeof(group_wpa_s->p2p_pin));
2416 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2417 }
2418 if (res->role_go) {
2419 wpas_start_wps_go(group_wpa_s, res, 1);
2420 } else {
2421 os_get_reltime(&group_wpa_s->scan_min_time);
2422 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 }
2424
Hai Shalomfdcde762020-04-02 11:19:20 -07002425 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2427
2428 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2429 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2430 (res->peer_config_timeout % 100) * 10000,
2431 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2432}
2433
2434
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002435static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2436 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437{
2438 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002440 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2441 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002443 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444}
2445
2446
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002447static void wpas_dev_found(void *ctx, const u8 *addr,
2448 const struct p2p_peer_info *info,
2449 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002451 u8 *wfd_dev_info = NULL;
2452 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002453#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454 struct wpa_supplicant *wpa_s = ctx;
2455 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002456 char *wfd_dev_info_hex = NULL;
2457
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002458#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002459 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2460 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002461 if (wfd_dev_info_hex) {
2462 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2463 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2464 // Only used for notification, so not handling error.
2465 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2466 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002467#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002468
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002469 if (info->p2ps_instance) {
2470 char str[256];
2471 const u8 *buf = wpabuf_head(info->p2ps_instance);
2472 size_t len = wpabuf_len(info->p2ps_instance);
2473
2474 while (len) {
2475 u32 id;
2476 u16 methods;
2477 u8 str_len;
2478
2479 if (len < 4 + 2 + 1)
2480 break;
2481 id = WPA_GET_LE32(buf);
2482 buf += sizeof(u32);
2483 methods = WPA_GET_BE16(buf);
2484 buf += sizeof(u16);
2485 str_len = *buf++;
2486 if (str_len > len - 4 - 2 - 1)
2487 break;
2488 os_memcpy(str, buf, str_len);
2489 str[str_len] = '\0';
2490 buf += str_len;
2491 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2492
2493 wpa_msg_global(wpa_s, MSG_INFO,
2494 P2P_EVENT_DEVICE_FOUND MACSTR
2495 " p2p_dev_addr=" MACSTR
2496 " pri_dev_type=%s name='%s'"
2497 " config_methods=0x%x"
2498 " dev_capab=0x%x"
2499 " group_capab=0x%x"
2500 " adv_id=%x asp_svc=%s%s",
2501 MAC2STR(addr),
2502 MAC2STR(info->p2p_device_addr),
2503 wps_dev_type_bin2str(
2504 info->pri_dev_type,
2505 devtype, sizeof(devtype)),
2506 info->device_name, methods,
2507 info->dev_capab, info->group_capab,
2508 id, str,
2509 info->vendor_elems ?
2510 " vendor_elems=1" : "");
2511 }
2512 goto done;
2513 }
2514
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002515 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2516 " p2p_dev_addr=" MACSTR
2517 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002518 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002519 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2520 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2521 sizeof(devtype)),
2522 info->device_name, info->config_methods,
2523 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002524 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002525 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002526 info->vendor_elems ? " vendor_elems=1" : "",
2527 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002529done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002530 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002531#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002532
Roshan Piusfd2fd662017-01-23 13:41:57 -08002533 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2534 wfd_dev_info_len, new_device);
2535 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536}
2537
2538
2539static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2540{
2541 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002543 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2544 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2547}
2548
2549
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002550static void wpas_find_stopped(void *ctx)
2551{
2552 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002553
2554 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2555 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2556
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002557 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002558 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002559}
2560
2561
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002562struct wpas_p2p_listen_work {
2563 unsigned int freq;
2564 unsigned int duration;
2565 struct wpabuf *probe_resp_ie;
2566};
2567
2568
2569static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2570{
2571 if (lwork == NULL)
2572 return;
2573 wpabuf_free(lwork->probe_resp_ie);
2574 os_free(lwork);
2575}
2576
2577
2578static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2579{
2580 struct wpas_p2p_listen_work *lwork;
2581
2582 if (!wpa_s->p2p_listen_work)
2583 return;
2584
2585 lwork = wpa_s->p2p_listen_work->ctx;
2586 wpas_p2p_listen_work_free(lwork);
2587 radio_work_done(wpa_s->p2p_listen_work);
2588 wpa_s->p2p_listen_work = NULL;
2589}
2590
2591
2592static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2593{
2594 struct wpa_supplicant *wpa_s = work->wpa_s;
2595 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002596 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002597
2598 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002599 if (work->started) {
2600 wpa_s->p2p_listen_work = NULL;
2601 wpas_stop_listen(wpa_s);
2602 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002603 wpas_p2p_listen_work_free(lwork);
2604 return;
2605 }
2606
2607 wpa_s->p2p_listen_work = work;
2608
2609 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2610
2611 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2612 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2613 "report received Probe Request frames");
2614 wpas_p2p_listen_work_done(wpa_s);
2615 return;
2616 }
2617
2618 wpa_s->pending_listen_freq = lwork->freq;
2619 wpa_s->pending_listen_duration = lwork->duration;
2620
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002621 duration = lwork->duration;
2622#ifdef CONFIG_TESTING_OPTIONS
2623 if (wpa_s->extra_roc_dur) {
2624 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2625 duration, duration + wpa_s->extra_roc_dur);
2626 duration += wpa_s->extra_roc_dur;
2627 }
2628#endif /* CONFIG_TESTING_OPTIONS */
2629
2630 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002631 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2632 "to remain on channel (%u MHz) for Listen "
2633 "state", lwork->freq);
2634 wpas_p2p_listen_work_done(wpa_s);
2635 wpa_s->pending_listen_freq = 0;
2636 return;
2637 }
2638 wpa_s->off_channel_freq = 0;
2639 wpa_s->roc_waiting_drv_freq = lwork->freq;
2640}
2641
2642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643static int wpas_start_listen(void *ctx, unsigned int freq,
2644 unsigned int duration,
2645 const struct wpabuf *probe_resp_ie)
2646{
2647 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002648 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002650 if (wpa_s->p2p_listen_work) {
2651 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 return -1;
2653 }
2654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002655 lwork = os_zalloc(sizeof(*lwork));
2656 if (lwork == NULL)
2657 return -1;
2658 lwork->freq = freq;
2659 lwork->duration = duration;
2660 if (probe_resp_ie) {
2661 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2662 if (lwork->probe_resp_ie == NULL) {
2663 wpas_p2p_listen_work_free(lwork);
2664 return -1;
2665 }
2666 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002668 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2669 lwork) < 0) {
2670 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 return -1;
2672 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673
2674 return 0;
2675}
2676
2677
2678static void wpas_stop_listen(void *ctx)
2679{
2680 struct wpa_supplicant *wpa_s = ctx;
2681 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2682 wpa_drv_cancel_remain_on_channel(wpa_s);
2683 wpa_s->off_channel_freq = 0;
2684 wpa_s->roc_waiting_drv_freq = 0;
2685 }
2686 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002687
2688 /*
2689 * Don't cancel Probe Request RX reporting for a connected P2P Client
2690 * handling Probe Request frames.
2691 */
2692 if (!wpa_s->p2p_cli_probe)
2693 wpa_drv_probe_req_report(wpa_s, 0);
2694
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002695 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696}
2697
2698
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002699static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2700 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701{
2702 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002703 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002704 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705}
2706
2707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2709 const u8 *peer, const char *params,
2710 unsigned int generated_pin)
2711{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002712 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2713 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714}
2715
2716
2717static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2718 const u8 *peer, const char *params)
2719{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002720 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2721 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722}
2723
2724
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002725static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2726 const u8 *dev_addr, const u8 *pri_dev_type,
2727 const char *dev_name, u16 supp_config_methods,
2728 u8 dev_capab, u8 group_capab, const u8 *group_id,
2729 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730{
2731 struct wpa_supplicant *wpa_s = ctx;
2732 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002733 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 u8 empty_dev_type[8];
2735 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002737 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002738
2739 if (group_id) {
2740 for (group = wpa_s->global->ifaces; group; group = group->next)
2741 {
2742 struct wpa_ssid *s = group->current_ssid;
2743 if (s != NULL &&
2744 s->mode == WPAS_MODE_P2P_GO &&
2745 group_id_len - ETH_ALEN == s->ssid_len &&
2746 os_memcmp(group_id + ETH_ALEN, s->ssid,
2747 s->ssid_len) == 0)
2748 break;
2749 }
2750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751
2752 if (pri_dev_type == NULL) {
2753 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2754 pri_dev_type = empty_dev_type;
2755 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002756 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2757 " pri_dev_type=%s name='%s' config_methods=0x%x "
2758 "dev_capab=0x%x group_capab=0x%x%s%s",
2759 MAC2STR(dev_addr),
2760 wps_dev_type_bin2str(pri_dev_type, devtype,
2761 sizeof(devtype)),
2762 dev_name, supp_config_methods, dev_capab, group_capab,
2763 group ? " group=" : "",
2764 group ? group->ifname : "");
2765 if (os_snprintf_error(sizeof(params), res))
2766 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002770 if (wps_generate_pin(&generated_pin) < 0) {
2771 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2772 wpas_notify_p2p_provision_discovery(
2773 wpa_s, peer, 0 /* response */,
2774 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2775 return;
2776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 wpas_prov_disc_local_display(wpa_s, peer, params,
2778 generated_pin);
2779 } else if (config_methods & WPS_CONFIG_KEYPAD)
2780 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2781 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002782 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2783 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002784
2785 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2786 P2P_PROV_DISC_SUCCESS,
2787 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788}
2789
2790
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002791static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792{
2793 struct wpa_supplicant *wpa_s = ctx;
2794 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002795 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002797 if (wpa_s->pending_pd_before_join &&
2798 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2799 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2800 wpa_s->pending_pd_before_join = 0;
2801 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2802 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002803 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002804 return;
2805 }
2806
Dmitry Shmidt04949592012-07-19 12:16:46 -07002807 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002808 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2809 int res;
2810
2811 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2812 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2813 if (os_snprintf_error(sizeof(params), res))
2814 params[sizeof(params) - 1] = '\0';
2815 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002816 params[0] = '\0';
2817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002819 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002821 if (wps_generate_pin(&generated_pin) < 0) {
2822 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2823 wpas_notify_p2p_provision_discovery(
2824 wpa_s, peer, 0 /* response */,
2825 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2826 return;
2827 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002828 wpas_prov_disc_local_display(wpa_s, peer, params,
2829 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002831 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2832 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833
Jouni Malinen75ecf522011-06-27 15:19:46 -07002834 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2835 P2P_PROV_DISC_SUCCESS,
2836 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837}
2838
2839
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002841 enum p2p_prov_disc_status status,
2842 u32 adv_id, const u8 *adv_mac,
2843 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002844{
2845 struct wpa_supplicant *wpa_s = ctx;
2846
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847 if (wpa_s->p2p_fallback_to_go_neg) {
2848 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2849 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002850 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002851 P2P_EVENT_FALLBACK_TO_GO_NEG
2852 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002853 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2854 return;
2855 }
2856
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002857 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002858 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002859 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2860 "join-existing-group operation (no ACK for PD "
2861 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002862 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002863 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002864 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002865
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002866 if (adv_id && adv_mac && deferred_session_resp) {
2867 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2868 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2869 " deferred_session_resp='%s'",
2870 MAC2STR(peer), status, adv_id,
2871 deferred_session_resp);
2872 } else if (adv_id && adv_mac) {
2873 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2874 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2875 MAC2STR(peer), status, adv_id);
2876 } else {
2877 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2878 " p2p_dev_addr=" MACSTR " status=%d",
2879 MAC2STR(peer), status);
2880 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002881
Jouni Malinen75ecf522011-06-27 15:19:46 -07002882 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2883 status, 0, 0);
2884}
2885
2886
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002887static int freq_included(struct wpa_supplicant *wpa_s,
2888 const struct p2p_channels *channels,
2889 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002890{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002891 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2892 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2893 return 1;
2894 return 0;
2895}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002896
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002897
2898static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2899{
2900 unsigned int num = P2P_MAX_CHANNELS;
2901 int *common_freqs;
2902 int ret;
2903
2904 p2p_go_dump_common_freqs(wpa_s);
2905 common_freqs = os_calloc(num, sizeof(int));
2906 if (!common_freqs)
2907 return;
2908
2909 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2910 if (ret < 0) {
2911 wpa_dbg(wpa_s, MSG_DEBUG,
2912 "P2P: Failed to get group common freqs");
2913 os_free(common_freqs);
2914 return;
2915 }
2916
2917 os_free(wpa_s->p2p_group_common_freqs);
2918 wpa_s->p2p_group_common_freqs = common_freqs;
2919 wpa_s->p2p_group_common_freqs_num = num;
2920 p2p_go_dump_common_freqs(wpa_s);
2921}
2922
2923
2924/*
2925 * Check if the given frequency is one of the possible operating frequencies
2926 * set after the completion of the GO Negotiation.
2927 */
2928static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2929{
2930 unsigned int i;
2931
2932 p2p_go_dump_common_freqs(wpa_s);
2933
2934 /* assume no restrictions */
2935 if (!wpa_s->p2p_group_common_freqs_num)
2936 return 1;
2937
2938 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2939 if (wpa_s->p2p_group_common_freqs[i] == freq)
2940 return 1;
2941 }
2942 return 0;
2943}
2944
2945
2946static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2947 struct sta_info *sta, void *ctx)
2948{
2949 int *ecsa_support = ctx;
2950
2951 *ecsa_support &= sta->ecsa_supported;
2952
2953 return 0;
2954}
2955
2956
2957/* Check if all the peers support eCSA */
2958static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2959{
2960 int ecsa_support = 1;
2961
2962 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2963 &ecsa_support);
2964
2965 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002966}
2967
2968
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002969/**
2970 * Pick the best frequency to use from all the currently used frequencies.
2971 */
2972static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2973 struct wpa_used_freq_data *freqs,
2974 unsigned int num)
2975{
2976 unsigned int i, c;
2977
2978 /* find a candidate freq that is supported by P2P */
2979 for (c = 0; c < num; c++)
2980 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2981 break;
2982
2983 if (c == num)
2984 return 0;
2985
2986 /* once we have a candidate, try to find a 'better' one */
2987 for (i = c + 1; i < num; i++) {
2988 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2989 continue;
2990
2991 /*
2992 * 1. Infrastructure station interfaces have higher preference.
2993 * 2. P2P Clients have higher preference.
2994 * 3. All others.
2995 */
2996 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2997 c = i;
2998 break;
2999 }
3000
3001 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3002 c = i;
3003 }
3004 return freqs[c].freq;
3005}
3006
3007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3009 const u8 *go_dev_addr, const u8 *ssid,
3010 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003011 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003012 const struct p2p_channels *channels,
3013 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014{
3015 struct wpa_supplicant *wpa_s = ctx;
3016 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003017 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003019 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020
3021 if (!persistent_group) {
3022 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003023 " to join an active group (SSID: %s)",
3024 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3026 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3027 == 0 ||
3028 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3029 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3030 "authorized invitation");
3031 goto accept_inv;
3032 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003033
3034#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003035 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3036 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003037 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003038 wpa_s->p2p_wps_method = WPS_NFC;
3039 wpa_s->pending_join_wps_method = WPS_NFC;
3040 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003041 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003042 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003043 bssid, ETH_ALEN);
3044 goto accept_inv;
3045 }
3046#endif /* CONFIG_WPS_NFC */
3047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 /*
3049 * Do not accept the invitation automatically; notify user and
3050 * request approval.
3051 */
3052 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3053 }
3054
3055 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3056 if (grp) {
3057 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3058 "running persistent group");
3059 if (*go)
3060 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3061 goto accept_inv;
3062 }
3063
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003064 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3065 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3066 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3067 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003068 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003069 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3071
3072 for (s = wpa_s->conf->ssid; s; s = s->next) {
3073 if (s->disabled == 2 &&
3074 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3075 s->ssid_len == ssid_len &&
3076 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3077 break;
3078 }
3079
3080 if (!s) {
3081 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3082 " requested reinvocation of an unknown group",
3083 MAC2STR(sa));
3084 return P2P_SC_FAIL_UNKNOWN_GROUP;
3085 }
3086
3087 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3088 *go = 1;
3089 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3090 wpa_printf(MSG_DEBUG, "P2P: The only available "
3091 "interface is already in use - reject "
3092 "invitation");
3093 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3094 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003095 if (wpa_s->p2p_mgmt)
3096 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3097 ETH_ALEN);
3098 else
3099 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100 } else if (s->mode == WPAS_MODE_P2P_GO) {
3101 *go = 1;
3102 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3103 {
3104 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3105 "interface address for the group");
3106 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3107 }
3108 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3109 ETH_ALEN);
3110 }
3111
3112accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003113 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3114
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003115 best_freq = 0;
3116 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3117 sizeof(struct wpa_used_freq_data));
3118 if (freqs) {
3119 int num_channels = wpa_s->num_multichan_concurrent;
3120 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3121 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3122 os_free(freqs);
3123 }
3124
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003125 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003126 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003127 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003128 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003129
3130 if (wpa_s->num_multichan_concurrent < 2 ||
3131 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3132 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 -07003133 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 }
3136
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003137 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3138 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003139 if (*go == 0) {
3140 /* We are the client */
3141 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3142 "running a GO but we are capable of MCC, "
3143 "figure out the best channel to use");
3144 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003145 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003146 /* We are the GO, and *force_freq is not in the
3147 * intersection */
3148 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3149 "in intersection but we are capable of MCC, "
3150 "figure out the best channel to use",
3151 *force_freq);
3152 *force_freq = 0;
3153 }
3154 }
3155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 return P2P_SC_SUCCESS;
3157}
3158
3159
3160static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3161 const u8 *ssid, size_t ssid_len,
3162 const u8 *go_dev_addr, u8 status,
3163 int op_freq)
3164{
3165 struct wpa_supplicant *wpa_s = ctx;
3166 struct wpa_ssid *s;
3167
3168 for (s = wpa_s->conf->ssid; s; s = s->next) {
3169 if (s->disabled == 2 &&
3170 s->ssid_len == ssid_len &&
3171 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3172 break;
3173 }
3174
3175 if (status == P2P_SC_SUCCESS) {
3176 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003177 " was accepted; op_freq=%d MHz, SSID=%s",
3178 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003180 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003181 if (go) {
3182 wpa_msg_global(wpa_s, MSG_INFO,
3183 P2P_EVENT_INVITATION_ACCEPTED
3184 "sa=" MACSTR
3185 " persistent=%d freq=%d",
3186 MAC2STR(sa), s->id, op_freq);
3187 } else {
3188 wpa_msg_global(wpa_s, MSG_INFO,
3189 P2P_EVENT_INVITATION_ACCEPTED
3190 "sa=" MACSTR
3191 " persistent=%d",
3192 MAC2STR(sa), s->id);
3193 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003195 wpa_s, s, go, 0, op_freq, 0,
3196 wpa_s->conf->p2p_go_ht40,
3197 wpa_s->conf->p2p_go_vht,
3198 0,
3199 wpa_s->conf->p2p_go_he,
3200 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003201 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3202 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003204 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003205 wpa_msg_global(wpa_s, MSG_INFO,
3206 P2P_EVENT_INVITATION_ACCEPTED
3207 "sa=" MACSTR " go_dev_addr=" MACSTR
3208 " bssid=" MACSTR " unknown-network",
3209 MAC2STR(sa), MAC2STR(go_dev_addr),
3210 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003212 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003213 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 }
3215 return;
3216 }
3217
3218 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3219 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3220 " was rejected (status %u)", MAC2STR(sa), status);
3221 return;
3222 }
3223
3224 if (!s) {
3225 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003226 wpa_msg_global(wpa_s, MSG_INFO,
3227 P2P_EVENT_INVITATION_RECEIVED
3228 "sa=" MACSTR " go_dev_addr=" MACSTR
3229 " bssid=" MACSTR " unknown-network",
3230 MAC2STR(sa), MAC2STR(go_dev_addr),
3231 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003233 wpa_msg_global(wpa_s, MSG_INFO,
3234 P2P_EVENT_INVITATION_RECEIVED
3235 "sa=" MACSTR " go_dev_addr=" MACSTR
3236 " unknown-network",
3237 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003239 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3240 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241 return;
3242 }
3243
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003244 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3246 "sa=" MACSTR " persistent=%d freq=%d",
3247 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003248 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003249 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3250 "sa=" MACSTR " persistent=%d",
3251 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003252 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003253 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3254 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255}
3256
3257
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003258static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3259 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003260 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003261{
3262 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003263 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003264
3265 if (ssid == NULL)
3266 return;
3267
3268 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003269 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003270 ETH_ALEN) == 0)
3271 break;
3272 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003273 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003274 if (ssid->mode != WPAS_MODE_P2P_GO &&
3275 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3276 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3277 "due to invitation result", ssid->id);
3278 wpas_notify_network_removed(wpa_s, ssid);
3279 wpa_config_remove_network(wpa_s->conf, ssid->id);
3280 return;
3281 }
3282 return; /* Peer not found in client list */
3283 }
3284
3285 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003286 "group %d client list%s",
3287 MAC2STR(peer), ssid->id,
3288 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003289 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3290 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3291 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003292 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003293 if (p2p_wpa_s->conf->update_config &&
3294 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003295 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003296}
3297
3298
3299static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3300 const u8 *peer)
3301{
3302 struct wpa_ssid *ssid;
3303
3304 wpa_s = wpa_s->global->p2p_invite_group;
3305 if (wpa_s == NULL)
3306 return; /* No known invitation group */
3307 ssid = wpa_s->current_ssid;
3308 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3309 !ssid->p2p_persistent_group)
3310 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003311 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003312 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003313 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003314}
3315
3316
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003317static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003318 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003319 const u8 *peer, int neg_freq,
3320 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321{
3322 struct wpa_supplicant *wpa_s = ctx;
3323 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003324 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325
3326 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003327 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3328 "status=%d " MACSTR,
3329 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003331 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3332 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333 }
3334 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3335
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003336 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3337 status, MAC2STR(peer));
3338 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003339 struct wpa_supplicant *group_if =
3340 wpa_s->global->p2p_invite_group;
3341
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003342 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3343 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003344
3345 /*
3346 * Invitation to an active group. If this is successful and we
3347 * are the GO, set the client wait to postpone some concurrent
3348 * operations and to allow provisioning and connection to happen
3349 * more quickly.
3350 */
3351 if (status == P2P_SC_SUCCESS &&
3352 group_if && group_if->current_ssid &&
3353 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3354 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3355#ifdef CONFIG_TESTING_OPTIONS
3356 if (group_if->p2p_go_csa_on_inv) {
3357 wpa_printf(MSG_DEBUG,
3358 "Testing: force P2P GO CSA after invitation");
3359 eloop_cancel_timeout(
3360 wpas_p2p_reconsider_moving_go,
3361 wpa_s, NULL);
3362 eloop_register_timeout(
3363 0, 50000,
3364 wpas_p2p_reconsider_moving_go,
3365 wpa_s, NULL);
3366 }
3367#endif /* CONFIG_TESTING_OPTIONS */
3368 }
3369 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003370 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003371
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003372 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3373 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3374 "invitation exchange to indicate readiness for "
3375 "re-invocation");
3376 }
3377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003379 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3380 ssid = wpa_config_get_network(
3381 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003382 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384 wpas_p2p_remove_pending_group_interface(wpa_s);
3385 return;
3386 }
3387
3388 ssid = wpa_config_get_network(wpa_s->conf,
3389 wpa_s->pending_invite_ssid_id);
3390 if (ssid == NULL) {
3391 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3392 "data matching with invitation");
3393 return;
3394 }
3395
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003396 /*
3397 * The peer could have missed our ctrl::ack frame for Invitation
3398 * Response and continue retransmitting the frame. To reduce the
3399 * likelihood of the peer not getting successful TX status for the
3400 * Invitation Response frame, wait a short time here before starting
3401 * the persistent group so that we will remain on the current channel to
3402 * acknowledge any possible retransmission from the peer.
3403 */
3404 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3405 "starting persistent group");
3406 os_sleep(0, 50000);
3407
Dmitry Shmidt15907092014-03-25 10:42:57 -07003408 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003409 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003410 freq = neg_freq;
3411 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003412 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003413 freq = peer_oper_freq;
3414 else
3415 freq = 0;
3416
3417 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3418 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003420 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003421 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003422 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003423 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003424 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003425 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003426 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003427 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003428 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003429 ssid->mode == WPAS_MODE_P2P_GO ?
3430 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003431 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003432}
3433
3434
Dmitry Shmidt04949592012-07-19 12:16:46 -07003435static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3436 unsigned int freq)
3437{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003438 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3439 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003440 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003441}
3442
3443
3444static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3445{
3446 reg->channel[reg->channels] = chan;
3447 reg->channels++;
3448}
3449
3450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003452 struct p2p_channels *chan,
3453 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454{
3455 int i, cla = 0;
3456
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003457 wpa_s->global->p2p_24ghz_social_channels = 1;
3458
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003459 os_memset(cli_chan, 0, sizeof(*cli_chan));
3460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3462 "band");
3463
3464 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3465 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003466 chan->reg_class[cla].channels = 0;
3467 for (i = 0; i < 11; i++) {
3468 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3469 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3470 }
3471 if (chan->reg_class[cla].channels)
3472 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473
3474 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3475 "band");
3476
3477 /* Operating class 115 - 5 GHz, channels 36-48 */
3478 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003479 chan->reg_class[cla].channels = 0;
3480 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3481 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3482 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3483 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3484 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3485 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3486 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3487 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3488 if (chan->reg_class[cla].channels)
3489 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490
3491 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3492 "band");
3493
3494 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3495 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003496 chan->reg_class[cla].channels = 0;
3497 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3498 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3499 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3500 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3501 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3502 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3503 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3504 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3505 if (chan->reg_class[cla].channels)
3506 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507
3508 chan->reg_classes = cla;
3509 return 0;
3510}
3511
3512
Dmitry Shmidt04949592012-07-19 12:16:46 -07003513static int has_channel(struct wpa_global *global,
3514 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515{
3516 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003517 unsigned int freq;
3518
3519 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3520 chan * 5;
3521 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003522 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003523
3524 for (i = 0; i < mode->num_channels; i++) {
3525 if (mode->channels[i].chan == chan) {
3526 if (flags)
3527 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003528 if (mode->channels[i].flag &
3529 (HOSTAPD_CHAN_DISABLED |
3530 HOSTAPD_CHAN_RADAR))
3531 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003532 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3533 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003534 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535 }
3536 }
3537
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003538 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539}
3540
3541
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003542static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3543 struct hostapd_hw_modes *mode,
3544 u8 channel)
3545{
3546 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003547 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003548
3549 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3550 return 0;
3551
3552 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3553 /*
3554 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3555 * so the center channel is 6 channels away from the start/end.
3556 */
3557 if (channel >= center_channels[i] - 6 &&
3558 channel <= center_channels[i] + 6)
3559 return center_channels[i];
3560
3561 return 0;
3562}
3563
3564
3565static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3566 struct hostapd_hw_modes *mode,
3567 u8 channel, u8 bw)
3568{
3569 u8 center_chan;
3570 int i, flags;
3571 enum chan_allowed res, ret = ALLOWED;
3572
3573 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3574 if (!center_chan)
3575 return NOT_ALLOWED;
3576 if (center_chan >= 58 && center_chan <= 138)
3577 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3578
3579 /* check all the channels are available */
3580 for (i = 0; i < 4; i++) {
3581 int adj_chan = center_chan - 6 + i * 4;
3582
3583 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3584 if (res == NOT_ALLOWED)
3585 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003586 if (res == NO_IR)
3587 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003588
3589 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3590 return NOT_ALLOWED;
3591 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3592 return NOT_ALLOWED;
3593 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3594 return NOT_ALLOWED;
3595 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3596 return NOT_ALLOWED;
3597 }
3598
3599 return ret;
3600}
3601
3602
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003603static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3604 struct hostapd_hw_modes *mode,
3605 u8 channel)
3606{
3607 u8 center_channels[] = { 50, 114 };
3608 unsigned int i;
3609
3610 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3611 return 0;
3612
3613 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3614 /*
3615 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3616 * so the center channel is 14 channels away from the start/end.
3617 */
3618 if (channel >= center_channels[i] - 14 &&
3619 channel <= center_channels[i] + 14)
3620 return center_channels[i];
3621
3622 return 0;
3623}
3624
3625
3626static enum chan_allowed wpas_p2p_verify_160mhz(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_160mhz(wpa_s, mode, channel);
3635 if (!center_chan)
3636 return NOT_ALLOWED;
3637 /* VHT 160 MHz uses DFS channels in most countries. */
3638
3639 /* Check all the channels are available */
3640 for (i = 0; i < 8; i++) {
3641 int adj_chan = center_chan - 14 + i * 4;
3642
3643 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3644 if (res == NOT_ALLOWED)
3645 return NOT_ALLOWED;
3646
3647 if (res == NO_IR)
3648 ret = NO_IR;
3649
3650 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3651 return NOT_ALLOWED;
3652 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3653 return NOT_ALLOWED;
3654 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3655 return NOT_ALLOWED;
3656 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3657 return NOT_ALLOWED;
3658 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3659 return NOT_ALLOWED;
3660 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3661 return NOT_ALLOWED;
3662 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3663 return NOT_ALLOWED;
3664 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3665 return NOT_ALLOWED;
3666 }
3667
3668 return ret;
3669}
3670
3671
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003672static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3673 struct hostapd_hw_modes *mode,
3674 u8 channel)
3675{
3676 struct ieee80211_edmg_config edmg;
3677
3678 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3679 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3680 return ALLOWED;
3681
3682 return NOT_ALLOWED;
3683}
3684
3685
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003686static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3687 struct hostapd_hw_modes *mode,
3688 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003689{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003690 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003691 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003692
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003693 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3694 if (bw == BW40MINUS) {
3695 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3696 return NOT_ALLOWED;
3697 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3698 } else if (bw == BW40PLUS) {
3699 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3700 return NOT_ALLOWED;
3701 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3702 } else if (bw == BW80) {
3703 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003704 } else if (bw == BW160) {
3705 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003706 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3707 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003708 }
3709
3710 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3711 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003712 if (res == NO_IR || res2 == NO_IR)
3713 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003714 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003715}
3716
3717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003719 struct p2p_channels *chan,
3720 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003722 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003723 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003725 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3727 "of all supported channels; assume dualband "
3728 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003729 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730 }
3731
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003732 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003733
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003734 for (op = 0; global_op_class[op].op_class; op++) {
3735 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003737 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003738
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003739 if (o->p2p == NO_P2P_SUPP)
3740 continue;
3741
Hai Shalomfdcde762020-04-02 11:19:20 -07003742 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3743 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 if (mode == NULL)
3745 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003746 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3747 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003749 enum chan_allowed res;
3750 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3751 if (res == ALLOWED) {
3752 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003753 if (cla == P2P_MAX_REG_CLASSES)
3754 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003755 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3756 o->op_class);
3757 reg = &chan->reg_class[cla];
3758 cla++;
3759 reg->reg_class = o->op_class;
3760 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003761 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3762 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003763 reg->channel[reg->channels] = ch;
3764 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003765 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003766 wpa_s->conf->p2p_add_cli_chan) {
3767 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003768 if (cli_cla == P2P_MAX_REG_CLASSES)
3769 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003770 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3771 o->op_class);
3772 cli_reg = &cli_chan->reg_class[cli_cla];
3773 cli_cla++;
3774 cli_reg->reg_class = o->op_class;
3775 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003776 if (cli_reg->channels ==
3777 P2P_MAX_REG_CLASS_CHANNELS)
3778 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003779 cli_reg->channel[cli_reg->channels] = ch;
3780 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 }
3783 if (reg) {
3784 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3785 reg->channel, reg->channels);
3786 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003787 if (cli_reg) {
3788 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3789 cli_reg->channel, cli_reg->channels);
3790 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 }
3792
3793 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003794 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 return 0;
3797}
3798
3799
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003800int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3801 struct hostapd_hw_modes *mode, u8 channel)
3802{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003803 int op;
3804 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003805
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003806 for (op = 0; global_op_class[op].op_class; op++) {
3807 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003808 u8 ch;
3809
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003810 if (o->p2p == NO_P2P_SUPP)
3811 continue;
3812
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003813 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3814 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003815 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3816 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003817 continue;
3818 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003819 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003820 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003821 }
3822 }
3823 return 0;
3824}
3825
3826
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003827int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3828 struct hostapd_hw_modes *mode, u8 channel)
3829{
3830 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3831 return 0;
3832
3833 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3834}
3835
3836
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003837int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3838 struct hostapd_hw_modes *mode, u8 channel)
3839{
3840 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3841 return 0;
3842 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3843}
3844
3845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003846static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3847 size_t buf_len)
3848{
3849 struct wpa_supplicant *wpa_s = ctx;
3850
3851 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3852 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3853 break;
3854 }
3855 if (wpa_s == NULL)
3856 return -1;
3857
3858 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3859}
3860
3861
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003862struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3863 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003864{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003865 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3866 struct wpa_ssid *s = wpa_s->current_ssid;
3867 if (s == NULL)
3868 continue;
3869 if (s->mode != WPAS_MODE_P2P_GO &&
3870 s->mode != WPAS_MODE_AP &&
3871 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3872 continue;
3873 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003874 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003875 continue;
3876 return wpa_s;
3877 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003878
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003879 return NULL;
3880
3881}
3882
3883
3884struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3885 const u8 *peer_dev_addr)
3886{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003887 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3888 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003889 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003890 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003891 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3892 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003893 }
3894
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003895 return NULL;
3896}
3897
3898
3899static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3900{
3901 struct wpa_supplicant *wpa_s = ctx;
3902
3903 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003904}
3905
3906
Dmitry Shmidt18463232014-01-24 12:29:41 -08003907static int wpas_is_concurrent_session_active(void *ctx)
3908{
3909 struct wpa_supplicant *wpa_s = ctx;
3910 struct wpa_supplicant *ifs;
3911
3912 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3913 if (ifs == wpa_s)
3914 continue;
3915 if (ifs->wpa_state > WPA_ASSOCIATED)
3916 return 1;
3917 }
3918 return 0;
3919}
3920
3921
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003922static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3923{
3924 struct wpa_supplicant *wpa_s = ctx;
3925 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3926}
3927
3928
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003929int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3930 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003931{
3932 struct wpa_interface iface;
3933 struct wpa_supplicant *p2pdev_wpa_s;
3934 char ifname[100];
3935 char force_name[100];
3936 int ret;
3937
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003938 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3939 wpa_s->ifname);
3940 if (os_snprintf_error(sizeof(ifname), ret))
3941 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003942 force_name[0] = '\0';
3943 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3944 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3945 force_name, wpa_s->pending_interface_addr, NULL);
3946 if (ret < 0) {
3947 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3948 return ret;
3949 }
3950 os_strlcpy(wpa_s->pending_interface_name, ifname,
3951 sizeof(wpa_s->pending_interface_name));
3952
3953 os_memset(&iface, 0, sizeof(iface));
3954 iface.p2p_mgmt = 1;
3955 iface.ifname = wpa_s->pending_interface_name;
3956 iface.driver = wpa_s->driver->name;
3957 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003958
3959 /*
3960 * If a P2P Device configuration file was given, use it as the interface
3961 * configuration file (instead of using parent's configuration file.
3962 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003963 if (conf_p2p_dev) {
3964 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003965 iface.ctrl_interface = NULL;
3966 } else {
3967 iface.confname = wpa_s->confname;
3968 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3969 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003970
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003971 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003972 if (!p2pdev_wpa_s) {
3973 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3974 return -1;
3975 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003976
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003977 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003978 wpa_s->pending_interface_name[0] = '\0';
3979 return 0;
3980}
3981
3982
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003983static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3984 const u8 *noa, size_t noa_len)
3985{
3986 struct wpa_supplicant *wpa_s, *intf = ctx;
3987 char hex[100];
3988
3989 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3990 if (wpa_s->waiting_presence_resp)
3991 break;
3992 }
3993 if (!wpa_s) {
3994 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3995 return;
3996 }
3997 wpa_s->waiting_presence_resp = 0;
3998
3999 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4000 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4001 " status=%u noa=%s", MAC2STR(src), status, hex);
4002}
4003
4004
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004005static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4006 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004007 u8 *ret_ssid, size_t *ret_ssid_len,
4008 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004009{
4010 struct wpa_supplicant *wpa_s = ctx;
4011 struct wpa_ssid *s;
4012
4013 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4014 if (s) {
4015 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4016 *ret_ssid_len = s->ssid_len;
4017 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004018
4019 if (s->mode != WPAS_MODE_P2P_GO) {
4020 os_memset(intended_iface_addr, 0, ETH_ALEN);
4021 } else if (wpas_p2p_create_iface(wpa_s)) {
4022 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4023 return 0;
4024
4025 os_memcpy(intended_iface_addr,
4026 wpa_s->pending_interface_addr, ETH_ALEN);
4027 } else {
4028 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4029 ETH_ALEN);
4030 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004031 return 1;
4032 }
4033
4034 return 0;
4035}
4036
4037
4038static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004039 u8 *ssid, size_t *ssid_len, int *group_iface,
4040 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004041{
4042 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004043 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004044 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004045
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004046 /*
4047 * group_iface will be set to 1 only if a dedicated interface for P2P
4048 * role is required. First, we try to reuse an active GO. However,
4049 * if it is not present, we will try to reactivate an existing
4050 * persistent group and set group_iface to 1, so the caller will know
4051 * that the pending interface should be used.
4052 */
4053 *group_iface = 0;
4054
4055 if (freq)
4056 *freq = 0;
4057
4058 go = wpas_p2p_get_go_group(wpa_s);
4059 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004060 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004061 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004062 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004063 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4064 else
4065 return 0;
4066 } else {
4067 s = go->current_ssid;
4068 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4069 if (freq)
4070 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004071 }
4072
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004073 os_memcpy(ssid, s->ssid, s->ssid_len);
4074 *ssid_len = s->ssid_len;
4075
4076 return 1;
4077}
4078
4079
4080static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4081 const u8 *ssid, size_t ssid_len)
4082{
4083 struct wpa_supplicant *wpa_s = ctx;
4084 struct wpa_ssid *s;
4085 int save_config = 0;
4086 size_t i;
4087
4088 /* Start with our first choice of Persistent Groups */
4089 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4090 if (go && ssid && ssid_len &&
4091 s->ssid_len == ssid_len &&
4092 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4093 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4094 break;
4095
4096 /* Remove stale persistent group */
4097 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004098 wpa_dbg(wpa_s, MSG_DEBUG,
4099 "P2P: Remove stale persistent group id=%d",
4100 s->id);
4101 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004102 wpa_config_remove_network(wpa_s->conf, s->id);
4103 save_config = 1;
4104 continue;
4105 }
4106
4107 for (i = 0; i < s->num_p2p_clients; i++) {
4108 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4109 peer, ETH_ALEN) != 0)
4110 continue;
4111
4112 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4113 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4114 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4115 break;
4116 }
4117 s->num_p2p_clients--;
4118 save_config = 1;
4119 }
4120
4121 if (save_config)
4122 p2p_config_write(wpa_s);
4123
4124 /* Return TRUE if valid SSID remains */
4125 return s != NULL;
4126}
4127
4128
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004129static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4130 const u8 *feat_cap, size_t feat_cap_len)
4131{
4132 static const char pref[] = " feature_cap=";
4133 int ret;
4134
4135 buf[0] = '\0';
4136
4137 /*
4138 * We expect a feature capability to contain at least one byte to be
4139 * reported. The string buffer provided by the caller function is
4140 * expected to be big enough to contain all bytes of the attribute for
4141 * known specifications. This function truncates the reported bytes if
4142 * the feature capability data exceeds the string buffer size.
4143 */
4144 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4145 return;
4146
4147 os_memcpy(buf, pref, sizeof(pref));
4148 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4149 buf_len - sizeof(pref) + 1,
4150 feat_cap, feat_cap_len);
4151
4152 if (ret != (2 * (int) feat_cap_len))
4153 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4154}
4155
4156
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004157static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4158 const u8 *adv_mac, const u8 *ses_mac,
4159 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4160 u8 conncap, int passwd_id,
4161 const u8 *persist_ssid,
4162 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004163 int prov_start, const char *session_info,
4164 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004165 unsigned int freq,
4166 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004167{
4168 struct wpa_supplicant *wpa_s = ctx;
4169 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004170 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004171 int save_config = 0;
4172 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004173 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004174
4175 if (!dev)
4176 return;
4177
4178 os_memset(mac, 0, ETH_ALEN);
4179 if (!adv_mac)
4180 adv_mac = mac;
4181 if (!ses_mac)
4182 ses_mac = mac;
4183 if (!grp_mac)
4184 grp_mac = mac;
4185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004186 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4187 feat_cap, feat_cap_len);
4188
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004189 if (prov_start) {
4190 if (session_info == NULL) {
4191 wpa_msg_global(wpa_s, MSG_INFO,
4192 P2P_EVENT_P2PS_PROVISION_START MACSTR
4193 " adv_id=%x conncap=%x"
4194 " adv_mac=" MACSTR
4195 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004196 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004197 MAC2STR(dev), adv_id, conncap,
4198 MAC2STR(adv_mac),
4199 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004200 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004201 } else {
4202 wpa_msg_global(wpa_s, MSG_INFO,
4203 P2P_EVENT_P2PS_PROVISION_START MACSTR
4204 " adv_id=%x conncap=%x"
4205 " adv_mac=" MACSTR
4206 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004207 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004208 MAC2STR(dev), adv_id, conncap,
4209 MAC2STR(adv_mac),
4210 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004211 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004212 }
4213 return;
4214 }
4215
4216 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4217 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4218
4219 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4220 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4221 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4222
4223 if (persistent_go && !persistent_go->num_p2p_clients) {
4224 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004225 wpa_dbg(wpa_s, MSG_DEBUG,
4226 "P2P: Remove empty persistent group id=%d",
4227 persistent_go->id);
4228 wpas_notify_persistent_group_removed(wpa_s,
4229 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004230 wpa_config_remove_network(wpa_s->conf,
4231 persistent_go->id);
4232 }
4233
4234 wpa_msg_global(wpa_s, MSG_INFO,
4235 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4236 " status=%d"
4237 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004238 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004239 MAC2STR(dev), status,
4240 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004241 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004242 return;
4243 }
4244
4245 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004246 if (persist_ssid && persist_ssid_size)
4247 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4248 persist_ssid_size);
4249
4250 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4251 is_zero_ether_addr(grp_mac)) {
4252 wpa_dbg(wpa_s, MSG_ERROR,
4253 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4254 return;
4255 }
4256
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004257 for (;;) {
4258 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4259 if (!stale)
4260 break;
4261
4262 if (s && s->ssid_len == stale->ssid_len &&
4263 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4264 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4265 break;
4266
4267 /* Remove stale persistent group */
4268 if (stale->mode != WPAS_MODE_P2P_GO ||
4269 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004270 wpa_dbg(wpa_s, MSG_DEBUG,
4271 "P2P: Remove stale persistent group id=%d",
4272 stale->id);
4273 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274 wpa_config_remove_network(wpa_s->conf, stale->id);
4275 } else {
4276 size_t i;
4277
4278 for (i = 0; i < stale->num_p2p_clients; i++) {
4279 if (os_memcmp(stale->p2p_client_list +
4280 i * ETH_ALEN,
4281 dev, ETH_ALEN) == 0) {
4282 os_memmove(stale->p2p_client_list +
4283 i * ETH_ALEN,
4284 stale->p2p_client_list +
4285 (i + 1) * ETH_ALEN,
4286 (stale->num_p2p_clients -
4287 i - 1) * ETH_ALEN);
4288 break;
4289 }
4290 }
4291 stale->num_p2p_clients--;
4292 }
4293 save_config = 1;
4294 }
4295
4296 if (save_config)
4297 p2p_config_write(wpa_s);
4298
4299 if (s) {
4300 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4301 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4302
4303 if (persistent_go && s != persistent_go &&
4304 !persistent_go->num_p2p_clients) {
4305 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004306 wpa_dbg(wpa_s, MSG_DEBUG,
4307 "P2P: Remove empty persistent group id=%d",
4308 persistent_go->id);
4309 wpas_notify_persistent_group_removed(wpa_s,
4310 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004311 wpa_config_remove_network(wpa_s->conf,
4312 persistent_go->id);
4313 /* Save config */
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
4320 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004321 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004322 MAC2STR(dev), status,
4323 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004324 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004325 return;
4326 }
4327
Hai Shalom5f92bc92019-04-18 11:54:11 -07004328 wpa_s->global->pending_p2ps_group = 0;
4329 wpa_s->global->pending_p2ps_group_freq = 0;
4330
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004331 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004332 /*
4333 * We need to copy the interface name. Simply saving a
4334 * pointer isn't enough, since if we use pending_interface_name
4335 * it will be overwritten when the group is added.
4336 */
4337 char go_ifname[100];
4338
4339 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004340 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004341 if (!response_done) {
4342 wpa_s->global->pending_p2ps_group = 1;
4343 wpa_s->global->pending_p2ps_group_freq = freq;
4344 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004345
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004346 if (!wpas_p2p_create_iface(wpa_s))
4347 os_memcpy(go_ifname, wpa_s->ifname,
4348 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004349 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004350 os_memcpy(go_ifname,
4351 wpa_s->pending_interface_name,
4352 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004353
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004354 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004355 wpas_p2ps_prov_complete(
4356 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4357 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004358 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004359 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4360 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004361 return;
4362 }
4363
4364 /* If PD Resp complete, start up the GO */
4365 if (response_done && persistent_go) {
4366 wpas_p2p_group_add_persistent(
4367 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004368 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004369 persistent_go->mode ==
4370 WPAS_MODE_P2P_GO ?
4371 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004372 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004373 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004374 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004375 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004376 }
4377
4378 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004379 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4380 ETH_ALEN);
4381 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004382 }
4383 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004384 os_memcpy(go_ifname, go_wpa_s->ifname,
4385 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004386
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004387 if (is_zero_ether_addr(grp_mac)) {
4388 wpa_dbg(go_wpa_s, MSG_DEBUG,
4389 "P2P: Setting PIN-1 for ANY");
4390 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4391 "12345670", NULL, 0,
4392 0);
4393 } else {
4394 wpa_dbg(go_wpa_s, MSG_DEBUG,
4395 "P2P: Setting PIN-1 for " MACSTR,
4396 MAC2STR(grp_mac));
4397 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4398 "12345670", NULL, 0,
4399 0);
4400 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004401
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004402 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4403 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004404 }
4405
4406 wpa_msg_global(wpa_s, MSG_INFO,
4407 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4408 " status=%d conncap=%x"
4409 " adv_id=%x adv_mac=" MACSTR
4410 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004411 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004412 MAC2STR(dev), status, conncap,
4413 adv_id, MAC2STR(adv_mac),
4414 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004415 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004416 return;
4417 }
4418
4419 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4420 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4421
4422 if (persistent_go && !persistent_go->num_p2p_clients) {
4423 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004424 wpa_dbg(wpa_s, MSG_DEBUG,
4425 "P2P: Remove empty persistent group id=%d",
4426 persistent_go->id);
4427 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004428 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4429 }
4430
4431 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004432 char ssid_hex[32 * 2 + 1];
4433
4434 if (group_ssid)
4435 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4436 group_ssid, group_ssid_len);
4437 else
4438 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004439 wpa_msg_global(wpa_s, MSG_INFO,
4440 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4441 " status=%d conncap=%x"
4442 " adv_id=%x adv_mac=" MACSTR
4443 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004444 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004445 MAC2STR(dev), status, conncap,
4446 adv_id, MAC2STR(adv_mac),
4447 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004448 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4449 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004450 } else {
4451 wpa_msg_global(wpa_s, MSG_INFO,
4452 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4453 " status=%d conncap=%x"
4454 " adv_id=%x adv_mac=" MACSTR
4455 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004456 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004457 MAC2STR(dev), status, conncap,
4458 adv_id, MAC2STR(adv_mac),
4459 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004460 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004461 }
4462}
4463
4464
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004465static int _wpas_p2p_in_progress(void *ctx)
4466{
4467 struct wpa_supplicant *wpa_s = ctx;
4468 return wpas_p2p_in_progress(wpa_s);
4469}
4470
4471
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004472static int wpas_prov_disc_resp_cb(void *ctx)
4473{
4474 struct wpa_supplicant *wpa_s = ctx;
4475 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004476 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004477
4478 if (!wpa_s->global->pending_p2ps_group)
4479 return 0;
4480
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004481 freq = wpa_s->global->pending_p2ps_group_freq;
4482 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004483 wpa_s->global->pending_p2ps_group = 0;
4484
4485 if (wpas_p2p_get_go_group(wpa_s))
4486 return 0;
4487 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4488
4489 if (persistent_go) {
4490 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004491 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4492 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004493 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004494 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004495 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004496 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004497 }
4498
4499 return 1;
4500}
4501
4502
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004503static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4504 unsigned int *len,
4505 unsigned int *freq_list)
4506{
4507 struct wpa_supplicant *wpa_s = ctx;
4508
4509 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4510 WPA_IF_P2P_CLIENT, len, freq_list);
4511}
4512
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004513int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4514{
4515 u8 addr[ETH_ALEN] = {0};
4516
4517 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4518 return 0;
4519
4520 if (wpa_s->conf->ssid == NULL) {
4521 if (random_mac_addr(addr) < 0) {
4522 wpa_msg(wpa_s, MSG_INFO,
4523 "Failed to generate random MAC address");
4524 return -EINVAL;
4525 }
4526
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004527 /* Store generated MAC address. */
4528 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4529 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004530 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004531 /* If there are existing saved groups, restore last MAC address.
4532 * if there is no last used MAC address, the last one is
4533 * factory MAC. */
4534 if (is_zero_ether_addr(
4535 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004536 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004537 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4538 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004539 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4540 }
4541
4542 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4543 wpa_msg(wpa_s, MSG_INFO,
4544 "Failed to set random MAC address");
4545 return -EINVAL;
4546 }
4547
4548 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4549 wpa_msg(wpa_s, MSG_INFO,
4550 "Could not update MAC address information");
4551 return -EINVAL;
4552 }
4553
4554 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4555 MAC2STR(addr));
4556
4557 return 0;
4558}
4559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560/**
4561 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4562 * @global: Pointer to global data from wpa_supplicant_init()
4563 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4564 * Returns: 0 on success, -1 on failure
4565 */
4566int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4567{
4568 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 int i;
4570
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004571 if (wpa_s->conf->p2p_disabled)
4572 return 0;
4573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4575 return 0;
4576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 if (global->p2p)
4578 return 0;
4579
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004580 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4581 wpa_msg(wpa_s, MSG_ERROR,
4582 "Failed to initialize P2P random MAC address.");
4583 return -1;
4584 }
4585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004588 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 p2p.p2p_scan = wpas_p2p_scan;
4590 p2p.send_action = wpas_send_action;
4591 p2p.send_action_done = wpas_send_action_done;
4592 p2p.go_neg_completed = wpas_go_neg_completed;
4593 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4594 p2p.dev_found = wpas_dev_found;
4595 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004596 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597 p2p.start_listen = wpas_start_listen;
4598 p2p.stop_listen = wpas_stop_listen;
4599 p2p.send_probe_resp = wpas_send_probe_resp;
4600 p2p.sd_request = wpas_sd_request;
4601 p2p.sd_response = wpas_sd_response;
4602 p2p.prov_disc_req = wpas_prov_disc_req;
4603 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004604 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 p2p.invitation_process = wpas_invitation_process;
4606 p2p.invitation_received = wpas_invitation_received;
4607 p2p.invitation_result = wpas_invitation_result;
4608 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004609 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004610 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004611 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004612 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004613 p2p.get_persistent_group = wpas_get_persistent_group;
4614 p2p.get_go_info = wpas_get_go_info;
4615 p2p.remove_stale_groups = wpas_remove_stale_groups;
4616 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4617 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4618 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004619 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004620
4621 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004622 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 p2p.dev_name = wpa_s->conf->device_name;
4624 p2p.manufacturer = wpa_s->conf->manufacturer;
4625 p2p.model_name = wpa_s->conf->model_name;
4626 p2p.model_number = wpa_s->conf->model_number;
4627 p2p.serial_number = wpa_s->conf->serial_number;
4628 if (wpa_s->wps) {
4629 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4630 p2p.config_methods = wpa_s->wps->config_methods;
4631 }
4632
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004633 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4634 wpa_printf(MSG_ERROR,
4635 "P2P: Failed to configure supported channel list");
4636 return -1;
4637 }
4638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 if (wpa_s->conf->p2p_listen_reg_class &&
4640 wpa_s->conf->p2p_listen_channel) {
4641 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4642 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004643 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004644 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 /*
4646 * Pick one of the social channels randomly as the listen
4647 * channel.
4648 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004649 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004650 &p2p.channel,
4651 &global->p2p_go_avoid_freq,
4652 &global->p2p_disallow_freq) !=
4653 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004654 wpa_printf(MSG_INFO,
4655 "P2P: No social channels supported by the driver - do not enable P2P");
4656 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004657 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004658 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004659 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004660 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4661 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662
4663 if (wpa_s->conf->p2p_oper_reg_class &&
4664 wpa_s->conf->p2p_oper_channel) {
4665 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4666 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4667 p2p.cfg_op_channel = 1;
4668 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4669 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4670
4671 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004673 * Use random operation channel from 2.4 GHz band social
4674 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4675 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004677 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004678 &p2p.op_channel, NULL,
4679 NULL) != 0) {
4680 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004681 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004682 p2p.op_reg_class = 0;
4683 p2p.op_channel = 0;
4684 /* This will be overridden during group setup in
4685 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004686 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687 p2p.cfg_op_channel = 0;
4688 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4689 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4690 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004691
4692 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4693 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4694 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4695 }
4696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4698 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4699 p2p.country[2] = 0x04;
4700 } else
4701 os_memcpy(p2p.country, "XX\x04", 3);
4702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4704 WPS_DEV_TYPE_LEN);
4705
4706 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4707 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4708 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4709
4710 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4711 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4712
4713 p2p.max_peers = 100;
4714
4715 if (wpa_s->conf->p2p_ssid_postfix) {
4716 p2p.ssid_postfix_len =
4717 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4718 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4719 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4720 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4721 p2p.ssid_postfix_len);
4722 }
4723
4724 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4725
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004726 p2p.max_listen = wpa_s->max_remain_on_chan;
4727
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004728 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4729 wpa_s->conf->p2p_passphrase_len <= 63)
4730 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4731 else
4732 p2p.passphrase_len = 8;
4733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734 global->p2p = p2p_init(&p2p);
4735 if (global->p2p == NULL)
4736 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004737 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738
4739 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4740 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4741 continue;
4742 p2p_add_wps_vendor_extension(
4743 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4744 }
4745
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004746 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748 return 0;
4749}
4750
4751
4752/**
4753 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4754 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4755 *
4756 * This function deinitialize per-interface P2P data.
4757 */
4758void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4759{
4760 if (wpa_s->driver && wpa_s->drv_priv)
4761 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004762
4763 if (wpa_s->go_params) {
4764 /* Clear any stored provisioning info */
4765 p2p_clear_provisioning_info(
4766 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004767 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004768 }
4769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 os_free(wpa_s->go_params);
4771 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004772 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4774 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07004775 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004776 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4777 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4778 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004779 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004780 wpas_p2p_listen_work_done(wpa_s);
4781 if (wpa_s->p2p_send_action_work) {
4782 os_free(wpa_s->p2p_send_action_work->ctx);
4783 radio_work_done(wpa_s->p2p_send_action_work);
4784 wpa_s->p2p_send_action_work = NULL;
4785 }
4786 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004788 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4789 wpa_s->p2p_oob_dev_pw = NULL;
4790
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004791 os_free(wpa_s->p2p_group_common_freqs);
4792 wpa_s->p2p_group_common_freqs = NULL;
4793 wpa_s->p2p_group_common_freqs_num = 0;
4794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795 /* TODO: remove group interface from the driver if this wpa_s instance
4796 * is on top of a P2P group interface */
4797}
4798
4799
4800/**
4801 * wpas_p2p_deinit_global - Deinitialize global P2P module
4802 * @global: Pointer to global data from wpa_supplicant_init()
4803 *
4804 * This function deinitializes the global (per device) P2P module.
4805 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004806static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807{
4808 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004810 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004811
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004812 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004813
4814 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004815 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4816 wpa_s = wpa_s->next;
4817 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004818 tmp = global->ifaces;
4819 while (tmp &&
4820 (tmp == wpa_s ||
4821 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4822 tmp = tmp->next;
4823 }
4824 if (tmp == NULL)
4825 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004826 /* Disconnect from the P2P group and deinit the interface */
4827 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828 }
4829
4830 /*
4831 * Deinit GO data on any possibly remaining interface (if main
4832 * interface is used as GO).
4833 */
4834 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4835 if (wpa_s->ap_iface)
4836 wpas_p2p_group_deinit(wpa_s);
4837 }
4838
4839 p2p_deinit(global->p2p);
4840 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004841 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842}
4843
4844
4845static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4846{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004847 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004848 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849 if (wpa_s->drv_flags &
4850 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4851 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4852 return 1; /* P2P group requires a new interface in every case
4853 */
4854 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4855 return 0; /* driver does not support concurrent operations */
4856 if (wpa_s->global->ifaces->next)
4857 return 1; /* more that one interface already in use */
4858 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4859 return 1; /* this interface is already in use */
4860 return 0;
4861}
4862
4863
4864static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4865 const u8 *peer_addr,
4866 enum p2p_wps_method wps_method,
4867 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004868 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004869 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004871 if (persistent_group && wpa_s->conf->persistent_reconnect)
4872 persistent_group = 2;
4873
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004874 /*
4875 * Increase GO config timeout if HT40 is used since it takes some time
4876 * to scan channels for coex purposes before the BSS can be started.
4877 */
4878 p2p_set_config_timeout(wpa_s->global->p2p,
4879 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4882 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004883 persistent_group, ssid ? ssid->ssid : NULL,
4884 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004885 wpa_s->p2p_pd_before_go_neg, pref_freq,
4886 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4887 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888}
4889
4890
4891static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4892 const u8 *peer_addr,
4893 enum p2p_wps_method wps_method,
4894 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004895 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004896 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004898 if (persistent_group && wpa_s->conf->persistent_reconnect)
4899 persistent_group = 2;
4900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4902 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004903 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004904 ssid ? ssid->ssid_len : 0, pref_freq,
4905 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4906 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907}
4908
4909
4910static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4911{
4912 wpa_s->p2p_join_scan_count++;
4913 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4914 wpa_s->p2p_join_scan_count);
4915 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4916 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4917 " for join operationg - stop join attempt",
4918 MAC2STR(wpa_s->pending_join_iface_addr));
4919 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004920 if (wpa_s->p2p_auto_pd) {
4921 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004922 wpa_msg_global(wpa_s, MSG_INFO,
4923 P2P_EVENT_PROV_DISC_FAILURE
4924 " p2p_dev_addr=" MACSTR " status=N/A",
4925 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004926 return;
4927 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08004928 if (wpa_s->p2p_fallback_to_go_neg) {
4929 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
4930 "failed - fall back to GO Negotiation");
4931 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
4932 P2P_EVENT_FALLBACK_TO_GO_NEG
4933 "reason=join-failed");
4934 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
4935 return;
4936 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004937 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004938 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004939 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 }
4941}
4942
4943
Dmitry Shmidt04949592012-07-19 12:16:46 -07004944static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4945{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004946 int res;
4947 unsigned int num, i;
4948 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004949
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004950 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4951 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004952 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004953 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004954
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004955 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4956 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004957 if (!freqs)
4958 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004959
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004960 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4961 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004962
4963 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004964 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004965 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4966 freq);
4967 res = 0;
4968 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004969 }
4970 }
4971
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004972 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004973 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004974
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004975exit_free:
4976 os_free(freqs);
4977 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004978}
4979
4980
4981static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4982 const u8 *peer_dev_addr)
4983{
4984 struct wpa_bss *bss;
4985 int updated;
4986
4987 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4988 if (bss == NULL)
4989 return -1;
4990 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4991 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4992 "last scan");
4993 return 0;
4994 }
4995
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004996 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4997 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004998 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4999 "%ld.%06ld (%supdated in last scan)",
5000 bss->last_update.sec, bss->last_update.usec,
5001 updated ? "": "not ");
5002
5003 return updated;
5004}
5005
5006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5008 struct wpa_scan_results *scan_res)
5009{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005010 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 int freq;
5012 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5015
5016 if (wpa_s->global->p2p_disabled)
5017 return;
5018
Dmitry Shmidt04949592012-07-19 12:16:46 -07005019 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5020 scan_res ? (int) scan_res->num : -1,
5021 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022
5023 if (scan_res)
5024 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5025
Dmitry Shmidt04949592012-07-19 12:16:46 -07005026 if (wpa_s->p2p_auto_pd) {
5027 int join = wpas_p2p_peer_go(wpa_s,
5028 wpa_s->pending_join_dev_addr);
5029 if (join == 0 &&
5030 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5031 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005032 bss = wpa_bss_get_bssid_latest(
5033 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005034 if (bss) {
5035 freq = bss->freq;
5036 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5037 "the peer " MACSTR " at %d MHz",
5038 wpa_s->auto_pd_scan_retry,
5039 MAC2STR(wpa_s->
5040 pending_join_dev_addr),
5041 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005042 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005043 return;
5044 }
5045 }
5046
5047 if (join < 0)
5048 join = 0;
5049
5050 wpa_s->p2p_auto_pd = 0;
5051 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5052 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5053 MAC2STR(wpa_s->pending_join_dev_addr), join);
5054 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005055 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005056 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005057 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005058 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005059 wpa_msg_global(wpa_s, MSG_INFO,
5060 P2P_EVENT_PROV_DISC_FAILURE
5061 " p2p_dev_addr=" MACSTR " status=N/A",
5062 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005063 }
5064 return;
5065 }
5066
5067 if (wpa_s->p2p_auto_join) {
5068 int join = wpas_p2p_peer_go(wpa_s,
5069 wpa_s->pending_join_dev_addr);
5070 if (join < 0) {
5071 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5072 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005073 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005074 P2P_EVENT_FALLBACK_TO_GO_NEG
5075 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005076 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5077 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5078 wpa_s->p2p_persistent_group, 0, 0, 0,
5079 wpa_s->p2p_go_intent,
5080 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005081 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005082 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005083 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005084 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005085 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005086 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005087 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005088 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005089 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005090 return;
5091 }
5092
5093 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5094 "try to join the group", join ? "" :
5095 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005096 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005097 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005098 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005099 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005100 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005101 }
5102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005103 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5104 wpa_s->pending_join_iface_addr);
5105 if (freq < 0 &&
5106 p2p_get_interface_addr(wpa_s->global->p2p,
5107 wpa_s->pending_join_dev_addr,
5108 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005109 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5110 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5112 "address for join from " MACSTR " to " MACSTR
5113 " based on newly discovered P2P peer entry",
5114 MAC2STR(wpa_s->pending_join_iface_addr),
5115 MAC2STR(iface_addr));
5116 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5117 ETH_ALEN);
5118
5119 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5120 wpa_s->pending_join_iface_addr);
5121 }
5122 if (freq >= 0) {
5123 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5124 "from P2P peer table: %d MHz", freq);
5125 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005126 if (wpa_s->p2p_join_ssid_len) {
5127 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5128 MACSTR " and SSID %s",
5129 MAC2STR(wpa_s->pending_join_iface_addr),
5130 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5131 wpa_s->p2p_join_ssid_len));
5132 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5133 wpa_s->p2p_join_ssid,
5134 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005135 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005136 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5137 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5138 bss = wpa_bss_get_bssid_latest(wpa_s,
5139 wpa_s->pending_join_iface_addr);
5140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005142 u8 dev_addr[ETH_ALEN];
5143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005144 freq = bss->freq;
5145 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005146 "from BSS table: %d MHz (SSID %s)", freq,
5147 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005148 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5149 dev_addr) == 0 &&
5150 os_memcmp(wpa_s->pending_join_dev_addr,
5151 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5152 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5153 ETH_ALEN) != 0) {
5154 wpa_printf(MSG_DEBUG,
5155 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5156 MAC2STR(dev_addr),
5157 MAC2STR(wpa_s->pending_join_dev_addr));
5158 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5159 ETH_ALEN);
5160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 }
5162 if (freq > 0) {
5163 u16 method;
5164
Dmitry Shmidt04949592012-07-19 12:16:46 -07005165 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005166 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005167 P2P_EVENT_GROUP_FORMATION_FAILURE
5168 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005169 wpas_notify_p2p_group_formation_failure(
5170 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005171 return;
5172 }
5173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5175 "prior to joining an existing group (GO " MACSTR
5176 " freq=%u MHz)",
5177 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5178 wpa_s->pending_pd_before_join = 1;
5179
5180 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181 case WPS_PIN_DISPLAY:
5182 method = WPS_CONFIG_KEYPAD;
5183 break;
5184 case WPS_PIN_KEYPAD:
5185 method = WPS_CONFIG_DISPLAY;
5186 break;
5187 case WPS_PBC:
5188 method = WPS_CONFIG_PUSHBUTTON;
5189 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005190 case WPS_P2PS:
5191 method = WPS_CONFIG_P2PS;
5192 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 default:
5194 method = 0;
5195 break;
5196 }
5197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005198 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5199 wpa_s->pending_join_dev_addr) ==
5200 method)) {
5201 /*
5202 * We have already performed provision discovery for
5203 * joining the group. Proceed directly to join
5204 * operation without duplicated provision discovery. */
5205 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5206 "with " MACSTR " already done - proceed to "
5207 "join",
5208 MAC2STR(wpa_s->pending_join_dev_addr));
5209 wpa_s->pending_pd_before_join = 0;
5210 goto start;
5211 }
5212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005214 wpa_s->pending_join_dev_addr,
5215 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005216 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5218 "Discovery Request before joining an "
5219 "existing group");
5220 wpa_s->pending_pd_before_join = 0;
5221 goto start;
5222 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223 return;
5224 }
5225
5226 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5227 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5228 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5229 wpas_p2p_check_join_scan_limit(wpa_s);
5230 return;
5231
5232start:
5233 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005234 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5235 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005236}
5237
5238
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005239static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5240 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242 int ret;
5243 struct wpa_driver_scan_params params;
5244 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005245 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005246 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005247 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248
5249 os_memset(&params, 0, sizeof(params));
5250
5251 /* P2P Wildcard SSID */
5252 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005253 if (ssid && ssid_len) {
5254 params.ssids[0].ssid = ssid;
5255 params.ssids[0].ssid_len = ssid_len;
5256 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5257 wpa_s->p2p_join_ssid_len = ssid_len;
5258 } else {
5259 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5260 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5261 wpa_s->p2p_join_ssid_len = 0;
5262 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263
5264 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005265 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5266 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5267 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268 if (wps_ie == NULL) {
5269 wpas_p2p_scan_res_join(wpa_s, NULL);
5270 return;
5271 }
5272
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005273 if (!freq) {
5274 int oper_freq;
5275 /*
5276 * If freq is not provided, check the operating freq of the GO
5277 * and use a single channel scan on if possible.
5278 */
5279 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5280 wpa_s->pending_join_iface_addr);
5281 if (oper_freq > 0)
5282 freq = oper_freq;
5283 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005284 if (freq > 0) {
5285 freqs[0] = freq;
5286 params.freqs = freqs;
5287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005288
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005289 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5290 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5291 if (ies == NULL) {
5292 wpabuf_free(wps_ie);
5293 wpas_p2p_scan_res_join(wpa_s, NULL);
5294 return;
5295 }
5296 wpabuf_put_buf(ies, wps_ie);
5297 wpabuf_free(wps_ie);
5298
5299 bands = wpas_get_bands(wpa_s, freqs);
5300 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5301
5302 params.p2p_probe = 1;
5303 params.extra_ies = wpabuf_head(ies);
5304 params.extra_ies_len = wpabuf_len(ies);
5305
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005306 if (wpa_s->clear_driver_scan_cache) {
5307 wpa_printf(MSG_DEBUG,
5308 "Request driver to clear scan cache due to local BSS flush");
5309 params.only_new_results = 1;
5310 }
5311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312 /*
5313 * Run a scan to update BSS table and start Provision Discovery once
5314 * the new scan results become available.
5315 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005316 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005317 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005318 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005320 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005321 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323
5324 wpabuf_free(ies);
5325
5326 if (ret) {
5327 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5328 "try again later");
5329 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5330 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5331 wpas_p2p_check_join_scan_limit(wpa_s);
5332 }
5333}
5334
5335
Dmitry Shmidt04949592012-07-19 12:16:46 -07005336static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5337{
5338 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005339 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005340}
5341
5342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005345 int auto_join, int op_freq,
5346 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347{
5348 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005349 MACSTR " dev " MACSTR " op_freq=%d)%s",
5350 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005351 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005352 if (ssid && ssid_len) {
5353 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5354 wpa_ssid_txt(ssid, ssid_len));
5355 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356
Dmitry Shmidt04949592012-07-19 12:16:46 -07005357 wpa_s->p2p_auto_pd = 0;
5358 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5360 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5361 wpa_s->pending_join_wps_method = wps_method;
5362
5363 /* Make sure we are not running find during connection establishment */
5364 wpas_p2p_stop_find(wpa_s);
5365
5366 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005367 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 return 0;
5369}
5370
5371
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005372static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5373 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374{
5375 struct wpa_supplicant *group;
5376 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005377 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005378
5379 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5380 if (group == NULL)
5381 return -1;
5382 if (group != wpa_s) {
5383 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5384 sizeof(group->p2p_pin));
5385 group->p2p_wps_method = wpa_s->p2p_wps_method;
5386 }
5387
Hai Shalom74f70d42019-02-11 14:42:39 -08005388 /*
5389 * Need to mark the current interface for p2p_group_formation
5390 * when a separate group interface is not used. This is needed
5391 * to allow p2p_cancel stop a pending p2p_connect-join.
5392 * wpas_p2p_init_group_interface() addresses this for the case
5393 * where a separate group interface is used.
5394 */
5395 if (group == wpa_s->parent)
5396 wpa_s->global->p2p_group_formation = group;
5397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005399 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005400
5401 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005402 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005403 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5404 ETH_ALEN);
5405 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005406 if (freq && ssid && ssid_len) {
5407 res.freq = freq;
5408 res.ssid_len = ssid_len;
5409 os_memcpy(res.ssid, ssid, ssid_len);
5410 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005411 if (ssid && ssid_len) {
5412 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5413 ssid, ssid_len);
5414 } else {
5415 bss = wpa_bss_get_bssid_latest(
5416 wpa_s, wpa_s->pending_join_iface_addr);
5417 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005418 if (bss) {
5419 res.freq = bss->freq;
5420 res.ssid_len = bss->ssid_len;
5421 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5422 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5423 bss->freq,
5424 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005425 } else if (ssid && ssid_len) {
5426 res.ssid_len = ssid_len;
5427 os_memcpy(res.ssid, ssid, ssid_len);
5428 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5429 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005430 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005431 }
5432
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005433 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5434 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5435 "starting client");
5436 wpa_drv_cancel_remain_on_channel(wpa_s);
5437 wpa_s->off_channel_freq = 0;
5438 wpa_s->roc_waiting_drv_freq = 0;
5439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 wpas_start_wps_enrollee(group, &res);
5441
5442 /*
5443 * Allow a longer timeout for join-a-running-group than normal 15
5444 * second group formation timeout since the GO may not have authorized
5445 * our connection yet.
5446 */
5447 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5448 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5449 wpa_s, NULL);
5450
5451 return 0;
5452}
5453
5454
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005455static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005456 int *force_freq, int *pref_freq, int go,
5457 unsigned int *pref_freq_list,
5458 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005459{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005460 struct wpa_used_freq_data *freqs;
5461 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005462 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5463
5464 max_pref_freq = *num_pref_freq;
5465 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005466
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005467 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5468 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005469 if (!freqs)
5470 return -1;
5471
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005472 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5473 wpa_s->num_multichan_concurrent);
5474
5475 /*
5476 * It is possible that the total number of used frequencies is bigger
5477 * than the number of frequencies used for P2P, so get the system wide
5478 * number of unused frequencies.
5479 */
5480 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5481
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005482 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005483 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5484 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005485
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005486 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005487 int ret;
5488 if (go)
5489 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5490 else
5491 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5492 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005493 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005494 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5495 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005496 /*
5497 * If freq is a DFS channel and DFS is offloaded
5498 * to the driver, allow P2P GO to use it.
5499 */
5500 wpa_printf(MSG_DEBUG,
5501 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5502 freq);
5503 } else {
5504 wpa_printf(MSG_DEBUG,
5505 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5506 freq);
5507 res = -3;
5508 goto exit_free;
5509 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005510 }
5511
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005512 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005513 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005514 freq_in_use = 1;
5515 }
5516
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005517 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005518 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5519 freq);
5520 res = -2;
5521 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005522 }
5523 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5524 "requested channel (%u MHz)", freq);
5525 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005526 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005527 }
5528
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005529 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005530
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005531 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5532 enum wpa_driver_if_type iface_type;
5533
5534 if (go)
5535 iface_type = WPA_IF_P2P_GO;
5536 else
5537 iface_type = WPA_IF_P2P_CLIENT;
5538
5539 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5540 best_freq, go);
5541
5542 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5543 &max_pref_freq,
5544 pref_freq_list);
5545 if (!res && max_pref_freq > 0) {
5546 *num_pref_freq = max_pref_freq;
5547 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005548 while (i < *num_pref_freq &&
5549 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005550 pref_freq_list[i]) ||
5551 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005552 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005553 wpa_printf(MSG_DEBUG,
5554 "P2P: preferred_freq_list[%d]=%d is disallowed",
5555 i, pref_freq_list[i]);
5556 i++;
5557 }
5558 if (i != *num_pref_freq) {
5559 best_freq = pref_freq_list[i];
5560 wpa_printf(MSG_DEBUG,
5561 "P2P: Using preferred_freq_list[%d]=%d",
5562 i, best_freq);
5563 } else {
5564 wpa_printf(MSG_DEBUG,
5565 "P2P: All driver preferred frequencies are disallowed for P2P use");
5566 *num_pref_freq = 0;
5567 }
5568 } else {
5569 wpa_printf(MSG_DEBUG,
5570 "P2P: No preferred frequency list available");
5571 }
5572 }
5573
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005574 /* We have a candidate frequency to use */
5575 if (best_freq > 0) {
5576 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005577 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005578 best_freq);
5579 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005580 } else {
5581 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 -07005582 best_freq);
5583 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005584 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005585 } else if (num_unused > 0) {
5586 wpa_printf(MSG_DEBUG,
5587 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5588 *force_freq = 0;
5589 } else {
5590 wpa_printf(MSG_DEBUG,
5591 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5592 res = -2;
5593 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005594 }
5595
5596exit_ok:
5597 res = 0;
5598exit_free:
5599 os_free(freqs);
5600 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005601}
5602
5603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604/**
5605 * wpas_p2p_connect - Request P2P Group Formation to be started
5606 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5607 * @peer_addr: Address of the peer P2P Device
5608 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5609 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005610 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 * @join: Whether to join an existing group (as a client) instead of starting
5612 * Group Owner negotiation; @peer_addr is BSSID in that case
5613 * @auth: Whether to only authorize the connection instead of doing that and
5614 * initiating Group Owner negotiation
5615 * @go_intent: GO Intent or -1 to use default
5616 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005617 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005618 * @persistent_id: Persistent group credentials to use for forcing GO
5619 * parameters or -1 to generate new values (SSID/passphrase)
5620 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5621 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005622 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005623 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005624 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005625 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005626 * @group_ssid: Specific Group SSID for join or %NULL if not set
5627 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5629 * failure, -2 on failure due to channel not currently available,
5630 * -3 if forced channel is not supported
5631 */
5632int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5633 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005634 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005635 int go_intent, int freq, unsigned int vht_center_freq2,
5636 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005637 unsigned int vht_chwidth, int he, int edmg,
5638 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005639{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005640 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005641 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005644 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005645 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646
5647 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5648 return -1;
5649
Dmitry Shmidt04949592012-07-19 12:16:46 -07005650 if (persistent_id >= 0) {
5651 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5652 if (ssid == NULL || ssid->disabled != 2 ||
5653 ssid->mode != WPAS_MODE_P2P_GO)
5654 return -1;
5655 }
5656
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005657 os_free(wpa_s->global->add_psk);
5658 wpa_s->global->add_psk = NULL;
5659
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005660 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005661 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005662 wpa_s->global->pending_p2ps_group_freq = 0;
5663 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665 if (go_intent < 0)
5666 go_intent = wpa_s->conf->p2p_go_intent;
5667
5668 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005669 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670
5671 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005672 wpa_s->p2p_persistent_group = !!persistent_group;
5673 wpa_s->p2p_persistent_id = persistent_id;
5674 wpa_s->p2p_go_intent = go_intent;
5675 wpa_s->p2p_connect_freq = freq;
5676 wpa_s->p2p_fallback_to_go_neg = 0;
5677 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005678 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005679 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005680 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5681 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005682 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005683 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684
5685 if (pin)
5686 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5687 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005688 if (wps_generate_pin((unsigned int *) &ret) < 0)
5689 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005690 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5691 "%08d", ret);
5692 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5693 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5695 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005696 } else if (wps_method == WPS_P2PS) {
5697 /* Force the P2Ps default PIN to be used */
5698 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005699 } else
5700 wpa_s->p2p_pin[0] = '\0';
5701
Dmitry Shmidt04949592012-07-19 12:16:46 -07005702 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5704 if (auth) {
5705 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5706 "connect a running group from " MACSTR,
5707 MAC2STR(peer_addr));
5708 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5709 return ret;
5710 }
5711 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5712 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5713 iface_addr) < 0) {
5714 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5715 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5716 dev_addr);
5717 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005718 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005719 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005720 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5721 "%ld.%06ld",
5722 wpa_s->p2p_auto_started.sec,
5723 wpa_s->p2p_auto_started.usec);
5724 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005725 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005726 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005727 auto_join, freq,
5728 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005729 return -1;
5730 return ret;
5731 }
5732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005733 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005734 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005735 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005736 if (res)
5737 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005738 wpas_p2p_set_own_freq_preference(wpa_s,
5739 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005741 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5744
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005745 if (wpa_s->create_p2p_iface) {
5746 /* Prepare to add a new interface for the group */
5747 iftype = WPA_IF_P2P_GROUP;
5748 if (go_intent == 15)
5749 iftype = WPA_IF_P2P_GO;
5750 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5751 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5752 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005753 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005755
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005756 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005757 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005758 if (wpa_s->p2p_mgmt)
5759 if_addr = wpa_s->parent->own_addr;
5760 else
5761 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005762 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5763 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764
5765 if (auth) {
5766 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005767 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005768 force_freq, persistent_group, ssid,
5769 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 return -1;
5771 return ret;
5772 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005773
5774 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5775 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005776 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005777 if (wpa_s->create_p2p_iface)
5778 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005779 return -1;
5780 }
5781 return ret;
5782}
5783
5784
5785/**
5786 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5787 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5788 * @freq: Frequency of the channel in MHz
5789 * @duration: Duration of the stay on the channel in milliseconds
5790 *
5791 * This callback is called when the driver indicates that it has started the
5792 * requested remain-on-channel duration.
5793 */
5794void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5795 unsigned int freq, unsigned int duration)
5796{
5797 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5798 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005799 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)",
5800 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5801 wpa_s->roc_waiting_drv_freq, freq, duration);
5802 if (wpa_s->off_channel_freq &&
5803 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5805 wpa_s->pending_listen_duration);
5806 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005807 } else {
5808 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5809 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5810 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005811 }
5812}
5813
5814
Jithu Jance57cea1a2014-08-20 10:22:04 -07005815int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005816{
5817 /* Limit maximum Listen state time based on driver limitation. */
5818 if (timeout > wpa_s->max_remain_on_chan)
5819 timeout = wpa_s->max_remain_on_chan;
5820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005821 return p2p_listen(wpa_s->global->p2p, timeout);
5822}
5823
5824
5825/**
5826 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5827 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5828 * @freq: Frequency of the channel in MHz
5829 *
5830 * This callback is called when the driver indicates that a remain-on-channel
5831 * operation has been completed, i.e., the duration on the requested channel
5832 * has timed out.
5833 */
5834void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5835 unsigned int freq)
5836{
5837 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5838 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005839 wpa_s->global->p2p_long_listen,
5840 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005841 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005842 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5843 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005844 if (wpa_s->global->p2p_long_listen > 0)
5845 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5847 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005848 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005849 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005850 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005852 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005853 } else {
5854 /*
5855 * When listen duration is over, stop listen & update p2p_state
5856 * to IDLE.
5857 */
5858 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005859 }
5860}
5861
5862
5863/**
5864 * wpas_p2p_group_remove - Remove a P2P group
5865 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5866 * @ifname: Network interface name of the group interface or "*" to remove all
5867 * groups
5868 * Returns: 0 on success, -1 on failure
5869 *
5870 * This function is used to remove a P2P group. This can be used to disconnect
5871 * from a group in which the local end is a P2P Client or to end a P2P Group in
5872 * case the local end is the Group Owner. If a virtual network interface was
5873 * created for this group, that interface will be removed. Otherwise, only the
5874 * configured P2P group network will be removed from the interface.
5875 */
5876int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5877{
5878 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005879 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005880
5881 if (os_strcmp(ifname, "*") == 0) {
5882 struct wpa_supplicant *prev;
5883 wpa_s = global->ifaces;
5884 while (wpa_s) {
5885 prev = wpa_s;
5886 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005887 if (prev->p2p_group_interface !=
5888 NOT_P2P_GROUP_INTERFACE ||
5889 (prev->current_ssid &&
5890 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005891 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005892 }
5893 return 0;
5894 }
5895
5896 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5897 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5898 break;
5899 }
5900
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005901 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005902}
5903
5904
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005905static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5906{
5907 unsigned int r;
5908
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005909 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5910 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5911 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5912 int res;
5913
5914 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5915 &size, pref_freq_list);
5916 if (!res && size > 0) {
5917 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005918 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005919 (!p2p_supported_freq(wpa_s->global->p2p,
5920 pref_freq_list[i]) ||
5921 wpas_p2p_disallowed_freq(wpa_s->global,
5922 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005923 wpa_printf(MSG_DEBUG,
5924 "P2P: preferred_freq_list[%d]=%d is disallowed",
5925 i, pref_freq_list[i]);
5926 i++;
5927 }
5928 if (i != size) {
5929 freq = pref_freq_list[i];
5930 wpa_printf(MSG_DEBUG,
5931 "P2P: Using preferred_freq_list[%d]=%d",
5932 i, freq);
5933 } else {
5934 wpa_printf(MSG_DEBUG,
5935 "P2P: All driver preferred frequencies are disallowed for P2P use");
5936 }
5937 } else {
5938 wpa_printf(MSG_DEBUG,
5939 "P2P: No preferred frequency list available");
5940 }
5941 }
5942
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005943 if (freq == 2) {
5944 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5945 "band");
5946 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005947 p2p_supported_freq_go(wpa_s->global->p2p,
5948 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005949 freq = wpa_s->best_24_freq;
5950 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5951 "channel: %d MHz", freq);
5952 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005953 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5954 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005955 freq = 2412 + (r % 3) * 25;
5956 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5957 "channel: %d MHz", freq);
5958 }
5959 }
5960
5961 if (freq == 5) {
5962 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5963 "band");
5964 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005965 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005966 wpa_s->best_5_freq)) {
5967 freq = wpa_s->best_5_freq;
5968 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5969 "channel: %d MHz", freq);
5970 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005971 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5972 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005973 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005974 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005975 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5976 "5 GHz channel for P2P group");
5977 return -1;
5978 }
5979 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5980 "channel: %d MHz", freq);
5981 }
5982 }
5983
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005984 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005985 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005986 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5987 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005988 /*
5989 * If freq is a DFS channel and DFS is offloaded to the
5990 * driver, allow P2P GO to use it.
5991 */
5992 wpa_printf(MSG_DEBUG, "P2P: "
5993 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5994 __func__, freq);
5995 return freq;
5996 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005997 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5998 "(%u MHz) is not supported for P2P uses",
5999 freq);
6000 return -1;
6001 }
6002
6003 return freq;
6004}
6005
6006
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006007static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6008 const struct p2p_channels *channels,
6009 int freq)
6010{
6011 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6012 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6013 freq_included(wpa_s, channels, freq))
6014 return 1;
6015 return 0;
6016}
6017
6018
6019static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6020 struct p2p_go_neg_results *params,
6021 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006022{
6023 unsigned int i, r;
6024
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006025 /* try all channels in operating class 115 */
6026 for (i = 0; i < 4; i++) {
6027 params->freq = 5180 + i * 20;
6028 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006029 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006030 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6031 goto out;
6032 }
6033
6034 /* try all channels in operating class 124 */
6035 for (i = 0; i < 4; i++) {
6036 params->freq = 5745 + i * 20;
6037 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006038 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006039 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6040 goto out;
6041 }
6042
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006043 /* try social channel class 180 channel 2 */
6044 params->freq = 58320 + 1 * 2160;
6045 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006046 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006047 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6048 goto out;
6049
6050 /* try all channels in reg. class 180 */
6051 for (i = 0; i < 4; i++) {
6052 params->freq = 58320 + i * 2160;
6053 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006054 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006055 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6056 goto out;
6057 }
6058
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006059 /* try some random selection of the social channels */
6060 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6061 return;
6062
6063 for (i = 0; i < 3; i++) {
6064 params->freq = 2412 + ((r + i) % 3) * 25;
6065 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6066 goto out;
6067 }
6068
6069 /* try all other channels in operating class 81 */
6070 for (i = 0; i < 11; i++) {
6071 params->freq = 2412 + i * 5;
6072
6073 /* skip social channels; covered in the previous loop */
6074 if (params->freq == 2412 ||
6075 params->freq == 2437 ||
6076 params->freq == 2462)
6077 continue;
6078
6079 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6080 goto out;
6081 }
6082
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006083 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006084 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006085 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006086out:
6087 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6088 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006089}
6090
6091
Roshan Pius3a1667e2018-07-03 15:17:14 -07006092static int wpas_same_band(int freq1, int freq2)
6093{
6094 enum hostapd_hw_mode mode1, mode2;
6095 u8 chan1, chan2;
6096
6097 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6098 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6099 if (mode1 == NUM_HOSTAPD_MODES)
6100 return 0;
6101 return mode1 == mode2;
6102}
6103
6104
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006105static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6106 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006107 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006108 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006109 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006110 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006112 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006113 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006114 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006115 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006116 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117
6118 os_memset(params, 0, sizeof(*params));
6119 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006120 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006121 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006122 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006123 params->max_oper_chwidth = max_oper_chwidth;
6124 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006125 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006127 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6128 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006129 if (!freqs)
6130 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006131
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006132 num = get_shared_radio_freqs_data(wpa_s, freqs,
6133 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006134
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006135 if (wpa_s->current_ssid &&
6136 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6137 wpa_s->wpa_state == WPA_COMPLETED) {
6138 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6139 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006140
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006141 /*
6142 * If the frequency selection is done for an active P2P GO that
6143 * is not sharing a frequency, allow to select a new frequency
6144 * even if there are no unused frequencies as we are about to
6145 * move the P2P GO so its frequency can be re-used.
6146 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006147 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006148 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6149 freqs[i].flags == 0) {
6150 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006151 break;
6152 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006153 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006154 }
6155
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006156 /* Try to use EDMG channel */
6157 if (params->edmg) {
6158 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6159 goto success;
6160 params->edmg = 0;
6161 }
6162
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006163 /* try using the forced freq */
6164 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006165 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6166 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006167 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006168 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006169 freq);
6170 goto fail;
6171 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006172 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6173 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006174 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6175 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006176 /*
6177 * If freq is a DFS channel and DFS is offloaded
6178 * to the driver, allow P2P GO to use it.
6179 */
6180 wpa_printf(MSG_DEBUG,
6181 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6182 __func__, freq);
6183 } else {
6184 wpa_printf(MSG_DEBUG,
6185 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6186 freq);
6187 goto fail;
6188 }
6189 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006190
6191 for (i = 0; i < num; i++) {
6192 if (freqs[i].freq == freq) {
6193 wpa_printf(MSG_DEBUG,
6194 "P2P: forced freq (%d MHz) is also shared",
6195 freq);
6196 params->freq = freq;
6197 goto success;
6198 }
6199 }
6200
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006201 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006202 wpa_printf(MSG_DEBUG,
6203 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6204 freq);
6205 goto fail;
6206 }
6207
6208 wpa_printf(MSG_DEBUG,
6209 "P2P: force GO freq (%d MHz) on a free channel",
6210 freq);
6211 params->freq = freq;
6212 goto success;
6213 }
6214
6215 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006216 if (num &&
6217 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006218 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6219 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6220 wpa_printf(MSG_DEBUG,
6221 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006222 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006223 params->freq = cand;
6224 goto success;
6225 }
6226
6227 /* try using one of the shared freqs */
6228 for (i = 0; i < num; i++) {
6229 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6230 freqs[i].freq)) {
6231 wpa_printf(MSG_DEBUG,
6232 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006233 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006234 params->freq = freqs[i].freq;
6235 goto success;
6236 }
6237 }
6238 }
6239
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006240 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006241 wpa_printf(MSG_DEBUG,
6242 "P2P: Cannot force GO on any of the channels we are already using");
6243 goto fail;
6244 }
6245
6246 /* try using the setting from the configuration file */
6247 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6248 wpa_s->conf->p2p_oper_channel >= 1 &&
6249 wpa_s->conf->p2p_oper_channel <= 11 &&
6250 wpas_p2p_supported_freq_go(
6251 wpa_s, channels,
6252 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6253 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6254 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6255 "frequency %d MHz", params->freq);
6256 goto success;
6257 }
6258
6259 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6260 wpa_s->conf->p2p_oper_reg_class == 116 ||
6261 wpa_s->conf->p2p_oper_reg_class == 117 ||
6262 wpa_s->conf->p2p_oper_reg_class == 124 ||
6263 wpa_s->conf->p2p_oper_reg_class == 125 ||
6264 wpa_s->conf->p2p_oper_reg_class == 126 ||
6265 wpa_s->conf->p2p_oper_reg_class == 127) &&
6266 wpas_p2p_supported_freq_go(wpa_s, channels,
6267 5000 +
6268 5 * wpa_s->conf->p2p_oper_channel)) {
6269 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6270 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6271 "frequency %d MHz", params->freq);
6272 goto success;
6273 }
6274
6275 /* Try using best channels */
6276 if (wpa_s->conf->p2p_oper_channel == 0 &&
6277 wpa_s->best_overall_freq > 0 &&
6278 wpas_p2p_supported_freq_go(wpa_s, channels,
6279 wpa_s->best_overall_freq)) {
6280 params->freq = wpa_s->best_overall_freq;
6281 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6282 "channel %d MHz", params->freq);
6283 goto success;
6284 }
6285
6286 if (wpa_s->conf->p2p_oper_channel == 0 &&
6287 wpa_s->best_24_freq > 0 &&
6288 wpas_p2p_supported_freq_go(wpa_s, channels,
6289 wpa_s->best_24_freq)) {
6290 params->freq = wpa_s->best_24_freq;
6291 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6292 "channel %d MHz", params->freq);
6293 goto success;
6294 }
6295
6296 if (wpa_s->conf->p2p_oper_channel == 0 &&
6297 wpa_s->best_5_freq > 0 &&
6298 wpas_p2p_supported_freq_go(wpa_s, channels,
6299 wpa_s->best_5_freq)) {
6300 params->freq = wpa_s->best_5_freq;
6301 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6302 "channel %d MHz", params->freq);
6303 goto success;
6304 }
6305
6306 /* try using preferred channels */
6307 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6308 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6309 params->freq = cand;
6310 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6311 "channels", params->freq);
6312 goto success;
6313 }
6314
Roshan Pius3a1667e2018-07-03 15:17:14 -07006315 /* Try using a channel that allows VHT to be used with 80 MHz */
6316 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6317 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6318 enum hostapd_hw_mode mode;
6319 struct hostapd_hw_modes *hwmode;
6320 u8 chan;
6321
6322 cand = wpa_s->p2p_group_common_freqs[i];
6323 mode = ieee80211_freq_to_chan(cand, &chan);
6324 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006325 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006326 if (!hwmode ||
6327 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6328 BW80) != ALLOWED)
6329 continue;
6330 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6331 params->freq = cand;
6332 wpa_printf(MSG_DEBUG,
6333 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6334 params->freq);
6335 goto success;
6336 }
6337 }
6338 }
6339
6340 /* Try using a channel that allows HT to be used with 40 MHz on the same
6341 * band so that CSA can be used */
6342 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6343 wpa_s->p2p_group_common_freqs) {
6344 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6345 enum hostapd_hw_mode mode;
6346 struct hostapd_hw_modes *hwmode;
6347 u8 chan;
6348
6349 cand = wpa_s->p2p_group_common_freqs[i];
6350 mode = ieee80211_freq_to_chan(cand, &chan);
6351 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006352 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006353 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6354 cand) ||
6355 !hwmode ||
6356 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6357 BW40MINUS) != ALLOWED &&
6358 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6359 BW40PLUS) != ALLOWED))
6360 continue;
6361 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6362 params->freq = cand;
6363 wpa_printf(MSG_DEBUG,
6364 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6365 params->freq);
6366 goto success;
6367 }
6368 }
6369 }
6370
6371 /* Try using one of the group common freqs on the same band so that CSA
6372 * can be used */
6373 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6374 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6375 cand = wpa_s->p2p_group_common_freqs[i];
6376 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6377 cand))
6378 continue;
6379 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6380 params->freq = cand;
6381 wpa_printf(MSG_DEBUG,
6382 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6383 params->freq);
6384 goto success;
6385 }
6386 }
6387 }
6388
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006389 /* Try using one of the group common freqs */
6390 if (wpa_s->p2p_group_common_freqs) {
6391 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6392 cand = wpa_s->p2p_group_common_freqs[i];
6393 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6394 params->freq = cand;
6395 wpa_printf(MSG_DEBUG,
6396 "P2P: Use freq %d MHz common with the peer",
6397 params->freq);
6398 goto success;
6399 }
6400 }
6401 }
6402
6403 /* no preference, select some channel */
6404 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6405
6406 if (params->freq == 0) {
6407 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6408 goto fail;
6409 }
6410
6411success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006412 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006413 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006414fail:
6415 os_free(freqs);
6416 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417}
6418
6419
6420static struct wpa_supplicant *
6421wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6422 int go)
6423{
6424 struct wpa_supplicant *group_wpa_s;
6425
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006426 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006427 if (wpa_s->p2p_mgmt) {
6428 /*
6429 * We may be called on the p2p_dev interface which
6430 * cannot be used for group operations, so always use
6431 * the primary interface.
6432 */
6433 wpa_s->parent->p2pdev = wpa_s;
6434 wpa_s = wpa_s->parent;
6435 }
6436 wpa_dbg(wpa_s, MSG_DEBUG,
6437 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006438 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006439 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006440 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006443
6444 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006445 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006446 wpa_msg_global(wpa_s, MSG_ERROR,
6447 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006448 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006450 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6451 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006452 wpa_msg_global(wpa_s, MSG_ERROR,
6453 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454 wpas_p2p_remove_pending_group_interface(wpa_s);
6455 return NULL;
6456 }
6457
Roshan Pius3a1667e2018-07-03 15:17:14 -07006458 if (go && wpa_s->p2p_go_do_acs) {
6459 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6460 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6461 wpa_s->p2p_go_do_acs = 0;
6462 }
6463
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006464 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6465 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006466 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467 return group_wpa_s;
6468}
6469
6470
6471/**
6472 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6473 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6474 * @persistent_group: Whether to create a persistent group
6475 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006476 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006477 * @ht40: Start GO with 40 MHz channel width
6478 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006479 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006480 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006481 * Returns: 0 on success, -1 on failure
6482 *
6483 * This function creates a new P2P group with the local end as the Group Owner,
6484 * i.e., without using Group Owner Negotiation.
6485 */
6486int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006487 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006488 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006489{
6490 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006491
6492 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6493 return -1;
6494
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006495 os_free(wpa_s->global->add_psk);
6496 wpa_s->global->add_psk = NULL;
6497
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006498 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006499 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006500 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006501
Roshan Pius3a1667e2018-07-03 15:17:14 -07006502 if (!wpa_s->p2p_go_do_acs) {
6503 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6504 if (freq < 0)
6505 return -1;
6506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006508 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006509 ht40, vht, max_oper_chwidth, he, edmg,
6510 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006511 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006513 p2p_go_params(wpa_s->global->p2p, &params);
6514 params.persistent_group = persistent_group;
6515
6516 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6517 if (wpa_s == NULL)
6518 return -1;
6519 wpas_start_wps_go(wpa_s, &params, 0);
6520
6521 return 0;
6522}
6523
6524
6525static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006526 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006527 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528{
6529 struct wpa_ssid *ssid;
6530
6531 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6532 if (wpa_s == NULL)
6533 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006534 if (force_scan)
6535 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006536 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006537
6538 wpa_supplicant_ap_deinit(wpa_s);
6539
6540 ssid = wpa_config_add_network(wpa_s->conf);
6541 if (ssid == NULL)
6542 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006543 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544 wpa_config_set_network_defaults(ssid);
6545 ssid->temporary = 1;
6546 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006547 ssid->pbss = params->pbss;
6548 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6549 WPA_CIPHER_CCMP;
6550 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006551 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6552 ssid->ssid = os_malloc(params->ssid_len);
6553 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554 wpa_config_remove_network(wpa_s->conf, ssid->id);
6555 return -1;
6556 }
6557 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6558 ssid->ssid_len = params->ssid_len;
6559 ssid->p2p_group = 1;
6560 ssid->export_keys = 1;
6561 if (params->psk_set) {
6562 os_memcpy(ssid->psk, params->psk, 32);
6563 ssid->psk_set = 1;
6564 }
6565 if (params->passphrase)
6566 ssid->passphrase = os_strdup(params->passphrase);
6567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006569 wpa_s->p2p_in_invitation = 1;
6570 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006571 wpa_s->p2p_go_group_formation_completed = 0;
6572 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006574 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006575 NULL);
6576 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6577 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006578 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006579 wpa_supplicant_select_network(wpa_s, ssid);
6580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006581 return 0;
6582}
6583
6584
6585int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6586 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006587 int force_freq, int neg_freq,
6588 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006589 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006590 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006591 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006592 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006593{
6594 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006595 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596
6597 if (ssid->disabled != 2 || ssid->ssid == NULL)
6598 return -1;
6599
6600 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6601 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6602 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6603 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006604 if (go == 0 &&
6605 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006606 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006607 /*
6608 * This can happen if Invitation Response frame was lost
6609 * and the peer (GO of a persistent group) tries to
6610 * invite us again. Reschedule the timeout to avoid
6611 * terminating the wait for the connection too early
6612 * since we now know that the peer is still trying to
6613 * invite us instead of having already started the GO.
6614 */
6615 wpa_printf(MSG_DEBUG,
6616 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6617 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6618 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006619 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006620 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006621 return 0;
6622 }
6623
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006624 os_free(wpa_s->global->add_psk);
6625 wpa_s->global->add_psk = NULL;
6626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006627 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006628 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006629
Dmitry Shmidt04949592012-07-19 12:16:46 -07006630 wpa_s->p2p_fallback_to_go_neg = 0;
6631
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006632 if (ssid->mode == WPAS_MODE_P2P_GO) {
6633 if (force_freq > 0) {
6634 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6635 if (freq < 0)
6636 return -1;
6637 } else {
6638 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6639 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006640 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006641 freq = 0;
6642 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006643 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006644 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006645 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006646 struct os_reltime now;
6647 struct wpa_bss *bss =
6648 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006649
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006650 os_get_reltime(&now);
6651 if (bss &&
6652 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006653 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006654 freq = bss->freq;
6655 else
6656 freq = 0;
6657 }
6658
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006659 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6660 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006661 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006662 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006663 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006664
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006665 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006666 ht40, vht, max_oper_chwidth, he, edmg,
6667 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006668 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006669
6670 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006671 params.psk_set = ssid->psk_set;
6672 if (params.psk_set)
6673 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006674 if (ssid->passphrase) {
6675 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6676 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6677 "persistent group");
6678 return -1;
6679 }
6680 os_strlcpy(params.passphrase, ssid->passphrase,
6681 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006683 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6684 params.ssid_len = ssid->ssid_len;
6685 params.persistent_group = 1;
6686
6687 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6688 if (wpa_s == NULL)
6689 return -1;
6690
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006691 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6692
Dmitry Shmidt56052862013-10-04 10:23:25 -07006693 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 wpas_start_wps_go(wpa_s, &params, 0);
6695
6696 return 0;
6697}
6698
6699
6700static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6701 struct wpabuf *proberesp_ies)
6702{
6703 struct wpa_supplicant *wpa_s = ctx;
6704 if (wpa_s->ap_iface) {
6705 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006706 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6707 wpabuf_free(beacon_ies);
6708 wpabuf_free(proberesp_ies);
6709 return;
6710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006711 if (beacon_ies) {
6712 wpabuf_free(hapd->p2p_beacon_ie);
6713 hapd->p2p_beacon_ie = beacon_ies;
6714 }
6715 wpabuf_free(hapd->p2p_probe_resp_ie);
6716 hapd->p2p_probe_resp_ie = proberesp_ies;
6717 } else {
6718 wpabuf_free(beacon_ies);
6719 wpabuf_free(proberesp_ies);
6720 }
6721 wpa_supplicant_ap_update_beacon(wpa_s);
6722}
6723
6724
6725static void wpas_p2p_idle_update(void *ctx, int idle)
6726{
6727 struct wpa_supplicant *wpa_s = ctx;
6728 if (!wpa_s->ap_iface)
6729 return;
6730 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006731 if (idle) {
6732 if (wpa_s->global->p2p_fail_on_wps_complete &&
6733 wpa_s->p2p_in_provisioning) {
6734 wpas_p2p_grpform_fail_after_wps(wpa_s);
6735 return;
6736 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006737 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006738 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006739 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6740}
6741
6742
6743struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006744 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006745{
6746 struct p2p_group *group;
6747 struct p2p_group_config *cfg;
6748
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006749 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6750 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751 return NULL;
6752
6753 cfg = os_zalloc(sizeof(*cfg));
6754 if (cfg == NULL)
6755 return NULL;
6756
Dmitry Shmidt04949592012-07-19 12:16:46 -07006757 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006758 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006759 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006760 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006761 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6762 if (wpa_s->max_stations &&
6763 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6764 cfg->max_clients = wpa_s->max_stations;
6765 else
6766 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006767 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6768 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006769 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006770 cfg->cb_ctx = wpa_s;
6771 cfg->ie_update = wpas_p2p_ie_update;
6772 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006773 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6774 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006775
6776 group = p2p_group_init(wpa_s->global->p2p, cfg);
6777 if (group == NULL)
6778 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006779 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006780 p2p_group_notif_formation_done(group);
6781 wpa_s->p2p_group = group;
6782 return group;
6783}
6784
6785
6786void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6787 int registrar)
6788{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006789 struct wpa_ssid *ssid = wpa_s->current_ssid;
6790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791 if (!wpa_s->p2p_in_provisioning) {
6792 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6793 "provisioning not in progress");
6794 return;
6795 }
6796
Dmitry Shmidt04949592012-07-19 12:16:46 -07006797 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6798 u8 go_dev_addr[ETH_ALEN];
6799 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6800 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6801 ssid->ssid_len);
6802 /* Clear any stored provisioning info */
6803 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6804 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006805
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006806 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006807 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006808 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006809 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6810 /*
6811 * Use a separate timeout for initial data connection to
6812 * complete to allow the group to be removed automatically if
6813 * something goes wrong in this step before the P2P group idle
6814 * timeout mechanism is taken into use.
6815 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006816 wpa_dbg(wpa_s, MSG_DEBUG,
6817 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6818 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006819 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6820 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006821 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006822 /* Complete group formation on successful data connection. */
6823 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006824 } else if (ssid) {
6825 /*
6826 * Use a separate timeout for initial data connection to
6827 * complete to allow the group to be removed automatically if
6828 * the client does not complete data connection successfully.
6829 */
6830 wpa_dbg(wpa_s, MSG_DEBUG,
6831 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6832 P2P_MAX_INITIAL_CONN_WAIT_GO);
6833 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6834 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006835 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006836 /*
6837 * Complete group formation on first successful data connection
6838 */
6839 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006841 if (wpa_s->global->p2p)
6842 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006843 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844}
6845
6846
Jouni Malinen75ecf522011-06-27 15:19:46 -07006847void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6848 struct wps_event_fail *fail)
6849{
6850 if (!wpa_s->p2p_in_provisioning) {
6851 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6852 "provisioning not in progress");
6853 return;
6854 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006855
6856 if (wpa_s->go_params) {
6857 p2p_clear_provisioning_info(
6858 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006859 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006860 }
6861
Jouni Malinen75ecf522011-06-27 15:19:46 -07006862 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006863
6864 if (wpa_s == wpa_s->global->p2p_group_formation) {
6865 /*
6866 * Allow some time for the failed WPS negotiation exchange to
6867 * complete, but remove the group since group formation cannot
6868 * succeed after provisioning failure.
6869 */
6870 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6871 wpa_s->global->p2p_fail_on_wps_complete = 1;
6872 eloop_deplete_timeout(0, 50000,
6873 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006874 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006875 }
6876}
6877
6878
6879int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6880{
6881 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6882 !wpa_s->p2p_in_provisioning)
6883 return 0;
6884
6885 wpas_p2p_grpform_fail_after_wps(wpa_s);
6886
6887 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006888}
6889
6890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006891int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006892 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006893 enum wpas_p2p_prov_disc_use use,
6894 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006895{
6896 u16 config_methods;
6897
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006898 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006899 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006900 wpa_s->p2p_fallback_to_go_neg = 0;
6901 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006902 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6903 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006904 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6905 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6906
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006907 wpa_printf(MSG_DEBUG,
6908 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6909 __func__, p2ps_prov->conncap,
6910 p2ps_prov->adv_id, p2ps_prov->conncap,
6911 p2ps_prov->status, p2ps_prov->info);
6912
6913 config_methods = 0;
6914 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006915 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006916 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006918 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6919 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006920 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006921 else {
6922 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006923 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006925 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926
Dmitry Shmidt04949592012-07-19 12:16:46 -07006927 if (use == WPAS_P2P_PD_AUTO) {
6928 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6929 wpa_s->pending_pd_config_methods = config_methods;
6930 wpa_s->p2p_auto_pd = 1;
6931 wpa_s->p2p_auto_join = 0;
6932 wpa_s->pending_pd_before_join = 0;
6933 wpa_s->auto_pd_scan_retry = 0;
6934 wpas_p2p_stop_find(wpa_s);
6935 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006936 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006937 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6938 wpa_s->p2p_auto_started.sec,
6939 wpa_s->p2p_auto_started.usec);
6940 wpas_p2p_join_scan(wpa_s, NULL);
6941 return 0;
6942 }
6943
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006944 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6945 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006946 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006947 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006948
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006949 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006950 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006951 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952}
6953
6954
6955int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6956 char *end)
6957{
6958 return p2p_scan_result_text(ies, ies_len, buf, end);
6959}
6960
6961
6962static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6963{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006964 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006965 return;
6966
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006967 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006968 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006969 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6970 wpa_s, NULL);
6971 offchannel_send_action_done(wpa_s);
6972 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6975 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006976 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006977}
6978
6979
6980int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6981 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006982 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006983 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006984 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006985{
6986 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07006987 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006988
Dmitry Shmidt04949592012-07-19 12:16:46 -07006989 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006990 wpa_s->p2p_in_provisioning) {
6991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6992 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6993 " (P2P disabled)" : "",
6994 wpa_s->p2p_in_provisioning ?
6995 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006996 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006998
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006999 wpa_supplicant_cancel_sched_scan(wpa_s);
7000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007001 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007002 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007003 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004}
7005
7006
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007007static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7008 struct wpa_scan_results *scan_res)
7009{
7010 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7011
7012 if (wpa_s->p2p_scan_work) {
7013 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7014 wpa_s->p2p_scan_work = NULL;
7015 radio_work_done(work);
7016 }
7017
7018 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7019 return;
7020
7021 /*
7022 * Indicate that results have been processed so that the P2P module can
7023 * continue pending tasks.
7024 */
7025 p2p_scan_res_handled(wpa_s->global->p2p);
7026}
7027
7028
7029static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007030{
7031 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007032 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7034 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007035
7036 if (wpa_s->global->p2p)
7037 p2p_stop_find(wpa_s->global->p2p);
7038
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007039 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7040 wpa_printf(MSG_DEBUG,
7041 "P2P: Do not consider the scan results after stop_find");
7042 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7043 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007044}
7045
7046
7047void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7048{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007049 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007050 if (!wpa_s->global->pending_group_iface_for_p2ps)
7051 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007052}
7053
7054
7055static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7056{
7057 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007058 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007059}
7060
7061
7062int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7063{
7064 int res;
7065
7066 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7067 return -1;
7068
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007069 if (wpa_s->p2p_lo_started) {
7070 wpa_printf(MSG_DEBUG,
7071 "P2P: Cannot start P2P listen, it is offloaded");
7072 return -1;
7073 }
7074
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007075 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007076 wpas_p2p_clear_pending_action_tx(wpa_s);
7077
7078 if (timeout == 0) {
7079 /*
7080 * This is a request for unlimited Listen state. However, at
7081 * least for now, this is mapped to a Listen state for one
7082 * hour.
7083 */
7084 timeout = 3600;
7085 }
7086 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007087 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007089 /*
7090 * Stop previous find/listen operation to avoid trying to request a new
7091 * remain-on-channel operation while the driver is still running the
7092 * previous one.
7093 */
7094 if (wpa_s->global->p2p)
7095 p2p_stop_find(wpa_s->global->p2p);
7096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007097 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7098 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007099 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007100 eloop_register_timeout(timeout, 0,
7101 wpas_p2p_long_listen_timeout,
7102 wpa_s, NULL);
7103 }
7104
7105 return res;
7106}
7107
7108
7109int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7110 u8 *buf, size_t len, int p2p_group)
7111{
7112 struct wpabuf *p2p_ie;
7113 int ret;
7114
7115 if (wpa_s->global->p2p_disabled)
7116 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007117 /*
7118 * Advertize mandatory cross connection capability even on
7119 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7120 */
7121 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007122 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007123 if (wpa_s->global->p2p == NULL)
7124 return -1;
7125 if (bss == NULL)
7126 return -1;
7127
7128 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7129 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7130 p2p_group, p2p_ie);
7131 wpabuf_free(p2p_ie);
7132
7133 return ret;
7134}
7135
7136
7137int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007138 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007139 const u8 *ie, size_t ie_len,
7140 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007141{
7142 if (wpa_s->global->p2p_disabled)
7143 return 0;
7144 if (wpa_s->global->p2p == NULL)
7145 return 0;
7146
Dmitry Shmidt04949592012-07-19 12:16:46 -07007147 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007148 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007149 case P2P_PREQ_NOT_P2P:
7150 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7151 ssi_signal);
7152 /* fall through */
7153 case P2P_PREQ_MALFORMED:
7154 case P2P_PREQ_NOT_LISTEN:
7155 case P2P_PREQ_NOT_PROCESSED:
7156 default: /* make gcc happy */
7157 return 0;
7158 case P2P_PREQ_PROCESSED:
7159 return 1;
7160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007161}
7162
7163
7164void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7165 const u8 *sa, const u8 *bssid,
7166 u8 category, const u8 *data, size_t len, int freq)
7167{
7168 if (wpa_s->global->p2p_disabled)
7169 return;
7170 if (wpa_s->global->p2p == NULL)
7171 return;
7172
7173 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7174 freq);
7175}
7176
7177
7178void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7179{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007180 unsigned int bands;
7181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007182 if (wpa_s->global->p2p_disabled)
7183 return;
7184 if (wpa_s->global->p2p == NULL)
7185 return;
7186
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007187 bands = wpas_get_bands(wpa_s, NULL);
7188 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007189}
7190
7191
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007192static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007193{
7194 p2p_group_deinit(wpa_s->p2p_group);
7195 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007196
7197 wpa_s->ap_configured_cb = NULL;
7198 wpa_s->ap_configured_cb_ctx = NULL;
7199 wpa_s->ap_configured_cb_data = NULL;
7200 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007201}
7202
7203
7204int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7205{
Hai Shalomfdcde762020-04-02 11:19:20 -07007206 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007208 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7209 return -1;
7210
7211 return p2p_reject(wpa_s->global->p2p, addr);
7212}
7213
7214
7215/* Invite to reinvoke a persistent group */
7216int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007217 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007218 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007219 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220{
7221 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007222 u8 *bssid = NULL;
7223 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007224 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007225 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007226 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007227
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007228 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007229 if (peer_addr)
7230 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7231 else
7232 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233
Jouni Malinen31be0a42012-08-31 21:20:51 +03007234 wpa_s->p2p_persistent_go_freq = freq;
7235 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007236 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007237 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007238 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7239 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007240 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007241 if (ssid->mode == WPAS_MODE_P2P_GO) {
7242 role = P2P_INVITE_ROLE_GO;
7243 if (peer_addr == NULL) {
7244 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7245 "address in invitation command");
7246 return -1;
7247 }
7248 if (wpas_p2p_create_iface(wpa_s)) {
7249 if (wpas_p2p_add_group_interface(wpa_s,
7250 WPA_IF_P2P_GO) < 0) {
7251 wpa_printf(MSG_ERROR, "P2P: Failed to "
7252 "allocate a new interface for the "
7253 "group");
7254 return -1;
7255 }
7256 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007257 } else if (wpa_s->p2p_mgmt)
7258 bssid = wpa_s->parent->own_addr;
7259 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007260 bssid = wpa_s->own_addr;
7261 } else {
7262 role = P2P_INVITE_ROLE_CLIENT;
7263 peer_addr = ssid->bssid;
7264 }
7265 wpa_s->pending_invite_ssid_id = ssid->id;
7266
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007267 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007268 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007269 role == P2P_INVITE_ROLE_GO,
7270 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007271 if (res)
7272 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007274 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7275 return -1;
7276
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007277 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7278
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007279 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7280 no_pref_freq_given && pref_freq > 0 &&
7281 wpa_s->num_multichan_concurrent > 1 &&
7282 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7283 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7284 pref_freq);
7285 pref_freq = 0;
7286 }
7287
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007288 /*
7289 * Stop any find/listen operations before invitation and possibly
7290 * connection establishment.
7291 */
7292 wpas_p2p_stop_find_oper(wpa_s);
7293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007294 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007295 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007296 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007297}
7298
7299
7300/* Invite to join an active group */
7301int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7302 const u8 *peer_addr, const u8 *go_dev_addr)
7303{
7304 struct wpa_global *global = wpa_s->global;
7305 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007306 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007307 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007308 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007309 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007310 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007311 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312
Jouni Malinen31be0a42012-08-31 21:20:51 +03007313 wpa_s->p2p_persistent_go_freq = 0;
7314 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007315 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007316 wpa_s->p2p_go_vht_center_freq2 = 0;
7317 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007318 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007320 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7321 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7322 break;
7323 }
7324 if (wpa_s == NULL) {
7325 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7326 return -1;
7327 }
7328
7329 ssid = wpa_s->current_ssid;
7330 if (ssid == NULL) {
7331 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7332 "invitation");
7333 return -1;
7334 }
7335
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007336 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007337 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007338 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007339 ssid->ssid, ssid->ssid_len);
7340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007341 if (ssid->mode == WPAS_MODE_P2P_GO) {
7342 role = P2P_INVITE_ROLE_ACTIVE_GO;
7343 bssid = wpa_s->own_addr;
7344 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007345 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007346 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007347 } else {
7348 role = P2P_INVITE_ROLE_CLIENT;
7349 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7350 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7351 "invite to current group");
7352 return -1;
7353 }
7354 bssid = wpa_s->bssid;
7355 if (go_dev_addr == NULL &&
7356 !is_zero_ether_addr(wpa_s->go_dev_addr))
7357 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007358 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7359 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007360 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007361 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7364 return -1;
7365
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007366 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007367 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007368 role == P2P_INVITE_ROLE_ACTIVE_GO,
7369 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007370 if (res)
7371 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007372 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007374 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007375 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007376 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007377}
7378
7379
7380void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7381{
7382 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007383 u8 go_dev_addr[ETH_ALEN];
7384 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007385 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007386 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007387 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007388
Dmitry Shmidt04949592012-07-19 12:16:46 -07007389 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7390 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007391 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007392 }
7393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007395 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007396
7397 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007398 if (!wpa_s->p2p_go_group_formation_completed &&
7399 wpa_s->global->p2p_group_formation == wpa_s) {
7400 wpa_dbg(wpa_s, MSG_DEBUG,
7401 "P2P: Marking group formation completed on client on data connection");
7402 wpa_s->p2p_go_group_formation_completed = 1;
7403 wpa_s->global->p2p_group_formation = NULL;
7404 wpa_s->p2p_in_provisioning = 0;
7405 wpa_s->p2p_in_invitation = 0;
7406 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408 os_memset(go_dev_addr, 0, ETH_ALEN);
7409 if (ssid->bssid_set)
7410 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7411 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7412 ssid->ssid_len);
7413 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7414
7415 if (wpa_s->global->p2p_group_formation == wpa_s)
7416 wpa_s->global->p2p_group_formation = NULL;
7417
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007418 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7419 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007420
7421 ip_addr[0] = '\0';
7422 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007423 int res;
7424
7425 res = os_snprintf(ip_addr, sizeof(ip_addr),
7426 " ip_addr=%u.%u.%u.%u "
7427 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7428 ip[0], ip[1], ip[2], ip[3],
7429 ip[4], ip[5], ip[6], ip[7],
7430 ip[8], ip[9], ip[10], ip[11]);
7431 if (os_snprintf_error(sizeof(ip_addr), res))
7432 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007433 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007434 }
7435
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007436 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7437 ssid->passphrase == NULL && ssid->psk_set ?
7438 ssid->psk : NULL,
7439 ssid->passphrase, go_dev_addr, persistent,
7440 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007441
7442 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007443 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7444 ssid, go_dev_addr);
7445
Hai Shalom5f92bc92019-04-18 11:54:11 -07007446 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007447}
7448
7449
7450int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7451 u32 interval1, u32 duration2, u32 interval2)
7452{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007453 int ret;
7454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007455 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7456 return -1;
7457
7458 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7459 wpa_s->current_ssid == NULL ||
7460 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7461 return -1;
7462
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007463 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7464 wpa_s->own_addr, wpa_s->assoc_freq,
7465 duration1, interval1, duration2, interval2);
7466 if (ret == 0)
7467 wpa_s->waiting_presence_resp = 1;
7468
7469 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007470}
7471
7472
7473int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7474 unsigned int interval)
7475{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007476 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7477 return -1;
7478
7479 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7480}
7481
7482
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007483static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7484{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007485 if (wpa_s->current_ssid == NULL) {
7486 /*
7487 * current_ssid can be cleared when P2P client interface gets
7488 * disconnected, so assume this interface was used as P2P
7489 * client.
7490 */
7491 return 1;
7492 }
7493 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007494 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7495}
7496
7497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007498static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7499{
7500 struct wpa_supplicant *wpa_s = eloop_ctx;
7501
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007502 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007503 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7504 "disabled");
7505 return;
7506 }
7507
Dmitry Shmidt04949592012-07-19 12:16:46 -07007508 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7509 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007510 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007511}
7512
7513
7514static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7515{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007516 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007517
Dmitry Shmidt04949592012-07-19 12:16:46 -07007518 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7519 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7520
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007521 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7522 return;
7523
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007524 timeout = wpa_s->conf->p2p_group_idle;
7525 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7526 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7527 timeout = P2P_MAX_CLIENT_IDLE;
7528
7529 if (timeout == 0)
7530 return;
7531
Dmitry Shmidt04949592012-07-19 12:16:46 -07007532 if (timeout < 0) {
7533 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7534 timeout = 0; /* special client mode no-timeout */
7535 else
7536 return;
7537 }
7538
7539 if (wpa_s->p2p_in_provisioning) {
7540 /*
7541 * Use the normal group formation timeout during the
7542 * provisioning phase to avoid terminating this process too
7543 * early due to group idle timeout.
7544 */
7545 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7546 "during provisioning");
7547 return;
7548 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307549
Dmitry Shmidt04949592012-07-19 12:16:46 -07007550 if (wpa_s->show_group_started) {
7551 /*
7552 * Use the normal group formation timeout between the end of
7553 * the provisioning phase and completion of 4-way handshake to
7554 * avoid terminating this process too early due to group idle
7555 * timeout.
7556 */
7557 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7558 "while waiting for initial 4-way handshake to "
7559 "complete");
7560 return;
7561 }
7562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007563 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007564 timeout);
7565 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7566 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007567}
7568
7569
Jouni Malinen2b89da82012-08-31 22:04:41 +03007570/* Returns 1 if the interface was removed */
7571int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7572 u16 reason_code, const u8 *ie, size_t ie_len,
7573 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007574{
7575 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007576 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577
Dmitry Shmidt04949592012-07-19 12:16:46 -07007578 if (!locally_generated)
7579 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7580 ie_len);
7581
7582 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7583 wpa_s->current_ssid &&
7584 wpa_s->current_ssid->p2p_group &&
7585 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7586 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7587 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007588 if (wpas_p2p_group_delete(wpa_s,
7589 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7590 > 0)
7591 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007592 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007593
7594 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595}
7596
7597
7598void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007599 u16 reason_code, const u8 *ie, size_t ie_len,
7600 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007601{
7602 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7603 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007604
Dmitry Shmidt04949592012-07-19 12:16:46 -07007605 if (!locally_generated)
7606 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7607 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007608}
7609
7610
7611void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7612{
7613 struct p2p_data *p2p = wpa_s->global->p2p;
7614
7615 if (p2p == NULL)
7616 return;
7617
7618 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7619 return;
7620
7621 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7622 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7623
7624 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7625 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7626
7627 if (wpa_s->wps &&
7628 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7629 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7630
7631 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7632 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7633
7634 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7635 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7636 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7637 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7638 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7639 }
7640
7641 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7642 p2p_set_sec_dev_types(p2p,
7643 (void *) wpa_s->conf->sec_device_type,
7644 wpa_s->conf->num_sec_device_types);
7645
7646 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7647 int i;
7648 p2p_remove_wps_vendor_extensions(p2p);
7649 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7650 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7651 continue;
7652 p2p_add_wps_vendor_extension(
7653 p2p, wpa_s->conf->wps_vendor_ext[i]);
7654 }
7655 }
7656
7657 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7658 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7659 char country[3];
7660 country[0] = wpa_s->conf->country[0];
7661 country[1] = wpa_s->conf->country[1];
7662 country[2] = 0x04;
7663 p2p_set_country(p2p, country);
7664 }
7665
7666 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7667 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7668 wpa_s->conf->p2p_ssid_postfix ?
7669 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7670 0);
7671 }
7672
7673 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7674 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007675
7676 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7677 u8 reg_class, channel;
7678 int ret;
7679 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007680 u8 channel_forced;
7681
Jouni Malinen75ecf522011-06-27 15:19:46 -07007682 if (wpa_s->conf->p2p_listen_reg_class &&
7683 wpa_s->conf->p2p_listen_channel) {
7684 reg_class = wpa_s->conf->p2p_listen_reg_class;
7685 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007686 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007687 } else {
7688 reg_class = 81;
7689 /*
7690 * Pick one of the social channels randomly as the
7691 * listen channel.
7692 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007693 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7694 channel = 1;
7695 else
7696 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007697 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007698 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007699 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7700 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007701 if (ret)
7702 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7703 "failed: %d", ret);
7704 }
7705 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7706 u8 op_reg_class, op_channel, cfg_op_channel;
7707 int ret = 0;
7708 unsigned int r;
7709 if (wpa_s->conf->p2p_oper_reg_class &&
7710 wpa_s->conf->p2p_oper_channel) {
7711 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7712 op_channel = wpa_s->conf->p2p_oper_channel;
7713 cfg_op_channel = 1;
7714 } else {
7715 op_reg_class = 81;
7716 /*
7717 * Use random operation channel from (1, 6, 11)
7718 *if no other preference is indicated.
7719 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007720 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7721 op_channel = 1;
7722 else
7723 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007724 cfg_op_channel = 0;
7725 }
7726 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7727 cfg_op_channel);
7728 if (ret)
7729 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7730 "failed: %d", ret);
7731 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007732
7733 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7734 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7735 wpa_s->conf->p2p_pref_chan) < 0) {
7736 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7737 "update failed");
7738 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007739
7740 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7741 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7742 "update failed");
7743 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007744 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007745
7746 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7747 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007748}
7749
7750
7751int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7752 int duration)
7753{
7754 if (!wpa_s->ap_iface)
7755 return -1;
7756 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7757 duration);
7758}
7759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007760
7761int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7762{
7763 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7764 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007765
7766 wpa_s->global->cross_connection = enabled;
7767 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7768
7769 if (!enabled) {
7770 struct wpa_supplicant *iface;
7771
7772 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7773 {
7774 if (iface->cross_connect_enabled == 0)
7775 continue;
7776
7777 iface->cross_connect_enabled = 0;
7778 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007779 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007780 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7781 iface->ifname,
7782 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007783 }
7784 }
7785
7786 return 0;
7787}
7788
7789
7790static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7791{
7792 struct wpa_supplicant *iface;
7793
7794 if (!uplink->global->cross_connection)
7795 return;
7796
7797 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7798 if (!iface->cross_connect_enabled)
7799 continue;
7800 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7801 0)
7802 continue;
7803 if (iface->ap_iface == NULL)
7804 continue;
7805 if (iface->cross_connect_in_use)
7806 continue;
7807
7808 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007809 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007810 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7811 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007812 }
7813}
7814
7815
7816static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7817{
7818 struct wpa_supplicant *iface;
7819
7820 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7821 if (!iface->cross_connect_enabled)
7822 continue;
7823 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7824 0)
7825 continue;
7826 if (!iface->cross_connect_in_use)
7827 continue;
7828
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007829 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007830 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7831 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007832 iface->cross_connect_in_use = 0;
7833 }
7834}
7835
7836
7837void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7838{
7839 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7840 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7841 wpa_s->cross_connect_disallowed)
7842 wpas_p2p_disable_cross_connect(wpa_s);
7843 else
7844 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007845 if (!wpa_s->ap_iface &&
7846 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7847 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848}
7849
7850
7851void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7852{
7853 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007854 if (!wpa_s->ap_iface &&
7855 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7856 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007857 wpas_p2p_set_group_idle_timeout(wpa_s);
7858}
7859
7860
7861static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7862{
7863 struct wpa_supplicant *iface;
7864
7865 if (!wpa_s->global->cross_connection)
7866 return;
7867
7868 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7869 if (iface == wpa_s)
7870 continue;
7871 if (iface->drv_flags &
7872 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7873 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007874 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7875 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007876 continue;
7877
7878 wpa_s->cross_connect_enabled = 1;
7879 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7880 sizeof(wpa_s->cross_connect_uplink));
7881 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7882 "%s to %s whenever uplink is available",
7883 wpa_s->ifname, wpa_s->cross_connect_uplink);
7884
7885 if (iface->ap_iface || iface->current_ssid == NULL ||
7886 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7887 iface->cross_connect_disallowed ||
7888 iface->wpa_state != WPA_COMPLETED)
7889 break;
7890
7891 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007892 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007893 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7894 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007895 break;
7896 }
7897}
7898
7899
7900int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7901{
7902 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7903 !wpa_s->p2p_in_provisioning)
7904 return 0; /* not P2P client operation */
7905
7906 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7907 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007908 if (wpa_s != wpa_s->p2pdev)
7909 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007910 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007911 return 1;
7912}
7913
7914
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007915void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7916{
7917 struct wpa_supplicant *wpa_s = eloop_ctx;
7918 wpas_p2p_notif_pbc_overlap(wpa_s);
7919}
7920
7921
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007922void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7923 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007924{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007925 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007926 struct wpa_used_freq_data *freqs = NULL;
7927 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928
7929 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7930 return;
7931
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007932 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7933 if (!freqs)
7934 return;
7935
7936 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007938 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007939 os_memset(&cli_chan, 0, sizeof(cli_chan));
7940 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007941 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7942 "channel list");
7943 return;
7944 }
7945
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007946 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007947
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007948 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007949
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007950 /*
7951 * The used frequencies map changed, so it is possible that a GO is
7952 * using a channel that is no longer valid for P2P use. It is also
7953 * possible that due to policy consideration, it would be preferable to
7954 * move it to a frequency already used by other station interfaces.
7955 */
7956 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7957
7958 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007959}
7960
7961
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007962static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7963 struct wpa_scan_results *scan_res)
7964{
7965 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7966}
7967
7968
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007969int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7970{
7971 struct wpa_global *global = wpa_s->global;
7972 int found = 0;
7973 const u8 *peer;
7974
7975 if (global->p2p == NULL)
7976 return -1;
7977
7978 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7979
7980 if (wpa_s->pending_interface_name[0] &&
7981 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7982 found = 1;
7983
7984 peer = p2p_get_go_neg_peer(global->p2p);
7985 if (peer) {
7986 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7987 MACSTR, MAC2STR(peer));
7988 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007989 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007990 }
7991
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007992 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7993 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7994 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7995 found = 1;
7996 }
7997
7998 if (wpa_s->pending_pd_before_join) {
7999 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8000 wpa_s->pending_pd_before_join = 0;
8001 found = 1;
8002 }
8003
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008004 wpas_p2p_stop_find(wpa_s);
8005
8006 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8007 if (wpa_s == global->p2p_group_formation &&
8008 (wpa_s->p2p_in_provisioning ||
8009 wpa_s->parent->pending_interface_type ==
8010 WPA_IF_P2P_CLIENT)) {
8011 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8012 "formation found - cancelling",
8013 wpa_s->ifname);
8014 found = 1;
8015 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008016 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008017 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008018 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008019 break;
8020 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008021 wpas_p2p_group_delete(wpa_s,
8022 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008023 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008024 } else if (wpa_s->p2p_in_invitation) {
8025 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8026 wpa_s->ifname);
8027 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008028 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008029 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008030 }
8031 }
8032
8033 if (!found) {
8034 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8035 return -1;
8036 }
8037
8038 return 0;
8039}
8040
8041
8042void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8043{
8044 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8045 return;
8046
8047 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8048 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008049 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008050}
8051
8052
8053void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8054 int freq_24, int freq_5, int freq_overall)
8055{
8056 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008057 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008058 return;
8059 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8060}
8061
8062
8063int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8064{
8065 u8 peer[ETH_ALEN];
8066 struct p2p_data *p2p = wpa_s->global->p2p;
8067
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008068 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008069 return -1;
8070
8071 if (hwaddr_aton(addr, peer))
8072 return -1;
8073
8074 return p2p_unauthorize(p2p, peer);
8075}
8076
8077
8078/**
8079 * wpas_p2p_disconnect - Disconnect from a P2P Group
8080 * @wpa_s: Pointer to wpa_supplicant data
8081 * Returns: 0 on success, -1 on failure
8082 *
8083 * This can be used to disconnect from a group in which the local end is a P2P
8084 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8085 * virtual network interface was created for this group, that interface will be
8086 * removed. Otherwise, only the configured P2P group network will be removed
8087 * from the interface.
8088 */
8089int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8090{
8091
8092 if (wpa_s == NULL)
8093 return -1;
8094
Jouni Malinen2b89da82012-08-31 22:04:41 +03008095 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8096 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008097}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008098
8099
8100int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8101{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008102 int ret;
8103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008104 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8105 return 0;
8106
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008107 ret = p2p_in_progress(wpa_s->global->p2p);
8108 if (ret == 0) {
8109 /*
8110 * Check whether there is an ongoing WPS provisioning step (or
8111 * other parts of group formation) on another interface since
8112 * p2p_in_progress() does not report this to avoid issues for
8113 * scans during such provisioning step.
8114 */
8115 if (wpa_s->global->p2p_group_formation &&
8116 wpa_s->global->p2p_group_formation != wpa_s) {
8117 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8118 "in group formation",
8119 wpa_s->global->p2p_group_formation->ifname);
8120 ret = 1;
8121 }
8122 }
8123
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008124 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008125 struct os_reltime now;
8126 os_get_reltime(&now);
8127 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8128 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008129 /* Wait for the first client has expired */
8130 wpa_s->global->p2p_go_wait_client.sec = 0;
8131 } else {
8132 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8133 ret = 1;
8134 }
8135 }
8136
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008137 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008138}
8139
8140
8141void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8142 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008143{
8144 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8145 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008146 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008147 /**
8148 * Remove the network by scheduling the group formation
8149 * timeout to happen immediately. The teardown code
8150 * needs to be scheduled to run asynch later so that we
8151 * don't delete data from under ourselves unexpectedly.
8152 * Calling wpas_p2p_group_formation_timeout directly
8153 * causes a series of crashes in WPS failure scenarios.
8154 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008155 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8156 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008157 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008158 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008159 }
8160}
8161
8162
8163struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008164 const u8 *addr, const u8 *ssid,
8165 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008166{
8167 struct wpa_ssid *s;
8168 size_t i;
8169
8170 for (s = wpa_s->conf->ssid; s; s = s->next) {
8171 if (s->disabled != 2)
8172 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008173 if (ssid &&
8174 (ssid_len != s->ssid_len ||
8175 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8176 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008177 if (addr == NULL) {
8178 if (s->mode == WPAS_MODE_P2P_GO)
8179 return s;
8180 continue;
8181 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008182 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8183 return s; /* peer is GO in the persistent group */
8184 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8185 continue;
8186 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008187 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008188 addr, ETH_ALEN) == 0)
8189 return s; /* peer is P2P client in persistent
8190 * group */
8191 }
8192 }
8193
8194 return NULL;
8195}
8196
8197
8198void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8199 const u8 *addr)
8200{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008201 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008202 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008203 /*
8204 * This can happen if WPS provisioning step is not terminated
8205 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8206 * peer was able to connect, there is no need to time out group
8207 * formation after this, though. In addition, this is used with
8208 * the initial connection wait on the GO as a separate formation
8209 * timeout and as such, expected to be hit after the initial WPS
8210 * provisioning step.
8211 */
8212 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008213
8214 if (!wpa_s->p2p_go_group_formation_completed &&
8215 !wpa_s->group_formation_reported) {
8216 /*
8217 * GO has not yet notified group formation success since
8218 * the WPS step was not completed cleanly. Do that
8219 * notification now since the P2P Client was able to
8220 * connect and as such, must have received the
8221 * credential from the WPS step.
8222 */
8223 if (wpa_s->global->p2p)
8224 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008225 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008226 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008227 }
8228 if (!wpa_s->p2p_go_group_formation_completed) {
8229 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8230 wpa_s->p2p_go_group_formation_completed = 1;
8231 wpa_s->global->p2p_group_formation = NULL;
8232 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008233 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008234 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008235 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008236 if (addr == NULL)
8237 return;
8238 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8239}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008240
Dmitry Shmidt04949592012-07-19 12:16:46 -07008241
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008242static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8243 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008244{
8245 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008246 int ret = 0;
8247
Dmitry Shmidt04949592012-07-19 12:16:46 -07008248 if (wpa_s->global->p2p_group_formation)
8249 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008250 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008251 offchannel_send_action_done(wpa_s);
8252 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008253 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008254 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8255 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8256 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8257 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008258 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008259 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008260 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008261 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008262 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008263 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008264 wpa_s->p2p_go_he,
8265 wpa_s->p2p_go_edmg,
8266 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008267 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008268}
8269
8270
8271int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8272{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008273 int res;
8274
Dmitry Shmidt04949592012-07-19 12:16:46 -07008275 if (!wpa_s->p2p_fallback_to_go_neg ||
8276 wpa_s->p2p_in_provisioning <= 5)
8277 return 0;
8278
8279 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8280 return 0; /* peer operating as a GO */
8281
8282 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8283 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008284 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008285 "reason=GO-not-found");
8286 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008287
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008288 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008289}
8290
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008291
8292unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8293{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008294 struct wpa_supplicant *ifs;
8295
8296 if (wpa_s->wpa_state > WPA_SCANNING) {
8297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8298 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008299 wpa_s->conf->p2p_search_delay);
8300 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008301 }
8302
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008303 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8304 radio_list) {
8305 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008306 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8307 "delay due to concurrent operation on "
8308 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008309 wpa_s->conf->p2p_search_delay,
8310 ifs->ifname);
8311 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008312 }
8313 }
8314
8315 return 0;
8316}
8317
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008318
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008319static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8320 struct wpa_ssid *s, const u8 *addr,
8321 int iface_addr)
8322{
8323 struct psk_list_entry *psk, *tmp;
8324 int changed = 0;
8325
8326 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8327 list) {
8328 if ((iface_addr && !psk->p2p &&
8329 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8330 (!iface_addr && psk->p2p &&
8331 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8332 wpa_dbg(wpa_s, MSG_DEBUG,
8333 "P2P: Remove persistent group PSK list entry for "
8334 MACSTR " p2p=%u",
8335 MAC2STR(psk->addr), psk->p2p);
8336 dl_list_del(&psk->list);
8337 os_free(psk);
8338 changed++;
8339 }
8340 }
8341
8342 return changed;
8343}
8344
8345
8346void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8347 const u8 *p2p_dev_addr,
8348 const u8 *psk, size_t psk_len)
8349{
8350 struct wpa_ssid *ssid = wpa_s->current_ssid;
8351 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008352 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008353
8354 if (psk_len != sizeof(p->psk))
8355 return;
8356
8357 if (p2p_dev_addr) {
8358 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8359 " p2p_dev_addr=" MACSTR,
8360 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8361 if (is_zero_ether_addr(p2p_dev_addr))
8362 p2p_dev_addr = NULL;
8363 } else {
8364 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8365 MAC2STR(mac_addr));
8366 }
8367
8368 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8369 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8370 /* To be added to persistent group once created */
8371 if (wpa_s->global->add_psk == NULL) {
8372 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8373 if (wpa_s->global->add_psk == NULL)
8374 return;
8375 }
8376 p = wpa_s->global->add_psk;
8377 if (p2p_dev_addr) {
8378 p->p2p = 1;
8379 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8380 } else {
8381 p->p2p = 0;
8382 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8383 }
8384 os_memcpy(p->psk, psk, psk_len);
8385 return;
8386 }
8387
8388 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8389 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8390 return;
8391 }
8392
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008393 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008394 ssid->ssid_len);
8395 if (!persistent) {
8396 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8397 return;
8398 }
8399
8400 p = os_zalloc(sizeof(*p));
8401 if (p == NULL)
8402 return;
8403 if (p2p_dev_addr) {
8404 p->p2p = 1;
8405 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8406 } else {
8407 p->p2p = 0;
8408 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8409 }
8410 os_memcpy(p->psk, psk, psk_len);
8411
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008412 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8413 (last = dl_list_last(&persistent->psk_list,
8414 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008415 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8416 MACSTR " (p2p=%u) to make room for a new one",
8417 MAC2STR(last->addr), last->p2p);
8418 dl_list_del(&last->list);
8419 os_free(last);
8420 }
8421
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008422 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008423 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8424 p2p_dev_addr == NULL);
8425 if (p2p_dev_addr) {
8426 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8427 MACSTR, MAC2STR(p2p_dev_addr));
8428 } else {
8429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8430 MAC2STR(mac_addr));
8431 }
8432 dl_list_add(&persistent->psk_list, &p->list);
8433
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008434 if (wpa_s->p2pdev->conf->update_config &&
8435 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008436 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008437}
8438
8439
8440static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8441 struct wpa_ssid *s, const u8 *addr,
8442 int iface_addr)
8443{
8444 int res;
8445
8446 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008447 if (res > 0 && wpa_s->conf->update_config &&
8448 wpa_config_write(wpa_s->confname, wpa_s->conf))
8449 wpa_dbg(wpa_s, MSG_DEBUG,
8450 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008451}
8452
8453
8454static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8455 const u8 *peer, int iface_addr)
8456{
8457 struct hostapd_data *hapd;
8458 struct hostapd_wpa_psk *psk, *prev, *rem;
8459 struct sta_info *sta;
8460
8461 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8462 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8463 return;
8464
8465 /* Remove per-station PSK entry */
8466 hapd = wpa_s->ap_iface->bss[0];
8467 prev = NULL;
8468 psk = hapd->conf->ssid.wpa_psk;
8469 while (psk) {
8470 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8471 (!iface_addr &&
8472 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8473 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8474 MACSTR " iface_addr=%d",
8475 MAC2STR(peer), iface_addr);
8476 if (prev)
8477 prev->next = psk->next;
8478 else
8479 hapd->conf->ssid.wpa_psk = psk->next;
8480 rem = psk;
8481 psk = psk->next;
8482 os_free(rem);
8483 } else {
8484 prev = psk;
8485 psk = psk->next;
8486 }
8487 }
8488
8489 /* Disconnect from group */
8490 if (iface_addr)
8491 sta = ap_get_sta(hapd, peer);
8492 else
8493 sta = ap_get_sta_p2p(hapd, peer);
8494 if (sta) {
8495 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8496 " (iface_addr=%d) from group",
8497 MAC2STR(peer), iface_addr);
8498 hostapd_drv_sta_deauth(hapd, sta->addr,
8499 WLAN_REASON_DEAUTH_LEAVING);
8500 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8501 }
8502}
8503
8504
8505void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8506 int iface_addr)
8507{
8508 struct wpa_ssid *s;
8509 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008510 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008511
8512 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8513
8514 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008515 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008516 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8517 continue;
8518 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008519 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8520 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008521 }
8522
8523 /* Remove from any operating group */
8524 for (w = wpa_s->global->ifaces; w; w = w->next)
8525 wpas_p2p_remove_client_go(w, peer, iface_addr);
8526}
8527
8528
8529static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8530{
8531 struct wpa_supplicant *wpa_s = eloop_ctx;
8532 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8533}
8534
8535
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008536static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8537{
8538 struct wpa_supplicant *wpa_s = eloop_ctx;
8539
8540 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8541 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8542}
8543
8544
8545int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8546 struct wpa_ssid *ssid)
8547{
8548 struct wpa_supplicant *iface;
8549
8550 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8551 if (!iface->current_ssid ||
8552 iface->current_ssid->frequency == freq ||
8553 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8554 !iface->current_ssid->p2p_group))
8555 continue;
8556
8557 /* Remove the connection with least priority */
8558 if (!wpas_is_p2p_prioritized(iface)) {
8559 /* STA connection has priority over existing
8560 * P2P connection, so remove the interface. */
8561 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8562 eloop_register_timeout(0, 0,
8563 wpas_p2p_group_freq_conflict,
8564 iface, NULL);
8565 /* If connection in progress is P2P connection, do not
8566 * proceed for the connection. */
8567 if (wpa_s == iface)
8568 return -1;
8569 else
8570 return 0;
8571 } else {
8572 /* P2P connection has priority, disable the STA network
8573 */
8574 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8575 ssid);
8576 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8577 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8578 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8579 ETH_ALEN);
8580 /* If P2P connection is in progress, continue
8581 * connecting...*/
8582 if (wpa_s == iface)
8583 return 0;
8584 else
8585 return -1;
8586 }
8587 }
8588
8589 return 0;
8590}
8591
8592
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008593int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8594{
8595 struct wpa_ssid *ssid = wpa_s->current_ssid;
8596
8597 if (ssid == NULL || !ssid->p2p_group)
8598 return 0;
8599
8600 if (wpa_s->p2p_last_4way_hs_fail &&
8601 wpa_s->p2p_last_4way_hs_fail == ssid) {
8602 u8 go_dev_addr[ETH_ALEN];
8603 struct wpa_ssid *persistent;
8604
8605 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8606 ssid->ssid,
8607 ssid->ssid_len) <= 0) {
8608 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8609 goto disconnect;
8610 }
8611
8612 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8613 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008614 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008615 ssid->ssid,
8616 ssid->ssid_len);
8617 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8618 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8619 goto disconnect;
8620 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008621 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008622 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8623 persistent->id);
8624 disconnect:
8625 wpa_s->p2p_last_4way_hs_fail = NULL;
8626 /*
8627 * Remove the group from a timeout to avoid issues with caller
8628 * continuing to use the interface if this is on a P2P group
8629 * interface.
8630 */
8631 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8632 wpa_s, NULL);
8633 return 1;
8634 }
8635
8636 wpa_s->p2p_last_4way_hs_fail = ssid;
8637 return 0;
8638}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008639
8640
8641#ifdef CONFIG_WPS_NFC
8642
8643static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8644 struct wpabuf *p2p)
8645{
8646 struct wpabuf *ret;
8647 size_t wsc_len;
8648
8649 if (p2p == NULL) {
8650 wpabuf_free(wsc);
8651 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8652 return NULL;
8653 }
8654
8655 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8656 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8657 if (ret == NULL) {
8658 wpabuf_free(wsc);
8659 wpabuf_free(p2p);
8660 return NULL;
8661 }
8662
8663 wpabuf_put_be16(ret, wsc_len);
8664 if (wsc)
8665 wpabuf_put_buf(ret, wsc);
8666 wpabuf_put_be16(ret, wpabuf_len(p2p));
8667 wpabuf_put_buf(ret, p2p);
8668
8669 wpabuf_free(wsc);
8670 wpabuf_free(p2p);
8671 wpa_hexdump_buf(MSG_DEBUG,
8672 "P2P: Generated NFC connection handover message", ret);
8673
8674 if (ndef && ret) {
8675 struct wpabuf *tmp;
8676 tmp = ndef_build_p2p(ret);
8677 wpabuf_free(ret);
8678 if (tmp == NULL) {
8679 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8680 return NULL;
8681 }
8682 ret = tmp;
8683 }
8684
8685 return ret;
8686}
8687
8688
8689static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8690 struct wpa_ssid **ssid, u8 *go_dev_addr)
8691{
8692 struct wpa_supplicant *iface;
8693
8694 if (go_dev_addr)
8695 os_memset(go_dev_addr, 0, ETH_ALEN);
8696 if (ssid)
8697 *ssid = NULL;
8698 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8699 if (iface->wpa_state < WPA_ASSOCIATING ||
8700 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8701 !iface->current_ssid->p2p_group ||
8702 iface->current_ssid->mode != WPAS_MODE_INFRA)
8703 continue;
8704 if (ssid)
8705 *ssid = iface->current_ssid;
8706 if (go_dev_addr)
8707 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8708 return iface->assoc_freq;
8709 }
8710 return 0;
8711}
8712
8713
8714struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8715 int ndef)
8716{
8717 struct wpabuf *wsc, *p2p;
8718 struct wpa_ssid *ssid;
8719 u8 go_dev_addr[ETH_ALEN];
8720 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8721
8722 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8723 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8724 return NULL;
8725 }
8726
8727 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8728 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8729 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8730 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8731 return NULL;
8732 }
8733
8734 if (cli_freq == 0) {
8735 wsc = wps_build_nfc_handover_req_p2p(
8736 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8737 } else
8738 wsc = NULL;
8739 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8740 go_dev_addr, ssid ? ssid->ssid : NULL,
8741 ssid ? ssid->ssid_len : 0);
8742
8743 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8744}
8745
8746
8747struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8748 int ndef, int tag)
8749{
8750 struct wpabuf *wsc, *p2p;
8751 struct wpa_ssid *ssid;
8752 u8 go_dev_addr[ETH_ALEN];
8753 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8754
8755 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8756 return NULL;
8757
8758 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8759 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8760 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8761 return NULL;
8762
8763 if (cli_freq == 0) {
8764 wsc = wps_build_nfc_handover_sel_p2p(
8765 wpa_s->parent->wps,
8766 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8767 DEV_PW_NFC_CONNECTION_HANDOVER,
8768 wpa_s->conf->wps_nfc_dh_pubkey,
8769 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8770 } else
8771 wsc = NULL;
8772 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8773 go_dev_addr, ssid ? ssid->ssid : NULL,
8774 ssid ? ssid->ssid_len : 0);
8775
8776 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8777}
8778
8779
8780static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8781 struct p2p_nfc_params *params)
8782{
8783 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8784 "connection handover (freq=%d)",
8785 params->go_freq);
8786
8787 if (params->go_freq && params->go_ssid_len) {
8788 wpa_s->p2p_wps_method = WPS_NFC;
8789 wpa_s->pending_join_wps_method = WPS_NFC;
8790 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8791 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8792 ETH_ALEN);
8793 return wpas_p2p_join_start(wpa_s, params->go_freq,
8794 params->go_ssid,
8795 params->go_ssid_len);
8796 }
8797
8798 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8799 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008800 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008801 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008802 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008803 params->go_ssid_len ? params->go_ssid : NULL,
8804 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008805}
8806
8807
8808static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8809 struct p2p_nfc_params *params, int tag)
8810{
8811 int res, persistent;
8812 struct wpa_ssid *ssid;
8813
8814 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8815 "connection handover");
8816 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8817 ssid = wpa_s->current_ssid;
8818 if (ssid == NULL)
8819 continue;
8820 if (ssid->mode != WPAS_MODE_P2P_GO)
8821 continue;
8822 if (wpa_s->ap_iface == NULL)
8823 continue;
8824 break;
8825 }
8826 if (wpa_s == NULL) {
8827 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8828 return -1;
8829 }
8830
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008831 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008832 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008833 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008834 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8835 return -1;
8836 }
8837 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008838 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8839 wpa_s->p2pdev->p2p_oob_dev_pw,
8840 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8841 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008842 if (res)
8843 return res;
8844
8845 if (!tag) {
8846 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8847 return 0;
8848 }
8849
8850 if (!params->peer ||
8851 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8852 return 0;
8853
8854 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8855 " to join", MAC2STR(params->peer->p2p_device_addr));
8856
8857 wpa_s->global->p2p_invite_group = wpa_s;
8858 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008859 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008860 params->peer->p2p_device_addr,
8861 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008862 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008863
8864 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8865 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8866 ssid->ssid, ssid->ssid_len, ssid->frequency,
8867 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008868 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008869}
8870
8871
8872static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8873 struct p2p_nfc_params *params,
8874 int forced_freq)
8875{
8876 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8877 "connection handover");
8878 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8879 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008880 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008881 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008882 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8883 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008884}
8885
8886
8887static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8888 struct p2p_nfc_params *params,
8889 int forced_freq)
8890{
8891 int res;
8892
8893 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8894 "connection handover");
8895 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8896 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008897 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008898 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008899 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8900 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008901 if (res)
8902 return res;
8903
8904 res = wpas_p2p_listen(wpa_s, 60);
8905 if (res) {
8906 p2p_unauthorize(wpa_s->global->p2p,
8907 params->peer->p2p_device_addr);
8908 }
8909
8910 return res;
8911}
8912
8913
8914static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8915 const struct wpabuf *data,
8916 int sel, int tag, int forced_freq)
8917{
8918 const u8 *pos, *end;
8919 u16 len, id;
8920 struct p2p_nfc_params params;
8921 int res;
8922
8923 os_memset(&params, 0, sizeof(params));
8924 params.sel = sel;
8925
8926 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8927
8928 pos = wpabuf_head(data);
8929 end = pos + wpabuf_len(data);
8930
8931 if (end - pos < 2) {
8932 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8933 "attributes");
8934 return -1;
8935 }
8936 len = WPA_GET_BE16(pos);
8937 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008938 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008939 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8940 "attributes");
8941 return -1;
8942 }
8943 params.wsc_attr = pos;
8944 params.wsc_len = len;
8945 pos += len;
8946
8947 if (end - pos < 2) {
8948 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8949 "attributes");
8950 return -1;
8951 }
8952 len = WPA_GET_BE16(pos);
8953 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008954 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008955 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8956 "attributes");
8957 return -1;
8958 }
8959 params.p2p_attr = pos;
8960 params.p2p_len = len;
8961 pos += len;
8962
8963 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8964 params.wsc_attr, params.wsc_len);
8965 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8966 params.p2p_attr, params.p2p_len);
8967 if (pos < end) {
8968 wpa_hexdump(MSG_DEBUG,
8969 "P2P: Ignored extra data after P2P attributes",
8970 pos, end - pos);
8971 }
8972
8973 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8974 if (res)
8975 return res;
8976
8977 if (params.next_step == NO_ACTION)
8978 return 0;
8979
8980 if (params.next_step == BOTH_GO) {
8981 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8982 MAC2STR(params.peer->p2p_device_addr));
8983 return 0;
8984 }
8985
8986 if (params.next_step == PEER_CLIENT) {
8987 if (!is_zero_ether_addr(params.go_dev_addr)) {
8988 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8989 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8990 " ssid=\"%s\"",
8991 MAC2STR(params.peer->p2p_device_addr),
8992 params.go_freq,
8993 MAC2STR(params.go_dev_addr),
8994 wpa_ssid_txt(params.go_ssid,
8995 params.go_ssid_len));
8996 } else {
8997 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8998 "peer=" MACSTR " freq=%d",
8999 MAC2STR(params.peer->p2p_device_addr),
9000 params.go_freq);
9001 }
9002 return 0;
9003 }
9004
9005 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9006 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9007 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9008 return 0;
9009 }
9010
9011 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9012 wpa_s->p2p_oob_dev_pw = NULL;
9013
9014 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9015 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9016 "received");
9017 return -1;
9018 }
9019
9020 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9021 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9022 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9023 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9024 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9025 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9026 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9027
9028 if (tag) {
9029 if (id < 0x10) {
9030 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9031 "peer OOB Device Password Id %u", id);
9032 return -1;
9033 }
9034 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9035 "Device Password Id %u", id);
9036 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9037 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9038 params.oob_dev_pw_len -
9039 WPS_OOB_PUBKEY_HASH_LEN - 2);
9040 wpa_s->p2p_oob_dev_pw_id = id;
9041 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9042 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9043 params.oob_dev_pw_len -
9044 WPS_OOB_PUBKEY_HASH_LEN - 2);
9045 if (wpa_s->p2p_oob_dev_pw == NULL)
9046 return -1;
9047
9048 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9049 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9050 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9051 return -1;
9052 } else {
9053 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9054 "without Device Password");
9055 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9056 }
9057
9058 switch (params.next_step) {
9059 case NO_ACTION:
9060 case BOTH_GO:
9061 case PEER_CLIENT:
9062 /* already covered above */
9063 return 0;
9064 case JOIN_GROUP:
9065 return wpas_p2p_nfc_join_group(wpa_s, &params);
9066 case AUTH_JOIN:
9067 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9068 case INIT_GO_NEG:
9069 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9070 case RESP_GO_NEG:
9071 /* TODO: use own OOB Dev Pw */
9072 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9073 }
9074
9075 return -1;
9076}
9077
9078
9079int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9080 const struct wpabuf *data, int forced_freq)
9081{
9082 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9083 return -1;
9084
9085 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9086}
9087
9088
9089int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9090 const struct wpabuf *req,
9091 const struct wpabuf *sel, int forced_freq)
9092{
9093 struct wpabuf *tmp;
9094 int ret;
9095
9096 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9097 return -1;
9098
9099 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9100
9101 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9102 wpabuf_head(req), wpabuf_len(req));
9103 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9104 wpabuf_head(sel), wpabuf_len(sel));
9105 if (forced_freq)
9106 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9107 tmp = ndef_parse_p2p(init ? sel : req);
9108 if (tmp == NULL) {
9109 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9110 return -1;
9111 }
9112
9113 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9114 forced_freq);
9115 wpabuf_free(tmp);
9116
9117 return ret;
9118}
9119
9120
9121int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9122{
9123 const u8 *if_addr;
9124 int go_intent = wpa_s->conf->p2p_go_intent;
9125 struct wpa_supplicant *iface;
9126
9127 if (wpa_s->global->p2p == NULL)
9128 return -1;
9129
9130 if (!enabled) {
9131 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9132 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9133 {
9134 if (!iface->ap_iface)
9135 continue;
9136 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9137 }
9138 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9139 0, NULL);
9140 if (wpa_s->p2p_nfc_tag_enabled)
9141 wpas_p2p_remove_pending_group_interface(wpa_s);
9142 wpa_s->p2p_nfc_tag_enabled = 0;
9143 return 0;
9144 }
9145
9146 if (wpa_s->global->p2p_disabled)
9147 return -1;
9148
9149 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9150 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9151 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9152 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9153 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9154 "to allow static handover cases");
9155 return -1;
9156 }
9157
9158 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9159
9160 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9161 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9162 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9163 if (wpa_s->p2p_oob_dev_pw == NULL)
9164 return -1;
9165 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9166
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009167 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9168 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9169 /*
9170 * P2P Group Interface present and the command came on group
9171 * interface, so enable the token for the current interface.
9172 */
9173 wpa_s->create_p2p_iface = 0;
9174 } else {
9175 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9176 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009177
9178 if (wpa_s->create_p2p_iface) {
9179 enum wpa_driver_if_type iftype;
9180 /* Prepare to add a new interface for the group */
9181 iftype = WPA_IF_P2P_GROUP;
9182 if (go_intent == 15)
9183 iftype = WPA_IF_P2P_GO;
9184 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9185 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9186 "interface for the group");
9187 return -1;
9188 }
9189
9190 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009191 } else if (wpa_s->p2p_mgmt)
9192 if_addr = wpa_s->parent->own_addr;
9193 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009194 if_addr = wpa_s->own_addr;
9195
9196 wpa_s->p2p_nfc_tag_enabled = enabled;
9197
9198 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9199 struct hostapd_data *hapd;
9200 if (iface->ap_iface == NULL)
9201 continue;
9202 hapd = iface->ap_iface->bss[0];
9203 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9204 hapd->conf->wps_nfc_dh_pubkey =
9205 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9206 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9207 hapd->conf->wps_nfc_dh_privkey =
9208 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9209 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9210 hapd->conf->wps_nfc_dev_pw =
9211 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9212 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9213
9214 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9215 wpa_dbg(iface, MSG_DEBUG,
9216 "P2P: Failed to enable NFC Tag for GO");
9217 }
9218 }
9219 p2p_set_authorized_oob_dev_pw_id(
9220 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9221 if_addr);
9222
9223 return 0;
9224}
9225
9226#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009227
9228
9229static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9230 struct wpa_used_freq_data *freqs,
9231 unsigned int num)
9232{
9233 u8 curr_chan, cand, chan;
9234 unsigned int i;
9235
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009236 /*
9237 * If possible, optimize the Listen channel to be a channel that is
9238 * already used by one of the other interfaces.
9239 */
9240 if (!wpa_s->conf->p2p_optimize_listen_chan)
9241 return;
9242
9243 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9244 return;
9245
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009246 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9247 for (i = 0, cand = 0; i < num; i++) {
9248 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9249 if (curr_chan == chan) {
9250 cand = 0;
9251 break;
9252 }
9253
9254 if (chan == 1 || chan == 6 || chan == 11)
9255 cand = chan;
9256 }
9257
9258 if (cand) {
9259 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009260 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009261 cand);
9262 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9263 }
9264}
9265
9266
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009267static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009268{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009269 struct hostapd_config *conf;
9270 struct p2p_go_neg_results params;
9271 struct csa_settings csa_settings;
9272 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9273 int old_freq = current_ssid->frequency;
9274 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009275
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009276 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9277 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9278 return -1;
9279 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009280
9281 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009282 * TODO: This function may not always work correctly. For example,
9283 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009284 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009285 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9286 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009287 wpa_dbg(wpa_s, MSG_DEBUG,
9288 "P2P CSA: Failed to select new frequency for GO");
9289 return -1;
9290 }
9291
9292 if (current_ssid->frequency == params.freq) {
9293 wpa_dbg(wpa_s, MSG_DEBUG,
9294 "P2P CSA: Selected same frequency - not moving GO");
9295 return 0;
9296 }
9297
9298 conf = hostapd_config_defaults();
9299 if (!conf) {
9300 wpa_dbg(wpa_s, MSG_DEBUG,
9301 "P2P CSA: Failed to allocate default config");
9302 return -1;
9303 }
9304
9305 current_ssid->frequency = params.freq;
9306 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9307 wpa_dbg(wpa_s, MSG_DEBUG,
9308 "P2P CSA: Failed to create new GO config");
9309 ret = -1;
9310 goto out;
9311 }
9312
9313 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9314 wpa_dbg(wpa_s, MSG_DEBUG,
9315 "P2P CSA: CSA to a different band is not supported");
9316 ret = -1;
9317 goto out;
9318 }
9319
9320 os_memset(&csa_settings, 0, sizeof(csa_settings));
9321 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9322 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9323 csa_settings.freq_params.freq = params.freq;
9324 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9325 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9326 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9327
9328 if (conf->ieee80211ac) {
9329 int freq1 = 0, freq2 = 0;
9330 u8 chan, opclass;
9331
9332 if (ieee80211_freq_to_channel_ext(params.freq,
9333 conf->secondary_channel,
9334 conf->vht_oper_chwidth,
9335 &opclass, &chan) ==
9336 NUM_HOSTAPD_MODES) {
9337 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9338 ret = -1;
9339 goto out;
9340 }
9341
9342 if (conf->vht_oper_centr_freq_seg0_idx)
9343 freq1 = ieee80211_chan_to_freq(
9344 NULL, opclass,
9345 conf->vht_oper_centr_freq_seg0_idx);
9346
9347 if (conf->vht_oper_centr_freq_seg1_idx)
9348 freq2 = ieee80211_chan_to_freq(
9349 NULL, opclass,
9350 conf->vht_oper_centr_freq_seg1_idx);
9351
9352 if (freq1 < 0 || freq2 < 0) {
9353 wpa_dbg(wpa_s, MSG_DEBUG,
9354 "P2P CSA: Selected invalid VHT center freqs");
9355 ret = -1;
9356 goto out;
9357 }
9358
9359 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9360 csa_settings.freq_params.center_freq1 = freq1;
9361 csa_settings.freq_params.center_freq2 = freq2;
9362
9363 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009364 case CHANWIDTH_80MHZ:
9365 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009366 csa_settings.freq_params.bandwidth = 80;
9367 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009368 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009369 csa_settings.freq_params.bandwidth = 160;
9370 break;
9371 }
9372 }
9373
9374 ret = ap_switch_channel(wpa_s, &csa_settings);
9375out:
9376 current_ssid->frequency = old_freq;
9377 hostapd_config_free(conf);
9378 return ret;
9379}
9380
9381
9382static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9383{
9384 struct p2p_go_neg_results params;
9385 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9386
9387 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9388
9389 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9390 current_ssid->frequency);
9391
9392 /* Stop the AP functionality */
9393 /* TODO: Should do this in a way that does not indicated to possible
9394 * P2P Clients in the group that the group is terminated. */
9395 wpa_supplicant_ap_deinit(wpa_s);
9396
9397 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009398 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9399 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009400 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9401 wpas_p2p_group_delete(wpa_s,
9402 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9403 return;
9404 }
9405 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9406 params.freq);
9407
9408 if (params.freq &&
9409 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9410 wpa_printf(MSG_DEBUG,
9411 "P2P: Selected freq (%u MHz) is not valid for P2P",
9412 params.freq);
9413 wpas_p2p_group_delete(wpa_s,
9414 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9415 return;
9416 }
9417
9418 /* Update the frequency */
9419 current_ssid->frequency = params.freq;
9420 wpa_s->connect_without_scan = current_ssid;
9421 wpa_s->reassociate = 1;
9422 wpa_s->disconnected = 0;
9423 wpa_supplicant_req_scan(wpa_s, 0, 0);
9424}
9425
9426
9427static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9428{
9429 struct wpa_supplicant *wpa_s = eloop_ctx;
9430
9431 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009432 return;
9433
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009434 wpas_p2p_go_update_common_freqs(wpa_s);
9435
9436 /* Do not move GO in the middle of a CSA */
9437 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9438 wpa_printf(MSG_DEBUG,
9439 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009440 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009441 }
9442
9443 /*
9444 * First, try a channel switch flow. If it is not supported or fails,
9445 * take down the GO and bring it up again.
9446 */
9447 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9448 wpas_p2p_move_go_no_csa(wpa_s);
9449}
9450
9451
9452static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9453{
9454 struct wpa_supplicant *wpa_s = eloop_ctx;
9455 struct wpa_used_freq_data *freqs = NULL;
9456 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009457
9458 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9459 if (!freqs)
9460 return;
9461
9462 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9463
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009464 /* Previous attempt to move a GO was not possible -- try again. */
9465 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9466 WPAS_P2P_CHANNEL_UPDATE_ANY);
9467
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009468 os_free(freqs);
9469}
9470
9471
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009472/*
9473 * Consider moving a GO from its currently used frequency:
9474 * 1. It is possible that due to regulatory consideration the frequency
9475 * can no longer be used and there is a need to evacuate the GO.
9476 * 2. It is possible that due to MCC considerations, it would be preferable
9477 * to move the GO to a channel that is currently used by some other
9478 * station interface.
9479 *
9480 * In case a frequency that became invalid is once again valid, cancel a
9481 * previously initiated GO frequency change.
9482 */
9483static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9484 struct wpa_used_freq_data *freqs,
9485 unsigned int num)
9486{
9487 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9488 unsigned int timeout;
9489 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009490 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009491
9492 wpas_p2p_go_update_common_freqs(wpa_s);
9493
9494 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009495 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009496 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009497 for (i = 0, invalid_freq = 0; i < num; i++) {
9498 if (freqs[i].freq == freq) {
9499 flags = freqs[i].flags;
9500
9501 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009502 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9503 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009504 wpa_dbg(wpa_s, MSG_DEBUG,
9505 "P2P: Freq=%d MHz no longer valid for GO",
9506 freq);
9507 invalid_freq = 1;
9508 }
9509 } else if (freqs[i].flags == 0) {
9510 /* Freq is not used by any other station interface */
9511 continue;
9512 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009513 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009514 /* Freq is not valid for P2P use cases */
9515 continue;
9516 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9517 P2P_GO_FREQ_MOVE_SCM) {
9518 policy_move = 1;
9519 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9520 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9521 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9522 policy_move = 1;
9523 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9524 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9525 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9526 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9527 policy_move = 1;
9528 } else if ((wpa_s->drv_flags &
9529 WPA_DRIVER_FLAGS_AP_CSA) &&
9530 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9531 u8 chan;
9532
9533 /*
9534 * We do not support CSA between bands, so move
9535 * GO only within the same band.
9536 */
9537 if (wpa_s->ap_iface->current_mode->mode ==
9538 ieee80211_freq_to_chan(freqs[i].freq,
9539 &chan))
9540 policy_move = 1;
9541 }
9542 }
9543 }
9544
9545 wpa_dbg(wpa_s, MSG_DEBUG,
9546 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9547 invalid_freq, policy_move, flags);
9548
9549 /*
9550 * The channel is valid, or we are going to have a policy move, so
9551 * cancel timeout.
9552 */
9553 if (!invalid_freq || policy_move) {
9554 wpa_dbg(wpa_s, MSG_DEBUG,
9555 "P2P: Cancel a GO move from freq=%d MHz", freq);
9556 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9557
9558 if (wpas_p2p_in_progress(wpa_s)) {
9559 wpa_dbg(wpa_s, MSG_DEBUG,
9560 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9561 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9562 wpa_s, NULL);
9563 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9564 wpas_p2p_reconsider_moving_go,
9565 wpa_s, NULL);
9566 return;
9567 }
9568 }
9569
9570 if (!invalid_freq && (!policy_move || flags != 0)) {
9571 wpa_dbg(wpa_s, MSG_DEBUG,
9572 "P2P: Not initiating a GO frequency change");
9573 return;
9574 }
9575
9576 /*
9577 * Do not consider moving GO if it is in the middle of a CSA. When the
9578 * CSA is finished this flow should be retriggered.
9579 */
9580 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9581 wpa_dbg(wpa_s, MSG_DEBUG,
9582 "P2P: Not initiating a GO frequency change - CSA is in progress");
9583 return;
9584 }
9585
9586 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9587 timeout = P2P_GO_FREQ_CHANGE_TIME;
9588 else
9589 timeout = 0;
9590
9591 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9592 freq, timeout);
9593 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9594 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9595}
9596
9597
9598static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9599 struct wpa_used_freq_data *freqs,
9600 unsigned int num,
9601 enum wpas_p2p_channel_update_trig trig)
9602{
9603 struct wpa_supplicant *ifs;
9604
9605 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9606 NULL);
9607
9608 /*
9609 * Travers all the radio interfaces, and for each GO interface, check
9610 * if there is a need to move the GO from the frequency it is using,
9611 * or in case the frequency is valid again, cancel the evacuation flow.
9612 */
9613 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9614 radio_list) {
9615 if (ifs->current_ssid == NULL ||
9616 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9617 continue;
9618
9619 /*
9620 * The GO was just started or completed channel switch, no need
9621 * to move it.
9622 */
9623 if (wpa_s == ifs &&
9624 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9625 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9626 wpa_dbg(wpa_s, MSG_DEBUG,
9627 "P2P: GO move - schedule re-consideration");
9628 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9629 wpas_p2p_reconsider_moving_go,
9630 wpa_s, NULL);
9631 continue;
9632 }
9633
9634 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9635 }
9636}
9637
9638
9639void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9640{
9641 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9642 return;
9643
9644 wpas_p2p_update_channel_list(wpa_s,
9645 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9646}
9647
9648
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009649void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9650{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009651 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9652 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9653 "the management interface is being removed");
9654 wpas_p2p_deinit_global(wpa_s->global);
9655 }
9656}
9657
9658
9659void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9660{
9661 if (wpa_s->ap_iface->bss)
9662 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9663 wpas_p2p_group_deinit(wpa_s);
9664}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009665
9666
9667int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9668 unsigned int period, unsigned int interval,
9669 unsigned int count)
9670{
9671 struct p2p_data *p2p = wpa_s->global->p2p;
9672 u8 *device_types;
9673 size_t dev_types_len;
9674 struct wpabuf *buf;
9675 int ret;
9676
9677 if (wpa_s->p2p_lo_started) {
9678 wpa_dbg(wpa_s, MSG_DEBUG,
9679 "P2P Listen offload is already started");
9680 return 0;
9681 }
9682
9683 if (wpa_s->global->p2p == NULL ||
9684 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9685 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9686 return -1;
9687 }
9688
9689 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9690 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9691 freq);
9692 return -1;
9693 }
9694
9695 /* Get device type */
9696 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9697 WPS_DEV_TYPE_LEN;
9698 device_types = os_malloc(dev_types_len);
9699 if (!device_types)
9700 return -1;
9701 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9702 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9703 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9704
9705 /* Get Probe Response IE(s) */
9706 buf = p2p_build_probe_resp_template(p2p, freq);
9707 if (!buf) {
9708 os_free(device_types);
9709 return -1;
9710 }
9711
9712 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9713 device_types, dev_types_len,
9714 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9715 if (ret < 0)
9716 wpa_dbg(wpa_s, MSG_DEBUG,
9717 "P2P: Failed to start P2P listen offload");
9718
9719 os_free(device_types);
9720 wpabuf_free(buf);
9721
9722 if (ret == 0) {
9723 wpa_s->p2p_lo_started = 1;
9724
9725 /* Stop current P2P listen if any */
9726 wpas_stop_listen(wpa_s);
9727 }
9728
9729 return ret;
9730}
9731
9732
9733int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9734{
9735 int ret;
9736
9737 if (!wpa_s->p2p_lo_started)
9738 return 0;
9739
9740 ret = wpa_drv_p2p_lo_stop(wpa_s);
9741 if (ret < 0)
9742 wpa_dbg(wpa_s, MSG_DEBUG,
9743 "P2P: Failed to stop P2P listen offload");
9744
9745 wpa_s->p2p_lo_started = 0;
9746 return ret;
9747}