blob: a3c4574ec98cf0e894e3defaa16f104ed389aabc [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,
1915 HOSTAPD_MODE_IEEE80211AD);
1916 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
2425 wpa_s->p2p_long_listen = 0;
2426 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,
2704 freq);
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(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003195 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, 0,
3196 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003197 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3198 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003200 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003201 wpa_msg_global(wpa_s, MSG_INFO,
3202 P2P_EVENT_INVITATION_ACCEPTED
3203 "sa=" MACSTR " go_dev_addr=" MACSTR
3204 " bssid=" MACSTR " unknown-network",
3205 MAC2STR(sa), MAC2STR(go_dev_addr),
3206 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003208 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003209 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 }
3211 return;
3212 }
3213
3214 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3215 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3216 " was rejected (status %u)", MAC2STR(sa), status);
3217 return;
3218 }
3219
3220 if (!s) {
3221 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003222 wpa_msg_global(wpa_s, MSG_INFO,
3223 P2P_EVENT_INVITATION_RECEIVED
3224 "sa=" MACSTR " go_dev_addr=" MACSTR
3225 " bssid=" MACSTR " unknown-network",
3226 MAC2STR(sa), MAC2STR(go_dev_addr),
3227 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003229 wpa_msg_global(wpa_s, MSG_INFO,
3230 P2P_EVENT_INVITATION_RECEIVED
3231 "sa=" MACSTR " go_dev_addr=" MACSTR
3232 " unknown-network",
3233 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003235 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3236 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 return;
3238 }
3239
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003240 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003241 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3242 "sa=" MACSTR " persistent=%d freq=%d",
3243 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003244 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3246 "sa=" MACSTR " persistent=%d",
3247 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003248 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003249 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3250 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251}
3252
3253
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003254static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3255 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003256 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003257{
3258 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003259 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003260
3261 if (ssid == NULL)
3262 return;
3263
3264 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003265 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003266 ETH_ALEN) == 0)
3267 break;
3268 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003269 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003270 if (ssid->mode != WPAS_MODE_P2P_GO &&
3271 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3272 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3273 "due to invitation result", ssid->id);
3274 wpas_notify_network_removed(wpa_s, ssid);
3275 wpa_config_remove_network(wpa_s->conf, ssid->id);
3276 return;
3277 }
3278 return; /* Peer not found in client list */
3279 }
3280
3281 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003282 "group %d client list%s",
3283 MAC2STR(peer), ssid->id,
3284 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003285 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3286 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3287 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003288 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003289 if (p2p_wpa_s->conf->update_config &&
3290 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003291 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003292}
3293
3294
3295static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3296 const u8 *peer)
3297{
3298 struct wpa_ssid *ssid;
3299
3300 wpa_s = wpa_s->global->p2p_invite_group;
3301 if (wpa_s == NULL)
3302 return; /* No known invitation group */
3303 ssid = wpa_s->current_ssid;
3304 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3305 !ssid->p2p_persistent_group)
3306 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003307 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003308 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003309 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003310}
3311
3312
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003313static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003314 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003315 const u8 *peer, int neg_freq,
3316 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317{
3318 struct wpa_supplicant *wpa_s = ctx;
3319 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003320 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321
3322 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003323 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3324 "status=%d " MACSTR,
3325 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003327 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3328 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329 }
3330 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3331
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003332 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3333 status, MAC2STR(peer));
3334 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003335 struct wpa_supplicant *group_if =
3336 wpa_s->global->p2p_invite_group;
3337
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003338 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3339 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003340
3341 /*
3342 * Invitation to an active group. If this is successful and we
3343 * are the GO, set the client wait to postpone some concurrent
3344 * operations and to allow provisioning and connection to happen
3345 * more quickly.
3346 */
3347 if (status == P2P_SC_SUCCESS &&
3348 group_if && group_if->current_ssid &&
3349 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3350 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3351#ifdef CONFIG_TESTING_OPTIONS
3352 if (group_if->p2p_go_csa_on_inv) {
3353 wpa_printf(MSG_DEBUG,
3354 "Testing: force P2P GO CSA after invitation");
3355 eloop_cancel_timeout(
3356 wpas_p2p_reconsider_moving_go,
3357 wpa_s, NULL);
3358 eloop_register_timeout(
3359 0, 50000,
3360 wpas_p2p_reconsider_moving_go,
3361 wpa_s, NULL);
3362 }
3363#endif /* CONFIG_TESTING_OPTIONS */
3364 }
3365 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003368 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3369 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3370 "invitation exchange to indicate readiness for "
3371 "re-invocation");
3372 }
3373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003375 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3376 ssid = wpa_config_get_network(
3377 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003378 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380 wpas_p2p_remove_pending_group_interface(wpa_s);
3381 return;
3382 }
3383
3384 ssid = wpa_config_get_network(wpa_s->conf,
3385 wpa_s->pending_invite_ssid_id);
3386 if (ssid == NULL) {
3387 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3388 "data matching with invitation");
3389 return;
3390 }
3391
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003392 /*
3393 * The peer could have missed our ctrl::ack frame for Invitation
3394 * Response and continue retransmitting the frame. To reduce the
3395 * likelihood of the peer not getting successful TX status for the
3396 * Invitation Response frame, wait a short time here before starting
3397 * the persistent group so that we will remain on the current channel to
3398 * acknowledge any possible retransmission from the peer.
3399 */
3400 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3401 "starting persistent group");
3402 os_sleep(0, 50000);
3403
Dmitry Shmidt15907092014-03-25 10:42:57 -07003404 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003405 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003406 freq = neg_freq;
3407 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003408 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003409 freq = peer_oper_freq;
3410 else
3411 freq = 0;
3412
3413 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3414 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003416 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003417 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003418 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003419 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003420 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003421 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003422 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003423 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003424 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003425 ssid->mode == WPAS_MODE_P2P_GO ?
3426 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003427 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428}
3429
3430
Dmitry Shmidt04949592012-07-19 12:16:46 -07003431static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3432 unsigned int freq)
3433{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003434 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3435 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003436 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003437}
3438
3439
3440static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3441{
3442 reg->channel[reg->channels] = chan;
3443 reg->channels++;
3444}
3445
3446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003448 struct p2p_channels *chan,
3449 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450{
3451 int i, cla = 0;
3452
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003453 wpa_s->global->p2p_24ghz_social_channels = 1;
3454
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003455 os_memset(cli_chan, 0, sizeof(*cli_chan));
3456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3458 "band");
3459
3460 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3461 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003462 chan->reg_class[cla].channels = 0;
3463 for (i = 0; i < 11; i++) {
3464 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3465 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3466 }
3467 if (chan->reg_class[cla].channels)
3468 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469
3470 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3471 "band");
3472
3473 /* Operating class 115 - 5 GHz, channels 36-48 */
3474 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003475 chan->reg_class[cla].channels = 0;
3476 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3477 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3478 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3479 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3480 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3481 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3482 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3483 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3484 if (chan->reg_class[cla].channels)
3485 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486
3487 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3488 "band");
3489
3490 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3491 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003492 chan->reg_class[cla].channels = 0;
3493 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3494 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3495 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3496 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3497 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3498 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3499 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3500 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3501 if (chan->reg_class[cla].channels)
3502 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003503
3504 chan->reg_classes = cla;
3505 return 0;
3506}
3507
3508
Dmitry Shmidt04949592012-07-19 12:16:46 -07003509static int has_channel(struct wpa_global *global,
3510 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003511{
3512 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003513 unsigned int freq;
3514
3515 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3516 chan * 5;
3517 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003518 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519
3520 for (i = 0; i < mode->num_channels; i++) {
3521 if (mode->channels[i].chan == chan) {
3522 if (flags)
3523 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003524 if (mode->channels[i].flag &
3525 (HOSTAPD_CHAN_DISABLED |
3526 HOSTAPD_CHAN_RADAR))
3527 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003528 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3529 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003530 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003531 }
3532 }
3533
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003534 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535}
3536
3537
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003538static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3539 struct hostapd_hw_modes *mode,
3540 u8 channel)
3541{
3542 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003543 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003544
3545 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3546 return 0;
3547
3548 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3549 /*
3550 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3551 * so the center channel is 6 channels away from the start/end.
3552 */
3553 if (channel >= center_channels[i] - 6 &&
3554 channel <= center_channels[i] + 6)
3555 return center_channels[i];
3556
3557 return 0;
3558}
3559
3560
3561static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3562 struct hostapd_hw_modes *mode,
3563 u8 channel, u8 bw)
3564{
3565 u8 center_chan;
3566 int i, flags;
3567 enum chan_allowed res, ret = ALLOWED;
3568
3569 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3570 if (!center_chan)
3571 return NOT_ALLOWED;
3572 if (center_chan >= 58 && center_chan <= 138)
3573 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3574
3575 /* check all the channels are available */
3576 for (i = 0; i < 4; i++) {
3577 int adj_chan = center_chan - 6 + i * 4;
3578
3579 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3580 if (res == NOT_ALLOWED)
3581 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003582 if (res == NO_IR)
3583 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003584
3585 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3586 return NOT_ALLOWED;
3587 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3588 return NOT_ALLOWED;
3589 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3590 return NOT_ALLOWED;
3591 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3592 return NOT_ALLOWED;
3593 }
3594
3595 return ret;
3596}
3597
3598
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003599static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3600 struct hostapd_hw_modes *mode,
3601 u8 channel)
3602{
3603 u8 center_channels[] = { 50, 114 };
3604 unsigned int i;
3605
3606 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3607 return 0;
3608
3609 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3610 /*
3611 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3612 * so the center channel is 14 channels away from the start/end.
3613 */
3614 if (channel >= center_channels[i] - 14 &&
3615 channel <= center_channels[i] + 14)
3616 return center_channels[i];
3617
3618 return 0;
3619}
3620
3621
3622static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3623 struct hostapd_hw_modes *mode,
3624 u8 channel, u8 bw)
3625{
3626 u8 center_chan;
3627 int i, flags;
3628 enum chan_allowed res, ret = ALLOWED;
3629
3630 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3631 if (!center_chan)
3632 return NOT_ALLOWED;
3633 /* VHT 160 MHz uses DFS channels in most countries. */
3634
3635 /* Check all the channels are available */
3636 for (i = 0; i < 8; i++) {
3637 int adj_chan = center_chan - 14 + i * 4;
3638
3639 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3640 if (res == NOT_ALLOWED)
3641 return NOT_ALLOWED;
3642
3643 if (res == NO_IR)
3644 ret = NO_IR;
3645
3646 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3647 return NOT_ALLOWED;
3648 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3649 return NOT_ALLOWED;
3650 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3651 return NOT_ALLOWED;
3652 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3653 return NOT_ALLOWED;
3654 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3655 return NOT_ALLOWED;
3656 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3657 return NOT_ALLOWED;
3658 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3659 return NOT_ALLOWED;
3660 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3661 return NOT_ALLOWED;
3662 }
3663
3664 return ret;
3665}
3666
3667
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003668static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3669 struct hostapd_hw_modes *mode,
3670 u8 channel)
3671{
3672 struct ieee80211_edmg_config edmg;
3673
3674 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3675 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3676 return ALLOWED;
3677
3678 return NOT_ALLOWED;
3679}
3680
3681
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003682static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3683 struct hostapd_hw_modes *mode,
3684 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003685{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003686 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003687 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003688
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003689 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3690 if (bw == BW40MINUS) {
3691 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3692 return NOT_ALLOWED;
3693 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3694 } else if (bw == BW40PLUS) {
3695 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3696 return NOT_ALLOWED;
3697 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3698 } else if (bw == BW80) {
3699 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003700 } else if (bw == BW160) {
3701 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003702 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3703 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003704 }
3705
3706 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3707 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003708 if (res == NO_IR || res2 == NO_IR)
3709 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003710 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003711}
3712
3713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003715 struct p2p_channels *chan,
3716 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003718 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003719 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003721 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3723 "of all supported channels; assume dualband "
3724 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003725 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 }
3727
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003728 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003730 for (op = 0; global_op_class[op].op_class; op++) {
3731 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003733 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003734
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003735 if (o->p2p == NO_P2P_SUPP)
3736 continue;
3737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003738 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 if (mode == NULL)
3740 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003741 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3742 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003744 enum chan_allowed res;
3745 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3746 if (res == ALLOWED) {
3747 if (reg == NULL) {
3748 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3749 o->op_class);
3750 reg = &chan->reg_class[cla];
3751 cla++;
3752 reg->reg_class = o->op_class;
3753 }
3754 reg->channel[reg->channels] = ch;
3755 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003756 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003757 wpa_s->conf->p2p_add_cli_chan) {
3758 if (cli_reg == NULL) {
3759 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3760 o->op_class);
3761 cli_reg = &cli_chan->reg_class[cli_cla];
3762 cli_cla++;
3763 cli_reg->reg_class = o->op_class;
3764 }
3765 cli_reg->channel[cli_reg->channels] = ch;
3766 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 }
3769 if (reg) {
3770 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3771 reg->channel, reg->channels);
3772 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003773 if (cli_reg) {
3774 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3775 cli_reg->channel, cli_reg->channels);
3776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777 }
3778
3779 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003780 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 return 0;
3783}
3784
3785
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003786int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3787 struct hostapd_hw_modes *mode, u8 channel)
3788{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003789 int op;
3790 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003791
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003792 for (op = 0; global_op_class[op].op_class; op++) {
3793 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003794 u8 ch;
3795
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003796 if (o->p2p == NO_P2P_SUPP)
3797 continue;
3798
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003799 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3800 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003801 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3802 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003803 continue;
3804 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003805 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003806 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003807 }
3808 }
3809 return 0;
3810}
3811
3812
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003813int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3814 struct hostapd_hw_modes *mode, u8 channel)
3815{
3816 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3817 return 0;
3818
3819 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3820}
3821
3822
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003823int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3824 struct hostapd_hw_modes *mode, u8 channel)
3825{
3826 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3827 return 0;
3828 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3829}
3830
3831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3833 size_t buf_len)
3834{
3835 struct wpa_supplicant *wpa_s = ctx;
3836
3837 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3838 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3839 break;
3840 }
3841 if (wpa_s == NULL)
3842 return -1;
3843
3844 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3845}
3846
3847
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003848struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3849 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003850{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003851 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3852 struct wpa_ssid *s = wpa_s->current_ssid;
3853 if (s == NULL)
3854 continue;
3855 if (s->mode != WPAS_MODE_P2P_GO &&
3856 s->mode != WPAS_MODE_AP &&
3857 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3858 continue;
3859 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003860 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003861 continue;
3862 return wpa_s;
3863 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003864
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003865 return NULL;
3866
3867}
3868
3869
3870struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3871 const u8 *peer_dev_addr)
3872{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003873 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3874 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003875 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003876 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003877 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3878 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003879 }
3880
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003881 return NULL;
3882}
3883
3884
3885static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3886{
3887 struct wpa_supplicant *wpa_s = ctx;
3888
3889 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003890}
3891
3892
Dmitry Shmidt18463232014-01-24 12:29:41 -08003893static int wpas_is_concurrent_session_active(void *ctx)
3894{
3895 struct wpa_supplicant *wpa_s = ctx;
3896 struct wpa_supplicant *ifs;
3897
3898 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3899 if (ifs == wpa_s)
3900 continue;
3901 if (ifs->wpa_state > WPA_ASSOCIATED)
3902 return 1;
3903 }
3904 return 0;
3905}
3906
3907
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003908static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3909{
3910 struct wpa_supplicant *wpa_s = ctx;
3911 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3912}
3913
3914
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003915int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3916 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003917{
3918 struct wpa_interface iface;
3919 struct wpa_supplicant *p2pdev_wpa_s;
3920 char ifname[100];
3921 char force_name[100];
3922 int ret;
3923
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003924 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3925 wpa_s->ifname);
3926 if (os_snprintf_error(sizeof(ifname), ret))
3927 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003928 force_name[0] = '\0';
3929 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3930 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3931 force_name, wpa_s->pending_interface_addr, NULL);
3932 if (ret < 0) {
3933 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3934 return ret;
3935 }
3936 os_strlcpy(wpa_s->pending_interface_name, ifname,
3937 sizeof(wpa_s->pending_interface_name));
3938
3939 os_memset(&iface, 0, sizeof(iface));
3940 iface.p2p_mgmt = 1;
3941 iface.ifname = wpa_s->pending_interface_name;
3942 iface.driver = wpa_s->driver->name;
3943 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003944
3945 /*
3946 * If a P2P Device configuration file was given, use it as the interface
3947 * configuration file (instead of using parent's configuration file.
3948 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003949 if (conf_p2p_dev) {
3950 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003951 iface.ctrl_interface = NULL;
3952 } else {
3953 iface.confname = wpa_s->confname;
3954 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3955 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003956
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003957 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003958 if (!p2pdev_wpa_s) {
3959 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3960 return -1;
3961 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003962
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003963 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003964 wpa_s->pending_interface_name[0] = '\0';
3965 return 0;
3966}
3967
3968
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003969static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3970 const u8 *noa, size_t noa_len)
3971{
3972 struct wpa_supplicant *wpa_s, *intf = ctx;
3973 char hex[100];
3974
3975 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3976 if (wpa_s->waiting_presence_resp)
3977 break;
3978 }
3979 if (!wpa_s) {
3980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3981 return;
3982 }
3983 wpa_s->waiting_presence_resp = 0;
3984
3985 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3986 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3987 " status=%u noa=%s", MAC2STR(src), status, hex);
3988}
3989
3990
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003991static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3992 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003993 u8 *ret_ssid, size_t *ret_ssid_len,
3994 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003995{
3996 struct wpa_supplicant *wpa_s = ctx;
3997 struct wpa_ssid *s;
3998
3999 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4000 if (s) {
4001 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4002 *ret_ssid_len = s->ssid_len;
4003 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004004
4005 if (s->mode != WPAS_MODE_P2P_GO) {
4006 os_memset(intended_iface_addr, 0, ETH_ALEN);
4007 } else if (wpas_p2p_create_iface(wpa_s)) {
4008 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4009 return 0;
4010
4011 os_memcpy(intended_iface_addr,
4012 wpa_s->pending_interface_addr, ETH_ALEN);
4013 } else {
4014 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4015 ETH_ALEN);
4016 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004017 return 1;
4018 }
4019
4020 return 0;
4021}
4022
4023
4024static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004025 u8 *ssid, size_t *ssid_len, int *group_iface,
4026 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004027{
4028 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004029 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004030 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004031
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004032 /*
4033 * group_iface will be set to 1 only if a dedicated interface for P2P
4034 * role is required. First, we try to reuse an active GO. However,
4035 * if it is not present, we will try to reactivate an existing
4036 * persistent group and set group_iface to 1, so the caller will know
4037 * that the pending interface should be used.
4038 */
4039 *group_iface = 0;
4040
4041 if (freq)
4042 *freq = 0;
4043
4044 go = wpas_p2p_get_go_group(wpa_s);
4045 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004046 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004047 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004048 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004049 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4050 else
4051 return 0;
4052 } else {
4053 s = go->current_ssid;
4054 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4055 if (freq)
4056 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004057 }
4058
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004059 os_memcpy(ssid, s->ssid, s->ssid_len);
4060 *ssid_len = s->ssid_len;
4061
4062 return 1;
4063}
4064
4065
4066static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4067 const u8 *ssid, size_t ssid_len)
4068{
4069 struct wpa_supplicant *wpa_s = ctx;
4070 struct wpa_ssid *s;
4071 int save_config = 0;
4072 size_t i;
4073
4074 /* Start with our first choice of Persistent Groups */
4075 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4076 if (go && ssid && ssid_len &&
4077 s->ssid_len == ssid_len &&
4078 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4079 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4080 break;
4081
4082 /* Remove stale persistent group */
4083 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004084 wpa_dbg(wpa_s, MSG_DEBUG,
4085 "P2P: Remove stale persistent group id=%d",
4086 s->id);
4087 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004088 wpa_config_remove_network(wpa_s->conf, s->id);
4089 save_config = 1;
4090 continue;
4091 }
4092
4093 for (i = 0; i < s->num_p2p_clients; i++) {
4094 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4095 peer, ETH_ALEN) != 0)
4096 continue;
4097
4098 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4099 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4100 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4101 break;
4102 }
4103 s->num_p2p_clients--;
4104 save_config = 1;
4105 }
4106
4107 if (save_config)
4108 p2p_config_write(wpa_s);
4109
4110 /* Return TRUE if valid SSID remains */
4111 return s != NULL;
4112}
4113
4114
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004115static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4116 const u8 *feat_cap, size_t feat_cap_len)
4117{
4118 static const char pref[] = " feature_cap=";
4119 int ret;
4120
4121 buf[0] = '\0';
4122
4123 /*
4124 * We expect a feature capability to contain at least one byte to be
4125 * reported. The string buffer provided by the caller function is
4126 * expected to be big enough to contain all bytes of the attribute for
4127 * known specifications. This function truncates the reported bytes if
4128 * the feature capability data exceeds the string buffer size.
4129 */
4130 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4131 return;
4132
4133 os_memcpy(buf, pref, sizeof(pref));
4134 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4135 buf_len - sizeof(pref) + 1,
4136 feat_cap, feat_cap_len);
4137
4138 if (ret != (2 * (int) feat_cap_len))
4139 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4140}
4141
4142
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004143static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4144 const u8 *adv_mac, const u8 *ses_mac,
4145 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4146 u8 conncap, int passwd_id,
4147 const u8 *persist_ssid,
4148 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004149 int prov_start, const char *session_info,
4150 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004151 unsigned int freq,
4152 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004153{
4154 struct wpa_supplicant *wpa_s = ctx;
4155 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004156 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004157 int save_config = 0;
4158 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004159 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004160
4161 if (!dev)
4162 return;
4163
4164 os_memset(mac, 0, ETH_ALEN);
4165 if (!adv_mac)
4166 adv_mac = mac;
4167 if (!ses_mac)
4168 ses_mac = mac;
4169 if (!grp_mac)
4170 grp_mac = mac;
4171
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004172 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4173 feat_cap, feat_cap_len);
4174
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004175 if (prov_start) {
4176 if (session_info == NULL) {
4177 wpa_msg_global(wpa_s, MSG_INFO,
4178 P2P_EVENT_P2PS_PROVISION_START MACSTR
4179 " adv_id=%x conncap=%x"
4180 " adv_mac=" MACSTR
4181 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004182 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004183 MAC2STR(dev), adv_id, conncap,
4184 MAC2STR(adv_mac),
4185 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004186 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004187 } else {
4188 wpa_msg_global(wpa_s, MSG_INFO,
4189 P2P_EVENT_P2PS_PROVISION_START MACSTR
4190 " adv_id=%x conncap=%x"
4191 " adv_mac=" MACSTR
4192 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004193 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004194 MAC2STR(dev), adv_id, conncap,
4195 MAC2STR(adv_mac),
4196 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004197 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004198 }
4199 return;
4200 }
4201
4202 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4203 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4204
4205 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4206 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4207 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4208
4209 if (persistent_go && !persistent_go->num_p2p_clients) {
4210 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004211 wpa_dbg(wpa_s, MSG_DEBUG,
4212 "P2P: Remove empty persistent group id=%d",
4213 persistent_go->id);
4214 wpas_notify_persistent_group_removed(wpa_s,
4215 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004216 wpa_config_remove_network(wpa_s->conf,
4217 persistent_go->id);
4218 }
4219
4220 wpa_msg_global(wpa_s, MSG_INFO,
4221 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4222 " status=%d"
4223 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004224 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004225 MAC2STR(dev), status,
4226 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004227 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004228 return;
4229 }
4230
4231 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004232 if (persist_ssid && persist_ssid_size)
4233 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4234 persist_ssid_size);
4235
4236 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4237 is_zero_ether_addr(grp_mac)) {
4238 wpa_dbg(wpa_s, MSG_ERROR,
4239 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4240 return;
4241 }
4242
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004243 for (;;) {
4244 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4245 if (!stale)
4246 break;
4247
4248 if (s && s->ssid_len == stale->ssid_len &&
4249 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4250 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4251 break;
4252
4253 /* Remove stale persistent group */
4254 if (stale->mode != WPAS_MODE_P2P_GO ||
4255 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004256 wpa_dbg(wpa_s, MSG_DEBUG,
4257 "P2P: Remove stale persistent group id=%d",
4258 stale->id);
4259 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004260 wpa_config_remove_network(wpa_s->conf, stale->id);
4261 } else {
4262 size_t i;
4263
4264 for (i = 0; i < stale->num_p2p_clients; i++) {
4265 if (os_memcmp(stale->p2p_client_list +
4266 i * ETH_ALEN,
4267 dev, ETH_ALEN) == 0) {
4268 os_memmove(stale->p2p_client_list +
4269 i * ETH_ALEN,
4270 stale->p2p_client_list +
4271 (i + 1) * ETH_ALEN,
4272 (stale->num_p2p_clients -
4273 i - 1) * ETH_ALEN);
4274 break;
4275 }
4276 }
4277 stale->num_p2p_clients--;
4278 }
4279 save_config = 1;
4280 }
4281
4282 if (save_config)
4283 p2p_config_write(wpa_s);
4284
4285 if (s) {
4286 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4287 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4288
4289 if (persistent_go && s != persistent_go &&
4290 !persistent_go->num_p2p_clients) {
4291 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004292 wpa_dbg(wpa_s, MSG_DEBUG,
4293 "P2P: Remove empty persistent group id=%d",
4294 persistent_go->id);
4295 wpas_notify_persistent_group_removed(wpa_s,
4296 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004297 wpa_config_remove_network(wpa_s->conf,
4298 persistent_go->id);
4299 /* Save config */
4300 }
4301
4302 wpa_msg_global(wpa_s, MSG_INFO,
4303 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4304 " status=%d"
4305 " adv_id=%x adv_mac=" MACSTR
4306 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004307 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004308 MAC2STR(dev), status,
4309 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004310 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004311 return;
4312 }
4313
Hai Shalom5f92bc92019-04-18 11:54:11 -07004314 wpa_s->global->pending_p2ps_group = 0;
4315 wpa_s->global->pending_p2ps_group_freq = 0;
4316
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004317 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004318 /*
4319 * We need to copy the interface name. Simply saving a
4320 * pointer isn't enough, since if we use pending_interface_name
4321 * it will be overwritten when the group is added.
4322 */
4323 char go_ifname[100];
4324
4325 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004326 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004327 if (!response_done) {
4328 wpa_s->global->pending_p2ps_group = 1;
4329 wpa_s->global->pending_p2ps_group_freq = freq;
4330 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004331
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004332 if (!wpas_p2p_create_iface(wpa_s))
4333 os_memcpy(go_ifname, wpa_s->ifname,
4334 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004335 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004336 os_memcpy(go_ifname,
4337 wpa_s->pending_interface_name,
4338 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004339
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004340 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004341 wpas_p2ps_prov_complete(
4342 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4343 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004344 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004345 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4346 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004347 return;
4348 }
4349
4350 /* If PD Resp complete, start up the GO */
4351 if (response_done && persistent_go) {
4352 wpas_p2p_group_add_persistent(
4353 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004354 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004355 persistent_go->mode ==
4356 WPAS_MODE_P2P_GO ?
4357 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004358 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004359 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004360 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004361 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004362 }
4363
4364 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004365 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4366 ETH_ALEN);
4367 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004368 }
4369 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004370 os_memcpy(go_ifname, go_wpa_s->ifname,
4371 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004372
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004373 if (is_zero_ether_addr(grp_mac)) {
4374 wpa_dbg(go_wpa_s, MSG_DEBUG,
4375 "P2P: Setting PIN-1 for ANY");
4376 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4377 "12345670", NULL, 0,
4378 0);
4379 } else {
4380 wpa_dbg(go_wpa_s, MSG_DEBUG,
4381 "P2P: Setting PIN-1 for " MACSTR,
4382 MAC2STR(grp_mac));
4383 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4384 "12345670", NULL, 0,
4385 0);
4386 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004387
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004388 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4389 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004390 }
4391
4392 wpa_msg_global(wpa_s, MSG_INFO,
4393 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4394 " status=%d conncap=%x"
4395 " adv_id=%x adv_mac=" MACSTR
4396 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004397 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004398 MAC2STR(dev), status, conncap,
4399 adv_id, MAC2STR(adv_mac),
4400 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004401 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004402 return;
4403 }
4404
4405 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4406 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4407
4408 if (persistent_go && !persistent_go->num_p2p_clients) {
4409 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004410 wpa_dbg(wpa_s, MSG_DEBUG,
4411 "P2P: Remove empty persistent group id=%d",
4412 persistent_go->id);
4413 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004414 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4415 }
4416
4417 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004418 char ssid_hex[32 * 2 + 1];
4419
4420 if (group_ssid)
4421 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4422 group_ssid, group_ssid_len);
4423 else
4424 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004425 wpa_msg_global(wpa_s, MSG_INFO,
4426 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4427 " status=%d conncap=%x"
4428 " adv_id=%x adv_mac=" MACSTR
4429 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004430 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004431 MAC2STR(dev), status, conncap,
4432 adv_id, MAC2STR(adv_mac),
4433 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004434 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4435 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004436 } else {
4437 wpa_msg_global(wpa_s, MSG_INFO,
4438 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4439 " status=%d conncap=%x"
4440 " adv_id=%x adv_mac=" MACSTR
4441 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004442 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004443 MAC2STR(dev), status, conncap,
4444 adv_id, MAC2STR(adv_mac),
4445 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004446 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004447 }
4448}
4449
4450
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004451static int _wpas_p2p_in_progress(void *ctx)
4452{
4453 struct wpa_supplicant *wpa_s = ctx;
4454 return wpas_p2p_in_progress(wpa_s);
4455}
4456
4457
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004458static int wpas_prov_disc_resp_cb(void *ctx)
4459{
4460 struct wpa_supplicant *wpa_s = ctx;
4461 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004462 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004463
4464 if (!wpa_s->global->pending_p2ps_group)
4465 return 0;
4466
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004467 freq = wpa_s->global->pending_p2ps_group_freq;
4468 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004469 wpa_s->global->pending_p2ps_group = 0;
4470
4471 if (wpas_p2p_get_go_group(wpa_s))
4472 return 0;
4473 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4474
4475 if (persistent_go) {
4476 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004477 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4478 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004479 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004480 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004481 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004482 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004483 }
4484
4485 return 1;
4486}
4487
4488
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004489static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4490 unsigned int *len,
4491 unsigned int *freq_list)
4492{
4493 struct wpa_supplicant *wpa_s = ctx;
4494
4495 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4496 WPA_IF_P2P_CLIENT, len, freq_list);
4497}
4498
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004499int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4500{
4501 u8 addr[ETH_ALEN] = {0};
4502
4503 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4504 return 0;
4505
4506 if (wpa_s->conf->ssid == NULL) {
4507 if (random_mac_addr(addr) < 0) {
4508 wpa_msg(wpa_s, MSG_INFO,
4509 "Failed to generate random MAC address");
4510 return -EINVAL;
4511 }
4512
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004513 /* Store generated MAC address. */
4514 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4515 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004516 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004517 /* If there are existing saved groups, restore last MAC address.
4518 * if there is no last used MAC address, the last one is
4519 * factory MAC. */
4520 if (is_zero_ether_addr(
4521 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004522 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004523 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4524 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004525 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4526 }
4527
4528 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4529 wpa_msg(wpa_s, MSG_INFO,
4530 "Failed to set random MAC address");
4531 return -EINVAL;
4532 }
4533
4534 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4535 wpa_msg(wpa_s, MSG_INFO,
4536 "Could not update MAC address information");
4537 return -EINVAL;
4538 }
4539
4540 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4541 MAC2STR(addr));
4542
4543 return 0;
4544}
4545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546/**
4547 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4548 * @global: Pointer to global data from wpa_supplicant_init()
4549 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4550 * Returns: 0 on success, -1 on failure
4551 */
4552int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4553{
4554 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555 int i;
4556
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004557 if (wpa_s->conf->p2p_disabled)
4558 return 0;
4559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4561 return 0;
4562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 if (global->p2p)
4564 return 0;
4565
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004566 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4567 wpa_msg(wpa_s, MSG_ERROR,
4568 "Failed to initialize P2P random MAC address.");
4569 return -1;
4570 }
4571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004574 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004575 p2p.p2p_scan = wpas_p2p_scan;
4576 p2p.send_action = wpas_send_action;
4577 p2p.send_action_done = wpas_send_action_done;
4578 p2p.go_neg_completed = wpas_go_neg_completed;
4579 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4580 p2p.dev_found = wpas_dev_found;
4581 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004582 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004583 p2p.start_listen = wpas_start_listen;
4584 p2p.stop_listen = wpas_stop_listen;
4585 p2p.send_probe_resp = wpas_send_probe_resp;
4586 p2p.sd_request = wpas_sd_request;
4587 p2p.sd_response = wpas_sd_response;
4588 p2p.prov_disc_req = wpas_prov_disc_req;
4589 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004590 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591 p2p.invitation_process = wpas_invitation_process;
4592 p2p.invitation_received = wpas_invitation_received;
4593 p2p.invitation_result = wpas_invitation_result;
4594 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004595 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004596 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004597 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004598 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004599 p2p.get_persistent_group = wpas_get_persistent_group;
4600 p2p.get_go_info = wpas_get_go_info;
4601 p2p.remove_stale_groups = wpas_remove_stale_groups;
4602 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4603 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4604 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004605 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606
4607 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004608 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 p2p.dev_name = wpa_s->conf->device_name;
4610 p2p.manufacturer = wpa_s->conf->manufacturer;
4611 p2p.model_name = wpa_s->conf->model_name;
4612 p2p.model_number = wpa_s->conf->model_number;
4613 p2p.serial_number = wpa_s->conf->serial_number;
4614 if (wpa_s->wps) {
4615 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4616 p2p.config_methods = wpa_s->wps->config_methods;
4617 }
4618
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004619 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4620 wpa_printf(MSG_ERROR,
4621 "P2P: Failed to configure supported channel list");
4622 return -1;
4623 }
4624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 if (wpa_s->conf->p2p_listen_reg_class &&
4626 wpa_s->conf->p2p_listen_channel) {
4627 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4628 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004629 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004630 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 /*
4632 * Pick one of the social channels randomly as the listen
4633 * channel.
4634 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004635 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004636 &p2p.channel,
4637 &global->p2p_go_avoid_freq,
4638 &global->p2p_disallow_freq) !=
4639 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004640 wpa_printf(MSG_INFO,
4641 "P2P: No social channels supported by the driver - do not enable P2P");
4642 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004643 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004644 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004646 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4647 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648
4649 if (wpa_s->conf->p2p_oper_reg_class &&
4650 wpa_s->conf->p2p_oper_channel) {
4651 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4652 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4653 p2p.cfg_op_channel = 1;
4654 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4655 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4656
4657 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004659 * Use random operation channel from 2.4 GHz band social
4660 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4661 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004663 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004664 &p2p.op_channel, NULL,
4665 NULL) != 0) {
4666 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004667 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004668 p2p.op_reg_class = 0;
4669 p2p.op_channel = 0;
4670 /* This will be overridden during group setup in
4671 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004672 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004673 p2p.cfg_op_channel = 0;
4674 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4675 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4676 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004677
4678 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4679 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4680 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4681 }
4682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4684 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4685 p2p.country[2] = 0x04;
4686 } else
4687 os_memcpy(p2p.country, "XX\x04", 3);
4688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4690 WPS_DEV_TYPE_LEN);
4691
4692 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4693 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4694 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4695
4696 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4697 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4698
4699 p2p.max_peers = 100;
4700
4701 if (wpa_s->conf->p2p_ssid_postfix) {
4702 p2p.ssid_postfix_len =
4703 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4704 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4705 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4706 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4707 p2p.ssid_postfix_len);
4708 }
4709
4710 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4711
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004712 p2p.max_listen = wpa_s->max_remain_on_chan;
4713
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004714 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4715 wpa_s->conf->p2p_passphrase_len <= 63)
4716 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4717 else
4718 p2p.passphrase_len = 8;
4719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 global->p2p = p2p_init(&p2p);
4721 if (global->p2p == NULL)
4722 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004723 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724
4725 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4726 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4727 continue;
4728 p2p_add_wps_vendor_extension(
4729 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4730 }
4731
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004732 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734 return 0;
4735}
4736
4737
4738/**
4739 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4740 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4741 *
4742 * This function deinitialize per-interface P2P data.
4743 */
4744void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4745{
4746 if (wpa_s->driver && wpa_s->drv_priv)
4747 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004748
4749 if (wpa_s->go_params) {
4750 /* Clear any stored provisioning info */
4751 p2p_clear_provisioning_info(
4752 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004753 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004754 }
4755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 os_free(wpa_s->go_params);
4757 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004758 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4760 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4761 wpa_s->p2p_long_listen = 0;
4762 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4763 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4764 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004765 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004766 wpas_p2p_listen_work_done(wpa_s);
4767 if (wpa_s->p2p_send_action_work) {
4768 os_free(wpa_s->p2p_send_action_work->ctx);
4769 radio_work_done(wpa_s->p2p_send_action_work);
4770 wpa_s->p2p_send_action_work = NULL;
4771 }
4772 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004774 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4775 wpa_s->p2p_oob_dev_pw = NULL;
4776
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004777 os_free(wpa_s->p2p_group_common_freqs);
4778 wpa_s->p2p_group_common_freqs = NULL;
4779 wpa_s->p2p_group_common_freqs_num = 0;
4780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781 /* TODO: remove group interface from the driver if this wpa_s instance
4782 * is on top of a P2P group interface */
4783}
4784
4785
4786/**
4787 * wpas_p2p_deinit_global - Deinitialize global P2P module
4788 * @global: Pointer to global data from wpa_supplicant_init()
4789 *
4790 * This function deinitializes the global (per device) P2P module.
4791 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004792static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004793{
4794 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004796 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004797
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004798 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799
4800 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4802 wpa_s = wpa_s->next;
4803 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 tmp = global->ifaces;
4805 while (tmp &&
4806 (tmp == wpa_s ||
4807 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4808 tmp = tmp->next;
4809 }
4810 if (tmp == NULL)
4811 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812 /* Disconnect from the P2P group and deinit the interface */
4813 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 }
4815
4816 /*
4817 * Deinit GO data on any possibly remaining interface (if main
4818 * interface is used as GO).
4819 */
4820 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4821 if (wpa_s->ap_iface)
4822 wpas_p2p_group_deinit(wpa_s);
4823 }
4824
4825 p2p_deinit(global->p2p);
4826 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004827 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004828}
4829
4830
4831static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4832{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004833 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004834 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 if (wpa_s->drv_flags &
4836 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4837 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4838 return 1; /* P2P group requires a new interface in every case
4839 */
4840 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4841 return 0; /* driver does not support concurrent operations */
4842 if (wpa_s->global->ifaces->next)
4843 return 1; /* more that one interface already in use */
4844 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4845 return 1; /* this interface is already in use */
4846 return 0;
4847}
4848
4849
4850static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4851 const u8 *peer_addr,
4852 enum p2p_wps_method wps_method,
4853 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004854 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004855 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004857 if (persistent_group && wpa_s->conf->persistent_reconnect)
4858 persistent_group = 2;
4859
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004860 /*
4861 * Increase GO config timeout if HT40 is used since it takes some time
4862 * to scan channels for coex purposes before the BSS can be started.
4863 */
4864 p2p_set_config_timeout(wpa_s->global->p2p,
4865 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4868 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004869 persistent_group, ssid ? ssid->ssid : NULL,
4870 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004871 wpa_s->p2p_pd_before_go_neg, pref_freq,
4872 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4873 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874}
4875
4876
4877static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4878 const u8 *peer_addr,
4879 enum p2p_wps_method wps_method,
4880 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004881 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004882 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004884 if (persistent_group && wpa_s->conf->persistent_reconnect)
4885 persistent_group = 2;
4886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4888 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004889 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004890 ssid ? ssid->ssid_len : 0, pref_freq,
4891 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4892 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893}
4894
4895
4896static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4897{
4898 wpa_s->p2p_join_scan_count++;
4899 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4900 wpa_s->p2p_join_scan_count);
4901 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4902 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4903 " for join operationg - stop join attempt",
4904 MAC2STR(wpa_s->pending_join_iface_addr));
4905 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004906 if (wpa_s->p2p_auto_pd) {
4907 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004908 wpa_msg_global(wpa_s, MSG_INFO,
4909 P2P_EVENT_PROV_DISC_FAILURE
4910 " p2p_dev_addr=" MACSTR " status=N/A",
4911 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004912 return;
4913 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004914 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004915 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004916 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 }
4918}
4919
4920
Dmitry Shmidt04949592012-07-19 12:16:46 -07004921static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4922{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004923 int res;
4924 unsigned int num, i;
4925 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004926
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004927 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4928 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004929 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004930 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004931
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004932 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4933 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004934 if (!freqs)
4935 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004936
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004937 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4938 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004939
4940 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004941 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004942 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4943 freq);
4944 res = 0;
4945 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004946 }
4947 }
4948
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004949 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004950 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004951
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004952exit_free:
4953 os_free(freqs);
4954 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004955}
4956
4957
4958static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4959 const u8 *peer_dev_addr)
4960{
4961 struct wpa_bss *bss;
4962 int updated;
4963
4964 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4965 if (bss == NULL)
4966 return -1;
4967 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4968 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4969 "last scan");
4970 return 0;
4971 }
4972
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004973 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4974 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004975 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4976 "%ld.%06ld (%supdated in last scan)",
4977 bss->last_update.sec, bss->last_update.usec,
4978 updated ? "": "not ");
4979
4980 return updated;
4981}
4982
4983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4985 struct wpa_scan_results *scan_res)
4986{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004987 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 int freq;
4989 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4992
4993 if (wpa_s->global->p2p_disabled)
4994 return;
4995
Dmitry Shmidt04949592012-07-19 12:16:46 -07004996 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4997 scan_res ? (int) scan_res->num : -1,
4998 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999
5000 if (scan_res)
5001 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5002
Dmitry Shmidt04949592012-07-19 12:16:46 -07005003 if (wpa_s->p2p_auto_pd) {
5004 int join = wpas_p2p_peer_go(wpa_s,
5005 wpa_s->pending_join_dev_addr);
5006 if (join == 0 &&
5007 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5008 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005009 bss = wpa_bss_get_bssid_latest(
5010 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005011 if (bss) {
5012 freq = bss->freq;
5013 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5014 "the peer " MACSTR " at %d MHz",
5015 wpa_s->auto_pd_scan_retry,
5016 MAC2STR(wpa_s->
5017 pending_join_dev_addr),
5018 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005019 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005020 return;
5021 }
5022 }
5023
5024 if (join < 0)
5025 join = 0;
5026
5027 wpa_s->p2p_auto_pd = 0;
5028 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5029 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5030 MAC2STR(wpa_s->pending_join_dev_addr), join);
5031 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005032 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005033 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005034 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005035 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005036 wpa_msg_global(wpa_s, MSG_INFO,
5037 P2P_EVENT_PROV_DISC_FAILURE
5038 " p2p_dev_addr=" MACSTR " status=N/A",
5039 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005040 }
5041 return;
5042 }
5043
5044 if (wpa_s->p2p_auto_join) {
5045 int join = wpas_p2p_peer_go(wpa_s,
5046 wpa_s->pending_join_dev_addr);
5047 if (join < 0) {
5048 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5049 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005050 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005051 P2P_EVENT_FALLBACK_TO_GO_NEG
5052 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005053 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5054 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5055 wpa_s->p2p_persistent_group, 0, 0, 0,
5056 wpa_s->p2p_go_intent,
5057 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005058 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005059 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005060 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005061 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005062 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005063 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005064 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005065 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005066 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005067 return;
5068 }
5069
5070 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5071 "try to join the group", join ? "" :
5072 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005073 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005074 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005075 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005076 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005077 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005078 }
5079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5081 wpa_s->pending_join_iface_addr);
5082 if (freq < 0 &&
5083 p2p_get_interface_addr(wpa_s->global->p2p,
5084 wpa_s->pending_join_dev_addr,
5085 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005086 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5087 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5089 "address for join from " MACSTR " to " MACSTR
5090 " based on newly discovered P2P peer entry",
5091 MAC2STR(wpa_s->pending_join_iface_addr),
5092 MAC2STR(iface_addr));
5093 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5094 ETH_ALEN);
5095
5096 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5097 wpa_s->pending_join_iface_addr);
5098 }
5099 if (freq >= 0) {
5100 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5101 "from P2P peer table: %d MHz", freq);
5102 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005103 if (wpa_s->p2p_join_ssid_len) {
5104 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5105 MACSTR " and SSID %s",
5106 MAC2STR(wpa_s->pending_join_iface_addr),
5107 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5108 wpa_s->p2p_join_ssid_len));
5109 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5110 wpa_s->p2p_join_ssid,
5111 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005112 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005113 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5114 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5115 bss = wpa_bss_get_bssid_latest(wpa_s,
5116 wpa_s->pending_join_iface_addr);
5117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005118 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005119 u8 dev_addr[ETH_ALEN];
5120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 freq = bss->freq;
5122 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005123 "from BSS table: %d MHz (SSID %s)", freq,
5124 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005125 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5126 dev_addr) == 0 &&
5127 os_memcmp(wpa_s->pending_join_dev_addr,
5128 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5129 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5130 ETH_ALEN) != 0) {
5131 wpa_printf(MSG_DEBUG,
5132 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5133 MAC2STR(dev_addr),
5134 MAC2STR(wpa_s->pending_join_dev_addr));
5135 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5136 ETH_ALEN);
5137 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005138 }
5139 if (freq > 0) {
5140 u16 method;
5141
Dmitry Shmidt04949592012-07-19 12:16:46 -07005142 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005143 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005144 P2P_EVENT_GROUP_FORMATION_FAILURE
5145 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005146 wpas_notify_p2p_group_formation_failure(
5147 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005148 return;
5149 }
5150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5152 "prior to joining an existing group (GO " MACSTR
5153 " freq=%u MHz)",
5154 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5155 wpa_s->pending_pd_before_join = 1;
5156
5157 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005158 case WPS_PIN_DISPLAY:
5159 method = WPS_CONFIG_KEYPAD;
5160 break;
5161 case WPS_PIN_KEYPAD:
5162 method = WPS_CONFIG_DISPLAY;
5163 break;
5164 case WPS_PBC:
5165 method = WPS_CONFIG_PUSHBUTTON;
5166 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005167 case WPS_P2PS:
5168 method = WPS_CONFIG_P2PS;
5169 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 default:
5171 method = 0;
5172 break;
5173 }
5174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005175 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5176 wpa_s->pending_join_dev_addr) ==
5177 method)) {
5178 /*
5179 * We have already performed provision discovery for
5180 * joining the group. Proceed directly to join
5181 * operation without duplicated provision discovery. */
5182 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5183 "with " MACSTR " already done - proceed to "
5184 "join",
5185 MAC2STR(wpa_s->pending_join_dev_addr));
5186 wpa_s->pending_pd_before_join = 0;
5187 goto start;
5188 }
5189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005191 wpa_s->pending_join_dev_addr,
5192 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005193 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005194 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5195 "Discovery Request before joining an "
5196 "existing group");
5197 wpa_s->pending_pd_before_join = 0;
5198 goto start;
5199 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 return;
5201 }
5202
5203 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5204 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5205 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5206 wpas_p2p_check_join_scan_limit(wpa_s);
5207 return;
5208
5209start:
5210 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005211 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5212 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213}
5214
5215
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005216static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5217 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005218{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005219 int ret;
5220 struct wpa_driver_scan_params params;
5221 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005222 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005223 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005224 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005225
5226 os_memset(&params, 0, sizeof(params));
5227
5228 /* P2P Wildcard SSID */
5229 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005230 if (ssid && ssid_len) {
5231 params.ssids[0].ssid = ssid;
5232 params.ssids[0].ssid_len = ssid_len;
5233 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5234 wpa_s->p2p_join_ssid_len = ssid_len;
5235 } else {
5236 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5237 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5238 wpa_s->p2p_join_ssid_len = 0;
5239 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005240
5241 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005242 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5243 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5244 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005245 if (wps_ie == NULL) {
5246 wpas_p2p_scan_res_join(wpa_s, NULL);
5247 return;
5248 }
5249
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005250 if (!freq) {
5251 int oper_freq;
5252 /*
5253 * If freq is not provided, check the operating freq of the GO
5254 * and use a single channel scan on if possible.
5255 */
5256 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5257 wpa_s->pending_join_iface_addr);
5258 if (oper_freq > 0)
5259 freq = oper_freq;
5260 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005261 if (freq > 0) {
5262 freqs[0] = freq;
5263 params.freqs = freqs;
5264 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005266 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5267 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5268 if (ies == NULL) {
5269 wpabuf_free(wps_ie);
5270 wpas_p2p_scan_res_join(wpa_s, NULL);
5271 return;
5272 }
5273 wpabuf_put_buf(ies, wps_ie);
5274 wpabuf_free(wps_ie);
5275
5276 bands = wpas_get_bands(wpa_s, freqs);
5277 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5278
5279 params.p2p_probe = 1;
5280 params.extra_ies = wpabuf_head(ies);
5281 params.extra_ies_len = wpabuf_len(ies);
5282
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005283 if (wpa_s->clear_driver_scan_cache) {
5284 wpa_printf(MSG_DEBUG,
5285 "Request driver to clear scan cache due to local BSS flush");
5286 params.only_new_results = 1;
5287 }
5288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005289 /*
5290 * Run a scan to update BSS table and start Provision Discovery once
5291 * the new scan results become available.
5292 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005293 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005294 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005295 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005296 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005297 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005298 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005299 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300
5301 wpabuf_free(ies);
5302
5303 if (ret) {
5304 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5305 "try again later");
5306 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5307 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5308 wpas_p2p_check_join_scan_limit(wpa_s);
5309 }
5310}
5311
5312
Dmitry Shmidt04949592012-07-19 12:16:46 -07005313static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5314{
5315 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005316 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005317}
5318
5319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005320static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005321 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005322 int auto_join, int op_freq,
5323 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005324{
5325 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005326 MACSTR " dev " MACSTR " op_freq=%d)%s",
5327 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005328 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005329 if (ssid && ssid_len) {
5330 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5331 wpa_ssid_txt(ssid, ssid_len));
5332 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334 wpa_s->p2p_auto_pd = 0;
5335 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5337 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5338 wpa_s->pending_join_wps_method = wps_method;
5339
5340 /* Make sure we are not running find during connection establishment */
5341 wpas_p2p_stop_find(wpa_s);
5342
5343 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005344 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345 return 0;
5346}
5347
5348
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005349static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5350 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005351{
5352 struct wpa_supplicant *group;
5353 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005354 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355
5356 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5357 if (group == NULL)
5358 return -1;
5359 if (group != wpa_s) {
5360 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5361 sizeof(group->p2p_pin));
5362 group->p2p_wps_method = wpa_s->p2p_wps_method;
5363 }
5364
Hai Shalom74f70d42019-02-11 14:42:39 -08005365 /*
5366 * Need to mark the current interface for p2p_group_formation
5367 * when a separate group interface is not used. This is needed
5368 * to allow p2p_cancel stop a pending p2p_connect-join.
5369 * wpas_p2p_init_group_interface() addresses this for the case
5370 * where a separate group interface is used.
5371 */
5372 if (group == wpa_s->parent)
5373 wpa_s->global->p2p_group_formation = group;
5374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005376 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005377
5378 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005379 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5381 ETH_ALEN);
5382 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005383 if (freq && ssid && ssid_len) {
5384 res.freq = freq;
5385 res.ssid_len = ssid_len;
5386 os_memcpy(res.ssid, ssid, ssid_len);
5387 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005388 if (ssid && ssid_len) {
5389 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5390 ssid, ssid_len);
5391 } else {
5392 bss = wpa_bss_get_bssid_latest(
5393 wpa_s, wpa_s->pending_join_iface_addr);
5394 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005395 if (bss) {
5396 res.freq = bss->freq;
5397 res.ssid_len = bss->ssid_len;
5398 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5399 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5400 bss->freq,
5401 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005402 } else if (ssid && ssid_len) {
5403 res.ssid_len = ssid_len;
5404 os_memcpy(res.ssid, ssid, ssid_len);
5405 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5406 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005407 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005408 }
5409
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005410 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5411 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5412 "starting client");
5413 wpa_drv_cancel_remain_on_channel(wpa_s);
5414 wpa_s->off_channel_freq = 0;
5415 wpa_s->roc_waiting_drv_freq = 0;
5416 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 wpas_start_wps_enrollee(group, &res);
5418
5419 /*
5420 * Allow a longer timeout for join-a-running-group than normal 15
5421 * second group formation timeout since the GO may not have authorized
5422 * our connection yet.
5423 */
5424 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5425 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5426 wpa_s, NULL);
5427
5428 return 0;
5429}
5430
5431
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005432static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005433 int *force_freq, int *pref_freq, int go,
5434 unsigned int *pref_freq_list,
5435 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005436{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005437 struct wpa_used_freq_data *freqs;
5438 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005439 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5440
5441 max_pref_freq = *num_pref_freq;
5442 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005443
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005444 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5445 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005446 if (!freqs)
5447 return -1;
5448
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005449 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5450 wpa_s->num_multichan_concurrent);
5451
5452 /*
5453 * It is possible that the total number of used frequencies is bigger
5454 * than the number of frequencies used for P2P, so get the system wide
5455 * number of unused frequencies.
5456 */
5457 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5458
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005459 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005460 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5461 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005462
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005463 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005464 int ret;
5465 if (go)
5466 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5467 else
5468 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5469 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005470 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005471 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5472 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005473 /*
5474 * If freq is a DFS channel and DFS is offloaded
5475 * to the driver, allow P2P GO to use it.
5476 */
5477 wpa_printf(MSG_DEBUG,
5478 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5479 freq);
5480 } else {
5481 wpa_printf(MSG_DEBUG,
5482 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5483 freq);
5484 res = -3;
5485 goto exit_free;
5486 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005487 }
5488
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005489 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005490 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005491 freq_in_use = 1;
5492 }
5493
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005494 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005495 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5496 freq);
5497 res = -2;
5498 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005499 }
5500 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5501 "requested channel (%u MHz)", freq);
5502 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005503 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005504 }
5505
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005506 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005507
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005508 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5509 enum wpa_driver_if_type iface_type;
5510
5511 if (go)
5512 iface_type = WPA_IF_P2P_GO;
5513 else
5514 iface_type = WPA_IF_P2P_CLIENT;
5515
5516 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5517 best_freq, go);
5518
5519 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5520 &max_pref_freq,
5521 pref_freq_list);
5522 if (!res && max_pref_freq > 0) {
5523 *num_pref_freq = max_pref_freq;
5524 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005525 while (i < *num_pref_freq &&
5526 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005527 pref_freq_list[i]) ||
5528 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005529 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005530 wpa_printf(MSG_DEBUG,
5531 "P2P: preferred_freq_list[%d]=%d is disallowed",
5532 i, pref_freq_list[i]);
5533 i++;
5534 }
5535 if (i != *num_pref_freq) {
5536 best_freq = pref_freq_list[i];
5537 wpa_printf(MSG_DEBUG,
5538 "P2P: Using preferred_freq_list[%d]=%d",
5539 i, best_freq);
5540 } else {
5541 wpa_printf(MSG_DEBUG,
5542 "P2P: All driver preferred frequencies are disallowed for P2P use");
5543 *num_pref_freq = 0;
5544 }
5545 } else {
5546 wpa_printf(MSG_DEBUG,
5547 "P2P: No preferred frequency list available");
5548 }
5549 }
5550
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005551 /* We have a candidate frequency to use */
5552 if (best_freq > 0) {
5553 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005554 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005555 best_freq);
5556 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005557 } else {
5558 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 -07005559 best_freq);
5560 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005561 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005562 } else if (num_unused > 0) {
5563 wpa_printf(MSG_DEBUG,
5564 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5565 *force_freq = 0;
5566 } else {
5567 wpa_printf(MSG_DEBUG,
5568 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5569 res = -2;
5570 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005571 }
5572
5573exit_ok:
5574 res = 0;
5575exit_free:
5576 os_free(freqs);
5577 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005578}
5579
5580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005581/**
5582 * wpas_p2p_connect - Request P2P Group Formation to be started
5583 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5584 * @peer_addr: Address of the peer P2P Device
5585 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5586 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005587 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588 * @join: Whether to join an existing group (as a client) instead of starting
5589 * Group Owner negotiation; @peer_addr is BSSID in that case
5590 * @auth: Whether to only authorize the connection instead of doing that and
5591 * initiating Group Owner negotiation
5592 * @go_intent: GO Intent or -1 to use default
5593 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005594 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005595 * @persistent_id: Persistent group credentials to use for forcing GO
5596 * parameters or -1 to generate new values (SSID/passphrase)
5597 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5598 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005599 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005600 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005601 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005602 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005603 * @group_ssid: Specific Group SSID for join or %NULL if not set
5604 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005605 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5606 * failure, -2 on failure due to channel not currently available,
5607 * -3 if forced channel is not supported
5608 */
5609int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5610 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005611 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005612 int go_intent, int freq, unsigned int vht_center_freq2,
5613 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005614 unsigned int vht_chwidth, int he, int edmg,
5615 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005616{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005617 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005618 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005620 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005621 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005622 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623
5624 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5625 return -1;
5626
Dmitry Shmidt04949592012-07-19 12:16:46 -07005627 if (persistent_id >= 0) {
5628 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5629 if (ssid == NULL || ssid->disabled != 2 ||
5630 ssid->mode != WPAS_MODE_P2P_GO)
5631 return -1;
5632 }
5633
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005634 os_free(wpa_s->global->add_psk);
5635 wpa_s->global->add_psk = NULL;
5636
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005637 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005638 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005639 wpa_s->global->pending_p2ps_group_freq = 0;
5640 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642 if (go_intent < 0)
5643 go_intent = wpa_s->conf->p2p_go_intent;
5644
5645 if (!auth)
5646 wpa_s->p2p_long_listen = 0;
5647
5648 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005649 wpa_s->p2p_persistent_group = !!persistent_group;
5650 wpa_s->p2p_persistent_id = persistent_id;
5651 wpa_s->p2p_go_intent = go_intent;
5652 wpa_s->p2p_connect_freq = freq;
5653 wpa_s->p2p_fallback_to_go_neg = 0;
5654 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005655 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005656 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005657 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5658 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005659 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005660 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661
5662 if (pin)
5663 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5664 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005665 if (wps_generate_pin((unsigned int *) &ret) < 0)
5666 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005667 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5668 "%08d", ret);
5669 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5670 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5672 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005673 } else if (wps_method == WPS_P2PS) {
5674 /* Force the P2Ps default PIN to be used */
5675 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676 } else
5677 wpa_s->p2p_pin[0] = '\0';
5678
Dmitry Shmidt04949592012-07-19 12:16:46 -07005679 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5681 if (auth) {
5682 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5683 "connect a running group from " MACSTR,
5684 MAC2STR(peer_addr));
5685 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5686 return ret;
5687 }
5688 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5689 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5690 iface_addr) < 0) {
5691 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5692 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5693 dev_addr);
5694 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005695 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005696 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005697 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5698 "%ld.%06ld",
5699 wpa_s->p2p_auto_started.sec,
5700 wpa_s->p2p_auto_started.usec);
5701 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005702 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005703 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005704 auto_join, freq,
5705 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 return -1;
5707 return ret;
5708 }
5709
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005710 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005711 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005712 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005713 if (res)
5714 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005715 wpas_p2p_set_own_freq_preference(wpa_s,
5716 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005717
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005718 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5721
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005722 if (wpa_s->create_p2p_iface) {
5723 /* Prepare to add a new interface for the group */
5724 iftype = WPA_IF_P2P_GROUP;
5725 if (go_intent == 15)
5726 iftype = WPA_IF_P2P_GO;
5727 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5728 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5729 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005732
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005733 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005734 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005735 if (wpa_s->p2p_mgmt)
5736 if_addr = wpa_s->parent->own_addr;
5737 else
5738 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005739 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5740 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005741
5742 if (auth) {
5743 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005744 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005745 force_freq, persistent_group, ssid,
5746 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005747 return -1;
5748 return ret;
5749 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005750
5751 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5752 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005753 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005754 if (wpa_s->create_p2p_iface)
5755 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756 return -1;
5757 }
5758 return ret;
5759}
5760
5761
5762/**
5763 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5764 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5765 * @freq: Frequency of the channel in MHz
5766 * @duration: Duration of the stay on the channel in milliseconds
5767 *
5768 * This callback is called when the driver indicates that it has started the
5769 * requested remain-on-channel duration.
5770 */
5771void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5772 unsigned int freq, unsigned int duration)
5773{
5774 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5775 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005776 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)",
5777 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5778 wpa_s->roc_waiting_drv_freq, freq, duration);
5779 if (wpa_s->off_channel_freq &&
5780 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005781 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5782 wpa_s->pending_listen_duration);
5783 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005784 } else {
5785 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5786 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5787 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788 }
5789}
5790
5791
Jithu Jance57cea1a2014-08-20 10:22:04 -07005792int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793{
5794 /* Limit maximum Listen state time based on driver limitation. */
5795 if (timeout > wpa_s->max_remain_on_chan)
5796 timeout = wpa_s->max_remain_on_chan;
5797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798 return p2p_listen(wpa_s->global->p2p, timeout);
5799}
5800
5801
5802/**
5803 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5804 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5805 * @freq: Frequency of the channel in MHz
5806 *
5807 * This callback is called when the driver indicates that a remain-on-channel
5808 * operation has been completed, i.e., the duration on the requested channel
5809 * has timed out.
5810 */
5811void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5812 unsigned int freq)
5813{
5814 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5815 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005816 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005817 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5819 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005820 if (wpa_s->p2p_long_listen > 0)
5821 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5823 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005824 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005826 if (wpa_s->p2p_long_listen > 0) {
5827 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5828 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005829 } else {
5830 /*
5831 * When listen duration is over, stop listen & update p2p_state
5832 * to IDLE.
5833 */
5834 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835 }
5836}
5837
5838
5839/**
5840 * wpas_p2p_group_remove - Remove a P2P group
5841 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5842 * @ifname: Network interface name of the group interface or "*" to remove all
5843 * groups
5844 * Returns: 0 on success, -1 on failure
5845 *
5846 * This function is used to remove a P2P group. This can be used to disconnect
5847 * from a group in which the local end is a P2P Client or to end a P2P Group in
5848 * case the local end is the Group Owner. If a virtual network interface was
5849 * created for this group, that interface will be removed. Otherwise, only the
5850 * configured P2P group network will be removed from the interface.
5851 */
5852int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5853{
5854 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005855 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856
5857 if (os_strcmp(ifname, "*") == 0) {
5858 struct wpa_supplicant *prev;
5859 wpa_s = global->ifaces;
5860 while (wpa_s) {
5861 prev = wpa_s;
5862 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005863 if (prev->p2p_group_interface !=
5864 NOT_P2P_GROUP_INTERFACE ||
5865 (prev->current_ssid &&
5866 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005867 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005868 }
5869 return 0;
5870 }
5871
5872 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5873 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5874 break;
5875 }
5876
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005877 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878}
5879
5880
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005881static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5882{
5883 unsigned int r;
5884
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005885 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5886 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5887 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5888 int res;
5889
5890 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5891 &size, pref_freq_list);
5892 if (!res && size > 0) {
5893 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005894 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005895 (!p2p_supported_freq(wpa_s->global->p2p,
5896 pref_freq_list[i]) ||
5897 wpas_p2p_disallowed_freq(wpa_s->global,
5898 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005899 wpa_printf(MSG_DEBUG,
5900 "P2P: preferred_freq_list[%d]=%d is disallowed",
5901 i, pref_freq_list[i]);
5902 i++;
5903 }
5904 if (i != size) {
5905 freq = pref_freq_list[i];
5906 wpa_printf(MSG_DEBUG,
5907 "P2P: Using preferred_freq_list[%d]=%d",
5908 i, freq);
5909 } else {
5910 wpa_printf(MSG_DEBUG,
5911 "P2P: All driver preferred frequencies are disallowed for P2P use");
5912 }
5913 } else {
5914 wpa_printf(MSG_DEBUG,
5915 "P2P: No preferred frequency list available");
5916 }
5917 }
5918
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005919 if (freq == 2) {
5920 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5921 "band");
5922 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005923 p2p_supported_freq_go(wpa_s->global->p2p,
5924 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005925 freq = wpa_s->best_24_freq;
5926 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5927 "channel: %d MHz", freq);
5928 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005929 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5930 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005931 freq = 2412 + (r % 3) * 25;
5932 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5933 "channel: %d MHz", freq);
5934 }
5935 }
5936
5937 if (freq == 5) {
5938 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5939 "band");
5940 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005941 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005942 wpa_s->best_5_freq)) {
5943 freq = wpa_s->best_5_freq;
5944 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5945 "channel: %d MHz", freq);
5946 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005947 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5948 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005949 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005950 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005951 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5952 "5 GHz channel for P2P group");
5953 return -1;
5954 }
5955 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5956 "channel: %d MHz", freq);
5957 }
5958 }
5959
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005960 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005961 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005962 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5963 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005964 /*
5965 * If freq is a DFS channel and DFS is offloaded to the
5966 * driver, allow P2P GO to use it.
5967 */
5968 wpa_printf(MSG_DEBUG, "P2P: "
5969 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5970 __func__, freq);
5971 return freq;
5972 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005973 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5974 "(%u MHz) is not supported for P2P uses",
5975 freq);
5976 return -1;
5977 }
5978
5979 return freq;
5980}
5981
5982
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005983static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5984 const struct p2p_channels *channels,
5985 int freq)
5986{
5987 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5988 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5989 freq_included(wpa_s, channels, freq))
5990 return 1;
5991 return 0;
5992}
5993
5994
5995static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5996 struct p2p_go_neg_results *params,
5997 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005998{
5999 unsigned int i, r;
6000
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006001 /* try all channels in operating class 115 */
6002 for (i = 0; i < 4; i++) {
6003 params->freq = 5180 + i * 20;
6004 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006005 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006006 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6007 goto out;
6008 }
6009
6010 /* try all channels in operating class 124 */
6011 for (i = 0; i < 4; i++) {
6012 params->freq = 5745 + i * 20;
6013 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006014 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006015 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6016 goto out;
6017 }
6018
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006019 /* try social channel class 180 channel 2 */
6020 params->freq = 58320 + 1 * 2160;
6021 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006022 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006023 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6024 goto out;
6025
6026 /* try all channels in reg. class 180 */
6027 for (i = 0; i < 4; i++) {
6028 params->freq = 58320 + i * 2160;
6029 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006030 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006031 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6032 goto out;
6033 }
6034
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006035 /* try some random selection of the social channels */
6036 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6037 return;
6038
6039 for (i = 0; i < 3; i++) {
6040 params->freq = 2412 + ((r + i) % 3) * 25;
6041 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6042 goto out;
6043 }
6044
6045 /* try all other channels in operating class 81 */
6046 for (i = 0; i < 11; i++) {
6047 params->freq = 2412 + i * 5;
6048
6049 /* skip social channels; covered in the previous loop */
6050 if (params->freq == 2412 ||
6051 params->freq == 2437 ||
6052 params->freq == 2462)
6053 continue;
6054
6055 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6056 goto out;
6057 }
6058
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006059 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006060 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006061 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006062out:
6063 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6064 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006065}
6066
6067
Roshan Pius3a1667e2018-07-03 15:17:14 -07006068static int wpas_same_band(int freq1, int freq2)
6069{
6070 enum hostapd_hw_mode mode1, mode2;
6071 u8 chan1, chan2;
6072
6073 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6074 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6075 if (mode1 == NUM_HOSTAPD_MODES)
6076 return 0;
6077 return mode1 == mode2;
6078}
6079
6080
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006081static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6082 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006083 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006084 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006085 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006086 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006087{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006088 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006089 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006090 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006091 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006092 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093
6094 os_memset(params, 0, sizeof(*params));
6095 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006096 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006097 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006098 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006099 params->max_oper_chwidth = max_oper_chwidth;
6100 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006101 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006103 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6104 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006105 if (!freqs)
6106 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006108 num = get_shared_radio_freqs_data(wpa_s, freqs,
6109 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006110
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006111 if (wpa_s->current_ssid &&
6112 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6113 wpa_s->wpa_state == WPA_COMPLETED) {
6114 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6115 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006116
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006117 /*
6118 * If the frequency selection is done for an active P2P GO that
6119 * is not sharing a frequency, allow to select a new frequency
6120 * even if there are no unused frequencies as we are about to
6121 * move the P2P GO so its frequency can be re-used.
6122 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006123 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006124 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6125 freqs[i].flags == 0) {
6126 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006127 break;
6128 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006129 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006130 }
6131
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006132 /* Try to use EDMG channel */
6133 if (params->edmg) {
6134 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6135 goto success;
6136 params->edmg = 0;
6137 }
6138
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006139 /* try using the forced freq */
6140 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006141 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6142 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006143 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006144 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006145 freq);
6146 goto fail;
6147 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006148 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6149 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006150 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6151 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006152 /*
6153 * If freq is a DFS channel and DFS is offloaded
6154 * to the driver, allow P2P GO to use it.
6155 */
6156 wpa_printf(MSG_DEBUG,
6157 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6158 __func__, freq);
6159 } else {
6160 wpa_printf(MSG_DEBUG,
6161 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6162 freq);
6163 goto fail;
6164 }
6165 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006166
6167 for (i = 0; i < num; i++) {
6168 if (freqs[i].freq == freq) {
6169 wpa_printf(MSG_DEBUG,
6170 "P2P: forced freq (%d MHz) is also shared",
6171 freq);
6172 params->freq = freq;
6173 goto success;
6174 }
6175 }
6176
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006177 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006178 wpa_printf(MSG_DEBUG,
6179 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6180 freq);
6181 goto fail;
6182 }
6183
6184 wpa_printf(MSG_DEBUG,
6185 "P2P: force GO freq (%d MHz) on a free channel",
6186 freq);
6187 params->freq = freq;
6188 goto success;
6189 }
6190
6191 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006192 if (num &&
6193 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006194 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6195 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6196 wpa_printf(MSG_DEBUG,
6197 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006198 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006199 params->freq = cand;
6200 goto success;
6201 }
6202
6203 /* try using one of the shared freqs */
6204 for (i = 0; i < num; i++) {
6205 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6206 freqs[i].freq)) {
6207 wpa_printf(MSG_DEBUG,
6208 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006209 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006210 params->freq = freqs[i].freq;
6211 goto success;
6212 }
6213 }
6214 }
6215
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006216 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006217 wpa_printf(MSG_DEBUG,
6218 "P2P: Cannot force GO on any of the channels we are already using");
6219 goto fail;
6220 }
6221
6222 /* try using the setting from the configuration file */
6223 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6224 wpa_s->conf->p2p_oper_channel >= 1 &&
6225 wpa_s->conf->p2p_oper_channel <= 11 &&
6226 wpas_p2p_supported_freq_go(
6227 wpa_s, channels,
6228 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6229 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6230 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6231 "frequency %d MHz", params->freq);
6232 goto success;
6233 }
6234
6235 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6236 wpa_s->conf->p2p_oper_reg_class == 116 ||
6237 wpa_s->conf->p2p_oper_reg_class == 117 ||
6238 wpa_s->conf->p2p_oper_reg_class == 124 ||
6239 wpa_s->conf->p2p_oper_reg_class == 125 ||
6240 wpa_s->conf->p2p_oper_reg_class == 126 ||
6241 wpa_s->conf->p2p_oper_reg_class == 127) &&
6242 wpas_p2p_supported_freq_go(wpa_s, channels,
6243 5000 +
6244 5 * wpa_s->conf->p2p_oper_channel)) {
6245 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6246 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6247 "frequency %d MHz", params->freq);
6248 goto success;
6249 }
6250
6251 /* Try using best channels */
6252 if (wpa_s->conf->p2p_oper_channel == 0 &&
6253 wpa_s->best_overall_freq > 0 &&
6254 wpas_p2p_supported_freq_go(wpa_s, channels,
6255 wpa_s->best_overall_freq)) {
6256 params->freq = wpa_s->best_overall_freq;
6257 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6258 "channel %d MHz", params->freq);
6259 goto success;
6260 }
6261
6262 if (wpa_s->conf->p2p_oper_channel == 0 &&
6263 wpa_s->best_24_freq > 0 &&
6264 wpas_p2p_supported_freq_go(wpa_s, channels,
6265 wpa_s->best_24_freq)) {
6266 params->freq = wpa_s->best_24_freq;
6267 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6268 "channel %d MHz", params->freq);
6269 goto success;
6270 }
6271
6272 if (wpa_s->conf->p2p_oper_channel == 0 &&
6273 wpa_s->best_5_freq > 0 &&
6274 wpas_p2p_supported_freq_go(wpa_s, channels,
6275 wpa_s->best_5_freq)) {
6276 params->freq = wpa_s->best_5_freq;
6277 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6278 "channel %d MHz", params->freq);
6279 goto success;
6280 }
6281
6282 /* try using preferred channels */
6283 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6284 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6285 params->freq = cand;
6286 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6287 "channels", params->freq);
6288 goto success;
6289 }
6290
Roshan Pius3a1667e2018-07-03 15:17:14 -07006291 /* Try using a channel that allows VHT to be used with 80 MHz */
6292 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6293 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6294 enum hostapd_hw_mode mode;
6295 struct hostapd_hw_modes *hwmode;
6296 u8 chan;
6297
6298 cand = wpa_s->p2p_group_common_freqs[i];
6299 mode = ieee80211_freq_to_chan(cand, &chan);
6300 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6301 mode);
6302 if (!hwmode ||
6303 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6304 BW80) != ALLOWED)
6305 continue;
6306 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6307 params->freq = cand;
6308 wpa_printf(MSG_DEBUG,
6309 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6310 params->freq);
6311 goto success;
6312 }
6313 }
6314 }
6315
6316 /* Try using a channel that allows HT to be used with 40 MHz on the same
6317 * band so that CSA can be used */
6318 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6319 wpa_s->p2p_group_common_freqs) {
6320 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6321 enum hostapd_hw_mode mode;
6322 struct hostapd_hw_modes *hwmode;
6323 u8 chan;
6324
6325 cand = wpa_s->p2p_group_common_freqs[i];
6326 mode = ieee80211_freq_to_chan(cand, &chan);
6327 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6328 mode);
6329 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6330 cand) ||
6331 !hwmode ||
6332 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6333 BW40MINUS) != ALLOWED &&
6334 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6335 BW40PLUS) != ALLOWED))
6336 continue;
6337 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6338 params->freq = cand;
6339 wpa_printf(MSG_DEBUG,
6340 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6341 params->freq);
6342 goto success;
6343 }
6344 }
6345 }
6346
6347 /* Try using one of the group common freqs on the same band so that CSA
6348 * can be used */
6349 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6350 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6351 cand = wpa_s->p2p_group_common_freqs[i];
6352 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6353 cand))
6354 continue;
6355 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6356 params->freq = cand;
6357 wpa_printf(MSG_DEBUG,
6358 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6359 params->freq);
6360 goto success;
6361 }
6362 }
6363 }
6364
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006365 /* Try using one of the group common freqs */
6366 if (wpa_s->p2p_group_common_freqs) {
6367 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6368 cand = wpa_s->p2p_group_common_freqs[i];
6369 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6370 params->freq = cand;
6371 wpa_printf(MSG_DEBUG,
6372 "P2P: Use freq %d MHz common with the peer",
6373 params->freq);
6374 goto success;
6375 }
6376 }
6377 }
6378
6379 /* no preference, select some channel */
6380 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6381
6382 if (params->freq == 0) {
6383 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6384 goto fail;
6385 }
6386
6387success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006388 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006389 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006390fail:
6391 os_free(freqs);
6392 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393}
6394
6395
6396static struct wpa_supplicant *
6397wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6398 int go)
6399{
6400 struct wpa_supplicant *group_wpa_s;
6401
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006402 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006403 if (wpa_s->p2p_mgmt) {
6404 /*
6405 * We may be called on the p2p_dev interface which
6406 * cannot be used for group operations, so always use
6407 * the primary interface.
6408 */
6409 wpa_s->parent->p2pdev = wpa_s;
6410 wpa_s = wpa_s->parent;
6411 }
6412 wpa_dbg(wpa_s, MSG_DEBUG,
6413 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006414 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006415 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006416 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006418 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006419
6420 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006421 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006422 wpa_msg_global(wpa_s, MSG_ERROR,
6423 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006424 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006425 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006426 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6427 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006428 wpa_msg_global(wpa_s, MSG_ERROR,
6429 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006430 wpas_p2p_remove_pending_group_interface(wpa_s);
6431 return NULL;
6432 }
6433
Roshan Pius3a1667e2018-07-03 15:17:14 -07006434 if (go && wpa_s->p2p_go_do_acs) {
6435 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6436 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6437 wpa_s->p2p_go_do_acs = 0;
6438 }
6439
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006440 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6441 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006442 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006443 return group_wpa_s;
6444}
6445
6446
6447/**
6448 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6449 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6450 * @persistent_group: Whether to create a persistent group
6451 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006452 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006453 * @ht40: Start GO with 40 MHz channel width
6454 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006455 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006456 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006457 * Returns: 0 on success, -1 on failure
6458 *
6459 * This function creates a new P2P group with the local end as the Group Owner,
6460 * i.e., without using Group Owner Negotiation.
6461 */
6462int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006463 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006464 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006465{
6466 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467
6468 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6469 return -1;
6470
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006471 os_free(wpa_s->global->add_psk);
6472 wpa_s->global->add_psk = NULL;
6473
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006474 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006475 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006476 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006477
Roshan Pius3a1667e2018-07-03 15:17:14 -07006478 if (!wpa_s->p2p_go_do_acs) {
6479 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6480 if (freq < 0)
6481 return -1;
6482 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006484 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006485 ht40, vht, max_oper_chwidth, he, edmg,
6486 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006487 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006489 p2p_go_params(wpa_s->global->p2p, &params);
6490 params.persistent_group = persistent_group;
6491
6492 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6493 if (wpa_s == NULL)
6494 return -1;
6495 wpas_start_wps_go(wpa_s, &params, 0);
6496
6497 return 0;
6498}
6499
6500
6501static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006502 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006503 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006504{
6505 struct wpa_ssid *ssid;
6506
6507 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6508 if (wpa_s == NULL)
6509 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006510 if (force_scan)
6511 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006512 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006513
6514 wpa_supplicant_ap_deinit(wpa_s);
6515
6516 ssid = wpa_config_add_network(wpa_s->conf);
6517 if (ssid == NULL)
6518 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006519 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006520 wpa_config_set_network_defaults(ssid);
6521 ssid->temporary = 1;
6522 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006523 ssid->pbss = params->pbss;
6524 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6525 WPA_CIPHER_CCMP;
6526 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006527 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6528 ssid->ssid = os_malloc(params->ssid_len);
6529 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006530 wpa_config_remove_network(wpa_s->conf, ssid->id);
6531 return -1;
6532 }
6533 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6534 ssid->ssid_len = params->ssid_len;
6535 ssid->p2p_group = 1;
6536 ssid->export_keys = 1;
6537 if (params->psk_set) {
6538 os_memcpy(ssid->psk, params->psk, 32);
6539 ssid->psk_set = 1;
6540 }
6541 if (params->passphrase)
6542 ssid->passphrase = os_strdup(params->passphrase);
6543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006545 wpa_s->p2p_in_invitation = 1;
6546 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006547 wpa_s->p2p_go_group_formation_completed = 0;
6548 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006549
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006550 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006551 NULL);
6552 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6553 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006554 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006555 wpa_supplicant_select_network(wpa_s, ssid);
6556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006557 return 0;
6558}
6559
6560
6561int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6562 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006563 int force_freq, int neg_freq,
6564 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006565 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006566 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006567 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006568 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006569{
6570 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006571 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006572
6573 if (ssid->disabled != 2 || ssid->ssid == NULL)
6574 return -1;
6575
6576 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6577 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6578 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6579 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006580 if (go == 0 &&
6581 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006582 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006583 /*
6584 * This can happen if Invitation Response frame was lost
6585 * and the peer (GO of a persistent group) tries to
6586 * invite us again. Reschedule the timeout to avoid
6587 * terminating the wait for the connection too early
6588 * since we now know that the peer is still trying to
6589 * invite us instead of having already started the GO.
6590 */
6591 wpa_printf(MSG_DEBUG,
6592 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6593 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6594 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006595 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006596 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006597 return 0;
6598 }
6599
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006600 os_free(wpa_s->global->add_psk);
6601 wpa_s->global->add_psk = NULL;
6602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006603 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006604 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006605
Dmitry Shmidt04949592012-07-19 12:16:46 -07006606 wpa_s->p2p_fallback_to_go_neg = 0;
6607
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006608 if (ssid->mode == WPAS_MODE_P2P_GO) {
6609 if (force_freq > 0) {
6610 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6611 if (freq < 0)
6612 return -1;
6613 } else {
6614 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6615 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006616 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006617 freq = 0;
6618 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006619 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006620 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006621 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006622 struct os_reltime now;
6623 struct wpa_bss *bss =
6624 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006625
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006626 os_get_reltime(&now);
6627 if (bss &&
6628 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006629 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006630 freq = bss->freq;
6631 else
6632 freq = 0;
6633 }
6634
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006635 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6636 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006637 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006638 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006639 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006640
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006641 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006642 ht40, vht, max_oper_chwidth, he, edmg,
6643 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006644 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006645
6646 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006647 params.psk_set = ssid->psk_set;
6648 if (params.psk_set)
6649 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006650 if (ssid->passphrase) {
6651 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6652 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6653 "persistent group");
6654 return -1;
6655 }
6656 os_strlcpy(params.passphrase, ssid->passphrase,
6657 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006658 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006659 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6660 params.ssid_len = ssid->ssid_len;
6661 params.persistent_group = 1;
6662
6663 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6664 if (wpa_s == NULL)
6665 return -1;
6666
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006667 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6668
Dmitry Shmidt56052862013-10-04 10:23:25 -07006669 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006670 wpas_start_wps_go(wpa_s, &params, 0);
6671
6672 return 0;
6673}
6674
6675
6676static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6677 struct wpabuf *proberesp_ies)
6678{
6679 struct wpa_supplicant *wpa_s = ctx;
6680 if (wpa_s->ap_iface) {
6681 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006682 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6683 wpabuf_free(beacon_ies);
6684 wpabuf_free(proberesp_ies);
6685 return;
6686 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006687 if (beacon_ies) {
6688 wpabuf_free(hapd->p2p_beacon_ie);
6689 hapd->p2p_beacon_ie = beacon_ies;
6690 }
6691 wpabuf_free(hapd->p2p_probe_resp_ie);
6692 hapd->p2p_probe_resp_ie = proberesp_ies;
6693 } else {
6694 wpabuf_free(beacon_ies);
6695 wpabuf_free(proberesp_ies);
6696 }
6697 wpa_supplicant_ap_update_beacon(wpa_s);
6698}
6699
6700
6701static void wpas_p2p_idle_update(void *ctx, int idle)
6702{
6703 struct wpa_supplicant *wpa_s = ctx;
6704 if (!wpa_s->ap_iface)
6705 return;
6706 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006707 if (idle) {
6708 if (wpa_s->global->p2p_fail_on_wps_complete &&
6709 wpa_s->p2p_in_provisioning) {
6710 wpas_p2p_grpform_fail_after_wps(wpa_s);
6711 return;
6712 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006713 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006714 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006715 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6716}
6717
6718
6719struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006720 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006721{
6722 struct p2p_group *group;
6723 struct p2p_group_config *cfg;
6724
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006725 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6726 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727 return NULL;
6728
6729 cfg = os_zalloc(sizeof(*cfg));
6730 if (cfg == NULL)
6731 return NULL;
6732
Dmitry Shmidt04949592012-07-19 12:16:46 -07006733 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006734 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006735 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006736 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006737 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6738 if (wpa_s->max_stations &&
6739 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6740 cfg->max_clients = wpa_s->max_stations;
6741 else
6742 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006743 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6744 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006745 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006746 cfg->cb_ctx = wpa_s;
6747 cfg->ie_update = wpas_p2p_ie_update;
6748 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006749 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6750 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751
6752 group = p2p_group_init(wpa_s->global->p2p, cfg);
6753 if (group == NULL)
6754 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006755 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006756 p2p_group_notif_formation_done(group);
6757 wpa_s->p2p_group = group;
6758 return group;
6759}
6760
6761
6762void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6763 int registrar)
6764{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006765 struct wpa_ssid *ssid = wpa_s->current_ssid;
6766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767 if (!wpa_s->p2p_in_provisioning) {
6768 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6769 "provisioning not in progress");
6770 return;
6771 }
6772
Dmitry Shmidt04949592012-07-19 12:16:46 -07006773 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6774 u8 go_dev_addr[ETH_ALEN];
6775 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6776 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6777 ssid->ssid_len);
6778 /* Clear any stored provisioning info */
6779 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6780 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006781
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006782 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006783 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006784 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006785 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6786 /*
6787 * Use a separate timeout for initial data connection to
6788 * complete to allow the group to be removed automatically if
6789 * something goes wrong in this step before the P2P group idle
6790 * timeout mechanism is taken into use.
6791 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006792 wpa_dbg(wpa_s, MSG_DEBUG,
6793 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6794 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006795 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6796 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006797 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006798 /* Complete group formation on successful data connection. */
6799 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006800 } else if (ssid) {
6801 /*
6802 * Use a separate timeout for initial data connection to
6803 * complete to allow the group to be removed automatically if
6804 * the client does not complete data connection successfully.
6805 */
6806 wpa_dbg(wpa_s, MSG_DEBUG,
6807 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6808 P2P_MAX_INITIAL_CONN_WAIT_GO);
6809 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6810 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006811 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006812 /*
6813 * Complete group formation on first successful data connection
6814 */
6815 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006817 if (wpa_s->global->p2p)
6818 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006819 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006820}
6821
6822
Jouni Malinen75ecf522011-06-27 15:19:46 -07006823void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6824 struct wps_event_fail *fail)
6825{
6826 if (!wpa_s->p2p_in_provisioning) {
6827 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6828 "provisioning not in progress");
6829 return;
6830 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006831
6832 if (wpa_s->go_params) {
6833 p2p_clear_provisioning_info(
6834 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006835 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006836 }
6837
Jouni Malinen75ecf522011-06-27 15:19:46 -07006838 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006839
6840 if (wpa_s == wpa_s->global->p2p_group_formation) {
6841 /*
6842 * Allow some time for the failed WPS negotiation exchange to
6843 * complete, but remove the group since group formation cannot
6844 * succeed after provisioning failure.
6845 */
6846 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6847 wpa_s->global->p2p_fail_on_wps_complete = 1;
6848 eloop_deplete_timeout(0, 50000,
6849 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006850 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006851 }
6852}
6853
6854
6855int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6856{
6857 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6858 !wpa_s->p2p_in_provisioning)
6859 return 0;
6860
6861 wpas_p2p_grpform_fail_after_wps(wpa_s);
6862
6863 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006864}
6865
6866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006867int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006868 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006869 enum wpas_p2p_prov_disc_use use,
6870 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006871{
6872 u16 config_methods;
6873
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006874 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006875 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006876 wpa_s->p2p_fallback_to_go_neg = 0;
6877 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006878 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6879 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006880 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6881 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6882
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006883 wpa_printf(MSG_DEBUG,
6884 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6885 __func__, p2ps_prov->conncap,
6886 p2ps_prov->adv_id, p2ps_prov->conncap,
6887 p2ps_prov->status, p2ps_prov->info);
6888
6889 config_methods = 0;
6890 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006891 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006892 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006893 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006894 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6895 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006896 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006897 else {
6898 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006899 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006901 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006902
Dmitry Shmidt04949592012-07-19 12:16:46 -07006903 if (use == WPAS_P2P_PD_AUTO) {
6904 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6905 wpa_s->pending_pd_config_methods = config_methods;
6906 wpa_s->p2p_auto_pd = 1;
6907 wpa_s->p2p_auto_join = 0;
6908 wpa_s->pending_pd_before_join = 0;
6909 wpa_s->auto_pd_scan_retry = 0;
6910 wpas_p2p_stop_find(wpa_s);
6911 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006912 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006913 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6914 wpa_s->p2p_auto_started.sec,
6915 wpa_s->p2p_auto_started.usec);
6916 wpas_p2p_join_scan(wpa_s, NULL);
6917 return 0;
6918 }
6919
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006920 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6921 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006922 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006925 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006926 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006927 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928}
6929
6930
6931int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6932 char *end)
6933{
6934 return p2p_scan_result_text(ies, ies_len, buf, end);
6935}
6936
6937
6938static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6939{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006940 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941 return;
6942
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006943 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006944 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006945 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6946 wpa_s, NULL);
6947 offchannel_send_action_done(wpa_s);
6948 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006950 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6951 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006952 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006953}
6954
6955
6956int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6957 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006958 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006959 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006960 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961{
6962 wpas_p2p_clear_pending_action_tx(wpa_s);
6963 wpa_s->p2p_long_listen = 0;
6964
Dmitry Shmidt04949592012-07-19 12:16:46 -07006965 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006966 wpa_s->p2p_in_provisioning) {
6967 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6968 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6969 " (P2P disabled)" : "",
6970 wpa_s->p2p_in_provisioning ?
6971 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006973 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006975 wpa_supplicant_cancel_sched_scan(wpa_s);
6976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006977 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006978 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006979 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006980}
6981
6982
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006983static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6984 struct wpa_scan_results *scan_res)
6985{
6986 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6987
6988 if (wpa_s->p2p_scan_work) {
6989 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6990 wpa_s->p2p_scan_work = NULL;
6991 radio_work_done(work);
6992 }
6993
6994 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6995 return;
6996
6997 /*
6998 * Indicate that results have been processed so that the P2P module can
6999 * continue pending tasks.
7000 */
7001 p2p_scan_res_handled(wpa_s->global->p2p);
7002}
7003
7004
7005static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007006{
7007 wpas_p2p_clear_pending_action_tx(wpa_s);
7008 wpa_s->p2p_long_listen = 0;
7009 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7010 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011
7012 if (wpa_s->global->p2p)
7013 p2p_stop_find(wpa_s->global->p2p);
7014
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007015 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7016 wpa_printf(MSG_DEBUG,
7017 "P2P: Do not consider the scan results after stop_find");
7018 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7019 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007020}
7021
7022
7023void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7024{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007025 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007026 if (!wpa_s->global->pending_group_iface_for_p2ps)
7027 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007028}
7029
7030
7031static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7032{
7033 struct wpa_supplicant *wpa_s = eloop_ctx;
7034 wpa_s->p2p_long_listen = 0;
7035}
7036
7037
7038int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7039{
7040 int res;
7041
7042 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7043 return -1;
7044
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007045 if (wpa_s->p2p_lo_started) {
7046 wpa_printf(MSG_DEBUG,
7047 "P2P: Cannot start P2P listen, it is offloaded");
7048 return -1;
7049 }
7050
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007051 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007052 wpas_p2p_clear_pending_action_tx(wpa_s);
7053
7054 if (timeout == 0) {
7055 /*
7056 * This is a request for unlimited Listen state. However, at
7057 * least for now, this is mapped to a Listen state for one
7058 * hour.
7059 */
7060 timeout = 3600;
7061 }
7062 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7063 wpa_s->p2p_long_listen = 0;
7064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007065 /*
7066 * Stop previous find/listen operation to avoid trying to request a new
7067 * remain-on-channel operation while the driver is still running the
7068 * previous one.
7069 */
7070 if (wpa_s->global->p2p)
7071 p2p_stop_find(wpa_s->global->p2p);
7072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007073 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7074 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
7075 wpa_s->p2p_long_listen = timeout * 1000;
7076 eloop_register_timeout(timeout, 0,
7077 wpas_p2p_long_listen_timeout,
7078 wpa_s, NULL);
7079 }
7080
7081 return res;
7082}
7083
7084
7085int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7086 u8 *buf, size_t len, int p2p_group)
7087{
7088 struct wpabuf *p2p_ie;
7089 int ret;
7090
7091 if (wpa_s->global->p2p_disabled)
7092 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007093 /*
7094 * Advertize mandatory cross connection capability even on
7095 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7096 */
7097 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007098 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007099 if (wpa_s->global->p2p == NULL)
7100 return -1;
7101 if (bss == NULL)
7102 return -1;
7103
7104 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7105 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7106 p2p_group, p2p_ie);
7107 wpabuf_free(p2p_ie);
7108
7109 return ret;
7110}
7111
7112
7113int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007114 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007115 const u8 *ie, size_t ie_len,
7116 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007117{
7118 if (wpa_s->global->p2p_disabled)
7119 return 0;
7120 if (wpa_s->global->p2p == NULL)
7121 return 0;
7122
Dmitry Shmidt04949592012-07-19 12:16:46 -07007123 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007124 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007125 case P2P_PREQ_NOT_P2P:
7126 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7127 ssi_signal);
7128 /* fall through */
7129 case P2P_PREQ_MALFORMED:
7130 case P2P_PREQ_NOT_LISTEN:
7131 case P2P_PREQ_NOT_PROCESSED:
7132 default: /* make gcc happy */
7133 return 0;
7134 case P2P_PREQ_PROCESSED:
7135 return 1;
7136 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137}
7138
7139
7140void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7141 const u8 *sa, const u8 *bssid,
7142 u8 category, const u8 *data, size_t len, int freq)
7143{
7144 if (wpa_s->global->p2p_disabled)
7145 return;
7146 if (wpa_s->global->p2p == NULL)
7147 return;
7148
7149 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7150 freq);
7151}
7152
7153
7154void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7155{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007156 unsigned int bands;
7157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158 if (wpa_s->global->p2p_disabled)
7159 return;
7160 if (wpa_s->global->p2p == NULL)
7161 return;
7162
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007163 bands = wpas_get_bands(wpa_s, NULL);
7164 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165}
7166
7167
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007168static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007169{
7170 p2p_group_deinit(wpa_s->p2p_group);
7171 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007172
7173 wpa_s->ap_configured_cb = NULL;
7174 wpa_s->ap_configured_cb_ctx = NULL;
7175 wpa_s->ap_configured_cb_data = NULL;
7176 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007177}
7178
7179
7180int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7181{
7182 wpa_s->p2p_long_listen = 0;
7183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007184 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7185 return -1;
7186
7187 return p2p_reject(wpa_s->global->p2p, addr);
7188}
7189
7190
7191/* Invite to reinvoke a persistent group */
7192int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007193 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007194 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007195 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007196{
7197 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007198 u8 *bssid = NULL;
7199 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007200 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007201 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007202 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007203
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007204 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007205 if (peer_addr)
7206 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7207 else
7208 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209
Jouni Malinen31be0a42012-08-31 21:20:51 +03007210 wpa_s->p2p_persistent_go_freq = freq;
7211 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007212 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007213 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007214 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7215 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007216 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007217 if (ssid->mode == WPAS_MODE_P2P_GO) {
7218 role = P2P_INVITE_ROLE_GO;
7219 if (peer_addr == NULL) {
7220 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7221 "address in invitation command");
7222 return -1;
7223 }
7224 if (wpas_p2p_create_iface(wpa_s)) {
7225 if (wpas_p2p_add_group_interface(wpa_s,
7226 WPA_IF_P2P_GO) < 0) {
7227 wpa_printf(MSG_ERROR, "P2P: Failed to "
7228 "allocate a new interface for the "
7229 "group");
7230 return -1;
7231 }
7232 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007233 } else if (wpa_s->p2p_mgmt)
7234 bssid = wpa_s->parent->own_addr;
7235 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007236 bssid = wpa_s->own_addr;
7237 } else {
7238 role = P2P_INVITE_ROLE_CLIENT;
7239 peer_addr = ssid->bssid;
7240 }
7241 wpa_s->pending_invite_ssid_id = ssid->id;
7242
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007243 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007244 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007245 role == P2P_INVITE_ROLE_GO,
7246 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007247 if (res)
7248 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007250 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7251 return -1;
7252
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007253 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7254
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007255 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7256 no_pref_freq_given && pref_freq > 0 &&
7257 wpa_s->num_multichan_concurrent > 1 &&
7258 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7259 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7260 pref_freq);
7261 pref_freq = 0;
7262 }
7263
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007264 /*
7265 * Stop any find/listen operations before invitation and possibly
7266 * connection establishment.
7267 */
7268 wpas_p2p_stop_find_oper(wpa_s);
7269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007270 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007271 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007272 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007273}
7274
7275
7276/* Invite to join an active group */
7277int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7278 const u8 *peer_addr, const u8 *go_dev_addr)
7279{
7280 struct wpa_global *global = wpa_s->global;
7281 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007282 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007283 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007284 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007285 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007286 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007287 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007288
Jouni Malinen31be0a42012-08-31 21:20:51 +03007289 wpa_s->p2p_persistent_go_freq = 0;
7290 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007291 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007292 wpa_s->p2p_go_vht_center_freq2 = 0;
7293 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007294 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007296 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7297 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7298 break;
7299 }
7300 if (wpa_s == NULL) {
7301 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7302 return -1;
7303 }
7304
7305 ssid = wpa_s->current_ssid;
7306 if (ssid == NULL) {
7307 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7308 "invitation");
7309 return -1;
7310 }
7311
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007312 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007313 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007314 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007315 ssid->ssid, ssid->ssid_len);
7316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007317 if (ssid->mode == WPAS_MODE_P2P_GO) {
7318 role = P2P_INVITE_ROLE_ACTIVE_GO;
7319 bssid = wpa_s->own_addr;
7320 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007321 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007322 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323 } else {
7324 role = P2P_INVITE_ROLE_CLIENT;
7325 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7326 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7327 "invite to current group");
7328 return -1;
7329 }
7330 bssid = wpa_s->bssid;
7331 if (go_dev_addr == NULL &&
7332 !is_zero_ether_addr(wpa_s->go_dev_addr))
7333 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007334 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7335 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007336 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007337 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007338
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007339 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7340 return -1;
7341
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007342 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007343 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007344 role == P2P_INVITE_ROLE_ACTIVE_GO,
7345 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007346 if (res)
7347 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007348 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007351 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007352 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007353}
7354
7355
7356void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7357{
7358 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359 u8 go_dev_addr[ETH_ALEN];
7360 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007361 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007362 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007363 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007364
Dmitry Shmidt04949592012-07-19 12:16:46 -07007365 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7366 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007367 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007368 }
7369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007370 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007371 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007372
7373 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007374 if (!wpa_s->p2p_go_group_formation_completed &&
7375 wpa_s->global->p2p_group_formation == wpa_s) {
7376 wpa_dbg(wpa_s, MSG_DEBUG,
7377 "P2P: Marking group formation completed on client on data connection");
7378 wpa_s->p2p_go_group_formation_completed = 1;
7379 wpa_s->global->p2p_group_formation = NULL;
7380 wpa_s->p2p_in_provisioning = 0;
7381 wpa_s->p2p_in_invitation = 0;
7382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007384 os_memset(go_dev_addr, 0, ETH_ALEN);
7385 if (ssid->bssid_set)
7386 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7387 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7388 ssid->ssid_len);
7389 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7390
7391 if (wpa_s->global->p2p_group_formation == wpa_s)
7392 wpa_s->global->p2p_group_formation = NULL;
7393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007394 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7395 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007396
7397 ip_addr[0] = '\0';
7398 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007399 int res;
7400
7401 res = os_snprintf(ip_addr, sizeof(ip_addr),
7402 " ip_addr=%u.%u.%u.%u "
7403 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7404 ip[0], ip[1], ip[2], ip[3],
7405 ip[4], ip[5], ip[6], ip[7],
7406 ip[8], ip[9], ip[10], ip[11]);
7407 if (os_snprintf_error(sizeof(ip_addr), res))
7408 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007409 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007410 }
7411
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007412 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7413 ssid->passphrase == NULL && ssid->psk_set ?
7414 ssid->psk : NULL,
7415 ssid->passphrase, go_dev_addr, persistent,
7416 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007417
7418 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007419 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7420 ssid, go_dev_addr);
7421
Hai Shalom5f92bc92019-04-18 11:54:11 -07007422 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007423}
7424
7425
7426int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7427 u32 interval1, u32 duration2, u32 interval2)
7428{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007429 int ret;
7430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7432 return -1;
7433
7434 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7435 wpa_s->current_ssid == NULL ||
7436 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7437 return -1;
7438
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007439 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7440 wpa_s->own_addr, wpa_s->assoc_freq,
7441 duration1, interval1, duration2, interval2);
7442 if (ret == 0)
7443 wpa_s->waiting_presence_resp = 1;
7444
7445 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007446}
7447
7448
7449int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7450 unsigned int interval)
7451{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007452 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7453 return -1;
7454
7455 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7456}
7457
7458
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007459static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7460{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007461 if (wpa_s->current_ssid == NULL) {
7462 /*
7463 * current_ssid can be cleared when P2P client interface gets
7464 * disconnected, so assume this interface was used as P2P
7465 * client.
7466 */
7467 return 1;
7468 }
7469 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007470 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7471}
7472
7473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7475{
7476 struct wpa_supplicant *wpa_s = eloop_ctx;
7477
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007478 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007479 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7480 "disabled");
7481 return;
7482 }
7483
Dmitry Shmidt04949592012-07-19 12:16:46 -07007484 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7485 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007486 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007487}
7488
7489
7490static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7491{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007492 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007493
Dmitry Shmidt04949592012-07-19 12:16:46 -07007494 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7495 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007497 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7498 return;
7499
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007500 timeout = wpa_s->conf->p2p_group_idle;
7501 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7502 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7503 timeout = P2P_MAX_CLIENT_IDLE;
7504
7505 if (timeout == 0)
7506 return;
7507
Dmitry Shmidt04949592012-07-19 12:16:46 -07007508 if (timeout < 0) {
7509 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7510 timeout = 0; /* special client mode no-timeout */
7511 else
7512 return;
7513 }
7514
7515 if (wpa_s->p2p_in_provisioning) {
7516 /*
7517 * Use the normal group formation timeout during the
7518 * provisioning phase to avoid terminating this process too
7519 * early due to group idle timeout.
7520 */
7521 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7522 "during provisioning");
7523 return;
7524 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307525
Dmitry Shmidt04949592012-07-19 12:16:46 -07007526 if (wpa_s->show_group_started) {
7527 /*
7528 * Use the normal group formation timeout between the end of
7529 * the provisioning phase and completion of 4-way handshake to
7530 * avoid terminating this process too early due to group idle
7531 * timeout.
7532 */
7533 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7534 "while waiting for initial 4-way handshake to "
7535 "complete");
7536 return;
7537 }
7538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007539 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007540 timeout);
7541 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7542 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543}
7544
7545
Jouni Malinen2b89da82012-08-31 22:04:41 +03007546/* Returns 1 if the interface was removed */
7547int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7548 u16 reason_code, const u8 *ie, size_t ie_len,
7549 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550{
7551 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007552 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553
Dmitry Shmidt04949592012-07-19 12:16:46 -07007554 if (!locally_generated)
7555 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7556 ie_len);
7557
7558 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7559 wpa_s->current_ssid &&
7560 wpa_s->current_ssid->p2p_group &&
7561 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7562 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7563 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007564 if (wpas_p2p_group_delete(wpa_s,
7565 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7566 > 0)
7567 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007568 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007569
7570 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007571}
7572
7573
7574void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007575 u16 reason_code, const u8 *ie, size_t ie_len,
7576 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577{
7578 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7579 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007580
Dmitry Shmidt04949592012-07-19 12:16:46 -07007581 if (!locally_generated)
7582 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7583 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007584}
7585
7586
7587void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7588{
7589 struct p2p_data *p2p = wpa_s->global->p2p;
7590
7591 if (p2p == NULL)
7592 return;
7593
7594 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7595 return;
7596
7597 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7598 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7599
7600 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7601 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7602
7603 if (wpa_s->wps &&
7604 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7605 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7606
7607 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7608 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7609
7610 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7611 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7612 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7613 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7614 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7615 }
7616
7617 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7618 p2p_set_sec_dev_types(p2p,
7619 (void *) wpa_s->conf->sec_device_type,
7620 wpa_s->conf->num_sec_device_types);
7621
7622 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7623 int i;
7624 p2p_remove_wps_vendor_extensions(p2p);
7625 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7626 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7627 continue;
7628 p2p_add_wps_vendor_extension(
7629 p2p, wpa_s->conf->wps_vendor_ext[i]);
7630 }
7631 }
7632
7633 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7634 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7635 char country[3];
7636 country[0] = wpa_s->conf->country[0];
7637 country[1] = wpa_s->conf->country[1];
7638 country[2] = 0x04;
7639 p2p_set_country(p2p, country);
7640 }
7641
7642 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7643 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7644 wpa_s->conf->p2p_ssid_postfix ?
7645 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7646 0);
7647 }
7648
7649 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7650 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007651
7652 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7653 u8 reg_class, channel;
7654 int ret;
7655 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007656 u8 channel_forced;
7657
Jouni Malinen75ecf522011-06-27 15:19:46 -07007658 if (wpa_s->conf->p2p_listen_reg_class &&
7659 wpa_s->conf->p2p_listen_channel) {
7660 reg_class = wpa_s->conf->p2p_listen_reg_class;
7661 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007662 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007663 } else {
7664 reg_class = 81;
7665 /*
7666 * Pick one of the social channels randomly as the
7667 * listen channel.
7668 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007669 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7670 channel = 1;
7671 else
7672 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007673 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007674 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007675 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7676 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007677 if (ret)
7678 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7679 "failed: %d", ret);
7680 }
7681 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7682 u8 op_reg_class, op_channel, cfg_op_channel;
7683 int ret = 0;
7684 unsigned int r;
7685 if (wpa_s->conf->p2p_oper_reg_class &&
7686 wpa_s->conf->p2p_oper_channel) {
7687 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7688 op_channel = wpa_s->conf->p2p_oper_channel;
7689 cfg_op_channel = 1;
7690 } else {
7691 op_reg_class = 81;
7692 /*
7693 * Use random operation channel from (1, 6, 11)
7694 *if no other preference is indicated.
7695 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007696 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7697 op_channel = 1;
7698 else
7699 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007700 cfg_op_channel = 0;
7701 }
7702 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7703 cfg_op_channel);
7704 if (ret)
7705 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7706 "failed: %d", ret);
7707 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007708
7709 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7710 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7711 wpa_s->conf->p2p_pref_chan) < 0) {
7712 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7713 "update failed");
7714 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007715
7716 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7717 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7718 "update failed");
7719 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007720 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007721
7722 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7723 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007724}
7725
7726
7727int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7728 int duration)
7729{
7730 if (!wpa_s->ap_iface)
7731 return -1;
7732 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7733 duration);
7734}
7735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736
7737int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7738{
7739 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7740 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007741
7742 wpa_s->global->cross_connection = enabled;
7743 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7744
7745 if (!enabled) {
7746 struct wpa_supplicant *iface;
7747
7748 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7749 {
7750 if (iface->cross_connect_enabled == 0)
7751 continue;
7752
7753 iface->cross_connect_enabled = 0;
7754 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007755 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007756 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7757 iface->ifname,
7758 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007759 }
7760 }
7761
7762 return 0;
7763}
7764
7765
7766static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7767{
7768 struct wpa_supplicant *iface;
7769
7770 if (!uplink->global->cross_connection)
7771 return;
7772
7773 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7774 if (!iface->cross_connect_enabled)
7775 continue;
7776 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7777 0)
7778 continue;
7779 if (iface->ap_iface == NULL)
7780 continue;
7781 if (iface->cross_connect_in_use)
7782 continue;
7783
7784 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007785 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007786 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7787 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007788 }
7789}
7790
7791
7792static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7793{
7794 struct wpa_supplicant *iface;
7795
7796 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7797 if (!iface->cross_connect_enabled)
7798 continue;
7799 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7800 0)
7801 continue;
7802 if (!iface->cross_connect_in_use)
7803 continue;
7804
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007805 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007806 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7807 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007808 iface->cross_connect_in_use = 0;
7809 }
7810}
7811
7812
7813void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7814{
7815 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7816 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7817 wpa_s->cross_connect_disallowed)
7818 wpas_p2p_disable_cross_connect(wpa_s);
7819 else
7820 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007821 if (!wpa_s->ap_iface &&
7822 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7823 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007824}
7825
7826
7827void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7828{
7829 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007830 if (!wpa_s->ap_iface &&
7831 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7832 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007833 wpas_p2p_set_group_idle_timeout(wpa_s);
7834}
7835
7836
7837static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7838{
7839 struct wpa_supplicant *iface;
7840
7841 if (!wpa_s->global->cross_connection)
7842 return;
7843
7844 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7845 if (iface == wpa_s)
7846 continue;
7847 if (iface->drv_flags &
7848 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7849 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007850 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7851 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852 continue;
7853
7854 wpa_s->cross_connect_enabled = 1;
7855 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7856 sizeof(wpa_s->cross_connect_uplink));
7857 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7858 "%s to %s whenever uplink is available",
7859 wpa_s->ifname, wpa_s->cross_connect_uplink);
7860
7861 if (iface->ap_iface || iface->current_ssid == NULL ||
7862 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7863 iface->cross_connect_disallowed ||
7864 iface->wpa_state != WPA_COMPLETED)
7865 break;
7866
7867 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007868 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007869 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7870 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007871 break;
7872 }
7873}
7874
7875
7876int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7877{
7878 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7879 !wpa_s->p2p_in_provisioning)
7880 return 0; /* not P2P client operation */
7881
7882 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7883 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007884 if (wpa_s != wpa_s->p2pdev)
7885 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007886 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007887 return 1;
7888}
7889
7890
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007891void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7892{
7893 struct wpa_supplicant *wpa_s = eloop_ctx;
7894 wpas_p2p_notif_pbc_overlap(wpa_s);
7895}
7896
7897
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007898void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7899 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007900{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007901 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007902 struct wpa_used_freq_data *freqs = NULL;
7903 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007904
7905 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7906 return;
7907
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007908 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7909 if (!freqs)
7910 return;
7911
7912 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007914 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007915 os_memset(&cli_chan, 0, sizeof(cli_chan));
7916 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007917 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7918 "channel list");
7919 return;
7920 }
7921
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007922 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007923
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007924 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007925
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007926 /*
7927 * The used frequencies map changed, so it is possible that a GO is
7928 * using a channel that is no longer valid for P2P use. It is also
7929 * possible that due to policy consideration, it would be preferable to
7930 * move it to a frequency already used by other station interfaces.
7931 */
7932 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7933
7934 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007935}
7936
7937
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007938static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7939 struct wpa_scan_results *scan_res)
7940{
7941 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7942}
7943
7944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007945int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7946{
7947 struct wpa_global *global = wpa_s->global;
7948 int found = 0;
7949 const u8 *peer;
7950
7951 if (global->p2p == NULL)
7952 return -1;
7953
7954 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7955
7956 if (wpa_s->pending_interface_name[0] &&
7957 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7958 found = 1;
7959
7960 peer = p2p_get_go_neg_peer(global->p2p);
7961 if (peer) {
7962 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7963 MACSTR, MAC2STR(peer));
7964 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007965 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007966 }
7967
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007968 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7969 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7970 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7971 found = 1;
7972 }
7973
7974 if (wpa_s->pending_pd_before_join) {
7975 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7976 wpa_s->pending_pd_before_join = 0;
7977 found = 1;
7978 }
7979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007980 wpas_p2p_stop_find(wpa_s);
7981
7982 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7983 if (wpa_s == global->p2p_group_formation &&
7984 (wpa_s->p2p_in_provisioning ||
7985 wpa_s->parent->pending_interface_type ==
7986 WPA_IF_P2P_CLIENT)) {
7987 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7988 "formation found - cancelling",
7989 wpa_s->ifname);
7990 found = 1;
7991 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007992 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007993 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007994 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007995 break;
7996 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007997 wpas_p2p_group_delete(wpa_s,
7998 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007999 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008000 } else if (wpa_s->p2p_in_invitation) {
8001 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8002 wpa_s->ifname);
8003 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008004 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008005 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008006 }
8007 }
8008
8009 if (!found) {
8010 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8011 return -1;
8012 }
8013
8014 return 0;
8015}
8016
8017
8018void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8019{
8020 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8021 return;
8022
8023 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8024 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008025 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008026}
8027
8028
8029void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8030 int freq_24, int freq_5, int freq_overall)
8031{
8032 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008033 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008034 return;
8035 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8036}
8037
8038
8039int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8040{
8041 u8 peer[ETH_ALEN];
8042 struct p2p_data *p2p = wpa_s->global->p2p;
8043
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008044 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008045 return -1;
8046
8047 if (hwaddr_aton(addr, peer))
8048 return -1;
8049
8050 return p2p_unauthorize(p2p, peer);
8051}
8052
8053
8054/**
8055 * wpas_p2p_disconnect - Disconnect from a P2P Group
8056 * @wpa_s: Pointer to wpa_supplicant data
8057 * Returns: 0 on success, -1 on failure
8058 *
8059 * This can be used to disconnect from a group in which the local end is a P2P
8060 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8061 * virtual network interface was created for this group, that interface will be
8062 * removed. Otherwise, only the configured P2P group network will be removed
8063 * from the interface.
8064 */
8065int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8066{
8067
8068 if (wpa_s == NULL)
8069 return -1;
8070
Jouni Malinen2b89da82012-08-31 22:04:41 +03008071 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8072 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008073}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008074
8075
8076int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8077{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008078 int ret;
8079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008080 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8081 return 0;
8082
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008083 ret = p2p_in_progress(wpa_s->global->p2p);
8084 if (ret == 0) {
8085 /*
8086 * Check whether there is an ongoing WPS provisioning step (or
8087 * other parts of group formation) on another interface since
8088 * p2p_in_progress() does not report this to avoid issues for
8089 * scans during such provisioning step.
8090 */
8091 if (wpa_s->global->p2p_group_formation &&
8092 wpa_s->global->p2p_group_formation != wpa_s) {
8093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8094 "in group formation",
8095 wpa_s->global->p2p_group_formation->ifname);
8096 ret = 1;
8097 }
8098 }
8099
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008100 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008101 struct os_reltime now;
8102 os_get_reltime(&now);
8103 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8104 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008105 /* Wait for the first client has expired */
8106 wpa_s->global->p2p_go_wait_client.sec = 0;
8107 } else {
8108 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8109 ret = 1;
8110 }
8111 }
8112
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008113 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008114}
8115
8116
8117void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8118 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008119{
8120 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8121 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008122 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008123 /**
8124 * Remove the network by scheduling the group formation
8125 * timeout to happen immediately. The teardown code
8126 * needs to be scheduled to run asynch later so that we
8127 * don't delete data from under ourselves unexpectedly.
8128 * Calling wpas_p2p_group_formation_timeout directly
8129 * causes a series of crashes in WPS failure scenarios.
8130 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008131 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8132 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008133 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008134 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008135 }
8136}
8137
8138
8139struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008140 const u8 *addr, const u8 *ssid,
8141 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008142{
8143 struct wpa_ssid *s;
8144 size_t i;
8145
8146 for (s = wpa_s->conf->ssid; s; s = s->next) {
8147 if (s->disabled != 2)
8148 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008149 if (ssid &&
8150 (ssid_len != s->ssid_len ||
8151 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8152 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008153 if (addr == NULL) {
8154 if (s->mode == WPAS_MODE_P2P_GO)
8155 return s;
8156 continue;
8157 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008158 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8159 return s; /* peer is GO in the persistent group */
8160 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8161 continue;
8162 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008163 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008164 addr, ETH_ALEN) == 0)
8165 return s; /* peer is P2P client in persistent
8166 * group */
8167 }
8168 }
8169
8170 return NULL;
8171}
8172
8173
8174void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8175 const u8 *addr)
8176{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008177 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008178 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008179 /*
8180 * This can happen if WPS provisioning step is not terminated
8181 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8182 * peer was able to connect, there is no need to time out group
8183 * formation after this, though. In addition, this is used with
8184 * the initial connection wait on the GO as a separate formation
8185 * timeout and as such, expected to be hit after the initial WPS
8186 * provisioning step.
8187 */
8188 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008189
8190 if (!wpa_s->p2p_go_group_formation_completed &&
8191 !wpa_s->group_formation_reported) {
8192 /*
8193 * GO has not yet notified group formation success since
8194 * the WPS step was not completed cleanly. Do that
8195 * notification now since the P2P Client was able to
8196 * connect and as such, must have received the
8197 * credential from the WPS step.
8198 */
8199 if (wpa_s->global->p2p)
8200 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008201 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008202 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008203 }
8204 if (!wpa_s->p2p_go_group_formation_completed) {
8205 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8206 wpa_s->p2p_go_group_formation_completed = 1;
8207 wpa_s->global->p2p_group_formation = NULL;
8208 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008209 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008210 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008211 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008212 if (addr == NULL)
8213 return;
8214 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8215}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008216
Dmitry Shmidt04949592012-07-19 12:16:46 -07008217
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008218static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8219 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008220{
8221 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008222 int ret = 0;
8223
Dmitry Shmidt04949592012-07-19 12:16:46 -07008224 if (wpa_s->global->p2p_group_formation)
8225 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008226 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008227 offchannel_send_action_done(wpa_s);
8228 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008229 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008230 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8231 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8232 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8233 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008234 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008235 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008236 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008237 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008238 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008239 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008240 wpa_s->p2p_go_he,
8241 wpa_s->p2p_go_edmg,
8242 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008243 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008244}
8245
8246
8247int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8248{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008249 int res;
8250
Dmitry Shmidt04949592012-07-19 12:16:46 -07008251 if (!wpa_s->p2p_fallback_to_go_neg ||
8252 wpa_s->p2p_in_provisioning <= 5)
8253 return 0;
8254
8255 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8256 return 0; /* peer operating as a GO */
8257
8258 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8259 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008260 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008261 "reason=GO-not-found");
8262 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008263
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008264 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008265}
8266
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008267
8268unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8269{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008270 struct wpa_supplicant *ifs;
8271
8272 if (wpa_s->wpa_state > WPA_SCANNING) {
8273 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8274 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008275 wpa_s->conf->p2p_search_delay);
8276 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008277 }
8278
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008279 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8280 radio_list) {
8281 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008282 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8283 "delay due to concurrent operation on "
8284 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008285 wpa_s->conf->p2p_search_delay,
8286 ifs->ifname);
8287 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008288 }
8289 }
8290
8291 return 0;
8292}
8293
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008294
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008295static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8296 struct wpa_ssid *s, const u8 *addr,
8297 int iface_addr)
8298{
8299 struct psk_list_entry *psk, *tmp;
8300 int changed = 0;
8301
8302 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8303 list) {
8304 if ((iface_addr && !psk->p2p &&
8305 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8306 (!iface_addr && psk->p2p &&
8307 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8308 wpa_dbg(wpa_s, MSG_DEBUG,
8309 "P2P: Remove persistent group PSK list entry for "
8310 MACSTR " p2p=%u",
8311 MAC2STR(psk->addr), psk->p2p);
8312 dl_list_del(&psk->list);
8313 os_free(psk);
8314 changed++;
8315 }
8316 }
8317
8318 return changed;
8319}
8320
8321
8322void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8323 const u8 *p2p_dev_addr,
8324 const u8 *psk, size_t psk_len)
8325{
8326 struct wpa_ssid *ssid = wpa_s->current_ssid;
8327 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008328 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008329
8330 if (psk_len != sizeof(p->psk))
8331 return;
8332
8333 if (p2p_dev_addr) {
8334 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8335 " p2p_dev_addr=" MACSTR,
8336 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8337 if (is_zero_ether_addr(p2p_dev_addr))
8338 p2p_dev_addr = NULL;
8339 } else {
8340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8341 MAC2STR(mac_addr));
8342 }
8343
8344 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8345 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8346 /* To be added to persistent group once created */
8347 if (wpa_s->global->add_psk == NULL) {
8348 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8349 if (wpa_s->global->add_psk == NULL)
8350 return;
8351 }
8352 p = wpa_s->global->add_psk;
8353 if (p2p_dev_addr) {
8354 p->p2p = 1;
8355 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8356 } else {
8357 p->p2p = 0;
8358 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8359 }
8360 os_memcpy(p->psk, psk, psk_len);
8361 return;
8362 }
8363
8364 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8365 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8366 return;
8367 }
8368
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008369 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008370 ssid->ssid_len);
8371 if (!persistent) {
8372 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8373 return;
8374 }
8375
8376 p = os_zalloc(sizeof(*p));
8377 if (p == NULL)
8378 return;
8379 if (p2p_dev_addr) {
8380 p->p2p = 1;
8381 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8382 } else {
8383 p->p2p = 0;
8384 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8385 }
8386 os_memcpy(p->psk, psk, psk_len);
8387
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008388 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8389 (last = dl_list_last(&persistent->psk_list,
8390 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008391 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8392 MACSTR " (p2p=%u) to make room for a new one",
8393 MAC2STR(last->addr), last->p2p);
8394 dl_list_del(&last->list);
8395 os_free(last);
8396 }
8397
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008398 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008399 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8400 p2p_dev_addr == NULL);
8401 if (p2p_dev_addr) {
8402 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8403 MACSTR, MAC2STR(p2p_dev_addr));
8404 } else {
8405 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8406 MAC2STR(mac_addr));
8407 }
8408 dl_list_add(&persistent->psk_list, &p->list);
8409
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008410 if (wpa_s->p2pdev->conf->update_config &&
8411 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008412 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008413}
8414
8415
8416static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8417 struct wpa_ssid *s, const u8 *addr,
8418 int iface_addr)
8419{
8420 int res;
8421
8422 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008423 if (res > 0 && wpa_s->conf->update_config &&
8424 wpa_config_write(wpa_s->confname, wpa_s->conf))
8425 wpa_dbg(wpa_s, MSG_DEBUG,
8426 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008427}
8428
8429
8430static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8431 const u8 *peer, int iface_addr)
8432{
8433 struct hostapd_data *hapd;
8434 struct hostapd_wpa_psk *psk, *prev, *rem;
8435 struct sta_info *sta;
8436
8437 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8438 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8439 return;
8440
8441 /* Remove per-station PSK entry */
8442 hapd = wpa_s->ap_iface->bss[0];
8443 prev = NULL;
8444 psk = hapd->conf->ssid.wpa_psk;
8445 while (psk) {
8446 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8447 (!iface_addr &&
8448 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8450 MACSTR " iface_addr=%d",
8451 MAC2STR(peer), iface_addr);
8452 if (prev)
8453 prev->next = psk->next;
8454 else
8455 hapd->conf->ssid.wpa_psk = psk->next;
8456 rem = psk;
8457 psk = psk->next;
8458 os_free(rem);
8459 } else {
8460 prev = psk;
8461 psk = psk->next;
8462 }
8463 }
8464
8465 /* Disconnect from group */
8466 if (iface_addr)
8467 sta = ap_get_sta(hapd, peer);
8468 else
8469 sta = ap_get_sta_p2p(hapd, peer);
8470 if (sta) {
8471 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8472 " (iface_addr=%d) from group",
8473 MAC2STR(peer), iface_addr);
8474 hostapd_drv_sta_deauth(hapd, sta->addr,
8475 WLAN_REASON_DEAUTH_LEAVING);
8476 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8477 }
8478}
8479
8480
8481void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8482 int iface_addr)
8483{
8484 struct wpa_ssid *s;
8485 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008486 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008487
8488 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8489
8490 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008491 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008492 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8493 continue;
8494 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008495 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8496 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008497 }
8498
8499 /* Remove from any operating group */
8500 for (w = wpa_s->global->ifaces; w; w = w->next)
8501 wpas_p2p_remove_client_go(w, peer, iface_addr);
8502}
8503
8504
8505static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8506{
8507 struct wpa_supplicant *wpa_s = eloop_ctx;
8508 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8509}
8510
8511
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008512static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8513{
8514 struct wpa_supplicant *wpa_s = eloop_ctx;
8515
8516 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8517 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8518}
8519
8520
8521int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8522 struct wpa_ssid *ssid)
8523{
8524 struct wpa_supplicant *iface;
8525
8526 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8527 if (!iface->current_ssid ||
8528 iface->current_ssid->frequency == freq ||
8529 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8530 !iface->current_ssid->p2p_group))
8531 continue;
8532
8533 /* Remove the connection with least priority */
8534 if (!wpas_is_p2p_prioritized(iface)) {
8535 /* STA connection has priority over existing
8536 * P2P connection, so remove the interface. */
8537 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8538 eloop_register_timeout(0, 0,
8539 wpas_p2p_group_freq_conflict,
8540 iface, NULL);
8541 /* If connection in progress is P2P connection, do not
8542 * proceed for the connection. */
8543 if (wpa_s == iface)
8544 return -1;
8545 else
8546 return 0;
8547 } else {
8548 /* P2P connection has priority, disable the STA network
8549 */
8550 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8551 ssid);
8552 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8553 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8554 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8555 ETH_ALEN);
8556 /* If P2P connection is in progress, continue
8557 * connecting...*/
8558 if (wpa_s == iface)
8559 return 0;
8560 else
8561 return -1;
8562 }
8563 }
8564
8565 return 0;
8566}
8567
8568
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008569int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8570{
8571 struct wpa_ssid *ssid = wpa_s->current_ssid;
8572
8573 if (ssid == NULL || !ssid->p2p_group)
8574 return 0;
8575
8576 if (wpa_s->p2p_last_4way_hs_fail &&
8577 wpa_s->p2p_last_4way_hs_fail == ssid) {
8578 u8 go_dev_addr[ETH_ALEN];
8579 struct wpa_ssid *persistent;
8580
8581 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8582 ssid->ssid,
8583 ssid->ssid_len) <= 0) {
8584 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8585 goto disconnect;
8586 }
8587
8588 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8589 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008590 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008591 ssid->ssid,
8592 ssid->ssid_len);
8593 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8594 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8595 goto disconnect;
8596 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008597 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008598 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8599 persistent->id);
8600 disconnect:
8601 wpa_s->p2p_last_4way_hs_fail = NULL;
8602 /*
8603 * Remove the group from a timeout to avoid issues with caller
8604 * continuing to use the interface if this is on a P2P group
8605 * interface.
8606 */
8607 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8608 wpa_s, NULL);
8609 return 1;
8610 }
8611
8612 wpa_s->p2p_last_4way_hs_fail = ssid;
8613 return 0;
8614}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008615
8616
8617#ifdef CONFIG_WPS_NFC
8618
8619static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8620 struct wpabuf *p2p)
8621{
8622 struct wpabuf *ret;
8623 size_t wsc_len;
8624
8625 if (p2p == NULL) {
8626 wpabuf_free(wsc);
8627 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8628 return NULL;
8629 }
8630
8631 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8632 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8633 if (ret == NULL) {
8634 wpabuf_free(wsc);
8635 wpabuf_free(p2p);
8636 return NULL;
8637 }
8638
8639 wpabuf_put_be16(ret, wsc_len);
8640 if (wsc)
8641 wpabuf_put_buf(ret, wsc);
8642 wpabuf_put_be16(ret, wpabuf_len(p2p));
8643 wpabuf_put_buf(ret, p2p);
8644
8645 wpabuf_free(wsc);
8646 wpabuf_free(p2p);
8647 wpa_hexdump_buf(MSG_DEBUG,
8648 "P2P: Generated NFC connection handover message", ret);
8649
8650 if (ndef && ret) {
8651 struct wpabuf *tmp;
8652 tmp = ndef_build_p2p(ret);
8653 wpabuf_free(ret);
8654 if (tmp == NULL) {
8655 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8656 return NULL;
8657 }
8658 ret = tmp;
8659 }
8660
8661 return ret;
8662}
8663
8664
8665static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8666 struct wpa_ssid **ssid, u8 *go_dev_addr)
8667{
8668 struct wpa_supplicant *iface;
8669
8670 if (go_dev_addr)
8671 os_memset(go_dev_addr, 0, ETH_ALEN);
8672 if (ssid)
8673 *ssid = NULL;
8674 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8675 if (iface->wpa_state < WPA_ASSOCIATING ||
8676 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8677 !iface->current_ssid->p2p_group ||
8678 iface->current_ssid->mode != WPAS_MODE_INFRA)
8679 continue;
8680 if (ssid)
8681 *ssid = iface->current_ssid;
8682 if (go_dev_addr)
8683 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8684 return iface->assoc_freq;
8685 }
8686 return 0;
8687}
8688
8689
8690struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8691 int ndef)
8692{
8693 struct wpabuf *wsc, *p2p;
8694 struct wpa_ssid *ssid;
8695 u8 go_dev_addr[ETH_ALEN];
8696 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8697
8698 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8699 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8700 return NULL;
8701 }
8702
8703 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8704 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8705 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8707 return NULL;
8708 }
8709
8710 if (cli_freq == 0) {
8711 wsc = wps_build_nfc_handover_req_p2p(
8712 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8713 } else
8714 wsc = NULL;
8715 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8716 go_dev_addr, ssid ? ssid->ssid : NULL,
8717 ssid ? ssid->ssid_len : 0);
8718
8719 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8720}
8721
8722
8723struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8724 int ndef, int tag)
8725{
8726 struct wpabuf *wsc, *p2p;
8727 struct wpa_ssid *ssid;
8728 u8 go_dev_addr[ETH_ALEN];
8729 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8730
8731 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8732 return NULL;
8733
8734 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8735 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8736 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8737 return NULL;
8738
8739 if (cli_freq == 0) {
8740 wsc = wps_build_nfc_handover_sel_p2p(
8741 wpa_s->parent->wps,
8742 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8743 DEV_PW_NFC_CONNECTION_HANDOVER,
8744 wpa_s->conf->wps_nfc_dh_pubkey,
8745 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8746 } else
8747 wsc = NULL;
8748 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8749 go_dev_addr, ssid ? ssid->ssid : NULL,
8750 ssid ? ssid->ssid_len : 0);
8751
8752 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8753}
8754
8755
8756static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8757 struct p2p_nfc_params *params)
8758{
8759 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8760 "connection handover (freq=%d)",
8761 params->go_freq);
8762
8763 if (params->go_freq && params->go_ssid_len) {
8764 wpa_s->p2p_wps_method = WPS_NFC;
8765 wpa_s->pending_join_wps_method = WPS_NFC;
8766 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8767 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8768 ETH_ALEN);
8769 return wpas_p2p_join_start(wpa_s, params->go_freq,
8770 params->go_ssid,
8771 params->go_ssid_len);
8772 }
8773
8774 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8775 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008776 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008777 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008778 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008779 params->go_ssid_len ? params->go_ssid : NULL,
8780 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008781}
8782
8783
8784static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8785 struct p2p_nfc_params *params, int tag)
8786{
8787 int res, persistent;
8788 struct wpa_ssid *ssid;
8789
8790 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8791 "connection handover");
8792 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8793 ssid = wpa_s->current_ssid;
8794 if (ssid == NULL)
8795 continue;
8796 if (ssid->mode != WPAS_MODE_P2P_GO)
8797 continue;
8798 if (wpa_s->ap_iface == NULL)
8799 continue;
8800 break;
8801 }
8802 if (wpa_s == NULL) {
8803 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8804 return -1;
8805 }
8806
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008807 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008808 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008809 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008810 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8811 return -1;
8812 }
8813 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008814 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8815 wpa_s->p2pdev->p2p_oob_dev_pw,
8816 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8817 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008818 if (res)
8819 return res;
8820
8821 if (!tag) {
8822 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8823 return 0;
8824 }
8825
8826 if (!params->peer ||
8827 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8828 return 0;
8829
8830 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8831 " to join", MAC2STR(params->peer->p2p_device_addr));
8832
8833 wpa_s->global->p2p_invite_group = wpa_s;
8834 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008835 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008836 params->peer->p2p_device_addr,
8837 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008838 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008839
8840 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8841 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8842 ssid->ssid, ssid->ssid_len, ssid->frequency,
8843 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008844 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008845}
8846
8847
8848static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8849 struct p2p_nfc_params *params,
8850 int forced_freq)
8851{
8852 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8853 "connection handover");
8854 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8855 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008856 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008857 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008858 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8859 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008860}
8861
8862
8863static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8864 struct p2p_nfc_params *params,
8865 int forced_freq)
8866{
8867 int res;
8868
8869 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8870 "connection handover");
8871 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8872 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008873 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008874 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008875 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
8876 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008877 if (res)
8878 return res;
8879
8880 res = wpas_p2p_listen(wpa_s, 60);
8881 if (res) {
8882 p2p_unauthorize(wpa_s->global->p2p,
8883 params->peer->p2p_device_addr);
8884 }
8885
8886 return res;
8887}
8888
8889
8890static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8891 const struct wpabuf *data,
8892 int sel, int tag, int forced_freq)
8893{
8894 const u8 *pos, *end;
8895 u16 len, id;
8896 struct p2p_nfc_params params;
8897 int res;
8898
8899 os_memset(&params, 0, sizeof(params));
8900 params.sel = sel;
8901
8902 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8903
8904 pos = wpabuf_head(data);
8905 end = pos + wpabuf_len(data);
8906
8907 if (end - pos < 2) {
8908 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8909 "attributes");
8910 return -1;
8911 }
8912 len = WPA_GET_BE16(pos);
8913 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008914 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008915 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8916 "attributes");
8917 return -1;
8918 }
8919 params.wsc_attr = pos;
8920 params.wsc_len = len;
8921 pos += len;
8922
8923 if (end - pos < 2) {
8924 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8925 "attributes");
8926 return -1;
8927 }
8928 len = WPA_GET_BE16(pos);
8929 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008930 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008931 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8932 "attributes");
8933 return -1;
8934 }
8935 params.p2p_attr = pos;
8936 params.p2p_len = len;
8937 pos += len;
8938
8939 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8940 params.wsc_attr, params.wsc_len);
8941 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8942 params.p2p_attr, params.p2p_len);
8943 if (pos < end) {
8944 wpa_hexdump(MSG_DEBUG,
8945 "P2P: Ignored extra data after P2P attributes",
8946 pos, end - pos);
8947 }
8948
8949 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8950 if (res)
8951 return res;
8952
8953 if (params.next_step == NO_ACTION)
8954 return 0;
8955
8956 if (params.next_step == BOTH_GO) {
8957 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8958 MAC2STR(params.peer->p2p_device_addr));
8959 return 0;
8960 }
8961
8962 if (params.next_step == PEER_CLIENT) {
8963 if (!is_zero_ether_addr(params.go_dev_addr)) {
8964 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8965 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8966 " ssid=\"%s\"",
8967 MAC2STR(params.peer->p2p_device_addr),
8968 params.go_freq,
8969 MAC2STR(params.go_dev_addr),
8970 wpa_ssid_txt(params.go_ssid,
8971 params.go_ssid_len));
8972 } else {
8973 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8974 "peer=" MACSTR " freq=%d",
8975 MAC2STR(params.peer->p2p_device_addr),
8976 params.go_freq);
8977 }
8978 return 0;
8979 }
8980
8981 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8982 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8983 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8984 return 0;
8985 }
8986
8987 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8988 wpa_s->p2p_oob_dev_pw = NULL;
8989
8990 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8991 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8992 "received");
8993 return -1;
8994 }
8995
8996 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8997 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8998 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8999 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9000 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9001 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9002 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9003
9004 if (tag) {
9005 if (id < 0x10) {
9006 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9007 "peer OOB Device Password Id %u", id);
9008 return -1;
9009 }
9010 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9011 "Device Password Id %u", id);
9012 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9013 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9014 params.oob_dev_pw_len -
9015 WPS_OOB_PUBKEY_HASH_LEN - 2);
9016 wpa_s->p2p_oob_dev_pw_id = id;
9017 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9018 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9019 params.oob_dev_pw_len -
9020 WPS_OOB_PUBKEY_HASH_LEN - 2);
9021 if (wpa_s->p2p_oob_dev_pw == NULL)
9022 return -1;
9023
9024 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9025 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9026 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9027 return -1;
9028 } else {
9029 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9030 "without Device Password");
9031 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9032 }
9033
9034 switch (params.next_step) {
9035 case NO_ACTION:
9036 case BOTH_GO:
9037 case PEER_CLIENT:
9038 /* already covered above */
9039 return 0;
9040 case JOIN_GROUP:
9041 return wpas_p2p_nfc_join_group(wpa_s, &params);
9042 case AUTH_JOIN:
9043 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9044 case INIT_GO_NEG:
9045 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9046 case RESP_GO_NEG:
9047 /* TODO: use own OOB Dev Pw */
9048 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9049 }
9050
9051 return -1;
9052}
9053
9054
9055int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9056 const struct wpabuf *data, int forced_freq)
9057{
9058 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9059 return -1;
9060
9061 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9062}
9063
9064
9065int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9066 const struct wpabuf *req,
9067 const struct wpabuf *sel, int forced_freq)
9068{
9069 struct wpabuf *tmp;
9070 int ret;
9071
9072 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9073 return -1;
9074
9075 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9076
9077 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9078 wpabuf_head(req), wpabuf_len(req));
9079 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9080 wpabuf_head(sel), wpabuf_len(sel));
9081 if (forced_freq)
9082 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9083 tmp = ndef_parse_p2p(init ? sel : req);
9084 if (tmp == NULL) {
9085 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9086 return -1;
9087 }
9088
9089 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9090 forced_freq);
9091 wpabuf_free(tmp);
9092
9093 return ret;
9094}
9095
9096
9097int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9098{
9099 const u8 *if_addr;
9100 int go_intent = wpa_s->conf->p2p_go_intent;
9101 struct wpa_supplicant *iface;
9102
9103 if (wpa_s->global->p2p == NULL)
9104 return -1;
9105
9106 if (!enabled) {
9107 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9108 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9109 {
9110 if (!iface->ap_iface)
9111 continue;
9112 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9113 }
9114 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9115 0, NULL);
9116 if (wpa_s->p2p_nfc_tag_enabled)
9117 wpas_p2p_remove_pending_group_interface(wpa_s);
9118 wpa_s->p2p_nfc_tag_enabled = 0;
9119 return 0;
9120 }
9121
9122 if (wpa_s->global->p2p_disabled)
9123 return -1;
9124
9125 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9126 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9127 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9128 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9129 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9130 "to allow static handover cases");
9131 return -1;
9132 }
9133
9134 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9135
9136 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9137 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9138 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9139 if (wpa_s->p2p_oob_dev_pw == NULL)
9140 return -1;
9141 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9142
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009143 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9144 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9145 /*
9146 * P2P Group Interface present and the command came on group
9147 * interface, so enable the token for the current interface.
9148 */
9149 wpa_s->create_p2p_iface = 0;
9150 } else {
9151 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9152 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009153
9154 if (wpa_s->create_p2p_iface) {
9155 enum wpa_driver_if_type iftype;
9156 /* Prepare to add a new interface for the group */
9157 iftype = WPA_IF_P2P_GROUP;
9158 if (go_intent == 15)
9159 iftype = WPA_IF_P2P_GO;
9160 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9161 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9162 "interface for the group");
9163 return -1;
9164 }
9165
9166 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009167 } else if (wpa_s->p2p_mgmt)
9168 if_addr = wpa_s->parent->own_addr;
9169 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009170 if_addr = wpa_s->own_addr;
9171
9172 wpa_s->p2p_nfc_tag_enabled = enabled;
9173
9174 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9175 struct hostapd_data *hapd;
9176 if (iface->ap_iface == NULL)
9177 continue;
9178 hapd = iface->ap_iface->bss[0];
9179 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9180 hapd->conf->wps_nfc_dh_pubkey =
9181 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9182 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9183 hapd->conf->wps_nfc_dh_privkey =
9184 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9185 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9186 hapd->conf->wps_nfc_dev_pw =
9187 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9188 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9189
9190 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9191 wpa_dbg(iface, MSG_DEBUG,
9192 "P2P: Failed to enable NFC Tag for GO");
9193 }
9194 }
9195 p2p_set_authorized_oob_dev_pw_id(
9196 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9197 if_addr);
9198
9199 return 0;
9200}
9201
9202#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009203
9204
9205static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9206 struct wpa_used_freq_data *freqs,
9207 unsigned int num)
9208{
9209 u8 curr_chan, cand, chan;
9210 unsigned int i;
9211
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009212 /*
9213 * If possible, optimize the Listen channel to be a channel that is
9214 * already used by one of the other interfaces.
9215 */
9216 if (!wpa_s->conf->p2p_optimize_listen_chan)
9217 return;
9218
9219 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9220 return;
9221
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009222 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9223 for (i = 0, cand = 0; i < num; i++) {
9224 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9225 if (curr_chan == chan) {
9226 cand = 0;
9227 break;
9228 }
9229
9230 if (chan == 1 || chan == 6 || chan == 11)
9231 cand = chan;
9232 }
9233
9234 if (cand) {
9235 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009236 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009237 cand);
9238 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9239 }
9240}
9241
9242
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009243static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009244{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009245 struct hostapd_config *conf;
9246 struct p2p_go_neg_results params;
9247 struct csa_settings csa_settings;
9248 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9249 int old_freq = current_ssid->frequency;
9250 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009251
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009252 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9253 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9254 return -1;
9255 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009256
9257 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009258 * TODO: This function may not always work correctly. For example,
9259 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009260 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009261 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9262 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009263 wpa_dbg(wpa_s, MSG_DEBUG,
9264 "P2P CSA: Failed to select new frequency for GO");
9265 return -1;
9266 }
9267
9268 if (current_ssid->frequency == params.freq) {
9269 wpa_dbg(wpa_s, MSG_DEBUG,
9270 "P2P CSA: Selected same frequency - not moving GO");
9271 return 0;
9272 }
9273
9274 conf = hostapd_config_defaults();
9275 if (!conf) {
9276 wpa_dbg(wpa_s, MSG_DEBUG,
9277 "P2P CSA: Failed to allocate default config");
9278 return -1;
9279 }
9280
9281 current_ssid->frequency = params.freq;
9282 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9283 wpa_dbg(wpa_s, MSG_DEBUG,
9284 "P2P CSA: Failed to create new GO config");
9285 ret = -1;
9286 goto out;
9287 }
9288
9289 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9290 wpa_dbg(wpa_s, MSG_DEBUG,
9291 "P2P CSA: CSA to a different band is not supported");
9292 ret = -1;
9293 goto out;
9294 }
9295
9296 os_memset(&csa_settings, 0, sizeof(csa_settings));
9297 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9298 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9299 csa_settings.freq_params.freq = params.freq;
9300 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9301 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9302 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9303
9304 if (conf->ieee80211ac) {
9305 int freq1 = 0, freq2 = 0;
9306 u8 chan, opclass;
9307
9308 if (ieee80211_freq_to_channel_ext(params.freq,
9309 conf->secondary_channel,
9310 conf->vht_oper_chwidth,
9311 &opclass, &chan) ==
9312 NUM_HOSTAPD_MODES) {
9313 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9314 ret = -1;
9315 goto out;
9316 }
9317
9318 if (conf->vht_oper_centr_freq_seg0_idx)
9319 freq1 = ieee80211_chan_to_freq(
9320 NULL, opclass,
9321 conf->vht_oper_centr_freq_seg0_idx);
9322
9323 if (conf->vht_oper_centr_freq_seg1_idx)
9324 freq2 = ieee80211_chan_to_freq(
9325 NULL, opclass,
9326 conf->vht_oper_centr_freq_seg1_idx);
9327
9328 if (freq1 < 0 || freq2 < 0) {
9329 wpa_dbg(wpa_s, MSG_DEBUG,
9330 "P2P CSA: Selected invalid VHT center freqs");
9331 ret = -1;
9332 goto out;
9333 }
9334
9335 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9336 csa_settings.freq_params.center_freq1 = freq1;
9337 csa_settings.freq_params.center_freq2 = freq2;
9338
9339 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009340 case CHANWIDTH_80MHZ:
9341 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009342 csa_settings.freq_params.bandwidth = 80;
9343 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009344 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009345 csa_settings.freq_params.bandwidth = 160;
9346 break;
9347 }
9348 }
9349
9350 ret = ap_switch_channel(wpa_s, &csa_settings);
9351out:
9352 current_ssid->frequency = old_freq;
9353 hostapd_config_free(conf);
9354 return ret;
9355}
9356
9357
9358static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9359{
9360 struct p2p_go_neg_results params;
9361 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9362
9363 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9364
9365 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9366 current_ssid->frequency);
9367
9368 /* Stop the AP functionality */
9369 /* TODO: Should do this in a way that does not indicated to possible
9370 * P2P Clients in the group that the group is terminated. */
9371 wpa_supplicant_ap_deinit(wpa_s);
9372
9373 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009374 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9375 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009376 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9377 wpas_p2p_group_delete(wpa_s,
9378 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9379 return;
9380 }
9381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9382 params.freq);
9383
9384 if (params.freq &&
9385 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9386 wpa_printf(MSG_DEBUG,
9387 "P2P: Selected freq (%u MHz) is not valid for P2P",
9388 params.freq);
9389 wpas_p2p_group_delete(wpa_s,
9390 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9391 return;
9392 }
9393
9394 /* Update the frequency */
9395 current_ssid->frequency = params.freq;
9396 wpa_s->connect_without_scan = current_ssid;
9397 wpa_s->reassociate = 1;
9398 wpa_s->disconnected = 0;
9399 wpa_supplicant_req_scan(wpa_s, 0, 0);
9400}
9401
9402
9403static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9404{
9405 struct wpa_supplicant *wpa_s = eloop_ctx;
9406
9407 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009408 return;
9409
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009410 wpas_p2p_go_update_common_freqs(wpa_s);
9411
9412 /* Do not move GO in the middle of a CSA */
9413 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9414 wpa_printf(MSG_DEBUG,
9415 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009416 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009417 }
9418
9419 /*
9420 * First, try a channel switch flow. If it is not supported or fails,
9421 * take down the GO and bring it up again.
9422 */
9423 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9424 wpas_p2p_move_go_no_csa(wpa_s);
9425}
9426
9427
9428static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9429{
9430 struct wpa_supplicant *wpa_s = eloop_ctx;
9431 struct wpa_used_freq_data *freqs = NULL;
9432 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009433
9434 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9435 if (!freqs)
9436 return;
9437
9438 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9439
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009440 /* Previous attempt to move a GO was not possible -- try again. */
9441 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9442 WPAS_P2P_CHANNEL_UPDATE_ANY);
9443
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009444 os_free(freqs);
9445}
9446
9447
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009448/*
9449 * Consider moving a GO from its currently used frequency:
9450 * 1. It is possible that due to regulatory consideration the frequency
9451 * can no longer be used and there is a need to evacuate the GO.
9452 * 2. It is possible that due to MCC considerations, it would be preferable
9453 * to move the GO to a channel that is currently used by some other
9454 * station interface.
9455 *
9456 * In case a frequency that became invalid is once again valid, cancel a
9457 * previously initiated GO frequency change.
9458 */
9459static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9460 struct wpa_used_freq_data *freqs,
9461 unsigned int num)
9462{
9463 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9464 unsigned int timeout;
9465 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009466 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009467
9468 wpas_p2p_go_update_common_freqs(wpa_s);
9469
9470 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009471 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009472 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009473 for (i = 0, invalid_freq = 0; i < num; i++) {
9474 if (freqs[i].freq == freq) {
9475 flags = freqs[i].flags;
9476
9477 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009478 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9479 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009480 wpa_dbg(wpa_s, MSG_DEBUG,
9481 "P2P: Freq=%d MHz no longer valid for GO",
9482 freq);
9483 invalid_freq = 1;
9484 }
9485 } else if (freqs[i].flags == 0) {
9486 /* Freq is not used by any other station interface */
9487 continue;
9488 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009489 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009490 /* Freq is not valid for P2P use cases */
9491 continue;
9492 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9493 P2P_GO_FREQ_MOVE_SCM) {
9494 policy_move = 1;
9495 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9496 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9497 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9498 policy_move = 1;
9499 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9500 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9501 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9502 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9503 policy_move = 1;
9504 } else if ((wpa_s->drv_flags &
9505 WPA_DRIVER_FLAGS_AP_CSA) &&
9506 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9507 u8 chan;
9508
9509 /*
9510 * We do not support CSA between bands, so move
9511 * GO only within the same band.
9512 */
9513 if (wpa_s->ap_iface->current_mode->mode ==
9514 ieee80211_freq_to_chan(freqs[i].freq,
9515 &chan))
9516 policy_move = 1;
9517 }
9518 }
9519 }
9520
9521 wpa_dbg(wpa_s, MSG_DEBUG,
9522 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9523 invalid_freq, policy_move, flags);
9524
9525 /*
9526 * The channel is valid, or we are going to have a policy move, so
9527 * cancel timeout.
9528 */
9529 if (!invalid_freq || policy_move) {
9530 wpa_dbg(wpa_s, MSG_DEBUG,
9531 "P2P: Cancel a GO move from freq=%d MHz", freq);
9532 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9533
9534 if (wpas_p2p_in_progress(wpa_s)) {
9535 wpa_dbg(wpa_s, MSG_DEBUG,
9536 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9537 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9538 wpa_s, NULL);
9539 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9540 wpas_p2p_reconsider_moving_go,
9541 wpa_s, NULL);
9542 return;
9543 }
9544 }
9545
9546 if (!invalid_freq && (!policy_move || flags != 0)) {
9547 wpa_dbg(wpa_s, MSG_DEBUG,
9548 "P2P: Not initiating a GO frequency change");
9549 return;
9550 }
9551
9552 /*
9553 * Do not consider moving GO if it is in the middle of a CSA. When the
9554 * CSA is finished this flow should be retriggered.
9555 */
9556 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9557 wpa_dbg(wpa_s, MSG_DEBUG,
9558 "P2P: Not initiating a GO frequency change - CSA is in progress");
9559 return;
9560 }
9561
9562 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9563 timeout = P2P_GO_FREQ_CHANGE_TIME;
9564 else
9565 timeout = 0;
9566
9567 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9568 freq, timeout);
9569 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9570 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9571}
9572
9573
9574static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9575 struct wpa_used_freq_data *freqs,
9576 unsigned int num,
9577 enum wpas_p2p_channel_update_trig trig)
9578{
9579 struct wpa_supplicant *ifs;
9580
9581 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9582 NULL);
9583
9584 /*
9585 * Travers all the radio interfaces, and for each GO interface, check
9586 * if there is a need to move the GO from the frequency it is using,
9587 * or in case the frequency is valid again, cancel the evacuation flow.
9588 */
9589 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9590 radio_list) {
9591 if (ifs->current_ssid == NULL ||
9592 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9593 continue;
9594
9595 /*
9596 * The GO was just started or completed channel switch, no need
9597 * to move it.
9598 */
9599 if (wpa_s == ifs &&
9600 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9601 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9602 wpa_dbg(wpa_s, MSG_DEBUG,
9603 "P2P: GO move - schedule re-consideration");
9604 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9605 wpas_p2p_reconsider_moving_go,
9606 wpa_s, NULL);
9607 continue;
9608 }
9609
9610 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9611 }
9612}
9613
9614
9615void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9616{
9617 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9618 return;
9619
9620 wpas_p2p_update_channel_list(wpa_s,
9621 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9622}
9623
9624
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009625void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9626{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009627 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9628 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9629 "the management interface is being removed");
9630 wpas_p2p_deinit_global(wpa_s->global);
9631 }
9632}
9633
9634
9635void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9636{
9637 if (wpa_s->ap_iface->bss)
9638 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9639 wpas_p2p_group_deinit(wpa_s);
9640}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009641
9642
9643int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9644 unsigned int period, unsigned int interval,
9645 unsigned int count)
9646{
9647 struct p2p_data *p2p = wpa_s->global->p2p;
9648 u8 *device_types;
9649 size_t dev_types_len;
9650 struct wpabuf *buf;
9651 int ret;
9652
9653 if (wpa_s->p2p_lo_started) {
9654 wpa_dbg(wpa_s, MSG_DEBUG,
9655 "P2P Listen offload is already started");
9656 return 0;
9657 }
9658
9659 if (wpa_s->global->p2p == NULL ||
9660 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9661 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9662 return -1;
9663 }
9664
9665 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9666 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9667 freq);
9668 return -1;
9669 }
9670
9671 /* Get device type */
9672 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9673 WPS_DEV_TYPE_LEN;
9674 device_types = os_malloc(dev_types_len);
9675 if (!device_types)
9676 return -1;
9677 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9678 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9679 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9680
9681 /* Get Probe Response IE(s) */
9682 buf = p2p_build_probe_resp_template(p2p, freq);
9683 if (!buf) {
9684 os_free(device_types);
9685 return -1;
9686 }
9687
9688 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9689 device_types, dev_types_len,
9690 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9691 if (ret < 0)
9692 wpa_dbg(wpa_s, MSG_DEBUG,
9693 "P2P: Failed to start P2P listen offload");
9694
9695 os_free(device_types);
9696 wpabuf_free(buf);
9697
9698 if (ret == 0) {
9699 wpa_s->p2p_lo_started = 1;
9700
9701 /* Stop current P2P listen if any */
9702 wpas_stop_listen(wpa_s);
9703 }
9704
9705 return ret;
9706}
9707
9708
9709int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9710{
9711 int ret;
9712
9713 if (!wpa_s->p2p_lo_started)
9714 return 0;
9715
9716 ret = wpa_drv_p2p_lo_stop(wpa_s);
9717 if (ret < 0)
9718 wpa_dbg(wpa_s, MSG_DEBUG,
9719 "P2P: Failed to stop P2P listen offload");
9720
9721 wpa_s->p2p_lo_started = 0;
9722 return ret;
9723}