blob: b0bea614c10513f89b5e66afd4dadc2aefed9d70 [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 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004928 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004929 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004930 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 }
4932}
4933
4934
Dmitry Shmidt04949592012-07-19 12:16:46 -07004935static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4936{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004937 int res;
4938 unsigned int num, i;
4939 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004940
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004941 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4942 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004943 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004944 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004945
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004946 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4947 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004948 if (!freqs)
4949 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004950
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004951 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4952 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004953
4954 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004955 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004956 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4957 freq);
4958 res = 0;
4959 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004960 }
4961 }
4962
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004963 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004964 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004965
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004966exit_free:
4967 os_free(freqs);
4968 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004969}
4970
4971
4972static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4973 const u8 *peer_dev_addr)
4974{
4975 struct wpa_bss *bss;
4976 int updated;
4977
4978 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4979 if (bss == NULL)
4980 return -1;
4981 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4982 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4983 "last scan");
4984 return 0;
4985 }
4986
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004987 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4988 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004989 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4990 "%ld.%06ld (%supdated in last scan)",
4991 bss->last_update.sec, bss->last_update.usec,
4992 updated ? "": "not ");
4993
4994 return updated;
4995}
4996
4997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4999 struct wpa_scan_results *scan_res)
5000{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005001 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 int freq;
5003 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5006
5007 if (wpa_s->global->p2p_disabled)
5008 return;
5009
Dmitry Shmidt04949592012-07-19 12:16:46 -07005010 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5011 scan_res ? (int) scan_res->num : -1,
5012 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005013
5014 if (scan_res)
5015 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5016
Dmitry Shmidt04949592012-07-19 12:16:46 -07005017 if (wpa_s->p2p_auto_pd) {
5018 int join = wpas_p2p_peer_go(wpa_s,
5019 wpa_s->pending_join_dev_addr);
5020 if (join == 0 &&
5021 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5022 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005023 bss = wpa_bss_get_bssid_latest(
5024 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005025 if (bss) {
5026 freq = bss->freq;
5027 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5028 "the peer " MACSTR " at %d MHz",
5029 wpa_s->auto_pd_scan_retry,
5030 MAC2STR(wpa_s->
5031 pending_join_dev_addr),
5032 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005033 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005034 return;
5035 }
5036 }
5037
5038 if (join < 0)
5039 join = 0;
5040
5041 wpa_s->p2p_auto_pd = 0;
5042 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5043 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5044 MAC2STR(wpa_s->pending_join_dev_addr), join);
5045 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005046 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005047 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005048 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005049 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005050 wpa_msg_global(wpa_s, MSG_INFO,
5051 P2P_EVENT_PROV_DISC_FAILURE
5052 " p2p_dev_addr=" MACSTR " status=N/A",
5053 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005054 }
5055 return;
5056 }
5057
5058 if (wpa_s->p2p_auto_join) {
5059 int join = wpas_p2p_peer_go(wpa_s,
5060 wpa_s->pending_join_dev_addr);
5061 if (join < 0) {
5062 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5063 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005064 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005065 P2P_EVENT_FALLBACK_TO_GO_NEG
5066 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005067 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5068 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5069 wpa_s->p2p_persistent_group, 0, 0, 0,
5070 wpa_s->p2p_go_intent,
5071 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005072 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005073 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005074 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005075 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005076 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005077 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005078 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005079 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005080 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005081 return;
5082 }
5083
5084 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5085 "try to join the group", join ? "" :
5086 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005087 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005088 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005089 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005090 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005091 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005092 }
5093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5095 wpa_s->pending_join_iface_addr);
5096 if (freq < 0 &&
5097 p2p_get_interface_addr(wpa_s->global->p2p,
5098 wpa_s->pending_join_dev_addr,
5099 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005100 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5101 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5103 "address for join from " MACSTR " to " MACSTR
5104 " based on newly discovered P2P peer entry",
5105 MAC2STR(wpa_s->pending_join_iface_addr),
5106 MAC2STR(iface_addr));
5107 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5108 ETH_ALEN);
5109
5110 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5111 wpa_s->pending_join_iface_addr);
5112 }
5113 if (freq >= 0) {
5114 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5115 "from P2P peer table: %d MHz", freq);
5116 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005117 if (wpa_s->p2p_join_ssid_len) {
5118 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5119 MACSTR " and SSID %s",
5120 MAC2STR(wpa_s->pending_join_iface_addr),
5121 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5122 wpa_s->p2p_join_ssid_len));
5123 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5124 wpa_s->p2p_join_ssid,
5125 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005126 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005127 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5128 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5129 bss = wpa_bss_get_bssid_latest(wpa_s,
5130 wpa_s->pending_join_iface_addr);
5131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005133 u8 dev_addr[ETH_ALEN];
5134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135 freq = bss->freq;
5136 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005137 "from BSS table: %d MHz (SSID %s)", freq,
5138 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005139 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5140 dev_addr) == 0 &&
5141 os_memcmp(wpa_s->pending_join_dev_addr,
5142 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5143 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5144 ETH_ALEN) != 0) {
5145 wpa_printf(MSG_DEBUG,
5146 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5147 MAC2STR(dev_addr),
5148 MAC2STR(wpa_s->pending_join_dev_addr));
5149 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5150 ETH_ALEN);
5151 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152 }
5153 if (freq > 0) {
5154 u16 method;
5155
Dmitry Shmidt04949592012-07-19 12:16:46 -07005156 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005157 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005158 P2P_EVENT_GROUP_FORMATION_FAILURE
5159 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005160 wpas_notify_p2p_group_formation_failure(
5161 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005162 return;
5163 }
5164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5166 "prior to joining an existing group (GO " MACSTR
5167 " freq=%u MHz)",
5168 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5169 wpa_s->pending_pd_before_join = 1;
5170
5171 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172 case WPS_PIN_DISPLAY:
5173 method = WPS_CONFIG_KEYPAD;
5174 break;
5175 case WPS_PIN_KEYPAD:
5176 method = WPS_CONFIG_DISPLAY;
5177 break;
5178 case WPS_PBC:
5179 method = WPS_CONFIG_PUSHBUTTON;
5180 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005181 case WPS_P2PS:
5182 method = WPS_CONFIG_P2PS;
5183 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184 default:
5185 method = 0;
5186 break;
5187 }
5188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005189 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5190 wpa_s->pending_join_dev_addr) ==
5191 method)) {
5192 /*
5193 * We have already performed provision discovery for
5194 * joining the group. Proceed directly to join
5195 * operation without duplicated provision discovery. */
5196 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5197 "with " MACSTR " already done - proceed to "
5198 "join",
5199 MAC2STR(wpa_s->pending_join_dev_addr));
5200 wpa_s->pending_pd_before_join = 0;
5201 goto start;
5202 }
5203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005205 wpa_s->pending_join_dev_addr,
5206 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005207 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5209 "Discovery Request before joining an "
5210 "existing group");
5211 wpa_s->pending_pd_before_join = 0;
5212 goto start;
5213 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214 return;
5215 }
5216
5217 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5218 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5219 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5220 wpas_p2p_check_join_scan_limit(wpa_s);
5221 return;
5222
5223start:
5224 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005225 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5226 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005227}
5228
5229
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005230static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5231 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233 int ret;
5234 struct wpa_driver_scan_params params;
5235 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005236 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005237 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005238 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239
5240 os_memset(&params, 0, sizeof(params));
5241
5242 /* P2P Wildcard SSID */
5243 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005244 if (ssid && ssid_len) {
5245 params.ssids[0].ssid = ssid;
5246 params.ssids[0].ssid_len = ssid_len;
5247 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5248 wpa_s->p2p_join_ssid_len = ssid_len;
5249 } else {
5250 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5251 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5252 wpa_s->p2p_join_ssid_len = 0;
5253 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005254
5255 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005256 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5257 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5258 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 if (wps_ie == NULL) {
5260 wpas_p2p_scan_res_join(wpa_s, NULL);
5261 return;
5262 }
5263
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005264 if (!freq) {
5265 int oper_freq;
5266 /*
5267 * If freq is not provided, check the operating freq of the GO
5268 * and use a single channel scan on if possible.
5269 */
5270 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5271 wpa_s->pending_join_iface_addr);
5272 if (oper_freq > 0)
5273 freq = oper_freq;
5274 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005275 if (freq > 0) {
5276 freqs[0] = freq;
5277 params.freqs = freqs;
5278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005279
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005280 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5281 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5282 if (ies == NULL) {
5283 wpabuf_free(wps_ie);
5284 wpas_p2p_scan_res_join(wpa_s, NULL);
5285 return;
5286 }
5287 wpabuf_put_buf(ies, wps_ie);
5288 wpabuf_free(wps_ie);
5289
5290 bands = wpas_get_bands(wpa_s, freqs);
5291 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5292
5293 params.p2p_probe = 1;
5294 params.extra_ies = wpabuf_head(ies);
5295 params.extra_ies_len = wpabuf_len(ies);
5296
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005297 if (wpa_s->clear_driver_scan_cache) {
5298 wpa_printf(MSG_DEBUG,
5299 "Request driver to clear scan cache due to local BSS flush");
5300 params.only_new_results = 1;
5301 }
5302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303 /*
5304 * Run a scan to update BSS table and start Provision Discovery once
5305 * the new scan results become available.
5306 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005307 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005308 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005309 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005310 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005311 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005312 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005314
5315 wpabuf_free(ies);
5316
5317 if (ret) {
5318 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5319 "try again later");
5320 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5321 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5322 wpas_p2p_check_join_scan_limit(wpa_s);
5323 }
5324}
5325
5326
Dmitry Shmidt04949592012-07-19 12:16:46 -07005327static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5328{
5329 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005330 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005331}
5332
5333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005335 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005336 int auto_join, int op_freq,
5337 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338{
5339 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005340 MACSTR " dev " MACSTR " op_freq=%d)%s",
5341 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005342 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005343 if (ssid && ssid_len) {
5344 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5345 wpa_ssid_txt(ssid, ssid_len));
5346 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347
Dmitry Shmidt04949592012-07-19 12:16:46 -07005348 wpa_s->p2p_auto_pd = 0;
5349 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005350 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5351 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5352 wpa_s->pending_join_wps_method = wps_method;
5353
5354 /* Make sure we are not running find during connection establishment */
5355 wpas_p2p_stop_find(wpa_s);
5356
5357 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005358 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 return 0;
5360}
5361
5362
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005363static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5364 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365{
5366 struct wpa_supplicant *group;
5367 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005368 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005369
5370 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5371 if (group == NULL)
5372 return -1;
5373 if (group != wpa_s) {
5374 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5375 sizeof(group->p2p_pin));
5376 group->p2p_wps_method = wpa_s->p2p_wps_method;
5377 }
5378
Hai Shalom74f70d42019-02-11 14:42:39 -08005379 /*
5380 * Need to mark the current interface for p2p_group_formation
5381 * when a separate group interface is not used. This is needed
5382 * to allow p2p_cancel stop a pending p2p_connect-join.
5383 * wpas_p2p_init_group_interface() addresses this for the case
5384 * where a separate group interface is used.
5385 */
5386 if (group == wpa_s->parent)
5387 wpa_s->global->p2p_group_formation = group;
5388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005390 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391
5392 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005393 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5395 ETH_ALEN);
5396 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005397 if (freq && ssid && ssid_len) {
5398 res.freq = freq;
5399 res.ssid_len = ssid_len;
5400 os_memcpy(res.ssid, ssid, ssid_len);
5401 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005402 if (ssid && ssid_len) {
5403 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5404 ssid, ssid_len);
5405 } else {
5406 bss = wpa_bss_get_bssid_latest(
5407 wpa_s, wpa_s->pending_join_iface_addr);
5408 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005409 if (bss) {
5410 res.freq = bss->freq;
5411 res.ssid_len = bss->ssid_len;
5412 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5413 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5414 bss->freq,
5415 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005416 } else if (ssid && ssid_len) {
5417 res.ssid_len = ssid_len;
5418 os_memcpy(res.ssid, ssid, ssid_len);
5419 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5420 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005421 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005422 }
5423
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005424 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5425 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5426 "starting client");
5427 wpa_drv_cancel_remain_on_channel(wpa_s);
5428 wpa_s->off_channel_freq = 0;
5429 wpa_s->roc_waiting_drv_freq = 0;
5430 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 wpas_start_wps_enrollee(group, &res);
5432
5433 /*
5434 * Allow a longer timeout for join-a-running-group than normal 15
5435 * second group formation timeout since the GO may not have authorized
5436 * our connection yet.
5437 */
5438 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5439 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5440 wpa_s, NULL);
5441
5442 return 0;
5443}
5444
5445
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005446static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005447 int *force_freq, int *pref_freq, int go,
5448 unsigned int *pref_freq_list,
5449 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005450{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005451 struct wpa_used_freq_data *freqs;
5452 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005453 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5454
5455 max_pref_freq = *num_pref_freq;
5456 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005457
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005458 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5459 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005460 if (!freqs)
5461 return -1;
5462
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005463 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5464 wpa_s->num_multichan_concurrent);
5465
5466 /*
5467 * It is possible that the total number of used frequencies is bigger
5468 * than the number of frequencies used for P2P, so get the system wide
5469 * number of unused frequencies.
5470 */
5471 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5472
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005473 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005474 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5475 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005476
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005477 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005478 int ret;
5479 if (go)
5480 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5481 else
5482 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5483 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005484 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005485 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5486 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005487 /*
5488 * If freq is a DFS channel and DFS is offloaded
5489 * to the driver, allow P2P GO to use it.
5490 */
5491 wpa_printf(MSG_DEBUG,
5492 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5493 freq);
5494 } else {
5495 wpa_printf(MSG_DEBUG,
5496 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5497 freq);
5498 res = -3;
5499 goto exit_free;
5500 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005501 }
5502
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005503 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005504 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005505 freq_in_use = 1;
5506 }
5507
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005508 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005509 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5510 freq);
5511 res = -2;
5512 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005513 }
5514 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5515 "requested channel (%u MHz)", freq);
5516 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005517 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005518 }
5519
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005520 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005521
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005522 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5523 enum wpa_driver_if_type iface_type;
5524
5525 if (go)
5526 iface_type = WPA_IF_P2P_GO;
5527 else
5528 iface_type = WPA_IF_P2P_CLIENT;
5529
5530 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5531 best_freq, go);
5532
5533 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5534 &max_pref_freq,
5535 pref_freq_list);
5536 if (!res && max_pref_freq > 0) {
5537 *num_pref_freq = max_pref_freq;
5538 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005539 while (i < *num_pref_freq &&
5540 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005541 pref_freq_list[i]) ||
5542 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005543 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005544 wpa_printf(MSG_DEBUG,
5545 "P2P: preferred_freq_list[%d]=%d is disallowed",
5546 i, pref_freq_list[i]);
5547 i++;
5548 }
5549 if (i != *num_pref_freq) {
5550 best_freq = pref_freq_list[i];
5551 wpa_printf(MSG_DEBUG,
5552 "P2P: Using preferred_freq_list[%d]=%d",
5553 i, best_freq);
5554 } else {
5555 wpa_printf(MSG_DEBUG,
5556 "P2P: All driver preferred frequencies are disallowed for P2P use");
5557 *num_pref_freq = 0;
5558 }
5559 } else {
5560 wpa_printf(MSG_DEBUG,
5561 "P2P: No preferred frequency list available");
5562 }
5563 }
5564
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005565 /* We have a candidate frequency to use */
5566 if (best_freq > 0) {
5567 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005568 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005569 best_freq);
5570 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005571 } else {
5572 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 -07005573 best_freq);
5574 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005575 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005576 } else if (num_unused > 0) {
5577 wpa_printf(MSG_DEBUG,
5578 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5579 *force_freq = 0;
5580 } else {
5581 wpa_printf(MSG_DEBUG,
5582 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5583 res = -2;
5584 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005585 }
5586
5587exit_ok:
5588 res = 0;
5589exit_free:
5590 os_free(freqs);
5591 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005592}
5593
5594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005595/**
5596 * wpas_p2p_connect - Request P2P Group Formation to be started
5597 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5598 * @peer_addr: Address of the peer P2P Device
5599 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5600 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005601 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005602 * @join: Whether to join an existing group (as a client) instead of starting
5603 * Group Owner negotiation; @peer_addr is BSSID in that case
5604 * @auth: Whether to only authorize the connection instead of doing that and
5605 * initiating Group Owner negotiation
5606 * @go_intent: GO Intent or -1 to use default
5607 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005608 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005609 * @persistent_id: Persistent group credentials to use for forcing GO
5610 * parameters or -1 to generate new values (SSID/passphrase)
5611 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5612 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005613 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005614 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005615 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005616 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005617 * @group_ssid: Specific Group SSID for join or %NULL if not set
5618 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5620 * failure, -2 on failure due to channel not currently available,
5621 * -3 if forced channel is not supported
5622 */
5623int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5624 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005626 int go_intent, int freq, unsigned int vht_center_freq2,
5627 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005628 unsigned int vht_chwidth, int he, int edmg,
5629 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005630{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005631 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005632 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005634 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005635 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005636 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005637
5638 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5639 return -1;
5640
Dmitry Shmidt04949592012-07-19 12:16:46 -07005641 if (persistent_id >= 0) {
5642 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5643 if (ssid == NULL || ssid->disabled != 2 ||
5644 ssid->mode != WPAS_MODE_P2P_GO)
5645 return -1;
5646 }
5647
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005648 os_free(wpa_s->global->add_psk);
5649 wpa_s->global->add_psk = NULL;
5650
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005651 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005652 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005653 wpa_s->global->pending_p2ps_group_freq = 0;
5654 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005656 if (go_intent < 0)
5657 go_intent = wpa_s->conf->p2p_go_intent;
5658
5659 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005660 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661
5662 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005663 wpa_s->p2p_persistent_group = !!persistent_group;
5664 wpa_s->p2p_persistent_id = persistent_id;
5665 wpa_s->p2p_go_intent = go_intent;
5666 wpa_s->p2p_connect_freq = freq;
5667 wpa_s->p2p_fallback_to_go_neg = 0;
5668 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005669 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005670 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005671 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5672 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005673 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005674 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005675
5676 if (pin)
5677 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5678 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005679 if (wps_generate_pin((unsigned int *) &ret) < 0)
5680 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005681 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5682 "%08d", ret);
5683 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5684 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5686 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005687 } else if (wps_method == WPS_P2PS) {
5688 /* Force the P2Ps default PIN to be used */
5689 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005690 } else
5691 wpa_s->p2p_pin[0] = '\0';
5692
Dmitry Shmidt04949592012-07-19 12:16:46 -07005693 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5695 if (auth) {
5696 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5697 "connect a running group from " MACSTR,
5698 MAC2STR(peer_addr));
5699 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5700 return ret;
5701 }
5702 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5703 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5704 iface_addr) < 0) {
5705 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5706 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5707 dev_addr);
5708 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005709 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005710 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005711 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5712 "%ld.%06ld",
5713 wpa_s->p2p_auto_started.sec,
5714 wpa_s->p2p_auto_started.usec);
5715 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005716 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005717 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005718 auto_join, freq,
5719 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 return -1;
5721 return ret;
5722 }
5723
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005724 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005725 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005726 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005727 if (res)
5728 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005729 wpas_p2p_set_own_freq_preference(wpa_s,
5730 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005732 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005734 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005736 if (wpa_s->create_p2p_iface) {
5737 /* Prepare to add a new interface for the group */
5738 iftype = WPA_IF_P2P_GROUP;
5739 if (go_intent == 15)
5740 iftype = WPA_IF_P2P_GO;
5741 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5742 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5743 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005744 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005745 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005747 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005748 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005749 if (wpa_s->p2p_mgmt)
5750 if_addr = wpa_s->parent->own_addr;
5751 else
5752 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005753 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5754 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005755
5756 if (auth) {
5757 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005758 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005759 force_freq, persistent_group, ssid,
5760 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761 return -1;
5762 return ret;
5763 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005764
5765 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5766 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005767 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005768 if (wpa_s->create_p2p_iface)
5769 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 return -1;
5771 }
5772 return ret;
5773}
5774
5775
5776/**
5777 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5778 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5779 * @freq: Frequency of the channel in MHz
5780 * @duration: Duration of the stay on the channel in milliseconds
5781 *
5782 * This callback is called when the driver indicates that it has started the
5783 * requested remain-on-channel duration.
5784 */
5785void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5786 unsigned int freq, unsigned int duration)
5787{
5788 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5789 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005790 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)",
5791 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5792 wpa_s->roc_waiting_drv_freq, freq, duration);
5793 if (wpa_s->off_channel_freq &&
5794 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5796 wpa_s->pending_listen_duration);
5797 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005798 } else {
5799 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5800 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5801 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802 }
5803}
5804
5805
Jithu Jance57cea1a2014-08-20 10:22:04 -07005806int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005807{
5808 /* Limit maximum Listen state time based on driver limitation. */
5809 if (timeout > wpa_s->max_remain_on_chan)
5810 timeout = wpa_s->max_remain_on_chan;
5811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812 return p2p_listen(wpa_s->global->p2p, timeout);
5813}
5814
5815
5816/**
5817 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5818 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5819 * @freq: Frequency of the channel in MHz
5820 *
5821 * This callback is called when the driver indicates that a remain-on-channel
5822 * operation has been completed, i.e., the duration on the requested channel
5823 * has timed out.
5824 */
5825void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5826 unsigned int freq)
5827{
5828 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5829 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005830 wpa_s->global->p2p_long_listen,
5831 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005832 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005833 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5834 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005835 if (wpa_s->global->p2p_long_listen > 0)
5836 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005837 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5838 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005839 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005841 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005842 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005843 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005844 } else {
5845 /*
5846 * When listen duration is over, stop listen & update p2p_state
5847 * to IDLE.
5848 */
5849 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 }
5851}
5852
5853
5854/**
5855 * wpas_p2p_group_remove - Remove a P2P group
5856 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5857 * @ifname: Network interface name of the group interface or "*" to remove all
5858 * groups
5859 * Returns: 0 on success, -1 on failure
5860 *
5861 * This function is used to remove a P2P group. This can be used to disconnect
5862 * from a group in which the local end is a P2P Client or to end a P2P Group in
5863 * case the local end is the Group Owner. If a virtual network interface was
5864 * created for this group, that interface will be removed. Otherwise, only the
5865 * configured P2P group network will be removed from the interface.
5866 */
5867int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5868{
5869 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005870 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871
5872 if (os_strcmp(ifname, "*") == 0) {
5873 struct wpa_supplicant *prev;
5874 wpa_s = global->ifaces;
5875 while (wpa_s) {
5876 prev = wpa_s;
5877 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005878 if (prev->p2p_group_interface !=
5879 NOT_P2P_GROUP_INTERFACE ||
5880 (prev->current_ssid &&
5881 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005882 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883 }
5884 return 0;
5885 }
5886
5887 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5888 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5889 break;
5890 }
5891
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005892 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893}
5894
5895
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005896static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5897{
5898 unsigned int r;
5899
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005900 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5901 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5902 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5903 int res;
5904
5905 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5906 &size, pref_freq_list);
5907 if (!res && size > 0) {
5908 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005909 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005910 (!p2p_supported_freq(wpa_s->global->p2p,
5911 pref_freq_list[i]) ||
5912 wpas_p2p_disallowed_freq(wpa_s->global,
5913 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005914 wpa_printf(MSG_DEBUG,
5915 "P2P: preferred_freq_list[%d]=%d is disallowed",
5916 i, pref_freq_list[i]);
5917 i++;
5918 }
5919 if (i != size) {
5920 freq = pref_freq_list[i];
5921 wpa_printf(MSG_DEBUG,
5922 "P2P: Using preferred_freq_list[%d]=%d",
5923 i, freq);
5924 } else {
5925 wpa_printf(MSG_DEBUG,
5926 "P2P: All driver preferred frequencies are disallowed for P2P use");
5927 }
5928 } else {
5929 wpa_printf(MSG_DEBUG,
5930 "P2P: No preferred frequency list available");
5931 }
5932 }
5933
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005934 if (freq == 2) {
5935 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5936 "band");
5937 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005938 p2p_supported_freq_go(wpa_s->global->p2p,
5939 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005940 freq = wpa_s->best_24_freq;
5941 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5942 "channel: %d MHz", freq);
5943 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005944 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5945 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005946 freq = 2412 + (r % 3) * 25;
5947 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5948 "channel: %d MHz", freq);
5949 }
5950 }
5951
5952 if (freq == 5) {
5953 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5954 "band");
5955 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005956 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005957 wpa_s->best_5_freq)) {
5958 freq = wpa_s->best_5_freq;
5959 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5960 "channel: %d MHz", freq);
5961 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005962 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5963 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005964 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005965 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005966 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5967 "5 GHz channel for P2P group");
5968 return -1;
5969 }
5970 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5971 "channel: %d MHz", freq);
5972 }
5973 }
5974
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005975 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005976 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005977 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5978 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005979 /*
5980 * If freq is a DFS channel and DFS is offloaded to the
5981 * driver, allow P2P GO to use it.
5982 */
5983 wpa_printf(MSG_DEBUG, "P2P: "
5984 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5985 __func__, freq);
5986 return freq;
5987 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005988 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5989 "(%u MHz) is not supported for P2P uses",
5990 freq);
5991 return -1;
5992 }
5993
5994 return freq;
5995}
5996
5997
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005998static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5999 const struct p2p_channels *channels,
6000 int freq)
6001{
6002 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6003 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6004 freq_included(wpa_s, channels, freq))
6005 return 1;
6006 return 0;
6007}
6008
6009
6010static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6011 struct p2p_go_neg_results *params,
6012 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006013{
6014 unsigned int i, r;
6015
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006016 /* try all channels in operating class 115 */
6017 for (i = 0; i < 4; i++) {
6018 params->freq = 5180 + i * 20;
6019 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006020 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006021 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6022 goto out;
6023 }
6024
6025 /* try all channels in operating class 124 */
6026 for (i = 0; i < 4; i++) {
6027 params->freq = 5745 + 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
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006034 /* try social channel class 180 channel 2 */
6035 params->freq = 58320 + 1 * 2160;
6036 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006037 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006038 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6039 goto out;
6040
6041 /* try all channels in reg. class 180 */
6042 for (i = 0; i < 4; i++) {
6043 params->freq = 58320 + i * 2160;
6044 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006045 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006046 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6047 goto out;
6048 }
6049
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006050 /* try some random selection of the social channels */
6051 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6052 return;
6053
6054 for (i = 0; i < 3; i++) {
6055 params->freq = 2412 + ((r + i) % 3) * 25;
6056 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6057 goto out;
6058 }
6059
6060 /* try all other channels in operating class 81 */
6061 for (i = 0; i < 11; i++) {
6062 params->freq = 2412 + i * 5;
6063
6064 /* skip social channels; covered in the previous loop */
6065 if (params->freq == 2412 ||
6066 params->freq == 2437 ||
6067 params->freq == 2462)
6068 continue;
6069
6070 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6071 goto out;
6072 }
6073
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006074 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006075 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006076 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006077out:
6078 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6079 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006080}
6081
6082
Roshan Pius3a1667e2018-07-03 15:17:14 -07006083static int wpas_same_band(int freq1, int freq2)
6084{
6085 enum hostapd_hw_mode mode1, mode2;
6086 u8 chan1, chan2;
6087
6088 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6089 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6090 if (mode1 == NUM_HOSTAPD_MODES)
6091 return 0;
6092 return mode1 == mode2;
6093}
6094
6095
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006096static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6097 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006098 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006099 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006100 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006101 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006103 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006104 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006105 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006106 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006107 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006108
6109 os_memset(params, 0, sizeof(*params));
6110 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006111 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006112 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006113 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006114 params->max_oper_chwidth = max_oper_chwidth;
6115 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006116 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006118 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6119 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006120 if (!freqs)
6121 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006122
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006123 num = get_shared_radio_freqs_data(wpa_s, freqs,
6124 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006125
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006126 if (wpa_s->current_ssid &&
6127 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6128 wpa_s->wpa_state == WPA_COMPLETED) {
6129 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6130 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006131
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006132 /*
6133 * If the frequency selection is done for an active P2P GO that
6134 * is not sharing a frequency, allow to select a new frequency
6135 * even if there are no unused frequencies as we are about to
6136 * move the P2P GO so its frequency can be re-used.
6137 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006138 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006139 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6140 freqs[i].flags == 0) {
6141 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006142 break;
6143 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006144 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006145 }
6146
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006147 /* Try to use EDMG channel */
6148 if (params->edmg) {
6149 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6150 goto success;
6151 params->edmg = 0;
6152 }
6153
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006154 /* try using the forced freq */
6155 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006156 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6157 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006158 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006159 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006160 freq);
6161 goto fail;
6162 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006163 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6164 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006165 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6166 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006167 /*
6168 * If freq is a DFS channel and DFS is offloaded
6169 * to the driver, allow P2P GO to use it.
6170 */
6171 wpa_printf(MSG_DEBUG,
6172 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6173 __func__, freq);
6174 } else {
6175 wpa_printf(MSG_DEBUG,
6176 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6177 freq);
6178 goto fail;
6179 }
6180 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006181
6182 for (i = 0; i < num; i++) {
6183 if (freqs[i].freq == freq) {
6184 wpa_printf(MSG_DEBUG,
6185 "P2P: forced freq (%d MHz) is also shared",
6186 freq);
6187 params->freq = freq;
6188 goto success;
6189 }
6190 }
6191
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006192 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006193 wpa_printf(MSG_DEBUG,
6194 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6195 freq);
6196 goto fail;
6197 }
6198
6199 wpa_printf(MSG_DEBUG,
6200 "P2P: force GO freq (%d MHz) on a free channel",
6201 freq);
6202 params->freq = freq;
6203 goto success;
6204 }
6205
6206 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006207 if (num &&
6208 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006209 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6210 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6211 wpa_printf(MSG_DEBUG,
6212 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006213 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006214 params->freq = cand;
6215 goto success;
6216 }
6217
6218 /* try using one of the shared freqs */
6219 for (i = 0; i < num; i++) {
6220 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6221 freqs[i].freq)) {
6222 wpa_printf(MSG_DEBUG,
6223 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006224 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006225 params->freq = freqs[i].freq;
6226 goto success;
6227 }
6228 }
6229 }
6230
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006231 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006232 wpa_printf(MSG_DEBUG,
6233 "P2P: Cannot force GO on any of the channels we are already using");
6234 goto fail;
6235 }
6236
6237 /* try using the setting from the configuration file */
6238 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6239 wpa_s->conf->p2p_oper_channel >= 1 &&
6240 wpa_s->conf->p2p_oper_channel <= 11 &&
6241 wpas_p2p_supported_freq_go(
6242 wpa_s, channels,
6243 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6244 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6245 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6246 "frequency %d MHz", params->freq);
6247 goto success;
6248 }
6249
6250 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6251 wpa_s->conf->p2p_oper_reg_class == 116 ||
6252 wpa_s->conf->p2p_oper_reg_class == 117 ||
6253 wpa_s->conf->p2p_oper_reg_class == 124 ||
6254 wpa_s->conf->p2p_oper_reg_class == 125 ||
6255 wpa_s->conf->p2p_oper_reg_class == 126 ||
6256 wpa_s->conf->p2p_oper_reg_class == 127) &&
6257 wpas_p2p_supported_freq_go(wpa_s, channels,
6258 5000 +
6259 5 * wpa_s->conf->p2p_oper_channel)) {
6260 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6261 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6262 "frequency %d MHz", params->freq);
6263 goto success;
6264 }
6265
6266 /* Try using best channels */
6267 if (wpa_s->conf->p2p_oper_channel == 0 &&
6268 wpa_s->best_overall_freq > 0 &&
6269 wpas_p2p_supported_freq_go(wpa_s, channels,
6270 wpa_s->best_overall_freq)) {
6271 params->freq = wpa_s->best_overall_freq;
6272 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6273 "channel %d MHz", params->freq);
6274 goto success;
6275 }
6276
6277 if (wpa_s->conf->p2p_oper_channel == 0 &&
6278 wpa_s->best_24_freq > 0 &&
6279 wpas_p2p_supported_freq_go(wpa_s, channels,
6280 wpa_s->best_24_freq)) {
6281 params->freq = wpa_s->best_24_freq;
6282 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6283 "channel %d MHz", params->freq);
6284 goto success;
6285 }
6286
6287 if (wpa_s->conf->p2p_oper_channel == 0 &&
6288 wpa_s->best_5_freq > 0 &&
6289 wpas_p2p_supported_freq_go(wpa_s, channels,
6290 wpa_s->best_5_freq)) {
6291 params->freq = wpa_s->best_5_freq;
6292 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6293 "channel %d MHz", params->freq);
6294 goto success;
6295 }
6296
6297 /* try using preferred channels */
6298 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6299 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6300 params->freq = cand;
6301 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6302 "channels", params->freq);
6303 goto success;
6304 }
6305
Roshan Pius3a1667e2018-07-03 15:17:14 -07006306 /* Try using a channel that allows VHT to be used with 80 MHz */
6307 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6308 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6309 enum hostapd_hw_mode mode;
6310 struct hostapd_hw_modes *hwmode;
6311 u8 chan;
6312
6313 cand = wpa_s->p2p_group_common_freqs[i];
6314 mode = ieee80211_freq_to_chan(cand, &chan);
6315 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006316 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006317 if (!hwmode ||
6318 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6319 BW80) != ALLOWED)
6320 continue;
6321 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6322 params->freq = cand;
6323 wpa_printf(MSG_DEBUG,
6324 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6325 params->freq);
6326 goto success;
6327 }
6328 }
6329 }
6330
6331 /* Try using a channel that allows HT to be used with 40 MHz on the same
6332 * band so that CSA can be used */
6333 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6334 wpa_s->p2p_group_common_freqs) {
6335 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6336 enum hostapd_hw_mode mode;
6337 struct hostapd_hw_modes *hwmode;
6338 u8 chan;
6339
6340 cand = wpa_s->p2p_group_common_freqs[i];
6341 mode = ieee80211_freq_to_chan(cand, &chan);
6342 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006343 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006344 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6345 cand) ||
6346 !hwmode ||
6347 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6348 BW40MINUS) != ALLOWED &&
6349 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6350 BW40PLUS) != ALLOWED))
6351 continue;
6352 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6353 params->freq = cand;
6354 wpa_printf(MSG_DEBUG,
6355 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6356 params->freq);
6357 goto success;
6358 }
6359 }
6360 }
6361
6362 /* Try using one of the group common freqs on the same band so that CSA
6363 * can be used */
6364 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6365 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6366 cand = wpa_s->p2p_group_common_freqs[i];
6367 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6368 cand))
6369 continue;
6370 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6371 params->freq = cand;
6372 wpa_printf(MSG_DEBUG,
6373 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6374 params->freq);
6375 goto success;
6376 }
6377 }
6378 }
6379
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006380 /* Try using one of the group common freqs */
6381 if (wpa_s->p2p_group_common_freqs) {
6382 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6383 cand = wpa_s->p2p_group_common_freqs[i];
6384 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6385 params->freq = cand;
6386 wpa_printf(MSG_DEBUG,
6387 "P2P: Use freq %d MHz common with the peer",
6388 params->freq);
6389 goto success;
6390 }
6391 }
6392 }
6393
6394 /* no preference, select some channel */
6395 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6396
6397 if (params->freq == 0) {
6398 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6399 goto fail;
6400 }
6401
6402success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006403 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006404 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006405fail:
6406 os_free(freqs);
6407 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006408}
6409
6410
6411static struct wpa_supplicant *
6412wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6413 int go)
6414{
6415 struct wpa_supplicant *group_wpa_s;
6416
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006417 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006418 if (wpa_s->p2p_mgmt) {
6419 /*
6420 * We may be called on the p2p_dev interface which
6421 * cannot be used for group operations, so always use
6422 * the primary interface.
6423 */
6424 wpa_s->parent->p2pdev = wpa_s;
6425 wpa_s = wpa_s->parent;
6426 }
6427 wpa_dbg(wpa_s, MSG_DEBUG,
6428 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006429 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006430 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006431 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006432 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006433 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006434
6435 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006436 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006437 wpa_msg_global(wpa_s, MSG_ERROR,
6438 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006439 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006440 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6442 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006443 wpa_msg_global(wpa_s, MSG_ERROR,
6444 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006445 wpas_p2p_remove_pending_group_interface(wpa_s);
6446 return NULL;
6447 }
6448
Roshan Pius3a1667e2018-07-03 15:17:14 -07006449 if (go && wpa_s->p2p_go_do_acs) {
6450 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6451 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6452 wpa_s->p2p_go_do_acs = 0;
6453 }
6454
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006455 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6456 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006457 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006458 return group_wpa_s;
6459}
6460
6461
6462/**
6463 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6464 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6465 * @persistent_group: Whether to create a persistent group
6466 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006467 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006468 * @ht40: Start GO with 40 MHz channel width
6469 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006470 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006471 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006472 * Returns: 0 on success, -1 on failure
6473 *
6474 * This function creates a new P2P group with the local end as the Group Owner,
6475 * i.e., without using Group Owner Negotiation.
6476 */
6477int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006478 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006479 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006480{
6481 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006482
6483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6484 return -1;
6485
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006486 os_free(wpa_s->global->add_psk);
6487 wpa_s->global->add_psk = NULL;
6488
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006489 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006490 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006491 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006492
Roshan Pius3a1667e2018-07-03 15:17:14 -07006493 if (!wpa_s->p2p_go_do_acs) {
6494 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6495 if (freq < 0)
6496 return -1;
6497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006498
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006499 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006500 ht40, vht, max_oper_chwidth, he, edmg,
6501 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006502 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006504 p2p_go_params(wpa_s->global->p2p, &params);
6505 params.persistent_group = persistent_group;
6506
6507 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6508 if (wpa_s == NULL)
6509 return -1;
6510 wpas_start_wps_go(wpa_s, &params, 0);
6511
6512 return 0;
6513}
6514
6515
6516static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006517 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006518 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006519{
6520 struct wpa_ssid *ssid;
6521
6522 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6523 if (wpa_s == NULL)
6524 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006525 if (force_scan)
6526 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006527 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528
6529 wpa_supplicant_ap_deinit(wpa_s);
6530
6531 ssid = wpa_config_add_network(wpa_s->conf);
6532 if (ssid == NULL)
6533 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006534 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535 wpa_config_set_network_defaults(ssid);
6536 ssid->temporary = 1;
6537 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006538 ssid->pbss = params->pbss;
6539 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6540 WPA_CIPHER_CCMP;
6541 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6543 ssid->ssid = os_malloc(params->ssid_len);
6544 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006545 wpa_config_remove_network(wpa_s->conf, ssid->id);
6546 return -1;
6547 }
6548 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6549 ssid->ssid_len = params->ssid_len;
6550 ssid->p2p_group = 1;
6551 ssid->export_keys = 1;
6552 if (params->psk_set) {
6553 os_memcpy(ssid->psk, params->psk, 32);
6554 ssid->psk_set = 1;
6555 }
6556 if (params->passphrase)
6557 ssid->passphrase = os_strdup(params->passphrase);
6558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006560 wpa_s->p2p_in_invitation = 1;
6561 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006562 wpa_s->p2p_go_group_formation_completed = 0;
6563 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006564
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006565 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006566 NULL);
6567 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6568 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006569 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006570 wpa_supplicant_select_network(wpa_s, ssid);
6571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006572 return 0;
6573}
6574
6575
6576int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6577 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006578 int force_freq, int neg_freq,
6579 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006580 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006581 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006582 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006583 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584{
6585 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006586 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587
6588 if (ssid->disabled != 2 || ssid->ssid == NULL)
6589 return -1;
6590
6591 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6592 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6593 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6594 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006595 if (go == 0 &&
6596 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006597 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006598 /*
6599 * This can happen if Invitation Response frame was lost
6600 * and the peer (GO of a persistent group) tries to
6601 * invite us again. Reschedule the timeout to avoid
6602 * terminating the wait for the connection too early
6603 * since we now know that the peer is still trying to
6604 * invite us instead of having already started the GO.
6605 */
6606 wpa_printf(MSG_DEBUG,
6607 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6608 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6609 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006610 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006611 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006612 return 0;
6613 }
6614
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006615 os_free(wpa_s->global->add_psk);
6616 wpa_s->global->add_psk = NULL;
6617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006618 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006619 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006620
Dmitry Shmidt04949592012-07-19 12:16:46 -07006621 wpa_s->p2p_fallback_to_go_neg = 0;
6622
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006623 if (ssid->mode == WPAS_MODE_P2P_GO) {
6624 if (force_freq > 0) {
6625 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6626 if (freq < 0)
6627 return -1;
6628 } else {
6629 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6630 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006631 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006632 freq = 0;
6633 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006634 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006635 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006636 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006637 struct os_reltime now;
6638 struct wpa_bss *bss =
6639 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006640
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006641 os_get_reltime(&now);
6642 if (bss &&
6643 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006644 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006645 freq = bss->freq;
6646 else
6647 freq = 0;
6648 }
6649
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006650 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6651 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006652 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006653 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006654 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006655
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006656 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006657 ht40, vht, max_oper_chwidth, he, edmg,
6658 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006659 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006660
6661 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006662 params.psk_set = ssid->psk_set;
6663 if (params.psk_set)
6664 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006665 if (ssid->passphrase) {
6666 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6667 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6668 "persistent group");
6669 return -1;
6670 }
6671 os_strlcpy(params.passphrase, ssid->passphrase,
6672 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006673 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006674 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6675 params.ssid_len = ssid->ssid_len;
6676 params.persistent_group = 1;
6677
6678 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6679 if (wpa_s == NULL)
6680 return -1;
6681
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006682 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6683
Dmitry Shmidt56052862013-10-04 10:23:25 -07006684 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006685 wpas_start_wps_go(wpa_s, &params, 0);
6686
6687 return 0;
6688}
6689
6690
6691static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6692 struct wpabuf *proberesp_ies)
6693{
6694 struct wpa_supplicant *wpa_s = ctx;
6695 if (wpa_s->ap_iface) {
6696 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006697 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6698 wpabuf_free(beacon_ies);
6699 wpabuf_free(proberesp_ies);
6700 return;
6701 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006702 if (beacon_ies) {
6703 wpabuf_free(hapd->p2p_beacon_ie);
6704 hapd->p2p_beacon_ie = beacon_ies;
6705 }
6706 wpabuf_free(hapd->p2p_probe_resp_ie);
6707 hapd->p2p_probe_resp_ie = proberesp_ies;
6708 } else {
6709 wpabuf_free(beacon_ies);
6710 wpabuf_free(proberesp_ies);
6711 }
6712 wpa_supplicant_ap_update_beacon(wpa_s);
6713}
6714
6715
6716static void wpas_p2p_idle_update(void *ctx, int idle)
6717{
6718 struct wpa_supplicant *wpa_s = ctx;
6719 if (!wpa_s->ap_iface)
6720 return;
6721 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006722 if (idle) {
6723 if (wpa_s->global->p2p_fail_on_wps_complete &&
6724 wpa_s->p2p_in_provisioning) {
6725 wpas_p2p_grpform_fail_after_wps(wpa_s);
6726 return;
6727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006728 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006729 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006730 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6731}
6732
6733
6734struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006735 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006736{
6737 struct p2p_group *group;
6738 struct p2p_group_config *cfg;
6739
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006740 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6741 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006742 return NULL;
6743
6744 cfg = os_zalloc(sizeof(*cfg));
6745 if (cfg == NULL)
6746 return NULL;
6747
Dmitry Shmidt04949592012-07-19 12:16:46 -07006748 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006749 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006750 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006751 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006752 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6753 if (wpa_s->max_stations &&
6754 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6755 cfg->max_clients = wpa_s->max_stations;
6756 else
6757 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006758 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6759 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006760 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006761 cfg->cb_ctx = wpa_s;
6762 cfg->ie_update = wpas_p2p_ie_update;
6763 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006764 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6765 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006766
6767 group = p2p_group_init(wpa_s->global->p2p, cfg);
6768 if (group == NULL)
6769 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006770 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006771 p2p_group_notif_formation_done(group);
6772 wpa_s->p2p_group = group;
6773 return group;
6774}
6775
6776
6777void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6778 int registrar)
6779{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006780 struct wpa_ssid *ssid = wpa_s->current_ssid;
6781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006782 if (!wpa_s->p2p_in_provisioning) {
6783 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6784 "provisioning not in progress");
6785 return;
6786 }
6787
Dmitry Shmidt04949592012-07-19 12:16:46 -07006788 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6789 u8 go_dev_addr[ETH_ALEN];
6790 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6791 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6792 ssid->ssid_len);
6793 /* Clear any stored provisioning info */
6794 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6795 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006796
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006797 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006798 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006799 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006800 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6801 /*
6802 * Use a separate timeout for initial data connection to
6803 * complete to allow the group to be removed automatically if
6804 * something goes wrong in this step before the P2P group idle
6805 * timeout mechanism is taken into use.
6806 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006807 wpa_dbg(wpa_s, MSG_DEBUG,
6808 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6809 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006810 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6811 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006812 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006813 /* Complete group formation on successful data connection. */
6814 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006815 } else if (ssid) {
6816 /*
6817 * Use a separate timeout for initial data connection to
6818 * complete to allow the group to be removed automatically if
6819 * the client does not complete data connection successfully.
6820 */
6821 wpa_dbg(wpa_s, MSG_DEBUG,
6822 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6823 P2P_MAX_INITIAL_CONN_WAIT_GO);
6824 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6825 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006826 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006827 /*
6828 * Complete group formation on first successful data connection
6829 */
6830 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006832 if (wpa_s->global->p2p)
6833 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006834 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835}
6836
6837
Jouni Malinen75ecf522011-06-27 15:19:46 -07006838void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6839 struct wps_event_fail *fail)
6840{
6841 if (!wpa_s->p2p_in_provisioning) {
6842 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6843 "provisioning not in progress");
6844 return;
6845 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006846
6847 if (wpa_s->go_params) {
6848 p2p_clear_provisioning_info(
6849 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006850 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006851 }
6852
Jouni Malinen75ecf522011-06-27 15:19:46 -07006853 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006854
6855 if (wpa_s == wpa_s->global->p2p_group_formation) {
6856 /*
6857 * Allow some time for the failed WPS negotiation exchange to
6858 * complete, but remove the group since group formation cannot
6859 * succeed after provisioning failure.
6860 */
6861 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6862 wpa_s->global->p2p_fail_on_wps_complete = 1;
6863 eloop_deplete_timeout(0, 50000,
6864 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006865 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006866 }
6867}
6868
6869
6870int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6871{
6872 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6873 !wpa_s->p2p_in_provisioning)
6874 return 0;
6875
6876 wpas_p2p_grpform_fail_after_wps(wpa_s);
6877
6878 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006879}
6880
6881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006882int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006883 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006884 enum wpas_p2p_prov_disc_use use,
6885 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886{
6887 u16 config_methods;
6888
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006889 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006890 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006891 wpa_s->p2p_fallback_to_go_neg = 0;
6892 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006893 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6894 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006895 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6896 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6897
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006898 wpa_printf(MSG_DEBUG,
6899 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6900 __func__, p2ps_prov->conncap,
6901 p2ps_prov->adv_id, p2ps_prov->conncap,
6902 p2ps_prov->status, p2ps_prov->info);
6903
6904 config_methods = 0;
6905 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006906 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006907 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006908 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006909 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6910 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006912 else {
6913 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006914 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006915 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917
Dmitry Shmidt04949592012-07-19 12:16:46 -07006918 if (use == WPAS_P2P_PD_AUTO) {
6919 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6920 wpa_s->pending_pd_config_methods = config_methods;
6921 wpa_s->p2p_auto_pd = 1;
6922 wpa_s->p2p_auto_join = 0;
6923 wpa_s->pending_pd_before_join = 0;
6924 wpa_s->auto_pd_scan_retry = 0;
6925 wpas_p2p_stop_find(wpa_s);
6926 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006927 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006928 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6929 wpa_s->p2p_auto_started.sec,
6930 wpa_s->p2p_auto_started.usec);
6931 wpas_p2p_join_scan(wpa_s, NULL);
6932 return 0;
6933 }
6934
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006935 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6936 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006938 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006939
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006940 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006941 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006942 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006943}
6944
6945
6946int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6947 char *end)
6948{
6949 return p2p_scan_result_text(ies, ies_len, buf, end);
6950}
6951
6952
6953static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6954{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006955 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006956 return;
6957
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006958 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006959 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006960 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6961 wpa_s, NULL);
6962 offchannel_send_action_done(wpa_s);
6963 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006965 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6966 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006967 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006968}
6969
6970
6971int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6972 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006973 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006974 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006975 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976{
6977 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07006978 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006979
Dmitry Shmidt04949592012-07-19 12:16:46 -07006980 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006981 wpa_s->p2p_in_provisioning) {
6982 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6983 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6984 " (P2P disabled)" : "",
6985 wpa_s->p2p_in_provisioning ?
6986 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006987 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006988 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006989
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006990 wpa_supplicant_cancel_sched_scan(wpa_s);
6991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006992 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006993 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006994 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995}
6996
6997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006998static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6999 struct wpa_scan_results *scan_res)
7000{
7001 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7002
7003 if (wpa_s->p2p_scan_work) {
7004 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7005 wpa_s->p2p_scan_work = NULL;
7006 radio_work_done(work);
7007 }
7008
7009 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7010 return;
7011
7012 /*
7013 * Indicate that results have been processed so that the P2P module can
7014 * continue pending tasks.
7015 */
7016 p2p_scan_res_handled(wpa_s->global->p2p);
7017}
7018
7019
7020static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021{
7022 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007023 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007024 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7025 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026
7027 if (wpa_s->global->p2p)
7028 p2p_stop_find(wpa_s->global->p2p);
7029
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007030 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7031 wpa_printf(MSG_DEBUG,
7032 "P2P: Do not consider the scan results after stop_find");
7033 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7034 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007035}
7036
7037
7038void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7039{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007040 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007041 if (!wpa_s->global->pending_group_iface_for_p2ps)
7042 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043}
7044
7045
7046static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7047{
7048 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007049 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050}
7051
7052
7053int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7054{
7055 int res;
7056
7057 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7058 return -1;
7059
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007060 if (wpa_s->p2p_lo_started) {
7061 wpa_printf(MSG_DEBUG,
7062 "P2P: Cannot start P2P listen, it is offloaded");
7063 return -1;
7064 }
7065
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007066 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067 wpas_p2p_clear_pending_action_tx(wpa_s);
7068
7069 if (timeout == 0) {
7070 /*
7071 * This is a request for unlimited Listen state. However, at
7072 * least for now, this is mapped to a Listen state for one
7073 * hour.
7074 */
7075 timeout = 3600;
7076 }
7077 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007078 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007080 /*
7081 * Stop previous find/listen operation to avoid trying to request a new
7082 * remain-on-channel operation while the driver is still running the
7083 * previous one.
7084 */
7085 if (wpa_s->global->p2p)
7086 p2p_stop_find(wpa_s->global->p2p);
7087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7089 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007090 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007091 eloop_register_timeout(timeout, 0,
7092 wpas_p2p_long_listen_timeout,
7093 wpa_s, NULL);
7094 }
7095
7096 return res;
7097}
7098
7099
7100int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7101 u8 *buf, size_t len, int p2p_group)
7102{
7103 struct wpabuf *p2p_ie;
7104 int ret;
7105
7106 if (wpa_s->global->p2p_disabled)
7107 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007108 /*
7109 * Advertize mandatory cross connection capability even on
7110 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7111 */
7112 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007113 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007114 if (wpa_s->global->p2p == NULL)
7115 return -1;
7116 if (bss == NULL)
7117 return -1;
7118
7119 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7120 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7121 p2p_group, p2p_ie);
7122 wpabuf_free(p2p_ie);
7123
7124 return ret;
7125}
7126
7127
7128int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007129 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007130 const u8 *ie, size_t ie_len,
7131 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007132{
7133 if (wpa_s->global->p2p_disabled)
7134 return 0;
7135 if (wpa_s->global->p2p == NULL)
7136 return 0;
7137
Dmitry Shmidt04949592012-07-19 12:16:46 -07007138 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007139 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007140 case P2P_PREQ_NOT_P2P:
7141 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7142 ssi_signal);
7143 /* fall through */
7144 case P2P_PREQ_MALFORMED:
7145 case P2P_PREQ_NOT_LISTEN:
7146 case P2P_PREQ_NOT_PROCESSED:
7147 default: /* make gcc happy */
7148 return 0;
7149 case P2P_PREQ_PROCESSED:
7150 return 1;
7151 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007152}
7153
7154
7155void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7156 const u8 *sa, const u8 *bssid,
7157 u8 category, const u8 *data, size_t len, int freq)
7158{
7159 if (wpa_s->global->p2p_disabled)
7160 return;
7161 if (wpa_s->global->p2p == NULL)
7162 return;
7163
7164 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7165 freq);
7166}
7167
7168
7169void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7170{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007171 unsigned int bands;
7172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173 if (wpa_s->global->p2p_disabled)
7174 return;
7175 if (wpa_s->global->p2p == NULL)
7176 return;
7177
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007178 bands = wpas_get_bands(wpa_s, NULL);
7179 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007180}
7181
7182
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007183static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007184{
7185 p2p_group_deinit(wpa_s->p2p_group);
7186 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007187
7188 wpa_s->ap_configured_cb = NULL;
7189 wpa_s->ap_configured_cb_ctx = NULL;
7190 wpa_s->ap_configured_cb_data = NULL;
7191 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007192}
7193
7194
7195int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7196{
Hai Shalomfdcde762020-04-02 11:19:20 -07007197 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7200 return -1;
7201
7202 return p2p_reject(wpa_s->global->p2p, addr);
7203}
7204
7205
7206/* Invite to reinvoke a persistent group */
7207int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007208 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007209 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007210 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007211{
7212 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007213 u8 *bssid = NULL;
7214 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007215 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007216 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007217 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007218
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007219 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007220 if (peer_addr)
7221 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7222 else
7223 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007224
Jouni Malinen31be0a42012-08-31 21:20:51 +03007225 wpa_s->p2p_persistent_go_freq = freq;
7226 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007227 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007228 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007229 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7230 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007231 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007232 if (ssid->mode == WPAS_MODE_P2P_GO) {
7233 role = P2P_INVITE_ROLE_GO;
7234 if (peer_addr == NULL) {
7235 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7236 "address in invitation command");
7237 return -1;
7238 }
7239 if (wpas_p2p_create_iface(wpa_s)) {
7240 if (wpas_p2p_add_group_interface(wpa_s,
7241 WPA_IF_P2P_GO) < 0) {
7242 wpa_printf(MSG_ERROR, "P2P: Failed to "
7243 "allocate a new interface for the "
7244 "group");
7245 return -1;
7246 }
7247 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007248 } else if (wpa_s->p2p_mgmt)
7249 bssid = wpa_s->parent->own_addr;
7250 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007251 bssid = wpa_s->own_addr;
7252 } else {
7253 role = P2P_INVITE_ROLE_CLIENT;
7254 peer_addr = ssid->bssid;
7255 }
7256 wpa_s->pending_invite_ssid_id = ssid->id;
7257
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007258 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007259 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007260 role == P2P_INVITE_ROLE_GO,
7261 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007262 if (res)
7263 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007265 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7266 return -1;
7267
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007268 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7269
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007270 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7271 no_pref_freq_given && pref_freq > 0 &&
7272 wpa_s->num_multichan_concurrent > 1 &&
7273 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7274 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7275 pref_freq);
7276 pref_freq = 0;
7277 }
7278
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007279 /*
7280 * Stop any find/listen operations before invitation and possibly
7281 * connection establishment.
7282 */
7283 wpas_p2p_stop_find_oper(wpa_s);
7284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007285 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007286 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007287 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007288}
7289
7290
7291/* Invite to join an active group */
7292int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7293 const u8 *peer_addr, const u8 *go_dev_addr)
7294{
7295 struct wpa_global *global = wpa_s->global;
7296 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007297 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007298 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007299 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007300 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007301 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007302 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007303
Jouni Malinen31be0a42012-08-31 21:20:51 +03007304 wpa_s->p2p_persistent_go_freq = 0;
7305 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007306 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007307 wpa_s->p2p_go_vht_center_freq2 = 0;
7308 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007309 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007311 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7312 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7313 break;
7314 }
7315 if (wpa_s == NULL) {
7316 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7317 return -1;
7318 }
7319
7320 ssid = wpa_s->current_ssid;
7321 if (ssid == NULL) {
7322 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7323 "invitation");
7324 return -1;
7325 }
7326
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007327 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007328 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007329 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007330 ssid->ssid, ssid->ssid_len);
7331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007332 if (ssid->mode == WPAS_MODE_P2P_GO) {
7333 role = P2P_INVITE_ROLE_ACTIVE_GO;
7334 bssid = wpa_s->own_addr;
7335 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007336 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007337 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007338 } else {
7339 role = P2P_INVITE_ROLE_CLIENT;
7340 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7341 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7342 "invite to current group");
7343 return -1;
7344 }
7345 bssid = wpa_s->bssid;
7346 if (go_dev_addr == NULL &&
7347 !is_zero_ether_addr(wpa_s->go_dev_addr))
7348 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007349 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7350 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007351 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007352 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007354 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7355 return -1;
7356
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007357 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007358 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007359 role == P2P_INVITE_ROLE_ACTIVE_GO,
7360 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007361 if (res)
7362 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007363 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007365 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007366 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007367 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007368}
7369
7370
7371void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7372{
7373 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007374 u8 go_dev_addr[ETH_ALEN];
7375 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007376 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007377 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007378 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007379
Dmitry Shmidt04949592012-07-19 12:16:46 -07007380 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7381 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007382 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007383 }
7384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007386 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007387
7388 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007389 if (!wpa_s->p2p_go_group_formation_completed &&
7390 wpa_s->global->p2p_group_formation == wpa_s) {
7391 wpa_dbg(wpa_s, MSG_DEBUG,
7392 "P2P: Marking group formation completed on client on data connection");
7393 wpa_s->p2p_go_group_formation_completed = 1;
7394 wpa_s->global->p2p_group_formation = NULL;
7395 wpa_s->p2p_in_provisioning = 0;
7396 wpa_s->p2p_in_invitation = 0;
7397 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007399 os_memset(go_dev_addr, 0, ETH_ALEN);
7400 if (ssid->bssid_set)
7401 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7402 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7403 ssid->ssid_len);
7404 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7405
7406 if (wpa_s->global->p2p_group_formation == wpa_s)
7407 wpa_s->global->p2p_group_formation = NULL;
7408
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007409 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7410 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007411
7412 ip_addr[0] = '\0';
7413 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007414 int res;
7415
7416 res = os_snprintf(ip_addr, sizeof(ip_addr),
7417 " ip_addr=%u.%u.%u.%u "
7418 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7419 ip[0], ip[1], ip[2], ip[3],
7420 ip[4], ip[5], ip[6], ip[7],
7421 ip[8], ip[9], ip[10], ip[11]);
7422 if (os_snprintf_error(sizeof(ip_addr), res))
7423 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007424 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007425 }
7426
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007427 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7428 ssid->passphrase == NULL && ssid->psk_set ?
7429 ssid->psk : NULL,
7430 ssid->passphrase, go_dev_addr, persistent,
7431 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007432
7433 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007434 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7435 ssid, go_dev_addr);
7436
Hai Shalom5f92bc92019-04-18 11:54:11 -07007437 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007438}
7439
7440
7441int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7442 u32 interval1, u32 duration2, u32 interval2)
7443{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007444 int ret;
7445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7447 return -1;
7448
7449 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7450 wpa_s->current_ssid == NULL ||
7451 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7452 return -1;
7453
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007454 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7455 wpa_s->own_addr, wpa_s->assoc_freq,
7456 duration1, interval1, duration2, interval2);
7457 if (ret == 0)
7458 wpa_s->waiting_presence_resp = 1;
7459
7460 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007461}
7462
7463
7464int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7465 unsigned int interval)
7466{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007467 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7468 return -1;
7469
7470 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7471}
7472
7473
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007474static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7475{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007476 if (wpa_s->current_ssid == NULL) {
7477 /*
7478 * current_ssid can be cleared when P2P client interface gets
7479 * disconnected, so assume this interface was used as P2P
7480 * client.
7481 */
7482 return 1;
7483 }
7484 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007485 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7486}
7487
7488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007489static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7490{
7491 struct wpa_supplicant *wpa_s = eloop_ctx;
7492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007493 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007494 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7495 "disabled");
7496 return;
7497 }
7498
Dmitry Shmidt04949592012-07-19 12:16:46 -07007499 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7500 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007501 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007502}
7503
7504
7505static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7506{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007507 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508
Dmitry Shmidt04949592012-07-19 12:16:46 -07007509 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7510 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007512 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7513 return;
7514
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007515 timeout = wpa_s->conf->p2p_group_idle;
7516 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7517 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7518 timeout = P2P_MAX_CLIENT_IDLE;
7519
7520 if (timeout == 0)
7521 return;
7522
Dmitry Shmidt04949592012-07-19 12:16:46 -07007523 if (timeout < 0) {
7524 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7525 timeout = 0; /* special client mode no-timeout */
7526 else
7527 return;
7528 }
7529
7530 if (wpa_s->p2p_in_provisioning) {
7531 /*
7532 * Use the normal group formation timeout during the
7533 * provisioning phase to avoid terminating this process too
7534 * early due to group idle timeout.
7535 */
7536 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7537 "during provisioning");
7538 return;
7539 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307540
Dmitry Shmidt04949592012-07-19 12:16:46 -07007541 if (wpa_s->show_group_started) {
7542 /*
7543 * Use the normal group formation timeout between the end of
7544 * the provisioning phase and completion of 4-way handshake to
7545 * avoid terminating this process too early due to group idle
7546 * timeout.
7547 */
7548 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7549 "while waiting for initial 4-way handshake to "
7550 "complete");
7551 return;
7552 }
7553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007555 timeout);
7556 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7557 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558}
7559
7560
Jouni Malinen2b89da82012-08-31 22:04:41 +03007561/* Returns 1 if the interface was removed */
7562int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7563 u16 reason_code, const u8 *ie, size_t ie_len,
7564 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007565{
7566 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007567 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007568
Dmitry Shmidt04949592012-07-19 12:16:46 -07007569 if (!locally_generated)
7570 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7571 ie_len);
7572
7573 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7574 wpa_s->current_ssid &&
7575 wpa_s->current_ssid->p2p_group &&
7576 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7577 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7578 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007579 if (wpas_p2p_group_delete(wpa_s,
7580 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7581 > 0)
7582 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007583 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007584
7585 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007586}
7587
7588
7589void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007590 u16 reason_code, const u8 *ie, size_t ie_len,
7591 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007592{
7593 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7594 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595
Dmitry Shmidt04949592012-07-19 12:16:46 -07007596 if (!locally_generated)
7597 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7598 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599}
7600
7601
7602void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7603{
7604 struct p2p_data *p2p = wpa_s->global->p2p;
7605
7606 if (p2p == NULL)
7607 return;
7608
7609 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7610 return;
7611
7612 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7613 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7614
7615 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7616 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7617
7618 if (wpa_s->wps &&
7619 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7620 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7621
7622 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7623 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7624
7625 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7626 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7627 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7628 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7629 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7630 }
7631
7632 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7633 p2p_set_sec_dev_types(p2p,
7634 (void *) wpa_s->conf->sec_device_type,
7635 wpa_s->conf->num_sec_device_types);
7636
7637 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7638 int i;
7639 p2p_remove_wps_vendor_extensions(p2p);
7640 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7641 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7642 continue;
7643 p2p_add_wps_vendor_extension(
7644 p2p, wpa_s->conf->wps_vendor_ext[i]);
7645 }
7646 }
7647
7648 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7649 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7650 char country[3];
7651 country[0] = wpa_s->conf->country[0];
7652 country[1] = wpa_s->conf->country[1];
7653 country[2] = 0x04;
7654 p2p_set_country(p2p, country);
7655 }
7656
7657 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7658 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7659 wpa_s->conf->p2p_ssid_postfix ?
7660 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7661 0);
7662 }
7663
7664 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7665 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007666
7667 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7668 u8 reg_class, channel;
7669 int ret;
7670 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007671 u8 channel_forced;
7672
Jouni Malinen75ecf522011-06-27 15:19:46 -07007673 if (wpa_s->conf->p2p_listen_reg_class &&
7674 wpa_s->conf->p2p_listen_channel) {
7675 reg_class = wpa_s->conf->p2p_listen_reg_class;
7676 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007677 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007678 } else {
7679 reg_class = 81;
7680 /*
7681 * Pick one of the social channels randomly as the
7682 * listen channel.
7683 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007684 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7685 channel = 1;
7686 else
7687 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007688 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007689 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007690 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7691 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007692 if (ret)
7693 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7694 "failed: %d", ret);
7695 }
7696 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7697 u8 op_reg_class, op_channel, cfg_op_channel;
7698 int ret = 0;
7699 unsigned int r;
7700 if (wpa_s->conf->p2p_oper_reg_class &&
7701 wpa_s->conf->p2p_oper_channel) {
7702 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7703 op_channel = wpa_s->conf->p2p_oper_channel;
7704 cfg_op_channel = 1;
7705 } else {
7706 op_reg_class = 81;
7707 /*
7708 * Use random operation channel from (1, 6, 11)
7709 *if no other preference is indicated.
7710 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007711 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7712 op_channel = 1;
7713 else
7714 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007715 cfg_op_channel = 0;
7716 }
7717 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7718 cfg_op_channel);
7719 if (ret)
7720 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7721 "failed: %d", ret);
7722 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007723
7724 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7725 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7726 wpa_s->conf->p2p_pref_chan) < 0) {
7727 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7728 "update failed");
7729 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007730
7731 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7732 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7733 "update failed");
7734 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007735 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007736
7737 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7738 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007739}
7740
7741
7742int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7743 int duration)
7744{
7745 if (!wpa_s->ap_iface)
7746 return -1;
7747 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7748 duration);
7749}
7750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007751
7752int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7753{
7754 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7755 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007756
7757 wpa_s->global->cross_connection = enabled;
7758 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7759
7760 if (!enabled) {
7761 struct wpa_supplicant *iface;
7762
7763 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7764 {
7765 if (iface->cross_connect_enabled == 0)
7766 continue;
7767
7768 iface->cross_connect_enabled = 0;
7769 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007770 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007771 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7772 iface->ifname,
7773 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007774 }
7775 }
7776
7777 return 0;
7778}
7779
7780
7781static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7782{
7783 struct wpa_supplicant *iface;
7784
7785 if (!uplink->global->cross_connection)
7786 return;
7787
7788 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7789 if (!iface->cross_connect_enabled)
7790 continue;
7791 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7792 0)
7793 continue;
7794 if (iface->ap_iface == NULL)
7795 continue;
7796 if (iface->cross_connect_in_use)
7797 continue;
7798
7799 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007800 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007801 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7802 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 }
7804}
7805
7806
7807static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7808{
7809 struct wpa_supplicant *iface;
7810
7811 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7812 if (!iface->cross_connect_enabled)
7813 continue;
7814 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7815 0)
7816 continue;
7817 if (!iface->cross_connect_in_use)
7818 continue;
7819
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007820 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007821 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7822 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007823 iface->cross_connect_in_use = 0;
7824 }
7825}
7826
7827
7828void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7829{
7830 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7831 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7832 wpa_s->cross_connect_disallowed)
7833 wpas_p2p_disable_cross_connect(wpa_s);
7834 else
7835 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007836 if (!wpa_s->ap_iface &&
7837 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7838 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007839}
7840
7841
7842void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7843{
7844 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007845 if (!wpa_s->ap_iface &&
7846 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7847 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848 wpas_p2p_set_group_idle_timeout(wpa_s);
7849}
7850
7851
7852static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7853{
7854 struct wpa_supplicant *iface;
7855
7856 if (!wpa_s->global->cross_connection)
7857 return;
7858
7859 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7860 if (iface == wpa_s)
7861 continue;
7862 if (iface->drv_flags &
7863 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7864 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007865 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7866 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007867 continue;
7868
7869 wpa_s->cross_connect_enabled = 1;
7870 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7871 sizeof(wpa_s->cross_connect_uplink));
7872 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7873 "%s to %s whenever uplink is available",
7874 wpa_s->ifname, wpa_s->cross_connect_uplink);
7875
7876 if (iface->ap_iface || iface->current_ssid == NULL ||
7877 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7878 iface->cross_connect_disallowed ||
7879 iface->wpa_state != WPA_COMPLETED)
7880 break;
7881
7882 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007883 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007884 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7885 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007886 break;
7887 }
7888}
7889
7890
7891int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7892{
7893 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7894 !wpa_s->p2p_in_provisioning)
7895 return 0; /* not P2P client operation */
7896
7897 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7898 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007899 if (wpa_s != wpa_s->p2pdev)
7900 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007901 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007902 return 1;
7903}
7904
7905
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007906void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7907{
7908 struct wpa_supplicant *wpa_s = eloop_ctx;
7909 wpas_p2p_notif_pbc_overlap(wpa_s);
7910}
7911
7912
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007913void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7914 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007915{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007916 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007917 struct wpa_used_freq_data *freqs = NULL;
7918 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007919
7920 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7921 return;
7922
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007923 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7924 if (!freqs)
7925 return;
7926
7927 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007929 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007930 os_memset(&cli_chan, 0, sizeof(cli_chan));
7931 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007932 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7933 "channel list");
7934 return;
7935 }
7936
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007937 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007938
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007939 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007940
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007941 /*
7942 * The used frequencies map changed, so it is possible that a GO is
7943 * using a channel that is no longer valid for P2P use. It is also
7944 * possible that due to policy consideration, it would be preferable to
7945 * move it to a frequency already used by other station interfaces.
7946 */
7947 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7948
7949 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007950}
7951
7952
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007953static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7954 struct wpa_scan_results *scan_res)
7955{
7956 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7957}
7958
7959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007960int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7961{
7962 struct wpa_global *global = wpa_s->global;
7963 int found = 0;
7964 const u8 *peer;
7965
7966 if (global->p2p == NULL)
7967 return -1;
7968
7969 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7970
7971 if (wpa_s->pending_interface_name[0] &&
7972 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7973 found = 1;
7974
7975 peer = p2p_get_go_neg_peer(global->p2p);
7976 if (peer) {
7977 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7978 MACSTR, MAC2STR(peer));
7979 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007980 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007981 }
7982
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007983 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7984 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7985 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7986 found = 1;
7987 }
7988
7989 if (wpa_s->pending_pd_before_join) {
7990 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7991 wpa_s->pending_pd_before_join = 0;
7992 found = 1;
7993 }
7994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007995 wpas_p2p_stop_find(wpa_s);
7996
7997 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7998 if (wpa_s == global->p2p_group_formation &&
7999 (wpa_s->p2p_in_provisioning ||
8000 wpa_s->parent->pending_interface_type ==
8001 WPA_IF_P2P_CLIENT)) {
8002 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8003 "formation found - cancelling",
8004 wpa_s->ifname);
8005 found = 1;
8006 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008007 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008008 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008009 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008010 break;
8011 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008012 wpas_p2p_group_delete(wpa_s,
8013 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008014 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008015 } else if (wpa_s->p2p_in_invitation) {
8016 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8017 wpa_s->ifname);
8018 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008019 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008020 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008021 }
8022 }
8023
8024 if (!found) {
8025 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8026 return -1;
8027 }
8028
8029 return 0;
8030}
8031
8032
8033void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8034{
8035 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8036 return;
8037
8038 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8039 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008040 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008041}
8042
8043
8044void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8045 int freq_24, int freq_5, int freq_overall)
8046{
8047 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008048 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008049 return;
8050 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8051}
8052
8053
8054int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8055{
8056 u8 peer[ETH_ALEN];
8057 struct p2p_data *p2p = wpa_s->global->p2p;
8058
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008059 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008060 return -1;
8061
8062 if (hwaddr_aton(addr, peer))
8063 return -1;
8064
8065 return p2p_unauthorize(p2p, peer);
8066}
8067
8068
8069/**
8070 * wpas_p2p_disconnect - Disconnect from a P2P Group
8071 * @wpa_s: Pointer to wpa_supplicant data
8072 * Returns: 0 on success, -1 on failure
8073 *
8074 * This can be used to disconnect from a group in which the local end is a P2P
8075 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8076 * virtual network interface was created for this group, that interface will be
8077 * removed. Otherwise, only the configured P2P group network will be removed
8078 * from the interface.
8079 */
8080int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8081{
8082
8083 if (wpa_s == NULL)
8084 return -1;
8085
Jouni Malinen2b89da82012-08-31 22:04:41 +03008086 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8087 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008088}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008089
8090
8091int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8092{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008093 int ret;
8094
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008095 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8096 return 0;
8097
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008098 ret = p2p_in_progress(wpa_s->global->p2p);
8099 if (ret == 0) {
8100 /*
8101 * Check whether there is an ongoing WPS provisioning step (or
8102 * other parts of group formation) on another interface since
8103 * p2p_in_progress() does not report this to avoid issues for
8104 * scans during such provisioning step.
8105 */
8106 if (wpa_s->global->p2p_group_formation &&
8107 wpa_s->global->p2p_group_formation != wpa_s) {
8108 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8109 "in group formation",
8110 wpa_s->global->p2p_group_formation->ifname);
8111 ret = 1;
8112 }
8113 }
8114
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008115 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008116 struct os_reltime now;
8117 os_get_reltime(&now);
8118 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8119 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008120 /* Wait for the first client has expired */
8121 wpa_s->global->p2p_go_wait_client.sec = 0;
8122 } else {
8123 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8124 ret = 1;
8125 }
8126 }
8127
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008128 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008129}
8130
8131
8132void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8133 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008134{
8135 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8136 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008137 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008138 /**
8139 * Remove the network by scheduling the group formation
8140 * timeout to happen immediately. The teardown code
8141 * needs to be scheduled to run asynch later so that we
8142 * don't delete data from under ourselves unexpectedly.
8143 * Calling wpas_p2p_group_formation_timeout directly
8144 * causes a series of crashes in WPS failure scenarios.
8145 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008146 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8147 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008148 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008149 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008150 }
8151}
8152
8153
8154struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008155 const u8 *addr, const u8 *ssid,
8156 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008157{
8158 struct wpa_ssid *s;
8159 size_t i;
8160
8161 for (s = wpa_s->conf->ssid; s; s = s->next) {
8162 if (s->disabled != 2)
8163 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008164 if (ssid &&
8165 (ssid_len != s->ssid_len ||
8166 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8167 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008168 if (addr == NULL) {
8169 if (s->mode == WPAS_MODE_P2P_GO)
8170 return s;
8171 continue;
8172 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008173 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8174 return s; /* peer is GO in the persistent group */
8175 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8176 continue;
8177 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008178 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008179 addr, ETH_ALEN) == 0)
8180 return s; /* peer is P2P client in persistent
8181 * group */
8182 }
8183 }
8184
8185 return NULL;
8186}
8187
8188
8189void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8190 const u8 *addr)
8191{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008192 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008193 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008194 /*
8195 * This can happen if WPS provisioning step is not terminated
8196 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8197 * peer was able to connect, there is no need to time out group
8198 * formation after this, though. In addition, this is used with
8199 * the initial connection wait on the GO as a separate formation
8200 * timeout and as such, expected to be hit after the initial WPS
8201 * provisioning step.
8202 */
8203 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008204
8205 if (!wpa_s->p2p_go_group_formation_completed &&
8206 !wpa_s->group_formation_reported) {
8207 /*
8208 * GO has not yet notified group formation success since
8209 * the WPS step was not completed cleanly. Do that
8210 * notification now since the P2P Client was able to
8211 * connect and as such, must have received the
8212 * credential from the WPS step.
8213 */
8214 if (wpa_s->global->p2p)
8215 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008216 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008217 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008218 }
8219 if (!wpa_s->p2p_go_group_formation_completed) {
8220 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8221 wpa_s->p2p_go_group_formation_completed = 1;
8222 wpa_s->global->p2p_group_formation = NULL;
8223 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008224 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008225 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008226 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008227 if (addr == NULL)
8228 return;
8229 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8230}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008231
Dmitry Shmidt04949592012-07-19 12:16:46 -07008232
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008233static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8234 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008235{
8236 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008237 int ret = 0;
8238
Dmitry Shmidt04949592012-07-19 12:16:46 -07008239 if (wpa_s->global->p2p_group_formation)
8240 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008241 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008242 offchannel_send_action_done(wpa_s);
8243 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008244 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008245 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8246 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8247 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8248 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008249 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008250 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008251 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008252 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008253 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008254 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008255 wpa_s->p2p_go_he,
8256 wpa_s->p2p_go_edmg,
8257 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008258 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008259}
8260
8261
8262int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8263{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008264 int res;
8265
Dmitry Shmidt04949592012-07-19 12:16:46 -07008266 if (!wpa_s->p2p_fallback_to_go_neg ||
8267 wpa_s->p2p_in_provisioning <= 5)
8268 return 0;
8269
8270 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8271 return 0; /* peer operating as a GO */
8272
8273 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8274 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008275 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008276 "reason=GO-not-found");
8277 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008278
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008279 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008280}
8281
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008282
8283unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8284{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008285 struct wpa_supplicant *ifs;
8286
8287 if (wpa_s->wpa_state > WPA_SCANNING) {
8288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8289 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008290 wpa_s->conf->p2p_search_delay);
8291 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008292 }
8293
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008294 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8295 radio_list) {
8296 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8298 "delay due to concurrent operation on "
8299 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008300 wpa_s->conf->p2p_search_delay,
8301 ifs->ifname);
8302 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008303 }
8304 }
8305
8306 return 0;
8307}
8308
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008309
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008310static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8311 struct wpa_ssid *s, const u8 *addr,
8312 int iface_addr)
8313{
8314 struct psk_list_entry *psk, *tmp;
8315 int changed = 0;
8316
8317 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8318 list) {
8319 if ((iface_addr && !psk->p2p &&
8320 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8321 (!iface_addr && psk->p2p &&
8322 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8323 wpa_dbg(wpa_s, MSG_DEBUG,
8324 "P2P: Remove persistent group PSK list entry for "
8325 MACSTR " p2p=%u",
8326 MAC2STR(psk->addr), psk->p2p);
8327 dl_list_del(&psk->list);
8328 os_free(psk);
8329 changed++;
8330 }
8331 }
8332
8333 return changed;
8334}
8335
8336
8337void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8338 const u8 *p2p_dev_addr,
8339 const u8 *psk, size_t psk_len)
8340{
8341 struct wpa_ssid *ssid = wpa_s->current_ssid;
8342 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008343 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008344
8345 if (psk_len != sizeof(p->psk))
8346 return;
8347
8348 if (p2p_dev_addr) {
8349 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8350 " p2p_dev_addr=" MACSTR,
8351 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8352 if (is_zero_ether_addr(p2p_dev_addr))
8353 p2p_dev_addr = NULL;
8354 } else {
8355 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8356 MAC2STR(mac_addr));
8357 }
8358
8359 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8360 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8361 /* To be added to persistent group once created */
8362 if (wpa_s->global->add_psk == NULL) {
8363 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8364 if (wpa_s->global->add_psk == NULL)
8365 return;
8366 }
8367 p = wpa_s->global->add_psk;
8368 if (p2p_dev_addr) {
8369 p->p2p = 1;
8370 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8371 } else {
8372 p->p2p = 0;
8373 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8374 }
8375 os_memcpy(p->psk, psk, psk_len);
8376 return;
8377 }
8378
8379 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8380 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8381 return;
8382 }
8383
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008384 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008385 ssid->ssid_len);
8386 if (!persistent) {
8387 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8388 return;
8389 }
8390
8391 p = os_zalloc(sizeof(*p));
8392 if (p == NULL)
8393 return;
8394 if (p2p_dev_addr) {
8395 p->p2p = 1;
8396 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8397 } else {
8398 p->p2p = 0;
8399 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8400 }
8401 os_memcpy(p->psk, psk, psk_len);
8402
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008403 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8404 (last = dl_list_last(&persistent->psk_list,
8405 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008406 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8407 MACSTR " (p2p=%u) to make room for a new one",
8408 MAC2STR(last->addr), last->p2p);
8409 dl_list_del(&last->list);
8410 os_free(last);
8411 }
8412
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008413 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008414 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8415 p2p_dev_addr == NULL);
8416 if (p2p_dev_addr) {
8417 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8418 MACSTR, MAC2STR(p2p_dev_addr));
8419 } else {
8420 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8421 MAC2STR(mac_addr));
8422 }
8423 dl_list_add(&persistent->psk_list, &p->list);
8424
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008425 if (wpa_s->p2pdev->conf->update_config &&
8426 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008427 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008428}
8429
8430
8431static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8432 struct wpa_ssid *s, const u8 *addr,
8433 int iface_addr)
8434{
8435 int res;
8436
8437 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008438 if (res > 0 && wpa_s->conf->update_config &&
8439 wpa_config_write(wpa_s->confname, wpa_s->conf))
8440 wpa_dbg(wpa_s, MSG_DEBUG,
8441 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008442}
8443
8444
8445static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8446 const u8 *peer, int iface_addr)
8447{
8448 struct hostapd_data *hapd;
8449 struct hostapd_wpa_psk *psk, *prev, *rem;
8450 struct sta_info *sta;
8451
8452 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8453 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8454 return;
8455
8456 /* Remove per-station PSK entry */
8457 hapd = wpa_s->ap_iface->bss[0];
8458 prev = NULL;
8459 psk = hapd->conf->ssid.wpa_psk;
8460 while (psk) {
8461 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8462 (!iface_addr &&
8463 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8464 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8465 MACSTR " iface_addr=%d",
8466 MAC2STR(peer), iface_addr);
8467 if (prev)
8468 prev->next = psk->next;
8469 else
8470 hapd->conf->ssid.wpa_psk = psk->next;
8471 rem = psk;
8472 psk = psk->next;
8473 os_free(rem);
8474 } else {
8475 prev = psk;
8476 psk = psk->next;
8477 }
8478 }
8479
8480 /* Disconnect from group */
8481 if (iface_addr)
8482 sta = ap_get_sta(hapd, peer);
8483 else
8484 sta = ap_get_sta_p2p(hapd, peer);
8485 if (sta) {
8486 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8487 " (iface_addr=%d) from group",
8488 MAC2STR(peer), iface_addr);
8489 hostapd_drv_sta_deauth(hapd, sta->addr,
8490 WLAN_REASON_DEAUTH_LEAVING);
8491 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8492 }
8493}
8494
8495
8496void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8497 int iface_addr)
8498{
8499 struct wpa_ssid *s;
8500 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008501 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008502
8503 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8504
8505 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008506 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008507 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8508 continue;
8509 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008510 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8511 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008512 }
8513
8514 /* Remove from any operating group */
8515 for (w = wpa_s->global->ifaces; w; w = w->next)
8516 wpas_p2p_remove_client_go(w, peer, iface_addr);
8517}
8518
8519
8520static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8521{
8522 struct wpa_supplicant *wpa_s = eloop_ctx;
8523 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8524}
8525
8526
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008527static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8528{
8529 struct wpa_supplicant *wpa_s = eloop_ctx;
8530
8531 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8532 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8533}
8534
8535
8536int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8537 struct wpa_ssid *ssid)
8538{
8539 struct wpa_supplicant *iface;
8540
8541 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8542 if (!iface->current_ssid ||
8543 iface->current_ssid->frequency == freq ||
8544 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8545 !iface->current_ssid->p2p_group))
8546 continue;
8547
8548 /* Remove the connection with least priority */
8549 if (!wpas_is_p2p_prioritized(iface)) {
8550 /* STA connection has priority over existing
8551 * P2P connection, so remove the interface. */
8552 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8553 eloop_register_timeout(0, 0,
8554 wpas_p2p_group_freq_conflict,
8555 iface, NULL);
8556 /* If connection in progress is P2P connection, do not
8557 * proceed for the connection. */
8558 if (wpa_s == iface)
8559 return -1;
8560 else
8561 return 0;
8562 } else {
8563 /* P2P connection has priority, disable the STA network
8564 */
8565 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8566 ssid);
8567 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8568 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8569 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8570 ETH_ALEN);
8571 /* If P2P connection is in progress, continue
8572 * connecting...*/
8573 if (wpa_s == iface)
8574 return 0;
8575 else
8576 return -1;
8577 }
8578 }
8579
8580 return 0;
8581}
8582
8583
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008584int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8585{
8586 struct wpa_ssid *ssid = wpa_s->current_ssid;
8587
8588 if (ssid == NULL || !ssid->p2p_group)
8589 return 0;
8590
8591 if (wpa_s->p2p_last_4way_hs_fail &&
8592 wpa_s->p2p_last_4way_hs_fail == ssid) {
8593 u8 go_dev_addr[ETH_ALEN];
8594 struct wpa_ssid *persistent;
8595
8596 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8597 ssid->ssid,
8598 ssid->ssid_len) <= 0) {
8599 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8600 goto disconnect;
8601 }
8602
8603 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8604 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008605 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008606 ssid->ssid,
8607 ssid->ssid_len);
8608 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8609 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8610 goto disconnect;
8611 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008612 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008613 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8614 persistent->id);
8615 disconnect:
8616 wpa_s->p2p_last_4way_hs_fail = NULL;
8617 /*
8618 * Remove the group from a timeout to avoid issues with caller
8619 * continuing to use the interface if this is on a P2P group
8620 * interface.
8621 */
8622 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8623 wpa_s, NULL);
8624 return 1;
8625 }
8626
8627 wpa_s->p2p_last_4way_hs_fail = ssid;
8628 return 0;
8629}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008630
8631
8632#ifdef CONFIG_WPS_NFC
8633
8634static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8635 struct wpabuf *p2p)
8636{
8637 struct wpabuf *ret;
8638 size_t wsc_len;
8639
8640 if (p2p == NULL) {
8641 wpabuf_free(wsc);
8642 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8643 return NULL;
8644 }
8645
8646 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8647 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8648 if (ret == NULL) {
8649 wpabuf_free(wsc);
8650 wpabuf_free(p2p);
8651 return NULL;
8652 }
8653
8654 wpabuf_put_be16(ret, wsc_len);
8655 if (wsc)
8656 wpabuf_put_buf(ret, wsc);
8657 wpabuf_put_be16(ret, wpabuf_len(p2p));
8658 wpabuf_put_buf(ret, p2p);
8659
8660 wpabuf_free(wsc);
8661 wpabuf_free(p2p);
8662 wpa_hexdump_buf(MSG_DEBUG,
8663 "P2P: Generated NFC connection handover message", ret);
8664
8665 if (ndef && ret) {
8666 struct wpabuf *tmp;
8667 tmp = ndef_build_p2p(ret);
8668 wpabuf_free(ret);
8669 if (tmp == NULL) {
8670 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8671 return NULL;
8672 }
8673 ret = tmp;
8674 }
8675
8676 return ret;
8677}
8678
8679
8680static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8681 struct wpa_ssid **ssid, u8 *go_dev_addr)
8682{
8683 struct wpa_supplicant *iface;
8684
8685 if (go_dev_addr)
8686 os_memset(go_dev_addr, 0, ETH_ALEN);
8687 if (ssid)
8688 *ssid = NULL;
8689 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8690 if (iface->wpa_state < WPA_ASSOCIATING ||
8691 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8692 !iface->current_ssid->p2p_group ||
8693 iface->current_ssid->mode != WPAS_MODE_INFRA)
8694 continue;
8695 if (ssid)
8696 *ssid = iface->current_ssid;
8697 if (go_dev_addr)
8698 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8699 return iface->assoc_freq;
8700 }
8701 return 0;
8702}
8703
8704
8705struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8706 int ndef)
8707{
8708 struct wpabuf *wsc, *p2p;
8709 struct wpa_ssid *ssid;
8710 u8 go_dev_addr[ETH_ALEN];
8711 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8712
8713 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8714 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8715 return NULL;
8716 }
8717
8718 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8719 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8720 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8721 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8722 return NULL;
8723 }
8724
8725 if (cli_freq == 0) {
8726 wsc = wps_build_nfc_handover_req_p2p(
8727 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8728 } else
8729 wsc = NULL;
8730 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8731 go_dev_addr, ssid ? ssid->ssid : NULL,
8732 ssid ? ssid->ssid_len : 0);
8733
8734 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8735}
8736
8737
8738struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8739 int ndef, int tag)
8740{
8741 struct wpabuf *wsc, *p2p;
8742 struct wpa_ssid *ssid;
8743 u8 go_dev_addr[ETH_ALEN];
8744 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8745
8746 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8747 return NULL;
8748
8749 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8750 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8751 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8752 return NULL;
8753
8754 if (cli_freq == 0) {
8755 wsc = wps_build_nfc_handover_sel_p2p(
8756 wpa_s->parent->wps,
8757 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8758 DEV_PW_NFC_CONNECTION_HANDOVER,
8759 wpa_s->conf->wps_nfc_dh_pubkey,
8760 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8761 } else
8762 wsc = NULL;
8763 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8764 go_dev_addr, ssid ? ssid->ssid : NULL,
8765 ssid ? ssid->ssid_len : 0);
8766
8767 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8768}
8769
8770
8771static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8772 struct p2p_nfc_params *params)
8773{
8774 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8775 "connection handover (freq=%d)",
8776 params->go_freq);
8777
8778 if (params->go_freq && params->go_ssid_len) {
8779 wpa_s->p2p_wps_method = WPS_NFC;
8780 wpa_s->pending_join_wps_method = WPS_NFC;
8781 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8782 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8783 ETH_ALEN);
8784 return wpas_p2p_join_start(wpa_s, params->go_freq,
8785 params->go_ssid,
8786 params->go_ssid_len);
8787 }
8788
8789 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8790 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008791 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008792 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008793 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008794 params->go_ssid_len ? params->go_ssid : NULL,
8795 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008796}
8797
8798
8799static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8800 struct p2p_nfc_params *params, int tag)
8801{
8802 int res, persistent;
8803 struct wpa_ssid *ssid;
8804
8805 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8806 "connection handover");
8807 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8808 ssid = wpa_s->current_ssid;
8809 if (ssid == NULL)
8810 continue;
8811 if (ssid->mode != WPAS_MODE_P2P_GO)
8812 continue;
8813 if (wpa_s->ap_iface == NULL)
8814 continue;
8815 break;
8816 }
8817 if (wpa_s == NULL) {
8818 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8819 return -1;
8820 }
8821
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008822 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008823 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008824 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008825 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8826 return -1;
8827 }
8828 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008829 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8830 wpa_s->p2pdev->p2p_oob_dev_pw,
8831 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8832 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008833 if (res)
8834 return res;
8835
8836 if (!tag) {
8837 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8838 return 0;
8839 }
8840
8841 if (!params->peer ||
8842 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8843 return 0;
8844
8845 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8846 " to join", MAC2STR(params->peer->p2p_device_addr));
8847
8848 wpa_s->global->p2p_invite_group = wpa_s;
8849 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008850 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008851 params->peer->p2p_device_addr,
8852 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008853 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008854
8855 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8856 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8857 ssid->ssid, ssid->ssid_len, ssid->frequency,
8858 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008859 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008860}
8861
8862
8863static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8864 struct p2p_nfc_params *params,
8865 int forced_freq)
8866{
8867 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8868 "connection handover");
8869 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8870 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008871 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008872 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008873 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8874 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008875}
8876
8877
8878static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8879 struct p2p_nfc_params *params,
8880 int forced_freq)
8881{
8882 int res;
8883
8884 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8885 "connection handover");
8886 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8887 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008888 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008889 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008890 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8891 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008892 if (res)
8893 return res;
8894
8895 res = wpas_p2p_listen(wpa_s, 60);
8896 if (res) {
8897 p2p_unauthorize(wpa_s->global->p2p,
8898 params->peer->p2p_device_addr);
8899 }
8900
8901 return res;
8902}
8903
8904
8905static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8906 const struct wpabuf *data,
8907 int sel, int tag, int forced_freq)
8908{
8909 const u8 *pos, *end;
8910 u16 len, id;
8911 struct p2p_nfc_params params;
8912 int res;
8913
8914 os_memset(&params, 0, sizeof(params));
8915 params.sel = sel;
8916
8917 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8918
8919 pos = wpabuf_head(data);
8920 end = pos + wpabuf_len(data);
8921
8922 if (end - pos < 2) {
8923 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8924 "attributes");
8925 return -1;
8926 }
8927 len = WPA_GET_BE16(pos);
8928 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008929 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008930 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8931 "attributes");
8932 return -1;
8933 }
8934 params.wsc_attr = pos;
8935 params.wsc_len = len;
8936 pos += len;
8937
8938 if (end - pos < 2) {
8939 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8940 "attributes");
8941 return -1;
8942 }
8943 len = WPA_GET_BE16(pos);
8944 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008945 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008946 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8947 "attributes");
8948 return -1;
8949 }
8950 params.p2p_attr = pos;
8951 params.p2p_len = len;
8952 pos += len;
8953
8954 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8955 params.wsc_attr, params.wsc_len);
8956 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8957 params.p2p_attr, params.p2p_len);
8958 if (pos < end) {
8959 wpa_hexdump(MSG_DEBUG,
8960 "P2P: Ignored extra data after P2P attributes",
8961 pos, end - pos);
8962 }
8963
8964 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8965 if (res)
8966 return res;
8967
8968 if (params.next_step == NO_ACTION)
8969 return 0;
8970
8971 if (params.next_step == BOTH_GO) {
8972 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8973 MAC2STR(params.peer->p2p_device_addr));
8974 return 0;
8975 }
8976
8977 if (params.next_step == PEER_CLIENT) {
8978 if (!is_zero_ether_addr(params.go_dev_addr)) {
8979 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8980 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8981 " ssid=\"%s\"",
8982 MAC2STR(params.peer->p2p_device_addr),
8983 params.go_freq,
8984 MAC2STR(params.go_dev_addr),
8985 wpa_ssid_txt(params.go_ssid,
8986 params.go_ssid_len));
8987 } else {
8988 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8989 "peer=" MACSTR " freq=%d",
8990 MAC2STR(params.peer->p2p_device_addr),
8991 params.go_freq);
8992 }
8993 return 0;
8994 }
8995
8996 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8997 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8998 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8999 return 0;
9000 }
9001
9002 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9003 wpa_s->p2p_oob_dev_pw = NULL;
9004
9005 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9006 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9007 "received");
9008 return -1;
9009 }
9010
9011 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9012 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9013 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9014 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9015 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9016 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9017 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9018
9019 if (tag) {
9020 if (id < 0x10) {
9021 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9022 "peer OOB Device Password Id %u", id);
9023 return -1;
9024 }
9025 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9026 "Device Password Id %u", id);
9027 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9028 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9029 params.oob_dev_pw_len -
9030 WPS_OOB_PUBKEY_HASH_LEN - 2);
9031 wpa_s->p2p_oob_dev_pw_id = id;
9032 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9033 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9034 params.oob_dev_pw_len -
9035 WPS_OOB_PUBKEY_HASH_LEN - 2);
9036 if (wpa_s->p2p_oob_dev_pw == NULL)
9037 return -1;
9038
9039 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9040 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9041 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9042 return -1;
9043 } else {
9044 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9045 "without Device Password");
9046 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9047 }
9048
9049 switch (params.next_step) {
9050 case NO_ACTION:
9051 case BOTH_GO:
9052 case PEER_CLIENT:
9053 /* already covered above */
9054 return 0;
9055 case JOIN_GROUP:
9056 return wpas_p2p_nfc_join_group(wpa_s, &params);
9057 case AUTH_JOIN:
9058 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9059 case INIT_GO_NEG:
9060 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9061 case RESP_GO_NEG:
9062 /* TODO: use own OOB Dev Pw */
9063 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9064 }
9065
9066 return -1;
9067}
9068
9069
9070int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9071 const struct wpabuf *data, int forced_freq)
9072{
9073 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9074 return -1;
9075
9076 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9077}
9078
9079
9080int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9081 const struct wpabuf *req,
9082 const struct wpabuf *sel, int forced_freq)
9083{
9084 struct wpabuf *tmp;
9085 int ret;
9086
9087 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9088 return -1;
9089
9090 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9091
9092 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9093 wpabuf_head(req), wpabuf_len(req));
9094 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9095 wpabuf_head(sel), wpabuf_len(sel));
9096 if (forced_freq)
9097 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9098 tmp = ndef_parse_p2p(init ? sel : req);
9099 if (tmp == NULL) {
9100 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9101 return -1;
9102 }
9103
9104 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9105 forced_freq);
9106 wpabuf_free(tmp);
9107
9108 return ret;
9109}
9110
9111
9112int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9113{
9114 const u8 *if_addr;
9115 int go_intent = wpa_s->conf->p2p_go_intent;
9116 struct wpa_supplicant *iface;
9117
9118 if (wpa_s->global->p2p == NULL)
9119 return -1;
9120
9121 if (!enabled) {
9122 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9123 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9124 {
9125 if (!iface->ap_iface)
9126 continue;
9127 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9128 }
9129 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9130 0, NULL);
9131 if (wpa_s->p2p_nfc_tag_enabled)
9132 wpas_p2p_remove_pending_group_interface(wpa_s);
9133 wpa_s->p2p_nfc_tag_enabled = 0;
9134 return 0;
9135 }
9136
9137 if (wpa_s->global->p2p_disabled)
9138 return -1;
9139
9140 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9141 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9142 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9143 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9144 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9145 "to allow static handover cases");
9146 return -1;
9147 }
9148
9149 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9150
9151 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9152 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9153 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9154 if (wpa_s->p2p_oob_dev_pw == NULL)
9155 return -1;
9156 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9157
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009158 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9159 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9160 /*
9161 * P2P Group Interface present and the command came on group
9162 * interface, so enable the token for the current interface.
9163 */
9164 wpa_s->create_p2p_iface = 0;
9165 } else {
9166 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9167 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009168
9169 if (wpa_s->create_p2p_iface) {
9170 enum wpa_driver_if_type iftype;
9171 /* Prepare to add a new interface for the group */
9172 iftype = WPA_IF_P2P_GROUP;
9173 if (go_intent == 15)
9174 iftype = WPA_IF_P2P_GO;
9175 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9176 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9177 "interface for the group");
9178 return -1;
9179 }
9180
9181 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009182 } else if (wpa_s->p2p_mgmt)
9183 if_addr = wpa_s->parent->own_addr;
9184 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009185 if_addr = wpa_s->own_addr;
9186
9187 wpa_s->p2p_nfc_tag_enabled = enabled;
9188
9189 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9190 struct hostapd_data *hapd;
9191 if (iface->ap_iface == NULL)
9192 continue;
9193 hapd = iface->ap_iface->bss[0];
9194 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9195 hapd->conf->wps_nfc_dh_pubkey =
9196 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9197 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9198 hapd->conf->wps_nfc_dh_privkey =
9199 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9200 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9201 hapd->conf->wps_nfc_dev_pw =
9202 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9203 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9204
9205 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9206 wpa_dbg(iface, MSG_DEBUG,
9207 "P2P: Failed to enable NFC Tag for GO");
9208 }
9209 }
9210 p2p_set_authorized_oob_dev_pw_id(
9211 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9212 if_addr);
9213
9214 return 0;
9215}
9216
9217#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009218
9219
9220static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9221 struct wpa_used_freq_data *freqs,
9222 unsigned int num)
9223{
9224 u8 curr_chan, cand, chan;
9225 unsigned int i;
9226
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009227 /*
9228 * If possible, optimize the Listen channel to be a channel that is
9229 * already used by one of the other interfaces.
9230 */
9231 if (!wpa_s->conf->p2p_optimize_listen_chan)
9232 return;
9233
9234 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9235 return;
9236
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009237 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9238 for (i = 0, cand = 0; i < num; i++) {
9239 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9240 if (curr_chan == chan) {
9241 cand = 0;
9242 break;
9243 }
9244
9245 if (chan == 1 || chan == 6 || chan == 11)
9246 cand = chan;
9247 }
9248
9249 if (cand) {
9250 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009251 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009252 cand);
9253 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9254 }
9255}
9256
9257
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009258static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009259{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009260 struct hostapd_config *conf;
9261 struct p2p_go_neg_results params;
9262 struct csa_settings csa_settings;
9263 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9264 int old_freq = current_ssid->frequency;
9265 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009266
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009267 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9268 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9269 return -1;
9270 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009271
9272 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009273 * TODO: This function may not always work correctly. For example,
9274 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009275 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009276 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9277 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009278 wpa_dbg(wpa_s, MSG_DEBUG,
9279 "P2P CSA: Failed to select new frequency for GO");
9280 return -1;
9281 }
9282
9283 if (current_ssid->frequency == params.freq) {
9284 wpa_dbg(wpa_s, MSG_DEBUG,
9285 "P2P CSA: Selected same frequency - not moving GO");
9286 return 0;
9287 }
9288
9289 conf = hostapd_config_defaults();
9290 if (!conf) {
9291 wpa_dbg(wpa_s, MSG_DEBUG,
9292 "P2P CSA: Failed to allocate default config");
9293 return -1;
9294 }
9295
9296 current_ssid->frequency = params.freq;
9297 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9298 wpa_dbg(wpa_s, MSG_DEBUG,
9299 "P2P CSA: Failed to create new GO config");
9300 ret = -1;
9301 goto out;
9302 }
9303
9304 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9305 wpa_dbg(wpa_s, MSG_DEBUG,
9306 "P2P CSA: CSA to a different band is not supported");
9307 ret = -1;
9308 goto out;
9309 }
9310
9311 os_memset(&csa_settings, 0, sizeof(csa_settings));
9312 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9313 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9314 csa_settings.freq_params.freq = params.freq;
9315 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9316 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9317 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9318
9319 if (conf->ieee80211ac) {
9320 int freq1 = 0, freq2 = 0;
9321 u8 chan, opclass;
9322
9323 if (ieee80211_freq_to_channel_ext(params.freq,
9324 conf->secondary_channel,
9325 conf->vht_oper_chwidth,
9326 &opclass, &chan) ==
9327 NUM_HOSTAPD_MODES) {
9328 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9329 ret = -1;
9330 goto out;
9331 }
9332
9333 if (conf->vht_oper_centr_freq_seg0_idx)
9334 freq1 = ieee80211_chan_to_freq(
9335 NULL, opclass,
9336 conf->vht_oper_centr_freq_seg0_idx);
9337
9338 if (conf->vht_oper_centr_freq_seg1_idx)
9339 freq2 = ieee80211_chan_to_freq(
9340 NULL, opclass,
9341 conf->vht_oper_centr_freq_seg1_idx);
9342
9343 if (freq1 < 0 || freq2 < 0) {
9344 wpa_dbg(wpa_s, MSG_DEBUG,
9345 "P2P CSA: Selected invalid VHT center freqs");
9346 ret = -1;
9347 goto out;
9348 }
9349
9350 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9351 csa_settings.freq_params.center_freq1 = freq1;
9352 csa_settings.freq_params.center_freq2 = freq2;
9353
9354 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009355 case CHANWIDTH_80MHZ:
9356 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009357 csa_settings.freq_params.bandwidth = 80;
9358 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009359 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009360 csa_settings.freq_params.bandwidth = 160;
9361 break;
9362 }
9363 }
9364
9365 ret = ap_switch_channel(wpa_s, &csa_settings);
9366out:
9367 current_ssid->frequency = old_freq;
9368 hostapd_config_free(conf);
9369 return ret;
9370}
9371
9372
9373static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9374{
9375 struct p2p_go_neg_results params;
9376 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9377
9378 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9379
9380 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9381 current_ssid->frequency);
9382
9383 /* Stop the AP functionality */
9384 /* TODO: Should do this in a way that does not indicated to possible
9385 * P2P Clients in the group that the group is terminated. */
9386 wpa_supplicant_ap_deinit(wpa_s);
9387
9388 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009389 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9390 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009391 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9392 wpas_p2p_group_delete(wpa_s,
9393 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9394 return;
9395 }
9396 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9397 params.freq);
9398
9399 if (params.freq &&
9400 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9401 wpa_printf(MSG_DEBUG,
9402 "P2P: Selected freq (%u MHz) is not valid for P2P",
9403 params.freq);
9404 wpas_p2p_group_delete(wpa_s,
9405 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9406 return;
9407 }
9408
9409 /* Update the frequency */
9410 current_ssid->frequency = params.freq;
9411 wpa_s->connect_without_scan = current_ssid;
9412 wpa_s->reassociate = 1;
9413 wpa_s->disconnected = 0;
9414 wpa_supplicant_req_scan(wpa_s, 0, 0);
9415}
9416
9417
9418static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9419{
9420 struct wpa_supplicant *wpa_s = eloop_ctx;
9421
9422 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009423 return;
9424
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009425 wpas_p2p_go_update_common_freqs(wpa_s);
9426
9427 /* Do not move GO in the middle of a CSA */
9428 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9429 wpa_printf(MSG_DEBUG,
9430 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009431 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009432 }
9433
9434 /*
9435 * First, try a channel switch flow. If it is not supported or fails,
9436 * take down the GO and bring it up again.
9437 */
9438 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9439 wpas_p2p_move_go_no_csa(wpa_s);
9440}
9441
9442
9443static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9444{
9445 struct wpa_supplicant *wpa_s = eloop_ctx;
9446 struct wpa_used_freq_data *freqs = NULL;
9447 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009448
9449 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9450 if (!freqs)
9451 return;
9452
9453 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9454
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009455 /* Previous attempt to move a GO was not possible -- try again. */
9456 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9457 WPAS_P2P_CHANNEL_UPDATE_ANY);
9458
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009459 os_free(freqs);
9460}
9461
9462
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009463/*
9464 * Consider moving a GO from its currently used frequency:
9465 * 1. It is possible that due to regulatory consideration the frequency
9466 * can no longer be used and there is a need to evacuate the GO.
9467 * 2. It is possible that due to MCC considerations, it would be preferable
9468 * to move the GO to a channel that is currently used by some other
9469 * station interface.
9470 *
9471 * In case a frequency that became invalid is once again valid, cancel a
9472 * previously initiated GO frequency change.
9473 */
9474static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9475 struct wpa_used_freq_data *freqs,
9476 unsigned int num)
9477{
9478 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9479 unsigned int timeout;
9480 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009481 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009482
9483 wpas_p2p_go_update_common_freqs(wpa_s);
9484
9485 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009486 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009487 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009488 for (i = 0, invalid_freq = 0; i < num; i++) {
9489 if (freqs[i].freq == freq) {
9490 flags = freqs[i].flags;
9491
9492 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009493 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9494 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009495 wpa_dbg(wpa_s, MSG_DEBUG,
9496 "P2P: Freq=%d MHz no longer valid for GO",
9497 freq);
9498 invalid_freq = 1;
9499 }
9500 } else if (freqs[i].flags == 0) {
9501 /* Freq is not used by any other station interface */
9502 continue;
9503 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009504 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009505 /* Freq is not valid for P2P use cases */
9506 continue;
9507 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9508 P2P_GO_FREQ_MOVE_SCM) {
9509 policy_move = 1;
9510 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9511 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9512 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9513 policy_move = 1;
9514 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9515 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9516 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9517 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9518 policy_move = 1;
9519 } else if ((wpa_s->drv_flags &
9520 WPA_DRIVER_FLAGS_AP_CSA) &&
9521 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9522 u8 chan;
9523
9524 /*
9525 * We do not support CSA between bands, so move
9526 * GO only within the same band.
9527 */
9528 if (wpa_s->ap_iface->current_mode->mode ==
9529 ieee80211_freq_to_chan(freqs[i].freq,
9530 &chan))
9531 policy_move = 1;
9532 }
9533 }
9534 }
9535
9536 wpa_dbg(wpa_s, MSG_DEBUG,
9537 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9538 invalid_freq, policy_move, flags);
9539
9540 /*
9541 * The channel is valid, or we are going to have a policy move, so
9542 * cancel timeout.
9543 */
9544 if (!invalid_freq || policy_move) {
9545 wpa_dbg(wpa_s, MSG_DEBUG,
9546 "P2P: Cancel a GO move from freq=%d MHz", freq);
9547 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9548
9549 if (wpas_p2p_in_progress(wpa_s)) {
9550 wpa_dbg(wpa_s, MSG_DEBUG,
9551 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9552 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9553 wpa_s, NULL);
9554 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9555 wpas_p2p_reconsider_moving_go,
9556 wpa_s, NULL);
9557 return;
9558 }
9559 }
9560
9561 if (!invalid_freq && (!policy_move || flags != 0)) {
9562 wpa_dbg(wpa_s, MSG_DEBUG,
9563 "P2P: Not initiating a GO frequency change");
9564 return;
9565 }
9566
9567 /*
9568 * Do not consider moving GO if it is in the middle of a CSA. When the
9569 * CSA is finished this flow should be retriggered.
9570 */
9571 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9572 wpa_dbg(wpa_s, MSG_DEBUG,
9573 "P2P: Not initiating a GO frequency change - CSA is in progress");
9574 return;
9575 }
9576
9577 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9578 timeout = P2P_GO_FREQ_CHANGE_TIME;
9579 else
9580 timeout = 0;
9581
9582 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9583 freq, timeout);
9584 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9585 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9586}
9587
9588
9589static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9590 struct wpa_used_freq_data *freqs,
9591 unsigned int num,
9592 enum wpas_p2p_channel_update_trig trig)
9593{
9594 struct wpa_supplicant *ifs;
9595
9596 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9597 NULL);
9598
9599 /*
9600 * Travers all the radio interfaces, and for each GO interface, check
9601 * if there is a need to move the GO from the frequency it is using,
9602 * or in case the frequency is valid again, cancel the evacuation flow.
9603 */
9604 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9605 radio_list) {
9606 if (ifs->current_ssid == NULL ||
9607 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9608 continue;
9609
9610 /*
9611 * The GO was just started or completed channel switch, no need
9612 * to move it.
9613 */
9614 if (wpa_s == ifs &&
9615 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9616 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9617 wpa_dbg(wpa_s, MSG_DEBUG,
9618 "P2P: GO move - schedule re-consideration");
9619 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9620 wpas_p2p_reconsider_moving_go,
9621 wpa_s, NULL);
9622 continue;
9623 }
9624
9625 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9626 }
9627}
9628
9629
9630void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9631{
9632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9633 return;
9634
9635 wpas_p2p_update_channel_list(wpa_s,
9636 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9637}
9638
9639
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009640void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9641{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009642 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9643 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9644 "the management interface is being removed");
9645 wpas_p2p_deinit_global(wpa_s->global);
9646 }
9647}
9648
9649
9650void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9651{
9652 if (wpa_s->ap_iface->bss)
9653 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9654 wpas_p2p_group_deinit(wpa_s);
9655}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009656
9657
9658int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9659 unsigned int period, unsigned int interval,
9660 unsigned int count)
9661{
9662 struct p2p_data *p2p = wpa_s->global->p2p;
9663 u8 *device_types;
9664 size_t dev_types_len;
9665 struct wpabuf *buf;
9666 int ret;
9667
9668 if (wpa_s->p2p_lo_started) {
9669 wpa_dbg(wpa_s, MSG_DEBUG,
9670 "P2P Listen offload is already started");
9671 return 0;
9672 }
9673
9674 if (wpa_s->global->p2p == NULL ||
9675 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9676 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9677 return -1;
9678 }
9679
9680 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9681 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9682 freq);
9683 return -1;
9684 }
9685
9686 /* Get device type */
9687 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9688 WPS_DEV_TYPE_LEN;
9689 device_types = os_malloc(dev_types_len);
9690 if (!device_types)
9691 return -1;
9692 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9693 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9694 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9695
9696 /* Get Probe Response IE(s) */
9697 buf = p2p_build_probe_resp_template(p2p, freq);
9698 if (!buf) {
9699 os_free(device_types);
9700 return -1;
9701 }
9702
9703 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9704 device_types, dev_types_len,
9705 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9706 if (ret < 0)
9707 wpa_dbg(wpa_s, MSG_DEBUG,
9708 "P2P: Failed to start P2P listen offload");
9709
9710 os_free(device_types);
9711 wpabuf_free(buf);
9712
9713 if (ret == 0) {
9714 wpa_s->p2p_lo_started = 1;
9715
9716 /* Stop current P2P listen if any */
9717 wpas_stop_listen(wpa_s);
9718 }
9719
9720 return ret;
9721}
9722
9723
9724int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9725{
9726 int ret;
9727
9728 if (!wpa_s->p2p_lo_started)
9729 return 0;
9730
9731 ret = wpa_drv_p2p_lo_stop(wpa_s);
9732 if (ret < 0)
9733 wpa_dbg(wpa_s, MSG_DEBUG,
9734 "P2P: Failed to stop P2P listen offload");
9735
9736 wpa_s->p2p_lo_started = 0;
9737 return ret;
9738}