blob: fb509a110fea79ea6de3aca4f79472b2d989f29e [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
1888static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1889 struct p2p_go_neg_results *params,
1890 int group_formation)
1891{
1892 struct wpa_ssid *ssid;
1893
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1895 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1896 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1897 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001899 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900
1901 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001902 if (ssid == NULL) {
1903 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001905 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001907 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001908 wpa_s->p2p_go_group_formation_completed = 0;
1909 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001910 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 wpa_config_set_network_defaults(ssid);
1913 ssid->temporary = 1;
1914 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001915 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1917 WPAS_MODE_P2P_GO;
1918 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001919 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001920 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001921 ssid->max_oper_chwidth = params->max_oper_chwidth;
1922 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08001923 ssid->he = params->he;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 ssid->ssid = os_zalloc(params->ssid_len + 1);
1925 if (ssid->ssid) {
1926 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1927 ssid->ssid_len = params->ssid_len;
1928 }
1929 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1930 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1931 ssid->proto = WPA_PROTO_RSN;
1932 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001933 ssid->group_cipher = WPA_CIPHER_CCMP;
1934 if (params->freq > 56160) {
1935 /*
1936 * Enable GCMP instead of CCMP as pairwise_cipher and
1937 * group_cipher in 60 GHz.
1938 */
1939 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1940 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001941 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1942 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001943 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001944 if (os_strlen(params->passphrase) > 0) {
1945 ssid->passphrase = os_strdup(params->passphrase);
1946 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001947 wpa_msg_global(wpa_s, MSG_ERROR,
1948 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001949 wpa_config_remove_network(wpa_s->conf, ssid->id);
1950 return;
1951 }
1952 } else
1953 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001954 ssid->psk_set = params->psk_set;
1955 if (ssid->psk_set)
1956 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001957 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001958 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001959 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960
1961 wpa_s->ap_configured_cb = p2p_go_configured;
1962 wpa_s->ap_configured_cb_ctx = wpa_s;
1963 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001964 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001965 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 wpa_s->reassociate = 1;
1967 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001968 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1969 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 wpa_supplicant_req_scan(wpa_s, 0, 0);
1971}
1972
1973
1974static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1975 const struct wpa_supplicant *src)
1976{
1977 struct wpa_config *d;
1978 const struct wpa_config *s;
1979
1980 d = dst->conf;
1981 s = src->conf;
1982
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001983#define C(n) \
1984do { \
1985 if (s->n && !d->n) \
1986 d->n = os_strdup(s->n); \
1987} while (0)
1988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 C(device_name);
1990 C(manufacturer);
1991 C(model_name);
1992 C(model_number);
1993 C(serial_number);
1994 C(config_methods);
1995#undef C
1996
1997 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1998 os_memcpy(d->sec_device_type, s->sec_device_type,
1999 sizeof(d->sec_device_type));
2000 d->num_sec_device_types = s->num_sec_device_types;
2001
2002 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002003 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002004 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002005 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002006 d->max_num_sta = s->max_num_sta;
2007 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002008 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002009 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002010 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002011 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002012 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002013 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002014 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002015
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002016 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2017 !d->wps_nfc_pw_from_config) {
2018 wpabuf_free(d->wps_nfc_dh_privkey);
2019 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002020 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2021 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2022 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002023 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002024 d->go_interworking = s->go_interworking;
2025 d->go_access_network_type = s->go_access_network_type;
2026 d->go_internet = s->go_internet;
2027 d->go_venue_group = s->go_venue_group;
2028 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029}
2030
2031
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002032static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2033 char *ifname, size_t len)
2034{
2035 char *ifname_ptr = wpa_s->ifname;
2036
2037 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2038 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2039 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2040 }
2041
2042 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2043 if (os_strlen(ifname) >= IFNAMSIZ &&
2044 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002045 int res;
2046
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002047 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002048 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2049 if (os_snprintf_error(len, res) && len)
2050 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002051 }
2052}
2053
2054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2056 enum wpa_driver_if_type type)
2057{
2058 char ifname[120], force_ifname[120];
2059
2060 if (wpa_s->pending_interface_name[0]) {
2061 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2062 "- skip creation of a new one");
2063 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2064 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2065 "unknown?! ifname='%s'",
2066 wpa_s->pending_interface_name);
2067 return -1;
2068 }
2069 return 0;
2070 }
2071
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002072 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 force_ifname[0] = '\0';
2074
2075 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2076 ifname);
2077 wpa_s->p2p_group_idx++;
2078
2079 wpa_s->pending_interface_type = type;
2080 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2081 wpa_s->pending_interface_addr, NULL) < 0) {
2082 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2083 "interface");
2084 return -1;
2085 }
2086
Jimmy Chen36c21992018-11-29 16:46:43 +08002087 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002088 random_mac_addr(wpa_s->pending_interface_addr);
2089 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2090 " for the group",
2091 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002092 }
2093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 if (force_ifname[0]) {
2095 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2096 force_ifname);
2097 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2098 sizeof(wpa_s->pending_interface_name));
2099 } else
2100 os_strlcpy(wpa_s->pending_interface_name, ifname,
2101 sizeof(wpa_s->pending_interface_name));
2102 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2103 MACSTR, wpa_s->pending_interface_name,
2104 MAC2STR(wpa_s->pending_interface_addr));
2105
2106 return 0;
2107}
2108
2109
2110static void wpas_p2p_remove_pending_group_interface(
2111 struct wpa_supplicant *wpa_s)
2112{
2113 if (!wpa_s->pending_interface_name[0] ||
2114 is_zero_ether_addr(wpa_s->pending_interface_addr))
2115 return; /* No pending virtual interface */
2116
2117 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2118 wpa_s->pending_interface_name);
2119 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2120 wpa_s->pending_interface_name);
2121 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2122 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002123 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124}
2125
2126
2127static struct wpa_supplicant *
2128wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2129{
2130 struct wpa_interface iface;
2131 struct wpa_supplicant *group_wpa_s;
2132
2133 if (!wpa_s->pending_interface_name[0]) {
2134 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2135 if (!wpas_p2p_create_iface(wpa_s))
2136 return NULL;
2137 /*
2138 * Something has forced us to remove the pending interface; try
2139 * to create a new one and hope for the best that we will get
2140 * the same local address.
2141 */
2142 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2143 WPA_IF_P2P_CLIENT) < 0)
2144 return NULL;
2145 }
2146
2147 os_memset(&iface, 0, sizeof(iface));
2148 iface.ifname = wpa_s->pending_interface_name;
2149 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002150 if (wpa_s->conf->ctrl_interface == NULL &&
2151 wpa_s->parent != wpa_s &&
2152 wpa_s->p2p_mgmt &&
2153 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2154 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2155 else
2156 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002158 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 if (group_wpa_s == NULL) {
2160 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2161 "wpa_supplicant interface");
2162 return NULL;
2163 }
2164 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2166 P2P_GROUP_INTERFACE_CLIENT;
2167 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002168 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169
2170 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2171
Jimmy Chen36c21992018-11-29 16:46:43 +08002172 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002173 if (wpa_drv_set_mac_addr(group_wpa_s,
2174 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002175 wpa_msg(group_wpa_s, MSG_INFO,
2176 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002177 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2178 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002179 return NULL;
2180 }
2181
2182 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2183 wpa_msg(group_wpa_s, MSG_INFO,
2184 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002185 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2186 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002187 return NULL;
2188 }
2189
Hai Shalom74f70d42019-02-11 14:42:39 -08002190 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2191 " for the group",
2192 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002193 }
2194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 return group_wpa_s;
2196}
2197
2198
2199static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2200 void *timeout_ctx)
2201{
2202 struct wpa_supplicant *wpa_s = eloop_ctx;
2203 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002204 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002205}
2206
2207
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002208static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2209 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002210{
2211 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002212 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 if (wpa_s->global->p2p)
2214 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002215 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216}
2217
2218
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002219static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2220{
2221 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2222 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002223 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002224 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002225 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002226 wpa_s->global->p2p_fail_on_wps_complete = 0;
2227}
2228
2229
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002230void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2231{
2232 if (wpa_s->global->p2p_group_formation != wpa_s)
2233 return;
2234 /* Speed up group formation timeout since this cannot succeed */
2235 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002236 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002237 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002238 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002239}
2240
2241
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002242static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243{
2244 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002245 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246
2247 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2248 wpa_drv_cancel_remain_on_channel(wpa_s);
2249 wpa_s->off_channel_freq = 0;
2250 wpa_s->roc_waiting_drv_freq = 0;
2251 }
2252
2253 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002254 wpa_msg_global(wpa_s, MSG_INFO,
2255 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2256 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002257 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 wpas_p2p_remove_pending_group_interface(wpa_s);
2259 return;
2260 }
2261
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002262 if (!res->role_go) {
2263 /* Inform driver of the operating channel of GO. */
2264 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2265 }
2266
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002267 if (wpa_s->p2p_go_ht40)
2268 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002269 if (wpa_s->p2p_go_vht)
2270 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002271 if (wpa_s->p2p_go_he)
2272 res->he = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002273 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2274 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002275
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002276 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2277 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2278 " wps_method=%s",
2279 res->role_go ? "GO" : "client", res->freq, res->ht40,
2280 MAC2STR(res->peer_device_addr),
2281 MAC2STR(res->peer_interface_addr),
2282 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002283 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284
Dmitry Shmidt04949592012-07-19 12:16:46 -07002285 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2286 struct wpa_ssid *ssid;
2287 ssid = wpa_config_get_network(wpa_s->conf,
2288 wpa_s->p2p_persistent_id);
2289 if (ssid && ssid->disabled == 2 &&
2290 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2291 size_t len = os_strlen(ssid->passphrase);
2292 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2293 "on requested persistent group");
2294 os_memcpy(res->passphrase, ssid->passphrase, len);
2295 res->passphrase[len] = '\0';
2296 }
2297 }
2298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002300 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2302 if (group_wpa_s == NULL) {
2303 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002304 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2305 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002306 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 return;
2308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2310 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002311 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002312 group_wpa_s = wpa_s->parent;
2313 wpa_s->global->p2p_group_formation = group_wpa_s;
2314 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002315 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002317
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002318 group_wpa_s->p2p_in_provisioning = 1;
2319 group_wpa_s->p2pdev = wpa_s;
2320 if (group_wpa_s != wpa_s) {
2321 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2322 sizeof(group_wpa_s->p2p_pin));
2323 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2324 }
2325 if (res->role_go) {
2326 wpas_start_wps_go(group_wpa_s, res, 1);
2327 } else {
2328 os_get_reltime(&group_wpa_s->scan_min_time);
2329 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 }
2331
2332 wpa_s->p2p_long_listen = 0;
2333 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2334
2335 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2336 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2337 (res->peer_config_timeout % 100) * 10000,
2338 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2339}
2340
2341
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002342static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2343 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344{
2345 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002346 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002347 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2348 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002350 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351}
2352
2353
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002354static void wpas_dev_found(void *ctx, const u8 *addr,
2355 const struct p2p_peer_info *info,
2356 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002358 u8 *wfd_dev_info = NULL;
2359 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002360#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 struct wpa_supplicant *wpa_s = ctx;
2362 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002363 char *wfd_dev_info_hex = NULL;
2364
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002365#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002366 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2367 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002368 if (wfd_dev_info_hex) {
2369 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2370 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2371 // Only used for notification, so not handling error.
2372 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2373 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002374#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002375
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002376 if (info->p2ps_instance) {
2377 char str[256];
2378 const u8 *buf = wpabuf_head(info->p2ps_instance);
2379 size_t len = wpabuf_len(info->p2ps_instance);
2380
2381 while (len) {
2382 u32 id;
2383 u16 methods;
2384 u8 str_len;
2385
2386 if (len < 4 + 2 + 1)
2387 break;
2388 id = WPA_GET_LE32(buf);
2389 buf += sizeof(u32);
2390 methods = WPA_GET_BE16(buf);
2391 buf += sizeof(u16);
2392 str_len = *buf++;
2393 if (str_len > len - 4 - 2 - 1)
2394 break;
2395 os_memcpy(str, buf, str_len);
2396 str[str_len] = '\0';
2397 buf += str_len;
2398 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2399
2400 wpa_msg_global(wpa_s, MSG_INFO,
2401 P2P_EVENT_DEVICE_FOUND MACSTR
2402 " p2p_dev_addr=" MACSTR
2403 " pri_dev_type=%s name='%s'"
2404 " config_methods=0x%x"
2405 " dev_capab=0x%x"
2406 " group_capab=0x%x"
2407 " adv_id=%x asp_svc=%s%s",
2408 MAC2STR(addr),
2409 MAC2STR(info->p2p_device_addr),
2410 wps_dev_type_bin2str(
2411 info->pri_dev_type,
2412 devtype, sizeof(devtype)),
2413 info->device_name, methods,
2414 info->dev_capab, info->group_capab,
2415 id, str,
2416 info->vendor_elems ?
2417 " vendor_elems=1" : "");
2418 }
2419 goto done;
2420 }
2421
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002422 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2423 " p2p_dev_addr=" MACSTR
2424 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002425 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002426 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2427 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2428 sizeof(devtype)),
2429 info->device_name, info->config_methods,
2430 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002431 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002432 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002433 info->vendor_elems ? " vendor_elems=1" : "",
2434 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002436done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002437 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002438#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002439
Roshan Piusfd2fd662017-01-23 13:41:57 -08002440 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2441 wfd_dev_info_len, new_device);
2442 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443}
2444
2445
2446static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2447{
2448 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002449
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002450 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2451 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2454}
2455
2456
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002457static void wpas_find_stopped(void *ctx)
2458{
2459 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002460
2461 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2462 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2463
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002464 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002465 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002466}
2467
2468
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002469struct wpas_p2p_listen_work {
2470 unsigned int freq;
2471 unsigned int duration;
2472 struct wpabuf *probe_resp_ie;
2473};
2474
2475
2476static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2477{
2478 if (lwork == NULL)
2479 return;
2480 wpabuf_free(lwork->probe_resp_ie);
2481 os_free(lwork);
2482}
2483
2484
2485static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2486{
2487 struct wpas_p2p_listen_work *lwork;
2488
2489 if (!wpa_s->p2p_listen_work)
2490 return;
2491
2492 lwork = wpa_s->p2p_listen_work->ctx;
2493 wpas_p2p_listen_work_free(lwork);
2494 radio_work_done(wpa_s->p2p_listen_work);
2495 wpa_s->p2p_listen_work = NULL;
2496}
2497
2498
2499static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2500{
2501 struct wpa_supplicant *wpa_s = work->wpa_s;
2502 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002503 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002504
2505 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002506 if (work->started) {
2507 wpa_s->p2p_listen_work = NULL;
2508 wpas_stop_listen(wpa_s);
2509 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002510 wpas_p2p_listen_work_free(lwork);
2511 return;
2512 }
2513
2514 wpa_s->p2p_listen_work = work;
2515
2516 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2517
2518 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2519 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2520 "report received Probe Request frames");
2521 wpas_p2p_listen_work_done(wpa_s);
2522 return;
2523 }
2524
2525 wpa_s->pending_listen_freq = lwork->freq;
2526 wpa_s->pending_listen_duration = lwork->duration;
2527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002528 duration = lwork->duration;
2529#ifdef CONFIG_TESTING_OPTIONS
2530 if (wpa_s->extra_roc_dur) {
2531 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2532 duration, duration + wpa_s->extra_roc_dur);
2533 duration += wpa_s->extra_roc_dur;
2534 }
2535#endif /* CONFIG_TESTING_OPTIONS */
2536
2537 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002538 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2539 "to remain on channel (%u MHz) for Listen "
2540 "state", lwork->freq);
2541 wpas_p2p_listen_work_done(wpa_s);
2542 wpa_s->pending_listen_freq = 0;
2543 return;
2544 }
2545 wpa_s->off_channel_freq = 0;
2546 wpa_s->roc_waiting_drv_freq = lwork->freq;
2547}
2548
2549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550static int wpas_start_listen(void *ctx, unsigned int freq,
2551 unsigned int duration,
2552 const struct wpabuf *probe_resp_ie)
2553{
2554 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002555 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002557 if (wpa_s->p2p_listen_work) {
2558 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 return -1;
2560 }
2561
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002562 lwork = os_zalloc(sizeof(*lwork));
2563 if (lwork == NULL)
2564 return -1;
2565 lwork->freq = freq;
2566 lwork->duration = duration;
2567 if (probe_resp_ie) {
2568 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2569 if (lwork->probe_resp_ie == NULL) {
2570 wpas_p2p_listen_work_free(lwork);
2571 return -1;
2572 }
2573 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002575 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2576 lwork) < 0) {
2577 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 return -1;
2579 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580
2581 return 0;
2582}
2583
2584
2585static void wpas_stop_listen(void *ctx)
2586{
2587 struct wpa_supplicant *wpa_s = ctx;
2588 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2589 wpa_drv_cancel_remain_on_channel(wpa_s);
2590 wpa_s->off_channel_freq = 0;
2591 wpa_s->roc_waiting_drv_freq = 0;
2592 }
2593 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002594
2595 /*
2596 * Don't cancel Probe Request RX reporting for a connected P2P Client
2597 * handling Probe Request frames.
2598 */
2599 if (!wpa_s->p2p_cli_probe)
2600 wpa_drv_probe_req_report(wpa_s, 0);
2601
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002602 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603}
2604
2605
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002606static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2607 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608{
2609 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002610 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2611 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612}
2613
2614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2616 const u8 *peer, const char *params,
2617 unsigned int generated_pin)
2618{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002619 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2620 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621}
2622
2623
2624static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2625 const u8 *peer, const char *params)
2626{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002627 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2628 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629}
2630
2631
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002632static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2633 const u8 *dev_addr, const u8 *pri_dev_type,
2634 const char *dev_name, u16 supp_config_methods,
2635 u8 dev_capab, u8 group_capab, const u8 *group_id,
2636 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637{
2638 struct wpa_supplicant *wpa_s = ctx;
2639 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 u8 empty_dev_type[8];
2642 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002643 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002644 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002645
2646 if (group_id) {
2647 for (group = wpa_s->global->ifaces; group; group = group->next)
2648 {
2649 struct wpa_ssid *s = group->current_ssid;
2650 if (s != NULL &&
2651 s->mode == WPAS_MODE_P2P_GO &&
2652 group_id_len - ETH_ALEN == s->ssid_len &&
2653 os_memcmp(group_id + ETH_ALEN, s->ssid,
2654 s->ssid_len) == 0)
2655 break;
2656 }
2657 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658
2659 if (pri_dev_type == NULL) {
2660 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2661 pri_dev_type = empty_dev_type;
2662 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002663 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2664 " pri_dev_type=%s name='%s' config_methods=0x%x "
2665 "dev_capab=0x%x group_capab=0x%x%s%s",
2666 MAC2STR(dev_addr),
2667 wps_dev_type_bin2str(pri_dev_type, devtype,
2668 sizeof(devtype)),
2669 dev_name, supp_config_methods, dev_capab, group_capab,
2670 group ? " group=" : "",
2671 group ? group->ifname : "");
2672 if (os_snprintf_error(sizeof(params), res))
2673 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002677 if (wps_generate_pin(&generated_pin) < 0) {
2678 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2679 wpas_notify_p2p_provision_discovery(
2680 wpa_s, peer, 0 /* response */,
2681 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2682 return;
2683 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 wpas_prov_disc_local_display(wpa_s, peer, params,
2685 generated_pin);
2686 } else if (config_methods & WPS_CONFIG_KEYPAD)
2687 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2688 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002689 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2690 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002691
2692 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2693 P2P_PROV_DISC_SUCCESS,
2694 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695}
2696
2697
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002698static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699{
2700 struct wpa_supplicant *wpa_s = ctx;
2701 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002702 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002704 if (wpa_s->pending_pd_before_join &&
2705 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2706 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2707 wpa_s->pending_pd_before_join = 0;
2708 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2709 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002710 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002711 return;
2712 }
2713
Dmitry Shmidt04949592012-07-19 12:16:46 -07002714 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002715 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2716 int res;
2717
2718 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2719 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2720 if (os_snprintf_error(sizeof(params), res))
2721 params[sizeof(params) - 1] = '\0';
2722 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002723 params[0] = '\0';
2724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002726 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002728 if (wps_generate_pin(&generated_pin) < 0) {
2729 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2730 wpas_notify_p2p_provision_discovery(
2731 wpa_s, peer, 0 /* response */,
2732 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2733 return;
2734 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002735 wpas_prov_disc_local_display(wpa_s, peer, params,
2736 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002738 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2739 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740
Jouni Malinen75ecf522011-06-27 15:19:46 -07002741 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2742 P2P_PROV_DISC_SUCCESS,
2743 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744}
2745
2746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002747static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002748 enum p2p_prov_disc_status status,
2749 u32 adv_id, const u8 *adv_mac,
2750 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002751{
2752 struct wpa_supplicant *wpa_s = ctx;
2753
Dmitry Shmidt04949592012-07-19 12:16:46 -07002754 if (wpa_s->p2p_fallback_to_go_neg) {
2755 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2756 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002757 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002758 P2P_EVENT_FALLBACK_TO_GO_NEG
2759 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002760 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2761 return;
2762 }
2763
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002764 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002765 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002766 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2767 "join-existing-group operation (no ACK for PD "
2768 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002769 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002770 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002771 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002772
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002773 if (adv_id && adv_mac && deferred_session_resp) {
2774 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2775 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2776 " deferred_session_resp='%s'",
2777 MAC2STR(peer), status, adv_id,
2778 deferred_session_resp);
2779 } else if (adv_id && adv_mac) {
2780 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2781 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2782 MAC2STR(peer), status, adv_id);
2783 } else {
2784 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2785 " p2p_dev_addr=" MACSTR " status=%d",
2786 MAC2STR(peer), status);
2787 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002788
Jouni Malinen75ecf522011-06-27 15:19:46 -07002789 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2790 status, 0, 0);
2791}
2792
2793
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002794static int freq_included(struct wpa_supplicant *wpa_s,
2795 const struct p2p_channels *channels,
2796 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002797{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002798 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2799 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2800 return 1;
2801 return 0;
2802}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002803
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002804
2805static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2806{
2807 unsigned int num = P2P_MAX_CHANNELS;
2808 int *common_freqs;
2809 int ret;
2810
2811 p2p_go_dump_common_freqs(wpa_s);
2812 common_freqs = os_calloc(num, sizeof(int));
2813 if (!common_freqs)
2814 return;
2815
2816 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2817 if (ret < 0) {
2818 wpa_dbg(wpa_s, MSG_DEBUG,
2819 "P2P: Failed to get group common freqs");
2820 os_free(common_freqs);
2821 return;
2822 }
2823
2824 os_free(wpa_s->p2p_group_common_freqs);
2825 wpa_s->p2p_group_common_freqs = common_freqs;
2826 wpa_s->p2p_group_common_freqs_num = num;
2827 p2p_go_dump_common_freqs(wpa_s);
2828}
2829
2830
2831/*
2832 * Check if the given frequency is one of the possible operating frequencies
2833 * set after the completion of the GO Negotiation.
2834 */
2835static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2836{
2837 unsigned int i;
2838
2839 p2p_go_dump_common_freqs(wpa_s);
2840
2841 /* assume no restrictions */
2842 if (!wpa_s->p2p_group_common_freqs_num)
2843 return 1;
2844
2845 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2846 if (wpa_s->p2p_group_common_freqs[i] == freq)
2847 return 1;
2848 }
2849 return 0;
2850}
2851
2852
2853static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2854 struct sta_info *sta, void *ctx)
2855{
2856 int *ecsa_support = ctx;
2857
2858 *ecsa_support &= sta->ecsa_supported;
2859
2860 return 0;
2861}
2862
2863
2864/* Check if all the peers support eCSA */
2865static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2866{
2867 int ecsa_support = 1;
2868
2869 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2870 &ecsa_support);
2871
2872 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002873}
2874
2875
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002876/**
2877 * Pick the best frequency to use from all the currently used frequencies.
2878 */
2879static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2880 struct wpa_used_freq_data *freqs,
2881 unsigned int num)
2882{
2883 unsigned int i, c;
2884
2885 /* find a candidate freq that is supported by P2P */
2886 for (c = 0; c < num; c++)
2887 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2888 break;
2889
2890 if (c == num)
2891 return 0;
2892
2893 /* once we have a candidate, try to find a 'better' one */
2894 for (i = c + 1; i < num; i++) {
2895 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2896 continue;
2897
2898 /*
2899 * 1. Infrastructure station interfaces have higher preference.
2900 * 2. P2P Clients have higher preference.
2901 * 3. All others.
2902 */
2903 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2904 c = i;
2905 break;
2906 }
2907
2908 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2909 c = i;
2910 }
2911 return freqs[c].freq;
2912}
2913
2914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2916 const u8 *go_dev_addr, const u8 *ssid,
2917 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002918 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002919 const struct p2p_channels *channels,
2920 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921{
2922 struct wpa_supplicant *wpa_s = ctx;
2923 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002924 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002926 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927
2928 if (!persistent_group) {
2929 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002930 " to join an active group (SSID: %s)",
2931 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2933 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2934 == 0 ||
2935 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2936 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2937 "authorized invitation");
2938 goto accept_inv;
2939 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002940
2941#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002942 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2943 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002944 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002945 wpa_s->p2p_wps_method = WPS_NFC;
2946 wpa_s->pending_join_wps_method = WPS_NFC;
2947 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002948 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002949 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002950 bssid, ETH_ALEN);
2951 goto accept_inv;
2952 }
2953#endif /* CONFIG_WPS_NFC */
2954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955 /*
2956 * Do not accept the invitation automatically; notify user and
2957 * request approval.
2958 */
2959 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2960 }
2961
2962 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2963 if (grp) {
2964 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2965 "running persistent group");
2966 if (*go)
2967 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2968 goto accept_inv;
2969 }
2970
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002971 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2972 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2973 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2974 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002975 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002976 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2978
2979 for (s = wpa_s->conf->ssid; s; s = s->next) {
2980 if (s->disabled == 2 &&
2981 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2982 s->ssid_len == ssid_len &&
2983 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2984 break;
2985 }
2986
2987 if (!s) {
2988 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2989 " requested reinvocation of an unknown group",
2990 MAC2STR(sa));
2991 return P2P_SC_FAIL_UNKNOWN_GROUP;
2992 }
2993
2994 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2995 *go = 1;
2996 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2997 wpa_printf(MSG_DEBUG, "P2P: The only available "
2998 "interface is already in use - reject "
2999 "invitation");
3000 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3001 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003002 if (wpa_s->p2p_mgmt)
3003 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3004 ETH_ALEN);
3005 else
3006 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 } else if (s->mode == WPAS_MODE_P2P_GO) {
3008 *go = 1;
3009 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3010 {
3011 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3012 "interface address for the group");
3013 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3014 }
3015 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3016 ETH_ALEN);
3017 }
3018
3019accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003020 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3021
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003022 best_freq = 0;
3023 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3024 sizeof(struct wpa_used_freq_data));
3025 if (freqs) {
3026 int num_channels = wpa_s->num_multichan_concurrent;
3027 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3028 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3029 os_free(freqs);
3030 }
3031
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003032 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003033 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003034 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003035 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003036
3037 if (wpa_s->num_multichan_concurrent < 2 ||
3038 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3039 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 -07003040 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003041 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042 }
3043
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003044 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3045 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003046 if (*go == 0) {
3047 /* We are the client */
3048 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3049 "running a GO but we are capable of MCC, "
3050 "figure out the best channel to use");
3051 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003052 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003053 /* We are the GO, and *force_freq is not in the
3054 * intersection */
3055 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3056 "in intersection but we are capable of MCC, "
3057 "figure out the best channel to use",
3058 *force_freq);
3059 *force_freq = 0;
3060 }
3061 }
3062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 return P2P_SC_SUCCESS;
3064}
3065
3066
3067static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3068 const u8 *ssid, size_t ssid_len,
3069 const u8 *go_dev_addr, u8 status,
3070 int op_freq)
3071{
3072 struct wpa_supplicant *wpa_s = ctx;
3073 struct wpa_ssid *s;
3074
3075 for (s = wpa_s->conf->ssid; s; s = s->next) {
3076 if (s->disabled == 2 &&
3077 s->ssid_len == ssid_len &&
3078 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3079 break;
3080 }
3081
3082 if (status == P2P_SC_SUCCESS) {
3083 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003084 " was accepted; op_freq=%d MHz, SSID=%s",
3085 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003087 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003088 if (go) {
3089 wpa_msg_global(wpa_s, MSG_INFO,
3090 P2P_EVENT_INVITATION_ACCEPTED
3091 "sa=" MACSTR
3092 " persistent=%d freq=%d",
3093 MAC2STR(sa), s->id, op_freq);
3094 } else {
3095 wpa_msg_global(wpa_s, MSG_INFO,
3096 P2P_EVENT_INVITATION_ACCEPTED
3097 "sa=" MACSTR
3098 " persistent=%d",
3099 MAC2STR(sa), s->id);
3100 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 wpas_p2p_group_add_persistent(
Hai Shalom74f70d42019-02-11 14:42:39 -08003102 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003103 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3104 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003106 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003107 wpa_msg_global(wpa_s, MSG_INFO,
3108 P2P_EVENT_INVITATION_ACCEPTED
3109 "sa=" MACSTR " go_dev_addr=" MACSTR
3110 " bssid=" MACSTR " unknown-network",
3111 MAC2STR(sa), MAC2STR(go_dev_addr),
3112 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003114 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003115 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 }
3117 return;
3118 }
3119
3120 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3121 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3122 " was rejected (status %u)", MAC2STR(sa), status);
3123 return;
3124 }
3125
3126 if (!s) {
3127 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003128 wpa_msg_global(wpa_s, MSG_INFO,
3129 P2P_EVENT_INVITATION_RECEIVED
3130 "sa=" MACSTR " go_dev_addr=" MACSTR
3131 " bssid=" MACSTR " unknown-network",
3132 MAC2STR(sa), MAC2STR(go_dev_addr),
3133 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003135 wpa_msg_global(wpa_s, MSG_INFO,
3136 P2P_EVENT_INVITATION_RECEIVED
3137 "sa=" MACSTR " go_dev_addr=" MACSTR
3138 " unknown-network",
3139 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003141 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3142 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 return;
3144 }
3145
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003146 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003147 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3148 "sa=" MACSTR " persistent=%d freq=%d",
3149 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003150 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003151 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3152 "sa=" MACSTR " persistent=%d",
3153 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003154 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003155 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3156 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157}
3158
3159
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003160static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3161 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003162 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003163{
3164 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003165 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003166
3167 if (ssid == NULL)
3168 return;
3169
3170 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003171 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003172 ETH_ALEN) == 0)
3173 break;
3174 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003175 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003176 if (ssid->mode != WPAS_MODE_P2P_GO &&
3177 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3178 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3179 "due to invitation result", ssid->id);
3180 wpas_notify_network_removed(wpa_s, ssid);
3181 wpa_config_remove_network(wpa_s->conf, ssid->id);
3182 return;
3183 }
3184 return; /* Peer not found in client list */
3185 }
3186
3187 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003188 "group %d client list%s",
3189 MAC2STR(peer), ssid->id,
3190 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003191 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3192 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3193 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003194 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003195 if (p2p_wpa_s->conf->update_config &&
3196 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003197 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003198}
3199
3200
3201static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3202 const u8 *peer)
3203{
3204 struct wpa_ssid *ssid;
3205
3206 wpa_s = wpa_s->global->p2p_invite_group;
3207 if (wpa_s == NULL)
3208 return; /* No known invitation group */
3209 ssid = wpa_s->current_ssid;
3210 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3211 !ssid->p2p_persistent_group)
3212 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003213 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003214 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003215 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003216}
3217
3218
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003219static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003220 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003221 const u8 *peer, int neg_freq,
3222 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223{
3224 struct wpa_supplicant *wpa_s = ctx;
3225 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003226 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227
3228 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003229 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3230 "status=%d " MACSTR,
3231 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003233 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3234 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 }
3236 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3237
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003238 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3239 status, MAC2STR(peer));
3240 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003241 struct wpa_supplicant *group_if =
3242 wpa_s->global->p2p_invite_group;
3243
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003244 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3245 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003246
3247 /*
3248 * Invitation to an active group. If this is successful and we
3249 * are the GO, set the client wait to postpone some concurrent
3250 * operations and to allow provisioning and connection to happen
3251 * more quickly.
3252 */
3253 if (status == P2P_SC_SUCCESS &&
3254 group_if && group_if->current_ssid &&
3255 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3256 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3257#ifdef CONFIG_TESTING_OPTIONS
3258 if (group_if->p2p_go_csa_on_inv) {
3259 wpa_printf(MSG_DEBUG,
3260 "Testing: force P2P GO CSA after invitation");
3261 eloop_cancel_timeout(
3262 wpas_p2p_reconsider_moving_go,
3263 wpa_s, NULL);
3264 eloop_register_timeout(
3265 0, 50000,
3266 wpas_p2p_reconsider_moving_go,
3267 wpa_s, NULL);
3268 }
3269#endif /* CONFIG_TESTING_OPTIONS */
3270 }
3271 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003272 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003273
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003274 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3275 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3276 "invitation exchange to indicate readiness for "
3277 "re-invocation");
3278 }
3279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003281 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3282 ssid = wpa_config_get_network(
3283 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003284 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 wpas_p2p_remove_pending_group_interface(wpa_s);
3287 return;
3288 }
3289
3290 ssid = wpa_config_get_network(wpa_s->conf,
3291 wpa_s->pending_invite_ssid_id);
3292 if (ssid == NULL) {
3293 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3294 "data matching with invitation");
3295 return;
3296 }
3297
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003298 /*
3299 * The peer could have missed our ctrl::ack frame for Invitation
3300 * Response and continue retransmitting the frame. To reduce the
3301 * likelihood of the peer not getting successful TX status for the
3302 * Invitation Response frame, wait a short time here before starting
3303 * the persistent group so that we will remain on the current channel to
3304 * acknowledge any possible retransmission from the peer.
3305 */
3306 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3307 "starting persistent group");
3308 os_sleep(0, 50000);
3309
Dmitry Shmidt15907092014-03-25 10:42:57 -07003310 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003311 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003312 freq = neg_freq;
3313 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003314 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003315 freq = peer_oper_freq;
3316 else
3317 freq = 0;
3318
3319 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3320 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003322 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003323 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003324 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003325 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003326 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003327 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003328 wpa_s->p2p_go_he,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003329 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003330 ssid->mode == WPAS_MODE_P2P_GO ?
3331 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003332 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333}
3334
3335
Dmitry Shmidt04949592012-07-19 12:16:46 -07003336static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3337 unsigned int freq)
3338{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003339 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3340 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003341 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003342}
3343
3344
3345static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3346{
3347 reg->channel[reg->channels] = chan;
3348 reg->channels++;
3349}
3350
3351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003352static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003353 struct p2p_channels *chan,
3354 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355{
3356 int i, cla = 0;
3357
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003358 wpa_s->global->p2p_24ghz_social_channels = 1;
3359
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003360 os_memset(cli_chan, 0, sizeof(*cli_chan));
3361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3363 "band");
3364
3365 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3366 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003367 chan->reg_class[cla].channels = 0;
3368 for (i = 0; i < 11; i++) {
3369 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3370 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3371 }
3372 if (chan->reg_class[cla].channels)
3373 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374
3375 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3376 "band");
3377
3378 /* Operating class 115 - 5 GHz, channels 36-48 */
3379 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003380 chan->reg_class[cla].channels = 0;
3381 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3382 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3383 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3384 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3385 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3386 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3387 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3388 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3389 if (chan->reg_class[cla].channels)
3390 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391
3392 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3393 "band");
3394
3395 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3396 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003397 chan->reg_class[cla].channels = 0;
3398 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3399 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3400 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3401 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3402 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3403 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3404 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3405 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3406 if (chan->reg_class[cla].channels)
3407 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408
3409 chan->reg_classes = cla;
3410 return 0;
3411}
3412
3413
Dmitry Shmidt04949592012-07-19 12:16:46 -07003414static int has_channel(struct wpa_global *global,
3415 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416{
3417 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003418 unsigned int freq;
3419
3420 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3421 chan * 5;
3422 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003423 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424
3425 for (i = 0; i < mode->num_channels; i++) {
3426 if (mode->channels[i].chan == chan) {
3427 if (flags)
3428 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003429 if (mode->channels[i].flag &
3430 (HOSTAPD_CHAN_DISABLED |
3431 HOSTAPD_CHAN_RADAR))
3432 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003433 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3434 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003435 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436 }
3437 }
3438
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003439 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440}
3441
3442
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003443static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3444 struct hostapd_hw_modes *mode,
3445 u8 channel)
3446{
3447 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003448 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003449
3450 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3451 return 0;
3452
3453 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3454 /*
3455 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3456 * so the center channel is 6 channels away from the start/end.
3457 */
3458 if (channel >= center_channels[i] - 6 &&
3459 channel <= center_channels[i] + 6)
3460 return center_channels[i];
3461
3462 return 0;
3463}
3464
3465
3466static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3467 struct hostapd_hw_modes *mode,
3468 u8 channel, u8 bw)
3469{
3470 u8 center_chan;
3471 int i, flags;
3472 enum chan_allowed res, ret = ALLOWED;
3473
3474 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3475 if (!center_chan)
3476 return NOT_ALLOWED;
3477 if (center_chan >= 58 && center_chan <= 138)
3478 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3479
3480 /* check all the channels are available */
3481 for (i = 0; i < 4; i++) {
3482 int adj_chan = center_chan - 6 + i * 4;
3483
3484 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3485 if (res == NOT_ALLOWED)
3486 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003487 if (res == NO_IR)
3488 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003489
3490 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3491 return NOT_ALLOWED;
3492 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3493 return NOT_ALLOWED;
3494 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3495 return NOT_ALLOWED;
3496 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3497 return NOT_ALLOWED;
3498 }
3499
3500 return ret;
3501}
3502
3503
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003504static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3505 struct hostapd_hw_modes *mode,
3506 u8 channel)
3507{
3508 u8 center_channels[] = { 50, 114 };
3509 unsigned int i;
3510
3511 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3512 return 0;
3513
3514 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3515 /*
3516 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3517 * so the center channel is 14 channels away from the start/end.
3518 */
3519 if (channel >= center_channels[i] - 14 &&
3520 channel <= center_channels[i] + 14)
3521 return center_channels[i];
3522
3523 return 0;
3524}
3525
3526
3527static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3528 struct hostapd_hw_modes *mode,
3529 u8 channel, u8 bw)
3530{
3531 u8 center_chan;
3532 int i, flags;
3533 enum chan_allowed res, ret = ALLOWED;
3534
3535 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3536 if (!center_chan)
3537 return NOT_ALLOWED;
3538 /* VHT 160 MHz uses DFS channels in most countries. */
3539
3540 /* Check all the channels are available */
3541 for (i = 0; i < 8; i++) {
3542 int adj_chan = center_chan - 14 + i * 4;
3543
3544 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3545 if (res == NOT_ALLOWED)
3546 return NOT_ALLOWED;
3547
3548 if (res == NO_IR)
3549 ret = NO_IR;
3550
3551 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3552 return NOT_ALLOWED;
3553 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3554 return NOT_ALLOWED;
3555 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3556 return NOT_ALLOWED;
3557 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3558 return NOT_ALLOWED;
3559 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3560 return NOT_ALLOWED;
3561 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3562 return NOT_ALLOWED;
3563 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3564 return NOT_ALLOWED;
3565 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3566 return NOT_ALLOWED;
3567 }
3568
3569 return ret;
3570}
3571
3572
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003573static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3574 struct hostapd_hw_modes *mode,
3575 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003576{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003577 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003578 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003579
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003580 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3581 if (bw == BW40MINUS) {
3582 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3583 return NOT_ALLOWED;
3584 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3585 } else if (bw == BW40PLUS) {
3586 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3587 return NOT_ALLOWED;
3588 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3589 } else if (bw == BW80) {
3590 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003591 } else if (bw == BW160) {
3592 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003593 }
3594
3595 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3596 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003597 if (res == NO_IR || res2 == NO_IR)
3598 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003599 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003600}
3601
3602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003604 struct p2p_channels *chan,
3605 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003607 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003608 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003610 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3612 "of all supported channels; assume dualband "
3613 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003614 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 }
3616
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003617 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003619 for (op = 0; global_op_class[op].op_class; op++) {
3620 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003622 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003624 if (o->p2p == NO_P2P_SUPP)
3625 continue;
3626
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003627 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 if (mode == NULL)
3629 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003630 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3631 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003633 enum chan_allowed res;
3634 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3635 if (res == ALLOWED) {
3636 if (reg == NULL) {
3637 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3638 o->op_class);
3639 reg = &chan->reg_class[cla];
3640 cla++;
3641 reg->reg_class = o->op_class;
3642 }
3643 reg->channel[reg->channels] = ch;
3644 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003645 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003646 wpa_s->conf->p2p_add_cli_chan) {
3647 if (cli_reg == NULL) {
3648 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3649 o->op_class);
3650 cli_reg = &cli_chan->reg_class[cli_cla];
3651 cli_cla++;
3652 cli_reg->reg_class = o->op_class;
3653 }
3654 cli_reg->channel[cli_reg->channels] = ch;
3655 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003657 }
3658 if (reg) {
3659 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3660 reg->channel, reg->channels);
3661 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003662 if (cli_reg) {
3663 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3664 cli_reg->channel, cli_reg->channels);
3665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 }
3667
3668 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003669 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671 return 0;
3672}
3673
3674
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003675int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3676 struct hostapd_hw_modes *mode, u8 channel)
3677{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003678 int op;
3679 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003680
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003681 for (op = 0; global_op_class[op].op_class; op++) {
3682 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003683 u8 ch;
3684
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003685 if (o->p2p == NO_P2P_SUPP)
3686 continue;
3687
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003688 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3689 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003690 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3691 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003692 continue;
3693 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003694 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003695 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003696 }
3697 }
3698 return 0;
3699}
3700
3701
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003702int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3703 struct hostapd_hw_modes *mode, u8 channel)
3704{
3705 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3706 return 0;
3707
3708 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3709}
3710
3711
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003712int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3713 struct hostapd_hw_modes *mode, u8 channel)
3714{
3715 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3716 return 0;
3717 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3718}
3719
3720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3722 size_t buf_len)
3723{
3724 struct wpa_supplicant *wpa_s = ctx;
3725
3726 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3727 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3728 break;
3729 }
3730 if (wpa_s == NULL)
3731 return -1;
3732
3733 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3734}
3735
3736
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003737struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3738 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003739{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003740 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3741 struct wpa_ssid *s = wpa_s->current_ssid;
3742 if (s == NULL)
3743 continue;
3744 if (s->mode != WPAS_MODE_P2P_GO &&
3745 s->mode != WPAS_MODE_AP &&
3746 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3747 continue;
3748 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003749 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003750 continue;
3751 return wpa_s;
3752 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003753
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003754 return NULL;
3755
3756}
3757
3758
3759struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3760 const u8 *peer_dev_addr)
3761{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003762 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3763 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003764 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003765 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003766 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3767 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003768 }
3769
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003770 return NULL;
3771}
3772
3773
3774static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3775{
3776 struct wpa_supplicant *wpa_s = ctx;
3777
3778 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003779}
3780
3781
Dmitry Shmidt18463232014-01-24 12:29:41 -08003782static int wpas_is_concurrent_session_active(void *ctx)
3783{
3784 struct wpa_supplicant *wpa_s = ctx;
3785 struct wpa_supplicant *ifs;
3786
3787 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3788 if (ifs == wpa_s)
3789 continue;
3790 if (ifs->wpa_state > WPA_ASSOCIATED)
3791 return 1;
3792 }
3793 return 0;
3794}
3795
3796
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003797static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3798{
3799 struct wpa_supplicant *wpa_s = ctx;
3800 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3801}
3802
3803
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003804int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3805 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003806{
3807 struct wpa_interface iface;
3808 struct wpa_supplicant *p2pdev_wpa_s;
3809 char ifname[100];
3810 char force_name[100];
3811 int ret;
3812
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003813 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3814 wpa_s->ifname);
3815 if (os_snprintf_error(sizeof(ifname), ret))
3816 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003817 force_name[0] = '\0';
3818 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3819 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3820 force_name, wpa_s->pending_interface_addr, NULL);
3821 if (ret < 0) {
3822 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3823 return ret;
3824 }
3825 os_strlcpy(wpa_s->pending_interface_name, ifname,
3826 sizeof(wpa_s->pending_interface_name));
3827
3828 os_memset(&iface, 0, sizeof(iface));
3829 iface.p2p_mgmt = 1;
3830 iface.ifname = wpa_s->pending_interface_name;
3831 iface.driver = wpa_s->driver->name;
3832 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003833
3834 /*
3835 * If a P2P Device configuration file was given, use it as the interface
3836 * configuration file (instead of using parent's configuration file.
3837 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003838 if (conf_p2p_dev) {
3839 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003840 iface.ctrl_interface = NULL;
3841 } else {
3842 iface.confname = wpa_s->confname;
3843 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3844 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003845
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003846 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003847 if (!p2pdev_wpa_s) {
3848 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3849 return -1;
3850 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003851
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003852 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003853 wpa_s->pending_interface_name[0] = '\0';
3854 return 0;
3855}
3856
3857
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003858static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3859 const u8 *noa, size_t noa_len)
3860{
3861 struct wpa_supplicant *wpa_s, *intf = ctx;
3862 char hex[100];
3863
3864 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3865 if (wpa_s->waiting_presence_resp)
3866 break;
3867 }
3868 if (!wpa_s) {
3869 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3870 return;
3871 }
3872 wpa_s->waiting_presence_resp = 0;
3873
3874 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3875 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3876 " status=%u noa=%s", MAC2STR(src), status, hex);
3877}
3878
3879
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003880static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3881 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003882 u8 *ret_ssid, size_t *ret_ssid_len,
3883 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003884{
3885 struct wpa_supplicant *wpa_s = ctx;
3886 struct wpa_ssid *s;
3887
3888 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3889 if (s) {
3890 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3891 *ret_ssid_len = s->ssid_len;
3892 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003893
3894 if (s->mode != WPAS_MODE_P2P_GO) {
3895 os_memset(intended_iface_addr, 0, ETH_ALEN);
3896 } else if (wpas_p2p_create_iface(wpa_s)) {
3897 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3898 return 0;
3899
3900 os_memcpy(intended_iface_addr,
3901 wpa_s->pending_interface_addr, ETH_ALEN);
3902 } else {
3903 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3904 ETH_ALEN);
3905 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003906 return 1;
3907 }
3908
3909 return 0;
3910}
3911
3912
3913static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003914 u8 *ssid, size_t *ssid_len, int *group_iface,
3915 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003916{
3917 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003918 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003919 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003920
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003921 /*
3922 * group_iface will be set to 1 only if a dedicated interface for P2P
3923 * role is required. First, we try to reuse an active GO. However,
3924 * if it is not present, we will try to reactivate an existing
3925 * persistent group and set group_iface to 1, so the caller will know
3926 * that the pending interface should be used.
3927 */
3928 *group_iface = 0;
3929
3930 if (freq)
3931 *freq = 0;
3932
3933 go = wpas_p2p_get_go_group(wpa_s);
3934 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003935 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003936 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003937 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003938 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3939 else
3940 return 0;
3941 } else {
3942 s = go->current_ssid;
3943 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3944 if (freq)
3945 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003946 }
3947
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003948 os_memcpy(ssid, s->ssid, s->ssid_len);
3949 *ssid_len = s->ssid_len;
3950
3951 return 1;
3952}
3953
3954
3955static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3956 const u8 *ssid, size_t ssid_len)
3957{
3958 struct wpa_supplicant *wpa_s = ctx;
3959 struct wpa_ssid *s;
3960 int save_config = 0;
3961 size_t i;
3962
3963 /* Start with our first choice of Persistent Groups */
3964 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3965 if (go && ssid && ssid_len &&
3966 s->ssid_len == ssid_len &&
3967 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3968 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3969 break;
3970
3971 /* Remove stale persistent group */
3972 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003973 wpa_dbg(wpa_s, MSG_DEBUG,
3974 "P2P: Remove stale persistent group id=%d",
3975 s->id);
3976 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003977 wpa_config_remove_network(wpa_s->conf, s->id);
3978 save_config = 1;
3979 continue;
3980 }
3981
3982 for (i = 0; i < s->num_p2p_clients; i++) {
3983 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3984 peer, ETH_ALEN) != 0)
3985 continue;
3986
3987 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3988 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3989 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3990 break;
3991 }
3992 s->num_p2p_clients--;
3993 save_config = 1;
3994 }
3995
3996 if (save_config)
3997 p2p_config_write(wpa_s);
3998
3999 /* Return TRUE if valid SSID remains */
4000 return s != NULL;
4001}
4002
4003
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004004static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4005 const u8 *feat_cap, size_t feat_cap_len)
4006{
4007 static const char pref[] = " feature_cap=";
4008 int ret;
4009
4010 buf[0] = '\0';
4011
4012 /*
4013 * We expect a feature capability to contain at least one byte to be
4014 * reported. The string buffer provided by the caller function is
4015 * expected to be big enough to contain all bytes of the attribute for
4016 * known specifications. This function truncates the reported bytes if
4017 * the feature capability data exceeds the string buffer size.
4018 */
4019 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4020 return;
4021
4022 os_memcpy(buf, pref, sizeof(pref));
4023 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4024 buf_len - sizeof(pref) + 1,
4025 feat_cap, feat_cap_len);
4026
4027 if (ret != (2 * (int) feat_cap_len))
4028 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4029}
4030
4031
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004032static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4033 const u8 *adv_mac, const u8 *ses_mac,
4034 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4035 u8 conncap, int passwd_id,
4036 const u8 *persist_ssid,
4037 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004038 int prov_start, const char *session_info,
4039 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004040 unsigned int freq,
4041 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004042{
4043 struct wpa_supplicant *wpa_s = ctx;
4044 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004045 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004046 int save_config = 0;
4047 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004048 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004049
4050 if (!dev)
4051 return;
4052
4053 os_memset(mac, 0, ETH_ALEN);
4054 if (!adv_mac)
4055 adv_mac = mac;
4056 if (!ses_mac)
4057 ses_mac = mac;
4058 if (!grp_mac)
4059 grp_mac = mac;
4060
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004061 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4062 feat_cap, feat_cap_len);
4063
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004064 if (prov_start) {
4065 if (session_info == NULL) {
4066 wpa_msg_global(wpa_s, MSG_INFO,
4067 P2P_EVENT_P2PS_PROVISION_START MACSTR
4068 " adv_id=%x conncap=%x"
4069 " adv_mac=" MACSTR
4070 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004071 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004072 MAC2STR(dev), adv_id, conncap,
4073 MAC2STR(adv_mac),
4074 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004075 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004076 } else {
4077 wpa_msg_global(wpa_s, MSG_INFO,
4078 P2P_EVENT_P2PS_PROVISION_START MACSTR
4079 " adv_id=%x conncap=%x"
4080 " adv_mac=" MACSTR
4081 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004082 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004083 MAC2STR(dev), adv_id, conncap,
4084 MAC2STR(adv_mac),
4085 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004086 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004087 }
4088 return;
4089 }
4090
4091 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4092 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4093
4094 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4095 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4096 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4097
4098 if (persistent_go && !persistent_go->num_p2p_clients) {
4099 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004100 wpa_dbg(wpa_s, MSG_DEBUG,
4101 "P2P: Remove empty persistent group id=%d",
4102 persistent_go->id);
4103 wpas_notify_persistent_group_removed(wpa_s,
4104 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004105 wpa_config_remove_network(wpa_s->conf,
4106 persistent_go->id);
4107 }
4108
4109 wpa_msg_global(wpa_s, MSG_INFO,
4110 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4111 " status=%d"
4112 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004113 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004114 MAC2STR(dev), status,
4115 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004116 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004117 return;
4118 }
4119
4120 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004121 if (persist_ssid && persist_ssid_size)
4122 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4123 persist_ssid_size);
4124
4125 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4126 is_zero_ether_addr(grp_mac)) {
4127 wpa_dbg(wpa_s, MSG_ERROR,
4128 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4129 return;
4130 }
4131
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004132 for (;;) {
4133 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4134 if (!stale)
4135 break;
4136
4137 if (s && s->ssid_len == stale->ssid_len &&
4138 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4139 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4140 break;
4141
4142 /* Remove stale persistent group */
4143 if (stale->mode != WPAS_MODE_P2P_GO ||
4144 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004145 wpa_dbg(wpa_s, MSG_DEBUG,
4146 "P2P: Remove stale persistent group id=%d",
4147 stale->id);
4148 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004149 wpa_config_remove_network(wpa_s->conf, stale->id);
4150 } else {
4151 size_t i;
4152
4153 for (i = 0; i < stale->num_p2p_clients; i++) {
4154 if (os_memcmp(stale->p2p_client_list +
4155 i * ETH_ALEN,
4156 dev, ETH_ALEN) == 0) {
4157 os_memmove(stale->p2p_client_list +
4158 i * ETH_ALEN,
4159 stale->p2p_client_list +
4160 (i + 1) * ETH_ALEN,
4161 (stale->num_p2p_clients -
4162 i - 1) * ETH_ALEN);
4163 break;
4164 }
4165 }
4166 stale->num_p2p_clients--;
4167 }
4168 save_config = 1;
4169 }
4170
4171 if (save_config)
4172 p2p_config_write(wpa_s);
4173
4174 if (s) {
4175 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4176 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4177
4178 if (persistent_go && s != persistent_go &&
4179 !persistent_go->num_p2p_clients) {
4180 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004181 wpa_dbg(wpa_s, MSG_DEBUG,
4182 "P2P: Remove empty persistent group id=%d",
4183 persistent_go->id);
4184 wpas_notify_persistent_group_removed(wpa_s,
4185 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004186 wpa_config_remove_network(wpa_s->conf,
4187 persistent_go->id);
4188 /* Save config */
4189 }
4190
4191 wpa_msg_global(wpa_s, MSG_INFO,
4192 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4193 " status=%d"
4194 " adv_id=%x adv_mac=" MACSTR
4195 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004196 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004197 MAC2STR(dev), status,
4198 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004199 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004200 return;
4201 }
4202
Hai Shalom5f92bc92019-04-18 11:54:11 -07004203 wpa_s->global->pending_p2ps_group = 0;
4204 wpa_s->global->pending_p2ps_group_freq = 0;
4205
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004206 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004207 /*
4208 * We need to copy the interface name. Simply saving a
4209 * pointer isn't enough, since if we use pending_interface_name
4210 * it will be overwritten when the group is added.
4211 */
4212 char go_ifname[100];
4213
4214 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004215 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004216 if (!response_done) {
4217 wpa_s->global->pending_p2ps_group = 1;
4218 wpa_s->global->pending_p2ps_group_freq = freq;
4219 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004220
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004221 if (!wpas_p2p_create_iface(wpa_s))
4222 os_memcpy(go_ifname, wpa_s->ifname,
4223 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004224 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004225 os_memcpy(go_ifname,
4226 wpa_s->pending_interface_name,
4227 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004228
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004229 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004230 wpas_p2ps_prov_complete(
4231 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4232 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004233 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004234 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4235 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004236 return;
4237 }
4238
4239 /* If PD Resp complete, start up the GO */
4240 if (response_done && persistent_go) {
4241 wpas_p2p_group_add_persistent(
4242 wpa_s, persistent_go,
Hai Shalom74f70d42019-02-11 14:42:39 -08004243 0, 0, freq, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004244 persistent_go->mode ==
4245 WPAS_MODE_P2P_GO ?
4246 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004247 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004248 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004249 wpas_p2p_group_add(wpa_s, 1, freq,
4250 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004251 }
4252
4253 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004254 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4255 ETH_ALEN);
4256 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004257 }
4258 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004259 os_memcpy(go_ifname, go_wpa_s->ifname,
4260 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004261
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004262 if (is_zero_ether_addr(grp_mac)) {
4263 wpa_dbg(go_wpa_s, MSG_DEBUG,
4264 "P2P: Setting PIN-1 for ANY");
4265 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4266 "12345670", NULL, 0,
4267 0);
4268 } else {
4269 wpa_dbg(go_wpa_s, MSG_DEBUG,
4270 "P2P: Setting PIN-1 for " MACSTR,
4271 MAC2STR(grp_mac));
4272 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4273 "12345670", NULL, 0,
4274 0);
4275 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004276
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004277 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4278 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004279 }
4280
4281 wpa_msg_global(wpa_s, MSG_INFO,
4282 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4283 " status=%d conncap=%x"
4284 " adv_id=%x adv_mac=" MACSTR
4285 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004286 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004287 MAC2STR(dev), status, conncap,
4288 adv_id, MAC2STR(adv_mac),
4289 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004290 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004291 return;
4292 }
4293
4294 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4295 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4296
4297 if (persistent_go && !persistent_go->num_p2p_clients) {
4298 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004299 wpa_dbg(wpa_s, MSG_DEBUG,
4300 "P2P: Remove empty persistent group id=%d",
4301 persistent_go->id);
4302 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004303 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4304 }
4305
4306 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004307 char ssid_hex[32 * 2 + 1];
4308
4309 if (group_ssid)
4310 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4311 group_ssid, group_ssid_len);
4312 else
4313 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004314 wpa_msg_global(wpa_s, MSG_INFO,
4315 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4316 " status=%d conncap=%x"
4317 " adv_id=%x adv_mac=" MACSTR
4318 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004319 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004320 MAC2STR(dev), status, conncap,
4321 adv_id, MAC2STR(adv_mac),
4322 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004323 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4324 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004325 } else {
4326 wpa_msg_global(wpa_s, MSG_INFO,
4327 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4328 " status=%d conncap=%x"
4329 " adv_id=%x adv_mac=" MACSTR
4330 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004331 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004332 MAC2STR(dev), status, conncap,
4333 adv_id, MAC2STR(adv_mac),
4334 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004335 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004336 }
4337}
4338
4339
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004340static int _wpas_p2p_in_progress(void *ctx)
4341{
4342 struct wpa_supplicant *wpa_s = ctx;
4343 return wpas_p2p_in_progress(wpa_s);
4344}
4345
4346
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004347static int wpas_prov_disc_resp_cb(void *ctx)
4348{
4349 struct wpa_supplicant *wpa_s = ctx;
4350 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004351 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004352
4353 if (!wpa_s->global->pending_p2ps_group)
4354 return 0;
4355
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004356 freq = wpa_s->global->pending_p2ps_group_freq;
4357 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004358 wpa_s->global->pending_p2ps_group = 0;
4359
4360 if (wpas_p2p_get_go_group(wpa_s))
4361 return 0;
4362 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4363
4364 if (persistent_go) {
4365 wpas_p2p_group_add_persistent(
Hai Shalom74f70d42019-02-11 14:42:39 -08004366 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004367 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004368 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004369 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08004370 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004371 }
4372
4373 return 1;
4374}
4375
4376
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004377static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4378 unsigned int *len,
4379 unsigned int *freq_list)
4380{
4381 struct wpa_supplicant *wpa_s = ctx;
4382
4383 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4384 WPA_IF_P2P_CLIENT, len, freq_list);
4385}
4386
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004387int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4388{
4389 u8 addr[ETH_ALEN] = {0};
4390
4391 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4392 return 0;
4393
4394 if (wpa_s->conf->ssid == NULL) {
4395 if (random_mac_addr(addr) < 0) {
4396 wpa_msg(wpa_s, MSG_INFO,
4397 "Failed to generate random MAC address");
4398 return -EINVAL;
4399 }
4400
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004401 /* Store generated MAC address. */
4402 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4403 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004404 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004405 /* If there are existing saved groups, restore last MAC address.
4406 * if there is no last used MAC address, the last one is
4407 * factory MAC. */
4408 if (is_zero_ether_addr(
4409 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004410 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004411 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4412 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004413 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4414 }
4415
4416 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4417 wpa_msg(wpa_s, MSG_INFO,
4418 "Failed to set random MAC address");
4419 return -EINVAL;
4420 }
4421
4422 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4423 wpa_msg(wpa_s, MSG_INFO,
4424 "Could not update MAC address information");
4425 return -EINVAL;
4426 }
4427
4428 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4429 MAC2STR(addr));
4430
4431 return 0;
4432}
4433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004434/**
4435 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4436 * @global: Pointer to global data from wpa_supplicant_init()
4437 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4438 * Returns: 0 on success, -1 on failure
4439 */
4440int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4441{
4442 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443 int i;
4444
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004445 if (wpa_s->conf->p2p_disabled)
4446 return 0;
4447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4449 return 0;
4450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451 if (global->p2p)
4452 return 0;
4453
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004454 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4455 wpa_msg(wpa_s, MSG_ERROR,
4456 "Failed to initialize P2P random MAC address.");
4457 return -1;
4458 }
4459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004460 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004462 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463 p2p.p2p_scan = wpas_p2p_scan;
4464 p2p.send_action = wpas_send_action;
4465 p2p.send_action_done = wpas_send_action_done;
4466 p2p.go_neg_completed = wpas_go_neg_completed;
4467 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4468 p2p.dev_found = wpas_dev_found;
4469 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004470 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471 p2p.start_listen = wpas_start_listen;
4472 p2p.stop_listen = wpas_stop_listen;
4473 p2p.send_probe_resp = wpas_send_probe_resp;
4474 p2p.sd_request = wpas_sd_request;
4475 p2p.sd_response = wpas_sd_response;
4476 p2p.prov_disc_req = wpas_prov_disc_req;
4477 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004478 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 p2p.invitation_process = wpas_invitation_process;
4480 p2p.invitation_received = wpas_invitation_received;
4481 p2p.invitation_result = wpas_invitation_result;
4482 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004483 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004484 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004485 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004486 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004487 p2p.get_persistent_group = wpas_get_persistent_group;
4488 p2p.get_go_info = wpas_get_go_info;
4489 p2p.remove_stale_groups = wpas_remove_stale_groups;
4490 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4491 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4492 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004493 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494
4495 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004496 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 p2p.dev_name = wpa_s->conf->device_name;
4498 p2p.manufacturer = wpa_s->conf->manufacturer;
4499 p2p.model_name = wpa_s->conf->model_name;
4500 p2p.model_number = wpa_s->conf->model_number;
4501 p2p.serial_number = wpa_s->conf->serial_number;
4502 if (wpa_s->wps) {
4503 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4504 p2p.config_methods = wpa_s->wps->config_methods;
4505 }
4506
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004507 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4508 wpa_printf(MSG_ERROR,
4509 "P2P: Failed to configure supported channel list");
4510 return -1;
4511 }
4512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513 if (wpa_s->conf->p2p_listen_reg_class &&
4514 wpa_s->conf->p2p_listen_channel) {
4515 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4516 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004517 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519 /*
4520 * Pick one of the social channels randomly as the listen
4521 * channel.
4522 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004523 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004524 &p2p.channel,
4525 &global->p2p_go_avoid_freq,
4526 &global->p2p_disallow_freq) !=
4527 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004528 wpa_printf(MSG_INFO,
4529 "P2P: No social channels supported by the driver - do not enable P2P");
4530 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004531 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004532 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004534 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4535 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536
4537 if (wpa_s->conf->p2p_oper_reg_class &&
4538 wpa_s->conf->p2p_oper_channel) {
4539 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4540 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4541 p2p.cfg_op_channel = 1;
4542 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4543 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4544
4545 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004547 * Use random operation channel from 2.4 GHz band social
4548 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4549 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004551 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004552 &p2p.op_channel, NULL,
4553 NULL) != 0) {
4554 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004555 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004556 p2p.op_reg_class = 0;
4557 p2p.op_channel = 0;
4558 /* This will be overridden during group setup in
4559 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004561 p2p.cfg_op_channel = 0;
4562 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4563 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4564 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004565
4566 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4567 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4568 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4569 }
4570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004571 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4572 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4573 p2p.country[2] = 0x04;
4574 } else
4575 os_memcpy(p2p.country, "XX\x04", 3);
4576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4578 WPS_DEV_TYPE_LEN);
4579
4580 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4581 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4582 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4583
4584 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4585 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4586
4587 p2p.max_peers = 100;
4588
4589 if (wpa_s->conf->p2p_ssid_postfix) {
4590 p2p.ssid_postfix_len =
4591 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4592 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4593 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4594 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4595 p2p.ssid_postfix_len);
4596 }
4597
4598 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4599
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004600 p2p.max_listen = wpa_s->max_remain_on_chan;
4601
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004602 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4603 wpa_s->conf->p2p_passphrase_len <= 63)
4604 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4605 else
4606 p2p.passphrase_len = 8;
4607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608 global->p2p = p2p_init(&p2p);
4609 if (global->p2p == NULL)
4610 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004611 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612
4613 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4614 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4615 continue;
4616 p2p_add_wps_vendor_extension(
4617 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4618 }
4619
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004620 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4621
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622 return 0;
4623}
4624
4625
4626/**
4627 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4628 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4629 *
4630 * This function deinitialize per-interface P2P data.
4631 */
4632void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4633{
4634 if (wpa_s->driver && wpa_s->drv_priv)
4635 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004636
4637 if (wpa_s->go_params) {
4638 /* Clear any stored provisioning info */
4639 p2p_clear_provisioning_info(
4640 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004641 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004642 }
4643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004644 os_free(wpa_s->go_params);
4645 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004646 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4648 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4649 wpa_s->p2p_long_listen = 0;
4650 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4651 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4652 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004653 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004654 wpas_p2p_listen_work_done(wpa_s);
4655 if (wpa_s->p2p_send_action_work) {
4656 os_free(wpa_s->p2p_send_action_work->ctx);
4657 radio_work_done(wpa_s->p2p_send_action_work);
4658 wpa_s->p2p_send_action_work = NULL;
4659 }
4660 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004662 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4663 wpa_s->p2p_oob_dev_pw = NULL;
4664
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004665 os_free(wpa_s->p2p_group_common_freqs);
4666 wpa_s->p2p_group_common_freqs = NULL;
4667 wpa_s->p2p_group_common_freqs_num = 0;
4668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669 /* TODO: remove group interface from the driver if this wpa_s instance
4670 * is on top of a P2P group interface */
4671}
4672
4673
4674/**
4675 * wpas_p2p_deinit_global - Deinitialize global P2P module
4676 * @global: Pointer to global data from wpa_supplicant_init()
4677 *
4678 * This function deinitializes the global (per device) P2P module.
4679 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004680static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681{
4682 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004684 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004685
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004686 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687
4688 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4690 wpa_s = wpa_s->next;
4691 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 tmp = global->ifaces;
4693 while (tmp &&
4694 (tmp == wpa_s ||
4695 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4696 tmp = tmp->next;
4697 }
4698 if (tmp == NULL)
4699 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004700 /* Disconnect from the P2P group and deinit the interface */
4701 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 }
4703
4704 /*
4705 * Deinit GO data on any possibly remaining interface (if main
4706 * interface is used as GO).
4707 */
4708 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4709 if (wpa_s->ap_iface)
4710 wpas_p2p_group_deinit(wpa_s);
4711 }
4712
4713 p2p_deinit(global->p2p);
4714 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004715 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716}
4717
4718
4719static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4720{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004721 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004722 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723 if (wpa_s->drv_flags &
4724 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4725 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4726 return 1; /* P2P group requires a new interface in every case
4727 */
4728 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4729 return 0; /* driver does not support concurrent operations */
4730 if (wpa_s->global->ifaces->next)
4731 return 1; /* more that one interface already in use */
4732 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4733 return 1; /* this interface is already in use */
4734 return 0;
4735}
4736
4737
4738static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4739 const u8 *peer_addr,
4740 enum p2p_wps_method wps_method,
4741 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004743 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004745 if (persistent_group && wpa_s->conf->persistent_reconnect)
4746 persistent_group = 2;
4747
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004748 /*
4749 * Increase GO config timeout if HT40 is used since it takes some time
4750 * to scan channels for coex purposes before the BSS can be started.
4751 */
4752 p2p_set_config_timeout(wpa_s->global->p2p,
4753 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4756 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004757 persistent_group, ssid ? ssid->ssid : NULL,
4758 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004759 wpa_s->p2p_pd_before_go_neg, pref_freq,
4760 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4761 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762}
4763
4764
4765static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4766 const u8 *peer_addr,
4767 enum p2p_wps_method wps_method,
4768 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004769 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004770 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004771{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004772 if (persistent_group && wpa_s->conf->persistent_reconnect)
4773 persistent_group = 2;
4774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4776 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004777 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004778 ssid ? ssid->ssid_len : 0, pref_freq,
4779 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4780 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004781}
4782
4783
4784static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4785{
4786 wpa_s->p2p_join_scan_count++;
4787 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4788 wpa_s->p2p_join_scan_count);
4789 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4790 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4791 " for join operationg - stop join attempt",
4792 MAC2STR(wpa_s->pending_join_iface_addr));
4793 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004794 if (wpa_s->p2p_auto_pd) {
4795 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004796 wpa_msg_global(wpa_s, MSG_INFO,
4797 P2P_EVENT_PROV_DISC_FAILURE
4798 " p2p_dev_addr=" MACSTR " status=N/A",
4799 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004800 return;
4801 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004802 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004803 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004804 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805 }
4806}
4807
4808
Dmitry Shmidt04949592012-07-19 12:16:46 -07004809static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4810{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004811 int res;
4812 unsigned int num, i;
4813 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004814
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004815 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4816 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004817 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004818 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004819
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004820 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4821 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004822 if (!freqs)
4823 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004824
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004825 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4826 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004827
4828 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004829 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004830 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4831 freq);
4832 res = 0;
4833 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004834 }
4835 }
4836
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004837 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004838 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004839
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004840exit_free:
4841 os_free(freqs);
4842 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004843}
4844
4845
4846static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4847 const u8 *peer_dev_addr)
4848{
4849 struct wpa_bss *bss;
4850 int updated;
4851
4852 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4853 if (bss == NULL)
4854 return -1;
4855 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4856 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4857 "last scan");
4858 return 0;
4859 }
4860
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004861 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4862 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004863 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4864 "%ld.%06ld (%supdated in last scan)",
4865 bss->last_update.sec, bss->last_update.usec,
4866 updated ? "": "not ");
4867
4868 return updated;
4869}
4870
4871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4873 struct wpa_scan_results *scan_res)
4874{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004875 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876 int freq;
4877 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4880
4881 if (wpa_s->global->p2p_disabled)
4882 return;
4883
Dmitry Shmidt04949592012-07-19 12:16:46 -07004884 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4885 scan_res ? (int) scan_res->num : -1,
4886 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887
4888 if (scan_res)
4889 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4890
Dmitry Shmidt04949592012-07-19 12:16:46 -07004891 if (wpa_s->p2p_auto_pd) {
4892 int join = wpas_p2p_peer_go(wpa_s,
4893 wpa_s->pending_join_dev_addr);
4894 if (join == 0 &&
4895 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4896 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004897 bss = wpa_bss_get_bssid_latest(
4898 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004899 if (bss) {
4900 freq = bss->freq;
4901 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4902 "the peer " MACSTR " at %d MHz",
4903 wpa_s->auto_pd_scan_retry,
4904 MAC2STR(wpa_s->
4905 pending_join_dev_addr),
4906 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004907 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004908 return;
4909 }
4910 }
4911
4912 if (join < 0)
4913 join = 0;
4914
4915 wpa_s->p2p_auto_pd = 0;
4916 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4917 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4918 MAC2STR(wpa_s->pending_join_dev_addr), join);
4919 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004920 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004921 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004922 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004923 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004924 wpa_msg_global(wpa_s, MSG_INFO,
4925 P2P_EVENT_PROV_DISC_FAILURE
4926 " p2p_dev_addr=" MACSTR " status=N/A",
4927 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004928 }
4929 return;
4930 }
4931
4932 if (wpa_s->p2p_auto_join) {
4933 int join = wpas_p2p_peer_go(wpa_s,
4934 wpa_s->pending_join_dev_addr);
4935 if (join < 0) {
4936 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4937 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004938 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004939 P2P_EVENT_FALLBACK_TO_GO_NEG
4940 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004941 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4942 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4943 wpa_s->p2p_persistent_group, 0, 0, 0,
4944 wpa_s->p2p_go_intent,
4945 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004946 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004947 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004948 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004949 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004950 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004951 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08004952 wpa_s->p2p_go_he,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004953 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004954 return;
4955 }
4956
4957 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4958 "try to join the group", join ? "" :
4959 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004960 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004961 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004962 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004963 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004964 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004965 }
4966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4968 wpa_s->pending_join_iface_addr);
4969 if (freq < 0 &&
4970 p2p_get_interface_addr(wpa_s->global->p2p,
4971 wpa_s->pending_join_dev_addr,
4972 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004973 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4974 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4976 "address for join from " MACSTR " to " MACSTR
4977 " based on newly discovered P2P peer entry",
4978 MAC2STR(wpa_s->pending_join_iface_addr),
4979 MAC2STR(iface_addr));
4980 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4981 ETH_ALEN);
4982
4983 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4984 wpa_s->pending_join_iface_addr);
4985 }
4986 if (freq >= 0) {
4987 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4988 "from P2P peer table: %d MHz", freq);
4989 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004990 if (wpa_s->p2p_join_ssid_len) {
4991 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4992 MACSTR " and SSID %s",
4993 MAC2STR(wpa_s->pending_join_iface_addr),
4994 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4995 wpa_s->p2p_join_ssid_len));
4996 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4997 wpa_s->p2p_join_ssid,
4998 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004999 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005000 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5001 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5002 bss = wpa_bss_get_bssid_latest(wpa_s,
5003 wpa_s->pending_join_iface_addr);
5004 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005006 u8 dev_addr[ETH_ALEN];
5007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 freq = bss->freq;
5009 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005010 "from BSS table: %d MHz (SSID %s)", freq,
5011 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005012 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5013 dev_addr) == 0 &&
5014 os_memcmp(wpa_s->pending_join_dev_addr,
5015 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5016 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5017 ETH_ALEN) != 0) {
5018 wpa_printf(MSG_DEBUG,
5019 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5020 MAC2STR(dev_addr),
5021 MAC2STR(wpa_s->pending_join_dev_addr));
5022 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5023 ETH_ALEN);
5024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 }
5026 if (freq > 0) {
5027 u16 method;
5028
Dmitry Shmidt04949592012-07-19 12:16:46 -07005029 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005030 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005031 P2P_EVENT_GROUP_FORMATION_FAILURE
5032 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005033 wpas_notify_p2p_group_formation_failure(
5034 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005035 return;
5036 }
5037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5039 "prior to joining an existing group (GO " MACSTR
5040 " freq=%u MHz)",
5041 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5042 wpa_s->pending_pd_before_join = 1;
5043
5044 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045 case WPS_PIN_DISPLAY:
5046 method = WPS_CONFIG_KEYPAD;
5047 break;
5048 case WPS_PIN_KEYPAD:
5049 method = WPS_CONFIG_DISPLAY;
5050 break;
5051 case WPS_PBC:
5052 method = WPS_CONFIG_PUSHBUTTON;
5053 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005054 case WPS_P2PS:
5055 method = WPS_CONFIG_P2PS;
5056 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057 default:
5058 method = 0;
5059 break;
5060 }
5061
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005062 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5063 wpa_s->pending_join_dev_addr) ==
5064 method)) {
5065 /*
5066 * We have already performed provision discovery for
5067 * joining the group. Proceed directly to join
5068 * operation without duplicated provision discovery. */
5069 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5070 "with " MACSTR " already done - proceed to "
5071 "join",
5072 MAC2STR(wpa_s->pending_join_dev_addr));
5073 wpa_s->pending_pd_before_join = 0;
5074 goto start;
5075 }
5076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005078 wpa_s->pending_join_dev_addr,
5079 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005080 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5082 "Discovery Request before joining an "
5083 "existing group");
5084 wpa_s->pending_pd_before_join = 0;
5085 goto start;
5086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087 return;
5088 }
5089
5090 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5091 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5092 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5093 wpas_p2p_check_join_scan_limit(wpa_s);
5094 return;
5095
5096start:
5097 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005098 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5099 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100}
5101
5102
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005103static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5104 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 int ret;
5107 struct wpa_driver_scan_params params;
5108 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005109 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005110 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005111 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112
5113 os_memset(&params, 0, sizeof(params));
5114
5115 /* P2P Wildcard SSID */
5116 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005117 if (ssid && ssid_len) {
5118 params.ssids[0].ssid = ssid;
5119 params.ssids[0].ssid_len = ssid_len;
5120 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5121 wpa_s->p2p_join_ssid_len = ssid_len;
5122 } else {
5123 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5124 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5125 wpa_s->p2p_join_ssid_len = 0;
5126 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127
5128 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005129 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5130 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5131 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005132 if (wps_ie == NULL) {
5133 wpas_p2p_scan_res_join(wpa_s, NULL);
5134 return;
5135 }
5136
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005137 if (!freq) {
5138 int oper_freq;
5139 /*
5140 * If freq is not provided, check the operating freq of the GO
5141 * and use a single channel scan on if possible.
5142 */
5143 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5144 wpa_s->pending_join_iface_addr);
5145 if (oper_freq > 0)
5146 freq = oper_freq;
5147 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005148 if (freq > 0) {
5149 freqs[0] = freq;
5150 params.freqs = freqs;
5151 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005153 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5154 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5155 if (ies == NULL) {
5156 wpabuf_free(wps_ie);
5157 wpas_p2p_scan_res_join(wpa_s, NULL);
5158 return;
5159 }
5160 wpabuf_put_buf(ies, wps_ie);
5161 wpabuf_free(wps_ie);
5162
5163 bands = wpas_get_bands(wpa_s, freqs);
5164 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5165
5166 params.p2p_probe = 1;
5167 params.extra_ies = wpabuf_head(ies);
5168 params.extra_ies_len = wpabuf_len(ies);
5169
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005170 if (wpa_s->clear_driver_scan_cache) {
5171 wpa_printf(MSG_DEBUG,
5172 "Request driver to clear scan cache due to local BSS flush");
5173 params.only_new_results = 1;
5174 }
5175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176 /*
5177 * Run a scan to update BSS table and start Provision Discovery once
5178 * the new scan results become available.
5179 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005180 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005181 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005182 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005183 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005184 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005185 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005186 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005187
5188 wpabuf_free(ies);
5189
5190 if (ret) {
5191 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5192 "try again later");
5193 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5194 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5195 wpas_p2p_check_join_scan_limit(wpa_s);
5196 }
5197}
5198
5199
Dmitry Shmidt04949592012-07-19 12:16:46 -07005200static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5201{
5202 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005203 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005204}
5205
5206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005209 int auto_join, int op_freq,
5210 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211{
5212 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005213 MACSTR " dev " MACSTR " op_freq=%d)%s",
5214 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005215 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005216 if (ssid && ssid_len) {
5217 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5218 wpa_ssid_txt(ssid, ssid_len));
5219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220
Dmitry Shmidt04949592012-07-19 12:16:46 -07005221 wpa_s->p2p_auto_pd = 0;
5222 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5224 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5225 wpa_s->pending_join_wps_method = wps_method;
5226
5227 /* Make sure we are not running find during connection establishment */
5228 wpas_p2p_stop_find(wpa_s);
5229
5230 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005231 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232 return 0;
5233}
5234
5235
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005236static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5237 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005238{
5239 struct wpa_supplicant *group;
5240 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005241 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242
5243 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5244 if (group == NULL)
5245 return -1;
5246 if (group != wpa_s) {
5247 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5248 sizeof(group->p2p_pin));
5249 group->p2p_wps_method = wpa_s->p2p_wps_method;
5250 }
5251
Hai Shalom74f70d42019-02-11 14:42:39 -08005252 /*
5253 * Need to mark the current interface for p2p_group_formation
5254 * when a separate group interface is not used. This is needed
5255 * to allow p2p_cancel stop a pending p2p_connect-join.
5256 * wpas_p2p_init_group_interface() addresses this for the case
5257 * where a separate group interface is used.
5258 */
5259 if (group == wpa_s->parent)
5260 wpa_s->global->p2p_group_formation = group;
5261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005262 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005263 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264
5265 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005266 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5268 ETH_ALEN);
5269 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005270 if (freq && ssid && ssid_len) {
5271 res.freq = freq;
5272 res.ssid_len = ssid_len;
5273 os_memcpy(res.ssid, ssid, ssid_len);
5274 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005275 if (ssid && ssid_len) {
5276 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5277 ssid, ssid_len);
5278 } else {
5279 bss = wpa_bss_get_bssid_latest(
5280 wpa_s, wpa_s->pending_join_iface_addr);
5281 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005282 if (bss) {
5283 res.freq = bss->freq;
5284 res.ssid_len = bss->ssid_len;
5285 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5286 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5287 bss->freq,
5288 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005289 } else if (ssid && ssid_len) {
5290 res.ssid_len = ssid_len;
5291 os_memcpy(res.ssid, ssid, ssid_len);
5292 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5293 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005294 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005295 }
5296
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005297 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5298 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5299 "starting client");
5300 wpa_drv_cancel_remain_on_channel(wpa_s);
5301 wpa_s->off_channel_freq = 0;
5302 wpa_s->roc_waiting_drv_freq = 0;
5303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005304 wpas_start_wps_enrollee(group, &res);
5305
5306 /*
5307 * Allow a longer timeout for join-a-running-group than normal 15
5308 * second group formation timeout since the GO may not have authorized
5309 * our connection yet.
5310 */
5311 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5312 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5313 wpa_s, NULL);
5314
5315 return 0;
5316}
5317
5318
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005319static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005320 int *force_freq, int *pref_freq, int go,
5321 unsigned int *pref_freq_list,
5322 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005323{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005324 struct wpa_used_freq_data *freqs;
5325 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005326 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5327
5328 max_pref_freq = *num_pref_freq;
5329 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005330
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005331 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5332 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005333 if (!freqs)
5334 return -1;
5335
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005336 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5337 wpa_s->num_multichan_concurrent);
5338
5339 /*
5340 * It is possible that the total number of used frequencies is bigger
5341 * than the number of frequencies used for P2P, so get the system wide
5342 * number of unused frequencies.
5343 */
5344 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5345
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005346 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005347 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5348 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005349
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005350 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005351 int ret;
5352 if (go)
5353 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5354 else
5355 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5356 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005357 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005358 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5359 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005360 /*
5361 * If freq is a DFS channel and DFS is offloaded
5362 * to the driver, allow P2P GO to use it.
5363 */
5364 wpa_printf(MSG_DEBUG,
5365 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5366 freq);
5367 } else {
5368 wpa_printf(MSG_DEBUG,
5369 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5370 freq);
5371 res = -3;
5372 goto exit_free;
5373 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005374 }
5375
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005376 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005377 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005378 freq_in_use = 1;
5379 }
5380
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005381 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005382 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5383 freq);
5384 res = -2;
5385 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005386 }
5387 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5388 "requested channel (%u MHz)", freq);
5389 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005390 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005391 }
5392
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005393 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005394
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005395 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5396 enum wpa_driver_if_type iface_type;
5397
5398 if (go)
5399 iface_type = WPA_IF_P2P_GO;
5400 else
5401 iface_type = WPA_IF_P2P_CLIENT;
5402
5403 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5404 best_freq, go);
5405
5406 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5407 &max_pref_freq,
5408 pref_freq_list);
5409 if (!res && max_pref_freq > 0) {
5410 *num_pref_freq = max_pref_freq;
5411 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005412 while (i < *num_pref_freq &&
5413 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005414 pref_freq_list[i]) ||
5415 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005416 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005417 wpa_printf(MSG_DEBUG,
5418 "P2P: preferred_freq_list[%d]=%d is disallowed",
5419 i, pref_freq_list[i]);
5420 i++;
5421 }
5422 if (i != *num_pref_freq) {
5423 best_freq = pref_freq_list[i];
5424 wpa_printf(MSG_DEBUG,
5425 "P2P: Using preferred_freq_list[%d]=%d",
5426 i, best_freq);
5427 } else {
5428 wpa_printf(MSG_DEBUG,
5429 "P2P: All driver preferred frequencies are disallowed for P2P use");
5430 *num_pref_freq = 0;
5431 }
5432 } else {
5433 wpa_printf(MSG_DEBUG,
5434 "P2P: No preferred frequency list available");
5435 }
5436 }
5437
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005438 /* We have a candidate frequency to use */
5439 if (best_freq > 0) {
5440 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005441 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005442 best_freq);
5443 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005444 } else {
5445 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 -07005446 best_freq);
5447 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005448 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005449 } else if (num_unused > 0) {
5450 wpa_printf(MSG_DEBUG,
5451 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5452 *force_freq = 0;
5453 } else {
5454 wpa_printf(MSG_DEBUG,
5455 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5456 res = -2;
5457 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005458 }
5459
5460exit_ok:
5461 res = 0;
5462exit_free:
5463 os_free(freqs);
5464 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005465}
5466
5467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468/**
5469 * wpas_p2p_connect - Request P2P Group Formation to be started
5470 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5471 * @peer_addr: Address of the peer P2P Device
5472 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5473 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005474 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475 * @join: Whether to join an existing group (as a client) instead of starting
5476 * Group Owner negotiation; @peer_addr is BSSID in that case
5477 * @auth: Whether to only authorize the connection instead of doing that and
5478 * initiating Group Owner negotiation
5479 * @go_intent: GO Intent or -1 to use default
5480 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005481 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005482 * @persistent_id: Persistent group credentials to use for forcing GO
5483 * parameters or -1 to generate new values (SSID/passphrase)
5484 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5485 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005486 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005487 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005488 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005489 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005490 * @group_ssid: Specific Group SSID for join or %NULL if not set
5491 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5493 * failure, -2 on failure due to channel not currently available,
5494 * -3 if forced channel is not supported
5495 */
5496int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5497 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005498 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005499 int go_intent, int freq, unsigned int vht_center_freq2,
5500 int persistent_id, int pd, int ht40, int vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08005501 unsigned int vht_chwidth, int he, const u8 *group_ssid,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005502 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005504 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005505 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005507 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005508 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005509 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510
5511 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5512 return -1;
5513
Dmitry Shmidt04949592012-07-19 12:16:46 -07005514 if (persistent_id >= 0) {
5515 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5516 if (ssid == NULL || ssid->disabled != 2 ||
5517 ssid->mode != WPAS_MODE_P2P_GO)
5518 return -1;
5519 }
5520
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005521 os_free(wpa_s->global->add_psk);
5522 wpa_s->global->add_psk = NULL;
5523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005524 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005525 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005526 wpa_s->global->pending_p2ps_group_freq = 0;
5527 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 if (go_intent < 0)
5530 go_intent = wpa_s->conf->p2p_go_intent;
5531
5532 if (!auth)
5533 wpa_s->p2p_long_listen = 0;
5534
5535 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005536 wpa_s->p2p_persistent_group = !!persistent_group;
5537 wpa_s->p2p_persistent_id = persistent_id;
5538 wpa_s->p2p_go_intent = go_intent;
5539 wpa_s->p2p_connect_freq = freq;
5540 wpa_s->p2p_fallback_to_go_neg = 0;
5541 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005542 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005543 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005544 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5545 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005546 wpa_s->p2p_go_he = !!he;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547
5548 if (pin)
5549 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5550 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005551 if (wps_generate_pin((unsigned int *) &ret) < 0)
5552 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005553 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5554 "%08d", ret);
5555 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5556 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5558 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005559 } else if (wps_method == WPS_P2PS) {
5560 /* Force the P2Ps default PIN to be used */
5561 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005562 } else
5563 wpa_s->p2p_pin[0] = '\0';
5564
Dmitry Shmidt04949592012-07-19 12:16:46 -07005565 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005566 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5567 if (auth) {
5568 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5569 "connect a running group from " MACSTR,
5570 MAC2STR(peer_addr));
5571 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5572 return ret;
5573 }
5574 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5575 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5576 iface_addr) < 0) {
5577 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5578 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5579 dev_addr);
5580 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005581 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005582 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005583 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5584 "%ld.%06ld",
5585 wpa_s->p2p_auto_started.sec,
5586 wpa_s->p2p_auto_started.usec);
5587 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005588 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005589 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005590 auto_join, freq,
5591 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005592 return -1;
5593 return ret;
5594 }
5595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005596 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005597 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005598 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005599 if (res)
5600 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005601 wpas_p2p_set_own_freq_preference(wpa_s,
5602 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005604 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005608 if (wpa_s->create_p2p_iface) {
5609 /* Prepare to add a new interface for the group */
5610 iftype = WPA_IF_P2P_GROUP;
5611 if (go_intent == 15)
5612 iftype = WPA_IF_P2P_GO;
5613 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5614 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5615 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005616 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005619 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005620 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005621 if (wpa_s->p2p_mgmt)
5622 if_addr = wpa_s->parent->own_addr;
5623 else
5624 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005625 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5626 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005627
5628 if (auth) {
5629 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005630 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005631 force_freq, persistent_group, ssid,
5632 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633 return -1;
5634 return ret;
5635 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005636
5637 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5638 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005639 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005640 if (wpa_s->create_p2p_iface)
5641 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642 return -1;
5643 }
5644 return ret;
5645}
5646
5647
5648/**
5649 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5650 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5651 * @freq: Frequency of the channel in MHz
5652 * @duration: Duration of the stay on the channel in milliseconds
5653 *
5654 * This callback is called when the driver indicates that it has started the
5655 * requested remain-on-channel duration.
5656 */
5657void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5658 unsigned int freq, unsigned int duration)
5659{
5660 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5661 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005662 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)",
5663 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5664 wpa_s->roc_waiting_drv_freq, freq, duration);
5665 if (wpa_s->off_channel_freq &&
5666 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5668 wpa_s->pending_listen_duration);
5669 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005670 } else {
5671 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5672 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5673 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674 }
5675}
5676
5677
Jithu Jance57cea1a2014-08-20 10:22:04 -07005678int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679{
5680 /* Limit maximum Listen state time based on driver limitation. */
5681 if (timeout > wpa_s->max_remain_on_chan)
5682 timeout = wpa_s->max_remain_on_chan;
5683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 return p2p_listen(wpa_s->global->p2p, timeout);
5685}
5686
5687
5688/**
5689 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5690 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5691 * @freq: Frequency of the channel in MHz
5692 *
5693 * This callback is called when the driver indicates that a remain-on-channel
5694 * operation has been completed, i.e., the duration on the requested channel
5695 * has timed out.
5696 */
5697void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5698 unsigned int freq)
5699{
5700 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5701 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005702 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005703 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5705 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005706 if (wpa_s->p2p_long_listen > 0)
5707 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005708 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5709 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005710 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712 if (wpa_s->p2p_long_listen > 0) {
5713 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5714 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005715 } else {
5716 /*
5717 * When listen duration is over, stop listen & update p2p_state
5718 * to IDLE.
5719 */
5720 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721 }
5722}
5723
5724
5725/**
5726 * wpas_p2p_group_remove - Remove a P2P group
5727 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5728 * @ifname: Network interface name of the group interface or "*" to remove all
5729 * groups
5730 * Returns: 0 on success, -1 on failure
5731 *
5732 * This function is used to remove a P2P group. This can be used to disconnect
5733 * from a group in which the local end is a P2P Client or to end a P2P Group in
5734 * case the local end is the Group Owner. If a virtual network interface was
5735 * created for this group, that interface will be removed. Otherwise, only the
5736 * configured P2P group network will be removed from the interface.
5737 */
5738int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5739{
5740 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005741 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742
5743 if (os_strcmp(ifname, "*") == 0) {
5744 struct wpa_supplicant *prev;
5745 wpa_s = global->ifaces;
5746 while (wpa_s) {
5747 prev = wpa_s;
5748 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005749 if (prev->p2p_group_interface !=
5750 NOT_P2P_GROUP_INTERFACE ||
5751 (prev->current_ssid &&
5752 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005753 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005754 }
5755 return 0;
5756 }
5757
5758 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5759 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5760 break;
5761 }
5762
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005763 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764}
5765
5766
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005767static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5768{
5769 unsigned int r;
5770
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005771 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5772 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5773 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5774 int res;
5775
5776 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5777 &size, pref_freq_list);
5778 if (!res && size > 0) {
5779 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005780 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005781 (!p2p_supported_freq(wpa_s->global->p2p,
5782 pref_freq_list[i]) ||
5783 wpas_p2p_disallowed_freq(wpa_s->global,
5784 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005785 wpa_printf(MSG_DEBUG,
5786 "P2P: preferred_freq_list[%d]=%d is disallowed",
5787 i, pref_freq_list[i]);
5788 i++;
5789 }
5790 if (i != size) {
5791 freq = pref_freq_list[i];
5792 wpa_printf(MSG_DEBUG,
5793 "P2P: Using preferred_freq_list[%d]=%d",
5794 i, freq);
5795 } else {
5796 wpa_printf(MSG_DEBUG,
5797 "P2P: All driver preferred frequencies are disallowed for P2P use");
5798 }
5799 } else {
5800 wpa_printf(MSG_DEBUG,
5801 "P2P: No preferred frequency list available");
5802 }
5803 }
5804
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005805 if (freq == 2) {
5806 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5807 "band");
5808 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005809 p2p_supported_freq_go(wpa_s->global->p2p,
5810 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005811 freq = wpa_s->best_24_freq;
5812 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5813 "channel: %d MHz", freq);
5814 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005815 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5816 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005817 freq = 2412 + (r % 3) * 25;
5818 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5819 "channel: %d MHz", freq);
5820 }
5821 }
5822
5823 if (freq == 5) {
5824 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5825 "band");
5826 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005827 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005828 wpa_s->best_5_freq)) {
5829 freq = wpa_s->best_5_freq;
5830 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5831 "channel: %d MHz", freq);
5832 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005833 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5834 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005835 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005836 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005837 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5838 "5 GHz channel for P2P group");
5839 return -1;
5840 }
5841 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5842 "channel: %d MHz", freq);
5843 }
5844 }
5845
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005846 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005847 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005848 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5849 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005850 /*
5851 * If freq is a DFS channel and DFS is offloaded to the
5852 * driver, allow P2P GO to use it.
5853 */
5854 wpa_printf(MSG_DEBUG, "P2P: "
5855 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5856 __func__, freq);
5857 return freq;
5858 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005859 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5860 "(%u MHz) is not supported for P2P uses",
5861 freq);
5862 return -1;
5863 }
5864
5865 return freq;
5866}
5867
5868
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005869static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5870 const struct p2p_channels *channels,
5871 int freq)
5872{
5873 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5874 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5875 freq_included(wpa_s, channels, freq))
5876 return 1;
5877 return 0;
5878}
5879
5880
5881static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5882 struct p2p_go_neg_results *params,
5883 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005884{
5885 unsigned int i, r;
5886
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005887 /* try all channels in operating class 115 */
5888 for (i = 0; i < 4; i++) {
5889 params->freq = 5180 + i * 20;
5890 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005891 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005892 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5893 goto out;
5894 }
5895
5896 /* try all channels in operating class 124 */
5897 for (i = 0; i < 4; i++) {
5898 params->freq = 5745 + i * 20;
5899 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005900 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005901 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5902 goto out;
5903 }
5904
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005905 /* try social channel class 180 channel 2 */
5906 params->freq = 58320 + 1 * 2160;
5907 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005908 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005909 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5910 goto out;
5911
5912 /* try all channels in reg. class 180 */
5913 for (i = 0; i < 4; i++) {
5914 params->freq = 58320 + i * 2160;
5915 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005916 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005917 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5918 goto out;
5919 }
5920
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005921 /* try some random selection of the social channels */
5922 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5923 return;
5924
5925 for (i = 0; i < 3; i++) {
5926 params->freq = 2412 + ((r + i) % 3) * 25;
5927 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5928 goto out;
5929 }
5930
5931 /* try all other channels in operating class 81 */
5932 for (i = 0; i < 11; i++) {
5933 params->freq = 2412 + i * 5;
5934
5935 /* skip social channels; covered in the previous loop */
5936 if (params->freq == 2412 ||
5937 params->freq == 2437 ||
5938 params->freq == 2462)
5939 continue;
5940
5941 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5942 goto out;
5943 }
5944
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005945 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005946 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005947 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005948out:
5949 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5950 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005951}
5952
5953
Roshan Pius3a1667e2018-07-03 15:17:14 -07005954static int wpas_same_band(int freq1, int freq2)
5955{
5956 enum hostapd_hw_mode mode1, mode2;
5957 u8 chan1, chan2;
5958
5959 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5960 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5961 if (mode1 == NUM_HOSTAPD_MODES)
5962 return 0;
5963 return mode1 == mode2;
5964}
5965
5966
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005967static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5968 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005969 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08005970 int vht, int max_oper_chwidth, int he,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005971 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005972{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005973 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005974 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005975 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005976 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005977 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005978
5979 os_memset(params, 0, sizeof(*params));
5980 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005981 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005982 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08005983 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005984 params->max_oper_chwidth = max_oper_chwidth;
5985 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005987 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5988 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005989 if (!freqs)
5990 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005992 num = get_shared_radio_freqs_data(wpa_s, freqs,
5993 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005994
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005995 if (wpa_s->current_ssid &&
5996 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5997 wpa_s->wpa_state == WPA_COMPLETED) {
5998 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5999 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006000
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006001 /*
6002 * If the frequency selection is done for an active P2P GO that
6003 * is not sharing a frequency, allow to select a new frequency
6004 * even if there are no unused frequencies as we are about to
6005 * move the P2P GO so its frequency can be re-used.
6006 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006007 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006008 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6009 freqs[i].flags == 0) {
6010 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006011 break;
6012 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006013 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006014 }
6015
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006016 /* try using the forced freq */
6017 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006018 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6019 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006020 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006021 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006022 freq);
6023 goto fail;
6024 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006025 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6026 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006027 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6028 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006029 /*
6030 * If freq is a DFS channel and DFS is offloaded
6031 * to the driver, allow P2P GO to use it.
6032 */
6033 wpa_printf(MSG_DEBUG,
6034 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6035 __func__, freq);
6036 } else {
6037 wpa_printf(MSG_DEBUG,
6038 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6039 freq);
6040 goto fail;
6041 }
6042 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006043
6044 for (i = 0; i < num; i++) {
6045 if (freqs[i].freq == freq) {
6046 wpa_printf(MSG_DEBUG,
6047 "P2P: forced freq (%d MHz) is also shared",
6048 freq);
6049 params->freq = freq;
6050 goto success;
6051 }
6052 }
6053
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006054 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006055 wpa_printf(MSG_DEBUG,
6056 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6057 freq);
6058 goto fail;
6059 }
6060
6061 wpa_printf(MSG_DEBUG,
6062 "P2P: force GO freq (%d MHz) on a free channel",
6063 freq);
6064 params->freq = freq;
6065 goto success;
6066 }
6067
6068 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006069 if (num &&
6070 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006071 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6072 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6073 wpa_printf(MSG_DEBUG,
6074 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006075 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006076 params->freq = cand;
6077 goto success;
6078 }
6079
6080 /* try using one of the shared freqs */
6081 for (i = 0; i < num; i++) {
6082 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6083 freqs[i].freq)) {
6084 wpa_printf(MSG_DEBUG,
6085 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006086 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006087 params->freq = freqs[i].freq;
6088 goto success;
6089 }
6090 }
6091 }
6092
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006093 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006094 wpa_printf(MSG_DEBUG,
6095 "P2P: Cannot force GO on any of the channels we are already using");
6096 goto fail;
6097 }
6098
6099 /* try using the setting from the configuration file */
6100 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6101 wpa_s->conf->p2p_oper_channel >= 1 &&
6102 wpa_s->conf->p2p_oper_channel <= 11 &&
6103 wpas_p2p_supported_freq_go(
6104 wpa_s, channels,
6105 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6106 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6107 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6108 "frequency %d MHz", params->freq);
6109 goto success;
6110 }
6111
6112 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6113 wpa_s->conf->p2p_oper_reg_class == 116 ||
6114 wpa_s->conf->p2p_oper_reg_class == 117 ||
6115 wpa_s->conf->p2p_oper_reg_class == 124 ||
6116 wpa_s->conf->p2p_oper_reg_class == 125 ||
6117 wpa_s->conf->p2p_oper_reg_class == 126 ||
6118 wpa_s->conf->p2p_oper_reg_class == 127) &&
6119 wpas_p2p_supported_freq_go(wpa_s, channels,
6120 5000 +
6121 5 * wpa_s->conf->p2p_oper_channel)) {
6122 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6123 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6124 "frequency %d MHz", params->freq);
6125 goto success;
6126 }
6127
6128 /* Try using best channels */
6129 if (wpa_s->conf->p2p_oper_channel == 0 &&
6130 wpa_s->best_overall_freq > 0 &&
6131 wpas_p2p_supported_freq_go(wpa_s, channels,
6132 wpa_s->best_overall_freq)) {
6133 params->freq = wpa_s->best_overall_freq;
6134 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6135 "channel %d MHz", params->freq);
6136 goto success;
6137 }
6138
6139 if (wpa_s->conf->p2p_oper_channel == 0 &&
6140 wpa_s->best_24_freq > 0 &&
6141 wpas_p2p_supported_freq_go(wpa_s, channels,
6142 wpa_s->best_24_freq)) {
6143 params->freq = wpa_s->best_24_freq;
6144 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6145 "channel %d MHz", params->freq);
6146 goto success;
6147 }
6148
6149 if (wpa_s->conf->p2p_oper_channel == 0 &&
6150 wpa_s->best_5_freq > 0 &&
6151 wpas_p2p_supported_freq_go(wpa_s, channels,
6152 wpa_s->best_5_freq)) {
6153 params->freq = wpa_s->best_5_freq;
6154 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6155 "channel %d MHz", params->freq);
6156 goto success;
6157 }
6158
6159 /* try using preferred channels */
6160 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6161 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6162 params->freq = cand;
6163 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6164 "channels", params->freq);
6165 goto success;
6166 }
6167
Roshan Pius3a1667e2018-07-03 15:17:14 -07006168 /* Try using a channel that allows VHT to be used with 80 MHz */
6169 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6170 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6171 enum hostapd_hw_mode mode;
6172 struct hostapd_hw_modes *hwmode;
6173 u8 chan;
6174
6175 cand = wpa_s->p2p_group_common_freqs[i];
6176 mode = ieee80211_freq_to_chan(cand, &chan);
6177 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6178 mode);
6179 if (!hwmode ||
6180 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6181 BW80) != ALLOWED)
6182 continue;
6183 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6184 params->freq = cand;
6185 wpa_printf(MSG_DEBUG,
6186 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6187 params->freq);
6188 goto success;
6189 }
6190 }
6191 }
6192
6193 /* Try using a channel that allows HT to be used with 40 MHz on the same
6194 * band so that CSA can be used */
6195 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6196 wpa_s->p2p_group_common_freqs) {
6197 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6198 enum hostapd_hw_mode mode;
6199 struct hostapd_hw_modes *hwmode;
6200 u8 chan;
6201
6202 cand = wpa_s->p2p_group_common_freqs[i];
6203 mode = ieee80211_freq_to_chan(cand, &chan);
6204 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6205 mode);
6206 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6207 cand) ||
6208 !hwmode ||
6209 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6210 BW40MINUS) != ALLOWED &&
6211 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6212 BW40PLUS) != ALLOWED))
6213 continue;
6214 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6215 params->freq = cand;
6216 wpa_printf(MSG_DEBUG,
6217 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6218 params->freq);
6219 goto success;
6220 }
6221 }
6222 }
6223
6224 /* Try using one of the group common freqs on the same band so that CSA
6225 * can be used */
6226 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6227 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6228 cand = wpa_s->p2p_group_common_freqs[i];
6229 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6230 cand))
6231 continue;
6232 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6233 params->freq = cand;
6234 wpa_printf(MSG_DEBUG,
6235 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6236 params->freq);
6237 goto success;
6238 }
6239 }
6240 }
6241
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006242 /* Try using one of the group common freqs */
6243 if (wpa_s->p2p_group_common_freqs) {
6244 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6245 cand = wpa_s->p2p_group_common_freqs[i];
6246 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6247 params->freq = cand;
6248 wpa_printf(MSG_DEBUG,
6249 "P2P: Use freq %d MHz common with the peer",
6250 params->freq);
6251 goto success;
6252 }
6253 }
6254 }
6255
6256 /* no preference, select some channel */
6257 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6258
6259 if (params->freq == 0) {
6260 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6261 goto fail;
6262 }
6263
6264success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006265 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006266 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006267fail:
6268 os_free(freqs);
6269 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270}
6271
6272
6273static struct wpa_supplicant *
6274wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6275 int go)
6276{
6277 struct wpa_supplicant *group_wpa_s;
6278
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006279 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006280 if (wpa_s->p2p_mgmt) {
6281 /*
6282 * We may be called on the p2p_dev interface which
6283 * cannot be used for group operations, so always use
6284 * the primary interface.
6285 */
6286 wpa_s->parent->p2pdev = wpa_s;
6287 wpa_s = wpa_s->parent;
6288 }
6289 wpa_dbg(wpa_s, MSG_DEBUG,
6290 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006291 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006292 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006293 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006294 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006296
6297 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006298 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006299 wpa_msg_global(wpa_s, MSG_ERROR,
6300 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006301 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6304 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006305 wpa_msg_global(wpa_s, MSG_ERROR,
6306 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006307 wpas_p2p_remove_pending_group_interface(wpa_s);
6308 return NULL;
6309 }
6310
Roshan Pius3a1667e2018-07-03 15:17:14 -07006311 if (go && wpa_s->p2p_go_do_acs) {
6312 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6313 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6314 wpa_s->p2p_go_do_acs = 0;
6315 }
6316
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006317 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6318 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006319 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006320 return group_wpa_s;
6321}
6322
6323
6324/**
6325 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6326 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6327 * @persistent_group: Whether to create a persistent group
6328 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006329 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006330 * @ht40: Start GO with 40 MHz channel width
6331 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006332 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006333 * Returns: 0 on success, -1 on failure
6334 *
6335 * This function creates a new P2P group with the local end as the Group Owner,
6336 * i.e., without using Group Owner Negotiation.
6337 */
6338int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006339 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08006340 int max_oper_chwidth, int he)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006341{
6342 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006343
6344 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6345 return -1;
6346
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006347 os_free(wpa_s->global->add_psk);
6348 wpa_s->global->add_psk = NULL;
6349
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006350 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006351 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006352 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006353
Roshan Pius3a1667e2018-07-03 15:17:14 -07006354 if (!wpa_s->p2p_go_do_acs) {
6355 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6356 if (freq < 0)
6357 return -1;
6358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006360 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Hai Shalom74f70d42019-02-11 14:42:39 -08006361 ht40, vht, max_oper_chwidth, he, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006362 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364 p2p_go_params(wpa_s->global->p2p, &params);
6365 params.persistent_group = persistent_group;
6366
6367 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6368 if (wpa_s == NULL)
6369 return -1;
6370 wpas_start_wps_go(wpa_s, &params, 0);
6371
6372 return 0;
6373}
6374
6375
6376static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006377 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006378 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006379{
6380 struct wpa_ssid *ssid;
6381
6382 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6383 if (wpa_s == NULL)
6384 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006385 if (force_scan)
6386 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006387 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388
6389 wpa_supplicant_ap_deinit(wpa_s);
6390
6391 ssid = wpa_config_add_network(wpa_s->conf);
6392 if (ssid == NULL)
6393 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006394 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006395 wpa_config_set_network_defaults(ssid);
6396 ssid->temporary = 1;
6397 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006398 ssid->pbss = params->pbss;
6399 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6400 WPA_CIPHER_CCMP;
6401 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006402 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6403 ssid->ssid = os_malloc(params->ssid_len);
6404 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006405 wpa_config_remove_network(wpa_s->conf, ssid->id);
6406 return -1;
6407 }
6408 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6409 ssid->ssid_len = params->ssid_len;
6410 ssid->p2p_group = 1;
6411 ssid->export_keys = 1;
6412 if (params->psk_set) {
6413 os_memcpy(ssid->psk, params->psk, 32);
6414 ssid->psk_set = 1;
6415 }
6416 if (params->passphrase)
6417 ssid->passphrase = os_strdup(params->passphrase);
6418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006419 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006420 wpa_s->p2p_in_invitation = 1;
6421 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006422 wpa_s->p2p_go_group_formation_completed = 0;
6423 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006424
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006425 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006426 NULL);
6427 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6428 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006429 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006430 wpa_supplicant_select_network(wpa_s, ssid);
6431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006432 return 0;
6433}
6434
6435
6436int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6437 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006438 int force_freq, int neg_freq,
6439 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006440 int vht, int max_oper_chwidth, int he,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006441 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006442 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006443{
6444 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006445 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006446
6447 if (ssid->disabled != 2 || ssid->ssid == NULL)
6448 return -1;
6449
6450 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6451 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6452 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6453 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006454 if (go == 0 &&
6455 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006456 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006457 /*
6458 * This can happen if Invitation Response frame was lost
6459 * and the peer (GO of a persistent group) tries to
6460 * invite us again. Reschedule the timeout to avoid
6461 * terminating the wait for the connection too early
6462 * since we now know that the peer is still trying to
6463 * invite us instead of having already started the GO.
6464 */
6465 wpa_printf(MSG_DEBUG,
6466 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6467 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6468 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006469 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006470 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006471 return 0;
6472 }
6473
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006474 os_free(wpa_s->global->add_psk);
6475 wpa_s->global->add_psk = NULL;
6476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006477 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006478 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006479
Dmitry Shmidt04949592012-07-19 12:16:46 -07006480 wpa_s->p2p_fallback_to_go_neg = 0;
6481
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006482 if (ssid->mode == WPAS_MODE_P2P_GO) {
6483 if (force_freq > 0) {
6484 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6485 if (freq < 0)
6486 return -1;
6487 } else {
6488 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6489 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006490 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006491 freq = 0;
6492 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006493 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006494 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006495 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006496 struct os_reltime now;
6497 struct wpa_bss *bss =
6498 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006499
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006500 os_get_reltime(&now);
6501 if (bss &&
6502 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006503 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006504 freq = bss->freq;
6505 else
6506 freq = 0;
6507 }
6508
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006509 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6510 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006511 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006512 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006513 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006514
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006515 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Hai Shalom74f70d42019-02-11 14:42:39 -08006516 ht40, vht, max_oper_chwidth, he, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006517 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006518
6519 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006520 params.psk_set = ssid->psk_set;
6521 if (params.psk_set)
6522 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006523 if (ssid->passphrase) {
6524 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6525 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6526 "persistent group");
6527 return -1;
6528 }
6529 os_strlcpy(params.passphrase, ssid->passphrase,
6530 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006531 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006532 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6533 params.ssid_len = ssid->ssid_len;
6534 params.persistent_group = 1;
6535
6536 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6537 if (wpa_s == NULL)
6538 return -1;
6539
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006540 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6541
Dmitry Shmidt56052862013-10-04 10:23:25 -07006542 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006543 wpas_start_wps_go(wpa_s, &params, 0);
6544
6545 return 0;
6546}
6547
6548
6549static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6550 struct wpabuf *proberesp_ies)
6551{
6552 struct wpa_supplicant *wpa_s = ctx;
6553 if (wpa_s->ap_iface) {
6554 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006555 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6556 wpabuf_free(beacon_ies);
6557 wpabuf_free(proberesp_ies);
6558 return;
6559 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560 if (beacon_ies) {
6561 wpabuf_free(hapd->p2p_beacon_ie);
6562 hapd->p2p_beacon_ie = beacon_ies;
6563 }
6564 wpabuf_free(hapd->p2p_probe_resp_ie);
6565 hapd->p2p_probe_resp_ie = proberesp_ies;
6566 } else {
6567 wpabuf_free(beacon_ies);
6568 wpabuf_free(proberesp_ies);
6569 }
6570 wpa_supplicant_ap_update_beacon(wpa_s);
6571}
6572
6573
6574static void wpas_p2p_idle_update(void *ctx, int idle)
6575{
6576 struct wpa_supplicant *wpa_s = ctx;
6577 if (!wpa_s->ap_iface)
6578 return;
6579 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006580 if (idle) {
6581 if (wpa_s->global->p2p_fail_on_wps_complete &&
6582 wpa_s->p2p_in_provisioning) {
6583 wpas_p2p_grpform_fail_after_wps(wpa_s);
6584 return;
6585 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006586 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006587 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006588 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6589}
6590
6591
6592struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006593 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006594{
6595 struct p2p_group *group;
6596 struct p2p_group_config *cfg;
6597
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006598 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6599 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006600 return NULL;
6601
6602 cfg = os_zalloc(sizeof(*cfg));
6603 if (cfg == NULL)
6604 return NULL;
6605
Dmitry Shmidt04949592012-07-19 12:16:46 -07006606 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006607 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006608 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006609 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006610 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6611 if (wpa_s->max_stations &&
6612 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6613 cfg->max_clients = wpa_s->max_stations;
6614 else
6615 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006616 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6617 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006618 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006619 cfg->cb_ctx = wpa_s;
6620 cfg->ie_update = wpas_p2p_ie_update;
6621 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006622 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6623 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006624
6625 group = p2p_group_init(wpa_s->global->p2p, cfg);
6626 if (group == NULL)
6627 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006628 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006629 p2p_group_notif_formation_done(group);
6630 wpa_s->p2p_group = group;
6631 return group;
6632}
6633
6634
6635void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6636 int registrar)
6637{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006638 struct wpa_ssid *ssid = wpa_s->current_ssid;
6639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006640 if (!wpa_s->p2p_in_provisioning) {
6641 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6642 "provisioning not in progress");
6643 return;
6644 }
6645
Dmitry Shmidt04949592012-07-19 12:16:46 -07006646 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6647 u8 go_dev_addr[ETH_ALEN];
6648 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6649 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6650 ssid->ssid_len);
6651 /* Clear any stored provisioning info */
6652 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6653 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006654
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006655 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006656 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006657 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006658 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6659 /*
6660 * Use a separate timeout for initial data connection to
6661 * complete to allow the group to be removed automatically if
6662 * something goes wrong in this step before the P2P group idle
6663 * timeout mechanism is taken into use.
6664 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006665 wpa_dbg(wpa_s, MSG_DEBUG,
6666 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6667 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006668 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6669 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006670 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006671 /* Complete group formation on successful data connection. */
6672 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006673 } else if (ssid) {
6674 /*
6675 * Use a separate timeout for initial data connection to
6676 * complete to allow the group to be removed automatically if
6677 * the client does not complete data connection successfully.
6678 */
6679 wpa_dbg(wpa_s, MSG_DEBUG,
6680 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6681 P2P_MAX_INITIAL_CONN_WAIT_GO);
6682 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6683 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006684 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006685 /*
6686 * Complete group formation on first successful data connection
6687 */
6688 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006689 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 if (wpa_s->global->p2p)
6691 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006692 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006693}
6694
6695
Jouni Malinen75ecf522011-06-27 15:19:46 -07006696void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6697 struct wps_event_fail *fail)
6698{
6699 if (!wpa_s->p2p_in_provisioning) {
6700 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6701 "provisioning not in progress");
6702 return;
6703 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006704
6705 if (wpa_s->go_params) {
6706 p2p_clear_provisioning_info(
6707 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006708 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006709 }
6710
Jouni Malinen75ecf522011-06-27 15:19:46 -07006711 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006712
6713 if (wpa_s == wpa_s->global->p2p_group_formation) {
6714 /*
6715 * Allow some time for the failed WPS negotiation exchange to
6716 * complete, but remove the group since group formation cannot
6717 * succeed after provisioning failure.
6718 */
6719 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6720 wpa_s->global->p2p_fail_on_wps_complete = 1;
6721 eloop_deplete_timeout(0, 50000,
6722 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006723 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006724 }
6725}
6726
6727
6728int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6729{
6730 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6731 !wpa_s->p2p_in_provisioning)
6732 return 0;
6733
6734 wpas_p2p_grpform_fail_after_wps(wpa_s);
6735
6736 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006737}
6738
6739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006740int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006741 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006742 enum wpas_p2p_prov_disc_use use,
6743 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006744{
6745 u16 config_methods;
6746
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006747 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006748 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006749 wpa_s->p2p_fallback_to_go_neg = 0;
6750 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006751 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6752 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006753 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6754 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6755
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006756 wpa_printf(MSG_DEBUG,
6757 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6758 __func__, p2ps_prov->conncap,
6759 p2ps_prov->adv_id, p2ps_prov->conncap,
6760 p2ps_prov->status, p2ps_prov->info);
6761
6762 config_methods = 0;
6763 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006764 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006765 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006766 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006767 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6768 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006769 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006770 else {
6771 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006772 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006773 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006774 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006775
Dmitry Shmidt04949592012-07-19 12:16:46 -07006776 if (use == WPAS_P2P_PD_AUTO) {
6777 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6778 wpa_s->pending_pd_config_methods = config_methods;
6779 wpa_s->p2p_auto_pd = 1;
6780 wpa_s->p2p_auto_join = 0;
6781 wpa_s->pending_pd_before_join = 0;
6782 wpa_s->auto_pd_scan_retry = 0;
6783 wpas_p2p_stop_find(wpa_s);
6784 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006785 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006786 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6787 wpa_s->p2p_auto_started.sec,
6788 wpa_s->p2p_auto_started.usec);
6789 wpas_p2p_join_scan(wpa_s, NULL);
6790 return 0;
6791 }
6792
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006793 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6794 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006795 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006796 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006798 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006799 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006800 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801}
6802
6803
6804int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6805 char *end)
6806{
6807 return p2p_scan_result_text(ies, ies_len, buf, end);
6808}
6809
6810
6811static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6812{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006813 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006814 return;
6815
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006816 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006817 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006818 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6819 wpa_s, NULL);
6820 offchannel_send_action_done(wpa_s);
6821 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006823 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6824 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006825 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826}
6827
6828
6829int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6830 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006831 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006832 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006833 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006834{
6835 wpas_p2p_clear_pending_action_tx(wpa_s);
6836 wpa_s->p2p_long_listen = 0;
6837
Dmitry Shmidt04949592012-07-19 12:16:46 -07006838 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006839 wpa_s->p2p_in_provisioning) {
6840 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6841 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6842 " (P2P disabled)" : "",
6843 wpa_s->p2p_in_provisioning ?
6844 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006845 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006846 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006847
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006848 wpa_supplicant_cancel_sched_scan(wpa_s);
6849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006851 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006852 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006853}
6854
6855
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006856static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6857 struct wpa_scan_results *scan_res)
6858{
6859 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6860
6861 if (wpa_s->p2p_scan_work) {
6862 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6863 wpa_s->p2p_scan_work = NULL;
6864 radio_work_done(work);
6865 }
6866
6867 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6868 return;
6869
6870 /*
6871 * Indicate that results have been processed so that the P2P module can
6872 * continue pending tasks.
6873 */
6874 p2p_scan_res_handled(wpa_s->global->p2p);
6875}
6876
6877
6878static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006879{
6880 wpas_p2p_clear_pending_action_tx(wpa_s);
6881 wpa_s->p2p_long_listen = 0;
6882 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6883 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006884
6885 if (wpa_s->global->p2p)
6886 p2p_stop_find(wpa_s->global->p2p);
6887
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006888 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6889 wpa_printf(MSG_DEBUG,
6890 "P2P: Do not consider the scan results after stop_find");
6891 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6892 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006893}
6894
6895
6896void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6897{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006898 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006899 if (!wpa_s->global->pending_group_iface_for_p2ps)
6900 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006901}
6902
6903
6904static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6905{
6906 struct wpa_supplicant *wpa_s = eloop_ctx;
6907 wpa_s->p2p_long_listen = 0;
6908}
6909
6910
6911int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6912{
6913 int res;
6914
6915 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6916 return -1;
6917
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006918 if (wpa_s->p2p_lo_started) {
6919 wpa_printf(MSG_DEBUG,
6920 "P2P: Cannot start P2P listen, it is offloaded");
6921 return -1;
6922 }
6923
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006924 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925 wpas_p2p_clear_pending_action_tx(wpa_s);
6926
6927 if (timeout == 0) {
6928 /*
6929 * This is a request for unlimited Listen state. However, at
6930 * least for now, this is mapped to a Listen state for one
6931 * hour.
6932 */
6933 timeout = 3600;
6934 }
6935 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6936 wpa_s->p2p_long_listen = 0;
6937
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006938 /*
6939 * Stop previous find/listen operation to avoid trying to request a new
6940 * remain-on-channel operation while the driver is still running the
6941 * previous one.
6942 */
6943 if (wpa_s->global->p2p)
6944 p2p_stop_find(wpa_s->global->p2p);
6945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006946 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6947 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6948 wpa_s->p2p_long_listen = timeout * 1000;
6949 eloop_register_timeout(timeout, 0,
6950 wpas_p2p_long_listen_timeout,
6951 wpa_s, NULL);
6952 }
6953
6954 return res;
6955}
6956
6957
6958int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6959 u8 *buf, size_t len, int p2p_group)
6960{
6961 struct wpabuf *p2p_ie;
6962 int ret;
6963
6964 if (wpa_s->global->p2p_disabled)
6965 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006966 /*
6967 * Advertize mandatory cross connection capability even on
6968 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6969 */
6970 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006971 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 if (wpa_s->global->p2p == NULL)
6973 return -1;
6974 if (bss == NULL)
6975 return -1;
6976
6977 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6978 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6979 p2p_group, p2p_ie);
6980 wpabuf_free(p2p_ie);
6981
6982 return ret;
6983}
6984
6985
6986int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006987 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006988 const u8 *ie, size_t ie_len,
6989 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006990{
6991 if (wpa_s->global->p2p_disabled)
6992 return 0;
6993 if (wpa_s->global->p2p == NULL)
6994 return 0;
6995
Dmitry Shmidt04949592012-07-19 12:16:46 -07006996 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006997 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006998 case P2P_PREQ_NOT_P2P:
6999 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7000 ssi_signal);
7001 /* fall through */
7002 case P2P_PREQ_MALFORMED:
7003 case P2P_PREQ_NOT_LISTEN:
7004 case P2P_PREQ_NOT_PROCESSED:
7005 default: /* make gcc happy */
7006 return 0;
7007 case P2P_PREQ_PROCESSED:
7008 return 1;
7009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007010}
7011
7012
7013void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7014 const u8 *sa, const u8 *bssid,
7015 u8 category, const u8 *data, size_t len, int freq)
7016{
7017 if (wpa_s->global->p2p_disabled)
7018 return;
7019 if (wpa_s->global->p2p == NULL)
7020 return;
7021
7022 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7023 freq);
7024}
7025
7026
7027void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7028{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007029 unsigned int bands;
7030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007031 if (wpa_s->global->p2p_disabled)
7032 return;
7033 if (wpa_s->global->p2p == NULL)
7034 return;
7035
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007036 bands = wpas_get_bands(wpa_s, NULL);
7037 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038}
7039
7040
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007041static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042{
7043 p2p_group_deinit(wpa_s->p2p_group);
7044 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007045
7046 wpa_s->ap_configured_cb = NULL;
7047 wpa_s->ap_configured_cb_ctx = NULL;
7048 wpa_s->ap_configured_cb_data = NULL;
7049 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050}
7051
7052
7053int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7054{
7055 wpa_s->p2p_long_listen = 0;
7056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7058 return -1;
7059
7060 return p2p_reject(wpa_s->global->p2p, addr);
7061}
7062
7063
7064/* Invite to reinvoke a persistent group */
7065int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007066 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007067 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08007068 int pref_freq, int he)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007069{
7070 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007071 u8 *bssid = NULL;
7072 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007073 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007074 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007075 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007076
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007077 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007078 if (peer_addr)
7079 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7080 else
7081 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082
Jouni Malinen31be0a42012-08-31 21:20:51 +03007083 wpa_s->p2p_persistent_go_freq = freq;
7084 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007085 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007086 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007087 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7088 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007089 if (ssid->mode == WPAS_MODE_P2P_GO) {
7090 role = P2P_INVITE_ROLE_GO;
7091 if (peer_addr == NULL) {
7092 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7093 "address in invitation command");
7094 return -1;
7095 }
7096 if (wpas_p2p_create_iface(wpa_s)) {
7097 if (wpas_p2p_add_group_interface(wpa_s,
7098 WPA_IF_P2P_GO) < 0) {
7099 wpa_printf(MSG_ERROR, "P2P: Failed to "
7100 "allocate a new interface for the "
7101 "group");
7102 return -1;
7103 }
7104 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007105 } else if (wpa_s->p2p_mgmt)
7106 bssid = wpa_s->parent->own_addr;
7107 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007108 bssid = wpa_s->own_addr;
7109 } else {
7110 role = P2P_INVITE_ROLE_CLIENT;
7111 peer_addr = ssid->bssid;
7112 }
7113 wpa_s->pending_invite_ssid_id = ssid->id;
7114
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007115 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007116 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007117 role == P2P_INVITE_ROLE_GO,
7118 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007119 if (res)
7120 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007122 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7123 return -1;
7124
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007125 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7126
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007127 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7128 no_pref_freq_given && pref_freq > 0 &&
7129 wpa_s->num_multichan_concurrent > 1 &&
7130 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7131 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7132 pref_freq);
7133 pref_freq = 0;
7134 }
7135
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007136 /*
7137 * Stop any find/listen operations before invitation and possibly
7138 * connection establishment.
7139 */
7140 wpas_p2p_stop_find_oper(wpa_s);
7141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007142 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007143 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007144 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007145}
7146
7147
7148/* Invite to join an active group */
7149int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7150 const u8 *peer_addr, const u8 *go_dev_addr)
7151{
7152 struct wpa_global *global = wpa_s->global;
7153 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007154 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007155 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007156 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007157 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007158 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007159 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007160
Jouni Malinen31be0a42012-08-31 21:20:51 +03007161 wpa_s->p2p_persistent_go_freq = 0;
7162 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007163 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007164 wpa_s->p2p_go_vht_center_freq2 = 0;
7165 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007167 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7168 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7169 break;
7170 }
7171 if (wpa_s == NULL) {
7172 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7173 return -1;
7174 }
7175
7176 ssid = wpa_s->current_ssid;
7177 if (ssid == NULL) {
7178 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7179 "invitation");
7180 return -1;
7181 }
7182
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007183 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007184 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007185 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007186 ssid->ssid, ssid->ssid_len);
7187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007188 if (ssid->mode == WPAS_MODE_P2P_GO) {
7189 role = P2P_INVITE_ROLE_ACTIVE_GO;
7190 bssid = wpa_s->own_addr;
7191 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007192 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007193 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194 } else {
7195 role = P2P_INVITE_ROLE_CLIENT;
7196 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7197 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7198 "invite to current group");
7199 return -1;
7200 }
7201 bssid = wpa_s->bssid;
7202 if (go_dev_addr == NULL &&
7203 !is_zero_ether_addr(wpa_s->go_dev_addr))
7204 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007205 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7206 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007208 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007210 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7211 return -1;
7212
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007213 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007214 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007215 role == P2P_INVITE_ROLE_ACTIVE_GO,
7216 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007217 if (res)
7218 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007219 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007221 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007222 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007223 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007224}
7225
7226
7227void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7228{
7229 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007230 u8 go_dev_addr[ETH_ALEN];
7231 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007232 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007233 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007234 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007235
Dmitry Shmidt04949592012-07-19 12:16:46 -07007236 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7237 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007238 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007239 }
7240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007241 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007242 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007243
7244 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007245 if (!wpa_s->p2p_go_group_formation_completed &&
7246 wpa_s->global->p2p_group_formation == wpa_s) {
7247 wpa_dbg(wpa_s, MSG_DEBUG,
7248 "P2P: Marking group formation completed on client on data connection");
7249 wpa_s->p2p_go_group_formation_completed = 1;
7250 wpa_s->global->p2p_group_formation = NULL;
7251 wpa_s->p2p_in_provisioning = 0;
7252 wpa_s->p2p_in_invitation = 0;
7253 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007255 os_memset(go_dev_addr, 0, ETH_ALEN);
7256 if (ssid->bssid_set)
7257 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7258 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7259 ssid->ssid_len);
7260 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7261
7262 if (wpa_s->global->p2p_group_formation == wpa_s)
7263 wpa_s->global->p2p_group_formation = NULL;
7264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007265 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7266 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007267
7268 ip_addr[0] = '\0';
7269 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007270 int res;
7271
7272 res = os_snprintf(ip_addr, sizeof(ip_addr),
7273 " ip_addr=%u.%u.%u.%u "
7274 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7275 ip[0], ip[1], ip[2], ip[3],
7276 ip[4], ip[5], ip[6], ip[7],
7277 ip[8], ip[9], ip[10], ip[11]);
7278 if (os_snprintf_error(sizeof(ip_addr), res))
7279 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007280 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007281 }
7282
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007283 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7284 ssid->passphrase == NULL && ssid->psk_set ?
7285 ssid->psk : NULL,
7286 ssid->passphrase, go_dev_addr, persistent,
7287 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007288
7289 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007290 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7291 ssid, go_dev_addr);
7292
Hai Shalom5f92bc92019-04-18 11:54:11 -07007293 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007294}
7295
7296
7297int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7298 u32 interval1, u32 duration2, u32 interval2)
7299{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007300 int ret;
7301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007302 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7303 return -1;
7304
7305 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7306 wpa_s->current_ssid == NULL ||
7307 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7308 return -1;
7309
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007310 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7311 wpa_s->own_addr, wpa_s->assoc_freq,
7312 duration1, interval1, duration2, interval2);
7313 if (ret == 0)
7314 wpa_s->waiting_presence_resp = 1;
7315
7316 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007317}
7318
7319
7320int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7321 unsigned int interval)
7322{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7324 return -1;
7325
7326 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7327}
7328
7329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007330static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7331{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007332 if (wpa_s->current_ssid == NULL) {
7333 /*
7334 * current_ssid can be cleared when P2P client interface gets
7335 * disconnected, so assume this interface was used as P2P
7336 * client.
7337 */
7338 return 1;
7339 }
7340 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007341 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7342}
7343
7344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7346{
7347 struct wpa_supplicant *wpa_s = eloop_ctx;
7348
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007349 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7351 "disabled");
7352 return;
7353 }
7354
Dmitry Shmidt04949592012-07-19 12:16:46 -07007355 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7356 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007357 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007358}
7359
7360
7361static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7362{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007363 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007364
Dmitry Shmidt04949592012-07-19 12:16:46 -07007365 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7366 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007368 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7369 return;
7370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007371 timeout = wpa_s->conf->p2p_group_idle;
7372 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7373 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7374 timeout = P2P_MAX_CLIENT_IDLE;
7375
7376 if (timeout == 0)
7377 return;
7378
Dmitry Shmidt04949592012-07-19 12:16:46 -07007379 if (timeout < 0) {
7380 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7381 timeout = 0; /* special client mode no-timeout */
7382 else
7383 return;
7384 }
7385
7386 if (wpa_s->p2p_in_provisioning) {
7387 /*
7388 * Use the normal group formation timeout during the
7389 * provisioning phase to avoid terminating this process too
7390 * early due to group idle timeout.
7391 */
7392 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7393 "during provisioning");
7394 return;
7395 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307396
Dmitry Shmidt04949592012-07-19 12:16:46 -07007397 if (wpa_s->show_group_started) {
7398 /*
7399 * Use the normal group formation timeout between the end of
7400 * the provisioning phase and completion of 4-way handshake to
7401 * avoid terminating this process too early due to group idle
7402 * timeout.
7403 */
7404 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7405 "while waiting for initial 4-way handshake to "
7406 "complete");
7407 return;
7408 }
7409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007410 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007411 timeout);
7412 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7413 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007414}
7415
7416
Jouni Malinen2b89da82012-08-31 22:04:41 +03007417/* Returns 1 if the interface was removed */
7418int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7419 u16 reason_code, const u8 *ie, size_t ie_len,
7420 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007421{
7422 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007423 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007424
Dmitry Shmidt04949592012-07-19 12:16:46 -07007425 if (!locally_generated)
7426 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7427 ie_len);
7428
7429 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7430 wpa_s->current_ssid &&
7431 wpa_s->current_ssid->p2p_group &&
7432 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7433 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7434 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007435 if (wpas_p2p_group_delete(wpa_s,
7436 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7437 > 0)
7438 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007439 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007440
7441 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007442}
7443
7444
7445void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007446 u16 reason_code, const u8 *ie, size_t ie_len,
7447 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007448{
7449 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7450 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007451
Dmitry Shmidt04949592012-07-19 12:16:46 -07007452 if (!locally_generated)
7453 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7454 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007455}
7456
7457
7458void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7459{
7460 struct p2p_data *p2p = wpa_s->global->p2p;
7461
7462 if (p2p == NULL)
7463 return;
7464
7465 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7466 return;
7467
7468 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7469 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7470
7471 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7472 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7473
7474 if (wpa_s->wps &&
7475 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7476 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7477
7478 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7479 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7480
7481 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7482 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7483 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7484 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7485 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7486 }
7487
7488 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7489 p2p_set_sec_dev_types(p2p,
7490 (void *) wpa_s->conf->sec_device_type,
7491 wpa_s->conf->num_sec_device_types);
7492
7493 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7494 int i;
7495 p2p_remove_wps_vendor_extensions(p2p);
7496 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7497 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7498 continue;
7499 p2p_add_wps_vendor_extension(
7500 p2p, wpa_s->conf->wps_vendor_ext[i]);
7501 }
7502 }
7503
7504 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7505 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7506 char country[3];
7507 country[0] = wpa_s->conf->country[0];
7508 country[1] = wpa_s->conf->country[1];
7509 country[2] = 0x04;
7510 p2p_set_country(p2p, country);
7511 }
7512
7513 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7514 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7515 wpa_s->conf->p2p_ssid_postfix ?
7516 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7517 0);
7518 }
7519
7520 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7521 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007522
7523 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7524 u8 reg_class, channel;
7525 int ret;
7526 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007527 u8 channel_forced;
7528
Jouni Malinen75ecf522011-06-27 15:19:46 -07007529 if (wpa_s->conf->p2p_listen_reg_class &&
7530 wpa_s->conf->p2p_listen_channel) {
7531 reg_class = wpa_s->conf->p2p_listen_reg_class;
7532 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007533 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007534 } else {
7535 reg_class = 81;
7536 /*
7537 * Pick one of the social channels randomly as the
7538 * listen channel.
7539 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007540 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7541 channel = 1;
7542 else
7543 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007544 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007545 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007546 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7547 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007548 if (ret)
7549 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7550 "failed: %d", ret);
7551 }
7552 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7553 u8 op_reg_class, op_channel, cfg_op_channel;
7554 int ret = 0;
7555 unsigned int r;
7556 if (wpa_s->conf->p2p_oper_reg_class &&
7557 wpa_s->conf->p2p_oper_channel) {
7558 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7559 op_channel = wpa_s->conf->p2p_oper_channel;
7560 cfg_op_channel = 1;
7561 } else {
7562 op_reg_class = 81;
7563 /*
7564 * Use random operation channel from (1, 6, 11)
7565 *if no other preference is indicated.
7566 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007567 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7568 op_channel = 1;
7569 else
7570 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007571 cfg_op_channel = 0;
7572 }
7573 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7574 cfg_op_channel);
7575 if (ret)
7576 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7577 "failed: %d", ret);
7578 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007579
7580 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7581 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7582 wpa_s->conf->p2p_pref_chan) < 0) {
7583 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7584 "update failed");
7585 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007586
7587 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7588 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7589 "update failed");
7590 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007591 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007592
7593 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7594 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007595}
7596
7597
7598int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7599 int duration)
7600{
7601 if (!wpa_s->ap_iface)
7602 return -1;
7603 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7604 duration);
7605}
7606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007607
7608int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7609{
7610 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7611 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007612
7613 wpa_s->global->cross_connection = enabled;
7614 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7615
7616 if (!enabled) {
7617 struct wpa_supplicant *iface;
7618
7619 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7620 {
7621 if (iface->cross_connect_enabled == 0)
7622 continue;
7623
7624 iface->cross_connect_enabled = 0;
7625 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007626 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007627 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7628 iface->ifname,
7629 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007630 }
7631 }
7632
7633 return 0;
7634}
7635
7636
7637static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7638{
7639 struct wpa_supplicant *iface;
7640
7641 if (!uplink->global->cross_connection)
7642 return;
7643
7644 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7645 if (!iface->cross_connect_enabled)
7646 continue;
7647 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7648 0)
7649 continue;
7650 if (iface->ap_iface == NULL)
7651 continue;
7652 if (iface->cross_connect_in_use)
7653 continue;
7654
7655 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007656 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007657 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7658 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007659 }
7660}
7661
7662
7663static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7664{
7665 struct wpa_supplicant *iface;
7666
7667 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7668 if (!iface->cross_connect_enabled)
7669 continue;
7670 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7671 0)
7672 continue;
7673 if (!iface->cross_connect_in_use)
7674 continue;
7675
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007676 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007677 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7678 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007679 iface->cross_connect_in_use = 0;
7680 }
7681}
7682
7683
7684void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7685{
7686 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7687 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7688 wpa_s->cross_connect_disallowed)
7689 wpas_p2p_disable_cross_connect(wpa_s);
7690 else
7691 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007692 if (!wpa_s->ap_iface &&
7693 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7694 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007695}
7696
7697
7698void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7699{
7700 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007701 if (!wpa_s->ap_iface &&
7702 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7703 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007704 wpas_p2p_set_group_idle_timeout(wpa_s);
7705}
7706
7707
7708static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7709{
7710 struct wpa_supplicant *iface;
7711
7712 if (!wpa_s->global->cross_connection)
7713 return;
7714
7715 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7716 if (iface == wpa_s)
7717 continue;
7718 if (iface->drv_flags &
7719 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7720 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007721 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7722 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723 continue;
7724
7725 wpa_s->cross_connect_enabled = 1;
7726 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7727 sizeof(wpa_s->cross_connect_uplink));
7728 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7729 "%s to %s whenever uplink is available",
7730 wpa_s->ifname, wpa_s->cross_connect_uplink);
7731
7732 if (iface->ap_iface || iface->current_ssid == NULL ||
7733 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7734 iface->cross_connect_disallowed ||
7735 iface->wpa_state != WPA_COMPLETED)
7736 break;
7737
7738 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007739 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007740 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7741 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007742 break;
7743 }
7744}
7745
7746
7747int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7748{
7749 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7750 !wpa_s->p2p_in_provisioning)
7751 return 0; /* not P2P client operation */
7752
7753 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7754 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007755 if (wpa_s != wpa_s->p2pdev)
7756 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007757 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007758 return 1;
7759}
7760
7761
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007762void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7763{
7764 struct wpa_supplicant *wpa_s = eloop_ctx;
7765 wpas_p2p_notif_pbc_overlap(wpa_s);
7766}
7767
7768
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007769void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7770 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007771{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007772 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007773 struct wpa_used_freq_data *freqs = NULL;
7774 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007775
7776 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7777 return;
7778
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007779 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7780 if (!freqs)
7781 return;
7782
7783 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007786 os_memset(&cli_chan, 0, sizeof(cli_chan));
7787 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007788 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7789 "channel list");
7790 return;
7791 }
7792
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007793 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007794
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007795 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007796
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007797 /*
7798 * The used frequencies map changed, so it is possible that a GO is
7799 * using a channel that is no longer valid for P2P use. It is also
7800 * possible that due to policy consideration, it would be preferable to
7801 * move it to a frequency already used by other station interfaces.
7802 */
7803 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7804
7805 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007806}
7807
7808
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007809static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7810 struct wpa_scan_results *scan_res)
7811{
7812 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7813}
7814
7815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007816int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7817{
7818 struct wpa_global *global = wpa_s->global;
7819 int found = 0;
7820 const u8 *peer;
7821
7822 if (global->p2p == NULL)
7823 return -1;
7824
7825 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7826
7827 if (wpa_s->pending_interface_name[0] &&
7828 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7829 found = 1;
7830
7831 peer = p2p_get_go_neg_peer(global->p2p);
7832 if (peer) {
7833 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7834 MACSTR, MAC2STR(peer));
7835 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007836 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007837 }
7838
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007839 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7840 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7841 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7842 found = 1;
7843 }
7844
7845 if (wpa_s->pending_pd_before_join) {
7846 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7847 wpa_s->pending_pd_before_join = 0;
7848 found = 1;
7849 }
7850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007851 wpas_p2p_stop_find(wpa_s);
7852
7853 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7854 if (wpa_s == global->p2p_group_formation &&
7855 (wpa_s->p2p_in_provisioning ||
7856 wpa_s->parent->pending_interface_type ==
7857 WPA_IF_P2P_CLIENT)) {
7858 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7859 "formation found - cancelling",
7860 wpa_s->ifname);
7861 found = 1;
7862 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007863 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007864 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007865 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007866 break;
7867 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007868 wpas_p2p_group_delete(wpa_s,
7869 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007870 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007871 } else if (wpa_s->p2p_in_invitation) {
7872 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7873 wpa_s->ifname);
7874 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007875 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007876 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007877 }
7878 }
7879
7880 if (!found) {
7881 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7882 return -1;
7883 }
7884
7885 return 0;
7886}
7887
7888
7889void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7890{
7891 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7892 return;
7893
7894 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7895 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007896 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007897}
7898
7899
7900void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7901 int freq_24, int freq_5, int freq_overall)
7902{
7903 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007904 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905 return;
7906 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7907}
7908
7909
7910int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7911{
7912 u8 peer[ETH_ALEN];
7913 struct p2p_data *p2p = wpa_s->global->p2p;
7914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007915 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007916 return -1;
7917
7918 if (hwaddr_aton(addr, peer))
7919 return -1;
7920
7921 return p2p_unauthorize(p2p, peer);
7922}
7923
7924
7925/**
7926 * wpas_p2p_disconnect - Disconnect from a P2P Group
7927 * @wpa_s: Pointer to wpa_supplicant data
7928 * Returns: 0 on success, -1 on failure
7929 *
7930 * This can be used to disconnect from a group in which the local end is a P2P
7931 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7932 * virtual network interface was created for this group, that interface will be
7933 * removed. Otherwise, only the configured P2P group network will be removed
7934 * from the interface.
7935 */
7936int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7937{
7938
7939 if (wpa_s == NULL)
7940 return -1;
7941
Jouni Malinen2b89da82012-08-31 22:04:41 +03007942 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7943 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007944}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007945
7946
7947int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7948{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007949 int ret;
7950
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007951 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7952 return 0;
7953
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007954 ret = p2p_in_progress(wpa_s->global->p2p);
7955 if (ret == 0) {
7956 /*
7957 * Check whether there is an ongoing WPS provisioning step (or
7958 * other parts of group formation) on another interface since
7959 * p2p_in_progress() does not report this to avoid issues for
7960 * scans during such provisioning step.
7961 */
7962 if (wpa_s->global->p2p_group_formation &&
7963 wpa_s->global->p2p_group_formation != wpa_s) {
7964 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7965 "in group formation",
7966 wpa_s->global->p2p_group_formation->ifname);
7967 ret = 1;
7968 }
7969 }
7970
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007971 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007972 struct os_reltime now;
7973 os_get_reltime(&now);
7974 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7975 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007976 /* Wait for the first client has expired */
7977 wpa_s->global->p2p_go_wait_client.sec = 0;
7978 } else {
7979 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7980 ret = 1;
7981 }
7982 }
7983
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007984 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007985}
7986
7987
7988void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7989 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007990{
7991 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7992 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007993 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007994 /**
7995 * Remove the network by scheduling the group formation
7996 * timeout to happen immediately. The teardown code
7997 * needs to be scheduled to run asynch later so that we
7998 * don't delete data from under ourselves unexpectedly.
7999 * Calling wpas_p2p_group_formation_timeout directly
8000 * causes a series of crashes in WPS failure scenarios.
8001 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008002 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8003 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008004 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008005 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008006 }
8007}
8008
8009
8010struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008011 const u8 *addr, const u8 *ssid,
8012 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008013{
8014 struct wpa_ssid *s;
8015 size_t i;
8016
8017 for (s = wpa_s->conf->ssid; s; s = s->next) {
8018 if (s->disabled != 2)
8019 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008020 if (ssid &&
8021 (ssid_len != s->ssid_len ||
8022 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8023 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008024 if (addr == NULL) {
8025 if (s->mode == WPAS_MODE_P2P_GO)
8026 return s;
8027 continue;
8028 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008029 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8030 return s; /* peer is GO in the persistent group */
8031 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8032 continue;
8033 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008034 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008035 addr, ETH_ALEN) == 0)
8036 return s; /* peer is P2P client in persistent
8037 * group */
8038 }
8039 }
8040
8041 return NULL;
8042}
8043
8044
8045void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8046 const u8 *addr)
8047{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008048 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008049 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008050 /*
8051 * This can happen if WPS provisioning step is not terminated
8052 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8053 * peer was able to connect, there is no need to time out group
8054 * formation after this, though. In addition, this is used with
8055 * the initial connection wait on the GO as a separate formation
8056 * timeout and as such, expected to be hit after the initial WPS
8057 * provisioning step.
8058 */
8059 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008060
8061 if (!wpa_s->p2p_go_group_formation_completed &&
8062 !wpa_s->group_formation_reported) {
8063 /*
8064 * GO has not yet notified group formation success since
8065 * the WPS step was not completed cleanly. Do that
8066 * notification now since the P2P Client was able to
8067 * connect and as such, must have received the
8068 * credential from the WPS step.
8069 */
8070 if (wpa_s->global->p2p)
8071 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008072 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008073 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008074 }
8075 if (!wpa_s->p2p_go_group_formation_completed) {
8076 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8077 wpa_s->p2p_go_group_formation_completed = 1;
8078 wpa_s->global->p2p_group_formation = NULL;
8079 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008080 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008081 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008082 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008083 if (addr == NULL)
8084 return;
8085 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8086}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008087
Dmitry Shmidt04949592012-07-19 12:16:46 -07008088
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008089static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8090 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008091{
8092 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008093 int ret = 0;
8094
Dmitry Shmidt04949592012-07-19 12:16:46 -07008095 if (wpa_s->global->p2p_group_formation)
8096 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008097 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008098 offchannel_send_action_done(wpa_s);
8099 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008100 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008101 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8102 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8103 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8104 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008105 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008106 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008107 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008108 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008109 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008110 wpa_s->p2p_go_max_oper_chwidth,
8111 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008112 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008113}
8114
8115
8116int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8117{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008118 int res;
8119
Dmitry Shmidt04949592012-07-19 12:16:46 -07008120 if (!wpa_s->p2p_fallback_to_go_neg ||
8121 wpa_s->p2p_in_provisioning <= 5)
8122 return 0;
8123
8124 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8125 return 0; /* peer operating as a GO */
8126
8127 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8128 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008129 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008130 "reason=GO-not-found");
8131 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008132
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008133 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008134}
8135
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008136
8137unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8138{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008139 struct wpa_supplicant *ifs;
8140
8141 if (wpa_s->wpa_state > WPA_SCANNING) {
8142 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8143 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008144 wpa_s->conf->p2p_search_delay);
8145 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008146 }
8147
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008148 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8149 radio_list) {
8150 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008151 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8152 "delay due to concurrent operation on "
8153 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008154 wpa_s->conf->p2p_search_delay,
8155 ifs->ifname);
8156 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008157 }
8158 }
8159
8160 return 0;
8161}
8162
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008163
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008164static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8165 struct wpa_ssid *s, const u8 *addr,
8166 int iface_addr)
8167{
8168 struct psk_list_entry *psk, *tmp;
8169 int changed = 0;
8170
8171 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8172 list) {
8173 if ((iface_addr && !psk->p2p &&
8174 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8175 (!iface_addr && psk->p2p &&
8176 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8177 wpa_dbg(wpa_s, MSG_DEBUG,
8178 "P2P: Remove persistent group PSK list entry for "
8179 MACSTR " p2p=%u",
8180 MAC2STR(psk->addr), psk->p2p);
8181 dl_list_del(&psk->list);
8182 os_free(psk);
8183 changed++;
8184 }
8185 }
8186
8187 return changed;
8188}
8189
8190
8191void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8192 const u8 *p2p_dev_addr,
8193 const u8 *psk, size_t psk_len)
8194{
8195 struct wpa_ssid *ssid = wpa_s->current_ssid;
8196 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008197 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008198
8199 if (psk_len != sizeof(p->psk))
8200 return;
8201
8202 if (p2p_dev_addr) {
8203 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8204 " p2p_dev_addr=" MACSTR,
8205 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8206 if (is_zero_ether_addr(p2p_dev_addr))
8207 p2p_dev_addr = NULL;
8208 } else {
8209 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8210 MAC2STR(mac_addr));
8211 }
8212
8213 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8214 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8215 /* To be added to persistent group once created */
8216 if (wpa_s->global->add_psk == NULL) {
8217 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8218 if (wpa_s->global->add_psk == NULL)
8219 return;
8220 }
8221 p = wpa_s->global->add_psk;
8222 if (p2p_dev_addr) {
8223 p->p2p = 1;
8224 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8225 } else {
8226 p->p2p = 0;
8227 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8228 }
8229 os_memcpy(p->psk, psk, psk_len);
8230 return;
8231 }
8232
8233 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8234 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8235 return;
8236 }
8237
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008238 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008239 ssid->ssid_len);
8240 if (!persistent) {
8241 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8242 return;
8243 }
8244
8245 p = os_zalloc(sizeof(*p));
8246 if (p == NULL)
8247 return;
8248 if (p2p_dev_addr) {
8249 p->p2p = 1;
8250 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8251 } else {
8252 p->p2p = 0;
8253 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8254 }
8255 os_memcpy(p->psk, psk, psk_len);
8256
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008257 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8258 (last = dl_list_last(&persistent->psk_list,
8259 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008260 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8261 MACSTR " (p2p=%u) to make room for a new one",
8262 MAC2STR(last->addr), last->p2p);
8263 dl_list_del(&last->list);
8264 os_free(last);
8265 }
8266
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008267 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008268 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8269 p2p_dev_addr == NULL);
8270 if (p2p_dev_addr) {
8271 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8272 MACSTR, MAC2STR(p2p_dev_addr));
8273 } else {
8274 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8275 MAC2STR(mac_addr));
8276 }
8277 dl_list_add(&persistent->psk_list, &p->list);
8278
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008279 if (wpa_s->p2pdev->conf->update_config &&
8280 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008281 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008282}
8283
8284
8285static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8286 struct wpa_ssid *s, const u8 *addr,
8287 int iface_addr)
8288{
8289 int res;
8290
8291 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008292 if (res > 0 && wpa_s->conf->update_config &&
8293 wpa_config_write(wpa_s->confname, wpa_s->conf))
8294 wpa_dbg(wpa_s, MSG_DEBUG,
8295 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008296}
8297
8298
8299static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8300 const u8 *peer, int iface_addr)
8301{
8302 struct hostapd_data *hapd;
8303 struct hostapd_wpa_psk *psk, *prev, *rem;
8304 struct sta_info *sta;
8305
8306 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8307 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8308 return;
8309
8310 /* Remove per-station PSK entry */
8311 hapd = wpa_s->ap_iface->bss[0];
8312 prev = NULL;
8313 psk = hapd->conf->ssid.wpa_psk;
8314 while (psk) {
8315 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8316 (!iface_addr &&
8317 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8318 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8319 MACSTR " iface_addr=%d",
8320 MAC2STR(peer), iface_addr);
8321 if (prev)
8322 prev->next = psk->next;
8323 else
8324 hapd->conf->ssid.wpa_psk = psk->next;
8325 rem = psk;
8326 psk = psk->next;
8327 os_free(rem);
8328 } else {
8329 prev = psk;
8330 psk = psk->next;
8331 }
8332 }
8333
8334 /* Disconnect from group */
8335 if (iface_addr)
8336 sta = ap_get_sta(hapd, peer);
8337 else
8338 sta = ap_get_sta_p2p(hapd, peer);
8339 if (sta) {
8340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8341 " (iface_addr=%d) from group",
8342 MAC2STR(peer), iface_addr);
8343 hostapd_drv_sta_deauth(hapd, sta->addr,
8344 WLAN_REASON_DEAUTH_LEAVING);
8345 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8346 }
8347}
8348
8349
8350void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8351 int iface_addr)
8352{
8353 struct wpa_ssid *s;
8354 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008355 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008356
8357 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8358
8359 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008360 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008361 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8362 continue;
8363 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008364 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8365 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008366 }
8367
8368 /* Remove from any operating group */
8369 for (w = wpa_s->global->ifaces; w; w = w->next)
8370 wpas_p2p_remove_client_go(w, peer, iface_addr);
8371}
8372
8373
8374static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8375{
8376 struct wpa_supplicant *wpa_s = eloop_ctx;
8377 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8378}
8379
8380
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008381static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8382{
8383 struct wpa_supplicant *wpa_s = eloop_ctx;
8384
8385 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8386 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8387}
8388
8389
8390int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8391 struct wpa_ssid *ssid)
8392{
8393 struct wpa_supplicant *iface;
8394
8395 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8396 if (!iface->current_ssid ||
8397 iface->current_ssid->frequency == freq ||
8398 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8399 !iface->current_ssid->p2p_group))
8400 continue;
8401
8402 /* Remove the connection with least priority */
8403 if (!wpas_is_p2p_prioritized(iface)) {
8404 /* STA connection has priority over existing
8405 * P2P connection, so remove the interface. */
8406 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8407 eloop_register_timeout(0, 0,
8408 wpas_p2p_group_freq_conflict,
8409 iface, NULL);
8410 /* If connection in progress is P2P connection, do not
8411 * proceed for the connection. */
8412 if (wpa_s == iface)
8413 return -1;
8414 else
8415 return 0;
8416 } else {
8417 /* P2P connection has priority, disable the STA network
8418 */
8419 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8420 ssid);
8421 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8422 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8423 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8424 ETH_ALEN);
8425 /* If P2P connection is in progress, continue
8426 * connecting...*/
8427 if (wpa_s == iface)
8428 return 0;
8429 else
8430 return -1;
8431 }
8432 }
8433
8434 return 0;
8435}
8436
8437
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008438int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8439{
8440 struct wpa_ssid *ssid = wpa_s->current_ssid;
8441
8442 if (ssid == NULL || !ssid->p2p_group)
8443 return 0;
8444
8445 if (wpa_s->p2p_last_4way_hs_fail &&
8446 wpa_s->p2p_last_4way_hs_fail == ssid) {
8447 u8 go_dev_addr[ETH_ALEN];
8448 struct wpa_ssid *persistent;
8449
8450 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8451 ssid->ssid,
8452 ssid->ssid_len) <= 0) {
8453 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8454 goto disconnect;
8455 }
8456
8457 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8458 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008459 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008460 ssid->ssid,
8461 ssid->ssid_len);
8462 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8463 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8464 goto disconnect;
8465 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008466 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008467 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8468 persistent->id);
8469 disconnect:
8470 wpa_s->p2p_last_4way_hs_fail = NULL;
8471 /*
8472 * Remove the group from a timeout to avoid issues with caller
8473 * continuing to use the interface if this is on a P2P group
8474 * interface.
8475 */
8476 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8477 wpa_s, NULL);
8478 return 1;
8479 }
8480
8481 wpa_s->p2p_last_4way_hs_fail = ssid;
8482 return 0;
8483}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008484
8485
8486#ifdef CONFIG_WPS_NFC
8487
8488static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8489 struct wpabuf *p2p)
8490{
8491 struct wpabuf *ret;
8492 size_t wsc_len;
8493
8494 if (p2p == NULL) {
8495 wpabuf_free(wsc);
8496 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8497 return NULL;
8498 }
8499
8500 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8501 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8502 if (ret == NULL) {
8503 wpabuf_free(wsc);
8504 wpabuf_free(p2p);
8505 return NULL;
8506 }
8507
8508 wpabuf_put_be16(ret, wsc_len);
8509 if (wsc)
8510 wpabuf_put_buf(ret, wsc);
8511 wpabuf_put_be16(ret, wpabuf_len(p2p));
8512 wpabuf_put_buf(ret, p2p);
8513
8514 wpabuf_free(wsc);
8515 wpabuf_free(p2p);
8516 wpa_hexdump_buf(MSG_DEBUG,
8517 "P2P: Generated NFC connection handover message", ret);
8518
8519 if (ndef && ret) {
8520 struct wpabuf *tmp;
8521 tmp = ndef_build_p2p(ret);
8522 wpabuf_free(ret);
8523 if (tmp == NULL) {
8524 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8525 return NULL;
8526 }
8527 ret = tmp;
8528 }
8529
8530 return ret;
8531}
8532
8533
8534static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8535 struct wpa_ssid **ssid, u8 *go_dev_addr)
8536{
8537 struct wpa_supplicant *iface;
8538
8539 if (go_dev_addr)
8540 os_memset(go_dev_addr, 0, ETH_ALEN);
8541 if (ssid)
8542 *ssid = NULL;
8543 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8544 if (iface->wpa_state < WPA_ASSOCIATING ||
8545 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8546 !iface->current_ssid->p2p_group ||
8547 iface->current_ssid->mode != WPAS_MODE_INFRA)
8548 continue;
8549 if (ssid)
8550 *ssid = iface->current_ssid;
8551 if (go_dev_addr)
8552 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8553 return iface->assoc_freq;
8554 }
8555 return 0;
8556}
8557
8558
8559struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8560 int ndef)
8561{
8562 struct wpabuf *wsc, *p2p;
8563 struct wpa_ssid *ssid;
8564 u8 go_dev_addr[ETH_ALEN];
8565 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8566
8567 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8568 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8569 return NULL;
8570 }
8571
8572 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8573 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8574 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8575 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8576 return NULL;
8577 }
8578
8579 if (cli_freq == 0) {
8580 wsc = wps_build_nfc_handover_req_p2p(
8581 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8582 } else
8583 wsc = NULL;
8584 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8585 go_dev_addr, ssid ? ssid->ssid : NULL,
8586 ssid ? ssid->ssid_len : 0);
8587
8588 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8589}
8590
8591
8592struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8593 int ndef, int tag)
8594{
8595 struct wpabuf *wsc, *p2p;
8596 struct wpa_ssid *ssid;
8597 u8 go_dev_addr[ETH_ALEN];
8598 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8599
8600 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8601 return NULL;
8602
8603 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8604 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8605 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8606 return NULL;
8607
8608 if (cli_freq == 0) {
8609 wsc = wps_build_nfc_handover_sel_p2p(
8610 wpa_s->parent->wps,
8611 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8612 DEV_PW_NFC_CONNECTION_HANDOVER,
8613 wpa_s->conf->wps_nfc_dh_pubkey,
8614 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8615 } else
8616 wsc = NULL;
8617 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8618 go_dev_addr, ssid ? ssid->ssid : NULL,
8619 ssid ? ssid->ssid_len : 0);
8620
8621 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8622}
8623
8624
8625static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8626 struct p2p_nfc_params *params)
8627{
8628 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8629 "connection handover (freq=%d)",
8630 params->go_freq);
8631
8632 if (params->go_freq && params->go_ssid_len) {
8633 wpa_s->p2p_wps_method = WPS_NFC;
8634 wpa_s->pending_join_wps_method = WPS_NFC;
8635 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8636 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8637 ETH_ALEN);
8638 return wpas_p2p_join_start(wpa_s, params->go_freq,
8639 params->go_ssid,
8640 params->go_ssid_len);
8641 }
8642
8643 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8644 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008645 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008646 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008647 wpa_s->p2p_go_he,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008648 params->go_ssid_len ? params->go_ssid : NULL,
8649 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008650}
8651
8652
8653static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8654 struct p2p_nfc_params *params, int tag)
8655{
8656 int res, persistent;
8657 struct wpa_ssid *ssid;
8658
8659 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8660 "connection handover");
8661 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8662 ssid = wpa_s->current_ssid;
8663 if (ssid == NULL)
8664 continue;
8665 if (ssid->mode != WPAS_MODE_P2P_GO)
8666 continue;
8667 if (wpa_s->ap_iface == NULL)
8668 continue;
8669 break;
8670 }
8671 if (wpa_s == NULL) {
8672 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8673 return -1;
8674 }
8675
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008676 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008677 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008678 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008679 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8680 return -1;
8681 }
8682 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008683 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8684 wpa_s->p2pdev->p2p_oob_dev_pw,
8685 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8686 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008687 if (res)
8688 return res;
8689
8690 if (!tag) {
8691 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8692 return 0;
8693 }
8694
8695 if (!params->peer ||
8696 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8697 return 0;
8698
8699 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8700 " to join", MAC2STR(params->peer->p2p_device_addr));
8701
8702 wpa_s->global->p2p_invite_group = wpa_s;
8703 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008704 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008705 params->peer->p2p_device_addr,
8706 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008707 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008708
8709 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8710 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8711 ssid->ssid, ssid->ssid_len, ssid->frequency,
8712 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008713 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008714}
8715
8716
8717static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8718 struct p2p_nfc_params *params,
8719 int forced_freq)
8720{
8721 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8722 "connection handover");
8723 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8724 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008725 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008726 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008727 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008728}
8729
8730
8731static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8732 struct p2p_nfc_params *params,
8733 int forced_freq)
8734{
8735 int res;
8736
8737 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8738 "connection handover");
8739 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8740 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008741 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008742 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008743 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008744 if (res)
8745 return res;
8746
8747 res = wpas_p2p_listen(wpa_s, 60);
8748 if (res) {
8749 p2p_unauthorize(wpa_s->global->p2p,
8750 params->peer->p2p_device_addr);
8751 }
8752
8753 return res;
8754}
8755
8756
8757static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8758 const struct wpabuf *data,
8759 int sel, int tag, int forced_freq)
8760{
8761 const u8 *pos, *end;
8762 u16 len, id;
8763 struct p2p_nfc_params params;
8764 int res;
8765
8766 os_memset(&params, 0, sizeof(params));
8767 params.sel = sel;
8768
8769 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8770
8771 pos = wpabuf_head(data);
8772 end = pos + wpabuf_len(data);
8773
8774 if (end - pos < 2) {
8775 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8776 "attributes");
8777 return -1;
8778 }
8779 len = WPA_GET_BE16(pos);
8780 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008781 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008782 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8783 "attributes");
8784 return -1;
8785 }
8786 params.wsc_attr = pos;
8787 params.wsc_len = len;
8788 pos += len;
8789
8790 if (end - pos < 2) {
8791 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8792 "attributes");
8793 return -1;
8794 }
8795 len = WPA_GET_BE16(pos);
8796 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008797 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008798 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8799 "attributes");
8800 return -1;
8801 }
8802 params.p2p_attr = pos;
8803 params.p2p_len = len;
8804 pos += len;
8805
8806 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8807 params.wsc_attr, params.wsc_len);
8808 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8809 params.p2p_attr, params.p2p_len);
8810 if (pos < end) {
8811 wpa_hexdump(MSG_DEBUG,
8812 "P2P: Ignored extra data after P2P attributes",
8813 pos, end - pos);
8814 }
8815
8816 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8817 if (res)
8818 return res;
8819
8820 if (params.next_step == NO_ACTION)
8821 return 0;
8822
8823 if (params.next_step == BOTH_GO) {
8824 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8825 MAC2STR(params.peer->p2p_device_addr));
8826 return 0;
8827 }
8828
8829 if (params.next_step == PEER_CLIENT) {
8830 if (!is_zero_ether_addr(params.go_dev_addr)) {
8831 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8832 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8833 " ssid=\"%s\"",
8834 MAC2STR(params.peer->p2p_device_addr),
8835 params.go_freq,
8836 MAC2STR(params.go_dev_addr),
8837 wpa_ssid_txt(params.go_ssid,
8838 params.go_ssid_len));
8839 } else {
8840 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8841 "peer=" MACSTR " freq=%d",
8842 MAC2STR(params.peer->p2p_device_addr),
8843 params.go_freq);
8844 }
8845 return 0;
8846 }
8847
8848 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8849 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8850 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8851 return 0;
8852 }
8853
8854 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8855 wpa_s->p2p_oob_dev_pw = NULL;
8856
8857 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8858 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8859 "received");
8860 return -1;
8861 }
8862
8863 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8864 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8865 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8866 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8867 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8868 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8869 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8870
8871 if (tag) {
8872 if (id < 0x10) {
8873 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8874 "peer OOB Device Password Id %u", id);
8875 return -1;
8876 }
8877 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8878 "Device Password Id %u", id);
8879 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8880 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8881 params.oob_dev_pw_len -
8882 WPS_OOB_PUBKEY_HASH_LEN - 2);
8883 wpa_s->p2p_oob_dev_pw_id = id;
8884 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8885 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8886 params.oob_dev_pw_len -
8887 WPS_OOB_PUBKEY_HASH_LEN - 2);
8888 if (wpa_s->p2p_oob_dev_pw == NULL)
8889 return -1;
8890
8891 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8892 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8893 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8894 return -1;
8895 } else {
8896 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8897 "without Device Password");
8898 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8899 }
8900
8901 switch (params.next_step) {
8902 case NO_ACTION:
8903 case BOTH_GO:
8904 case PEER_CLIENT:
8905 /* already covered above */
8906 return 0;
8907 case JOIN_GROUP:
8908 return wpas_p2p_nfc_join_group(wpa_s, &params);
8909 case AUTH_JOIN:
8910 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8911 case INIT_GO_NEG:
8912 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8913 case RESP_GO_NEG:
8914 /* TODO: use own OOB Dev Pw */
8915 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8916 }
8917
8918 return -1;
8919}
8920
8921
8922int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8923 const struct wpabuf *data, int forced_freq)
8924{
8925 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8926 return -1;
8927
8928 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8929}
8930
8931
8932int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8933 const struct wpabuf *req,
8934 const struct wpabuf *sel, int forced_freq)
8935{
8936 struct wpabuf *tmp;
8937 int ret;
8938
8939 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8940 return -1;
8941
8942 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8943
8944 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8945 wpabuf_head(req), wpabuf_len(req));
8946 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8947 wpabuf_head(sel), wpabuf_len(sel));
8948 if (forced_freq)
8949 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8950 tmp = ndef_parse_p2p(init ? sel : req);
8951 if (tmp == NULL) {
8952 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8953 return -1;
8954 }
8955
8956 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8957 forced_freq);
8958 wpabuf_free(tmp);
8959
8960 return ret;
8961}
8962
8963
8964int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8965{
8966 const u8 *if_addr;
8967 int go_intent = wpa_s->conf->p2p_go_intent;
8968 struct wpa_supplicant *iface;
8969
8970 if (wpa_s->global->p2p == NULL)
8971 return -1;
8972
8973 if (!enabled) {
8974 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8975 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8976 {
8977 if (!iface->ap_iface)
8978 continue;
8979 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8980 }
8981 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8982 0, NULL);
8983 if (wpa_s->p2p_nfc_tag_enabled)
8984 wpas_p2p_remove_pending_group_interface(wpa_s);
8985 wpa_s->p2p_nfc_tag_enabled = 0;
8986 return 0;
8987 }
8988
8989 if (wpa_s->global->p2p_disabled)
8990 return -1;
8991
8992 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8993 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8994 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8995 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8996 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8997 "to allow static handover cases");
8998 return -1;
8999 }
9000
9001 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9002
9003 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9004 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9005 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9006 if (wpa_s->p2p_oob_dev_pw == NULL)
9007 return -1;
9008 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9009
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009010 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9011 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9012 /*
9013 * P2P Group Interface present and the command came on group
9014 * interface, so enable the token for the current interface.
9015 */
9016 wpa_s->create_p2p_iface = 0;
9017 } else {
9018 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9019 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009020
9021 if (wpa_s->create_p2p_iface) {
9022 enum wpa_driver_if_type iftype;
9023 /* Prepare to add a new interface for the group */
9024 iftype = WPA_IF_P2P_GROUP;
9025 if (go_intent == 15)
9026 iftype = WPA_IF_P2P_GO;
9027 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9028 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9029 "interface for the group");
9030 return -1;
9031 }
9032
9033 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009034 } else if (wpa_s->p2p_mgmt)
9035 if_addr = wpa_s->parent->own_addr;
9036 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009037 if_addr = wpa_s->own_addr;
9038
9039 wpa_s->p2p_nfc_tag_enabled = enabled;
9040
9041 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9042 struct hostapd_data *hapd;
9043 if (iface->ap_iface == NULL)
9044 continue;
9045 hapd = iface->ap_iface->bss[0];
9046 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9047 hapd->conf->wps_nfc_dh_pubkey =
9048 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9049 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9050 hapd->conf->wps_nfc_dh_privkey =
9051 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9052 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9053 hapd->conf->wps_nfc_dev_pw =
9054 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9055 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9056
9057 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9058 wpa_dbg(iface, MSG_DEBUG,
9059 "P2P: Failed to enable NFC Tag for GO");
9060 }
9061 }
9062 p2p_set_authorized_oob_dev_pw_id(
9063 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9064 if_addr);
9065
9066 return 0;
9067}
9068
9069#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009070
9071
9072static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9073 struct wpa_used_freq_data *freqs,
9074 unsigned int num)
9075{
9076 u8 curr_chan, cand, chan;
9077 unsigned int i;
9078
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009079 /*
9080 * If possible, optimize the Listen channel to be a channel that is
9081 * already used by one of the other interfaces.
9082 */
9083 if (!wpa_s->conf->p2p_optimize_listen_chan)
9084 return;
9085
9086 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9087 return;
9088
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009089 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9090 for (i = 0, cand = 0; i < num; i++) {
9091 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9092 if (curr_chan == chan) {
9093 cand = 0;
9094 break;
9095 }
9096
9097 if (chan == 1 || chan == 6 || chan == 11)
9098 cand = chan;
9099 }
9100
9101 if (cand) {
9102 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009103 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009104 cand);
9105 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9106 }
9107}
9108
9109
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009110static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009111{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009112 struct hostapd_config *conf;
9113 struct p2p_go_neg_results params;
9114 struct csa_settings csa_settings;
9115 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9116 int old_freq = current_ssid->frequency;
9117 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009118
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009119 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9120 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9121 return -1;
9122 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009123
9124 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009125 * TODO: This function may not always work correctly. For example,
9126 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009127 */
Hai Shalom74f70d42019-02-11 14:42:39 -08009128 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009129 wpa_dbg(wpa_s, MSG_DEBUG,
9130 "P2P CSA: Failed to select new frequency for GO");
9131 return -1;
9132 }
9133
9134 if (current_ssid->frequency == params.freq) {
9135 wpa_dbg(wpa_s, MSG_DEBUG,
9136 "P2P CSA: Selected same frequency - not moving GO");
9137 return 0;
9138 }
9139
9140 conf = hostapd_config_defaults();
9141 if (!conf) {
9142 wpa_dbg(wpa_s, MSG_DEBUG,
9143 "P2P CSA: Failed to allocate default config");
9144 return -1;
9145 }
9146
9147 current_ssid->frequency = params.freq;
9148 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9149 wpa_dbg(wpa_s, MSG_DEBUG,
9150 "P2P CSA: Failed to create new GO config");
9151 ret = -1;
9152 goto out;
9153 }
9154
9155 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9156 wpa_dbg(wpa_s, MSG_DEBUG,
9157 "P2P CSA: CSA to a different band is not supported");
9158 ret = -1;
9159 goto out;
9160 }
9161
9162 os_memset(&csa_settings, 0, sizeof(csa_settings));
9163 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9164 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9165 csa_settings.freq_params.freq = params.freq;
9166 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9167 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9168 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9169
9170 if (conf->ieee80211ac) {
9171 int freq1 = 0, freq2 = 0;
9172 u8 chan, opclass;
9173
9174 if (ieee80211_freq_to_channel_ext(params.freq,
9175 conf->secondary_channel,
9176 conf->vht_oper_chwidth,
9177 &opclass, &chan) ==
9178 NUM_HOSTAPD_MODES) {
9179 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9180 ret = -1;
9181 goto out;
9182 }
9183
9184 if (conf->vht_oper_centr_freq_seg0_idx)
9185 freq1 = ieee80211_chan_to_freq(
9186 NULL, opclass,
9187 conf->vht_oper_centr_freq_seg0_idx);
9188
9189 if (conf->vht_oper_centr_freq_seg1_idx)
9190 freq2 = ieee80211_chan_to_freq(
9191 NULL, opclass,
9192 conf->vht_oper_centr_freq_seg1_idx);
9193
9194 if (freq1 < 0 || freq2 < 0) {
9195 wpa_dbg(wpa_s, MSG_DEBUG,
9196 "P2P CSA: Selected invalid VHT center freqs");
9197 ret = -1;
9198 goto out;
9199 }
9200
9201 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9202 csa_settings.freq_params.center_freq1 = freq1;
9203 csa_settings.freq_params.center_freq2 = freq2;
9204
9205 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009206 case CHANWIDTH_80MHZ:
9207 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009208 csa_settings.freq_params.bandwidth = 80;
9209 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009210 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009211 csa_settings.freq_params.bandwidth = 160;
9212 break;
9213 }
9214 }
9215
9216 ret = ap_switch_channel(wpa_s, &csa_settings);
9217out:
9218 current_ssid->frequency = old_freq;
9219 hostapd_config_free(conf);
9220 return ret;
9221}
9222
9223
9224static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9225{
9226 struct p2p_go_neg_results params;
9227 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9228
9229 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9230
9231 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9232 current_ssid->frequency);
9233
9234 /* Stop the AP functionality */
9235 /* TODO: Should do this in a way that does not indicated to possible
9236 * P2P Clients in the group that the group is terminated. */
9237 wpa_supplicant_ap_deinit(wpa_s);
9238
9239 /* Reselect the GO frequency */
Hai Shalom74f70d42019-02-11 14:42:39 -08009240 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009241 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9242 wpas_p2p_group_delete(wpa_s,
9243 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9244 return;
9245 }
9246 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9247 params.freq);
9248
9249 if (params.freq &&
9250 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9251 wpa_printf(MSG_DEBUG,
9252 "P2P: Selected freq (%u MHz) is not valid for P2P",
9253 params.freq);
9254 wpas_p2p_group_delete(wpa_s,
9255 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9256 return;
9257 }
9258
9259 /* Update the frequency */
9260 current_ssid->frequency = params.freq;
9261 wpa_s->connect_without_scan = current_ssid;
9262 wpa_s->reassociate = 1;
9263 wpa_s->disconnected = 0;
9264 wpa_supplicant_req_scan(wpa_s, 0, 0);
9265}
9266
9267
9268static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9269{
9270 struct wpa_supplicant *wpa_s = eloop_ctx;
9271
9272 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009273 return;
9274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009275 wpas_p2p_go_update_common_freqs(wpa_s);
9276
9277 /* Do not move GO in the middle of a CSA */
9278 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9279 wpa_printf(MSG_DEBUG,
9280 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009281 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009282 }
9283
9284 /*
9285 * First, try a channel switch flow. If it is not supported or fails,
9286 * take down the GO and bring it up again.
9287 */
9288 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9289 wpas_p2p_move_go_no_csa(wpa_s);
9290}
9291
9292
9293static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9294{
9295 struct wpa_supplicant *wpa_s = eloop_ctx;
9296 struct wpa_used_freq_data *freqs = NULL;
9297 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009298
9299 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9300 if (!freqs)
9301 return;
9302
9303 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9304
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009305 /* Previous attempt to move a GO was not possible -- try again. */
9306 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9307 WPAS_P2P_CHANNEL_UPDATE_ANY);
9308
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009309 os_free(freqs);
9310}
9311
9312
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009313/*
9314 * Consider moving a GO from its currently used frequency:
9315 * 1. It is possible that due to regulatory consideration the frequency
9316 * can no longer be used and there is a need to evacuate the GO.
9317 * 2. It is possible that due to MCC considerations, it would be preferable
9318 * to move the GO to a channel that is currently used by some other
9319 * station interface.
9320 *
9321 * In case a frequency that became invalid is once again valid, cancel a
9322 * previously initiated GO frequency change.
9323 */
9324static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9325 struct wpa_used_freq_data *freqs,
9326 unsigned int num)
9327{
9328 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9329 unsigned int timeout;
9330 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009331 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009332
9333 wpas_p2p_go_update_common_freqs(wpa_s);
9334
9335 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009336 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009337 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009338 for (i = 0, invalid_freq = 0; i < num; i++) {
9339 if (freqs[i].freq == freq) {
9340 flags = freqs[i].flags;
9341
9342 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009343 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9344 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009345 wpa_dbg(wpa_s, MSG_DEBUG,
9346 "P2P: Freq=%d MHz no longer valid for GO",
9347 freq);
9348 invalid_freq = 1;
9349 }
9350 } else if (freqs[i].flags == 0) {
9351 /* Freq is not used by any other station interface */
9352 continue;
9353 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009354 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009355 /* Freq is not valid for P2P use cases */
9356 continue;
9357 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9358 P2P_GO_FREQ_MOVE_SCM) {
9359 policy_move = 1;
9360 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9361 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9362 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9363 policy_move = 1;
9364 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9365 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9366 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9367 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9368 policy_move = 1;
9369 } else if ((wpa_s->drv_flags &
9370 WPA_DRIVER_FLAGS_AP_CSA) &&
9371 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9372 u8 chan;
9373
9374 /*
9375 * We do not support CSA between bands, so move
9376 * GO only within the same band.
9377 */
9378 if (wpa_s->ap_iface->current_mode->mode ==
9379 ieee80211_freq_to_chan(freqs[i].freq,
9380 &chan))
9381 policy_move = 1;
9382 }
9383 }
9384 }
9385
9386 wpa_dbg(wpa_s, MSG_DEBUG,
9387 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9388 invalid_freq, policy_move, flags);
9389
9390 /*
9391 * The channel is valid, or we are going to have a policy move, so
9392 * cancel timeout.
9393 */
9394 if (!invalid_freq || policy_move) {
9395 wpa_dbg(wpa_s, MSG_DEBUG,
9396 "P2P: Cancel a GO move from freq=%d MHz", freq);
9397 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9398
9399 if (wpas_p2p_in_progress(wpa_s)) {
9400 wpa_dbg(wpa_s, MSG_DEBUG,
9401 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9402 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9403 wpa_s, NULL);
9404 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9405 wpas_p2p_reconsider_moving_go,
9406 wpa_s, NULL);
9407 return;
9408 }
9409 }
9410
9411 if (!invalid_freq && (!policy_move || flags != 0)) {
9412 wpa_dbg(wpa_s, MSG_DEBUG,
9413 "P2P: Not initiating a GO frequency change");
9414 return;
9415 }
9416
9417 /*
9418 * Do not consider moving GO if it is in the middle of a CSA. When the
9419 * CSA is finished this flow should be retriggered.
9420 */
9421 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9422 wpa_dbg(wpa_s, MSG_DEBUG,
9423 "P2P: Not initiating a GO frequency change - CSA is in progress");
9424 return;
9425 }
9426
9427 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9428 timeout = P2P_GO_FREQ_CHANGE_TIME;
9429 else
9430 timeout = 0;
9431
9432 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9433 freq, timeout);
9434 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9435 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9436}
9437
9438
9439static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9440 struct wpa_used_freq_data *freqs,
9441 unsigned int num,
9442 enum wpas_p2p_channel_update_trig trig)
9443{
9444 struct wpa_supplicant *ifs;
9445
9446 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9447 NULL);
9448
9449 /*
9450 * Travers all the radio interfaces, and for each GO interface, check
9451 * if there is a need to move the GO from the frequency it is using,
9452 * or in case the frequency is valid again, cancel the evacuation flow.
9453 */
9454 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9455 radio_list) {
9456 if (ifs->current_ssid == NULL ||
9457 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9458 continue;
9459
9460 /*
9461 * The GO was just started or completed channel switch, no need
9462 * to move it.
9463 */
9464 if (wpa_s == ifs &&
9465 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9466 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9467 wpa_dbg(wpa_s, MSG_DEBUG,
9468 "P2P: GO move - schedule re-consideration");
9469 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9470 wpas_p2p_reconsider_moving_go,
9471 wpa_s, NULL);
9472 continue;
9473 }
9474
9475 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9476 }
9477}
9478
9479
9480void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9481{
9482 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9483 return;
9484
9485 wpas_p2p_update_channel_list(wpa_s,
9486 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9487}
9488
9489
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009490void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9491{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009492 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9493 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9494 "the management interface is being removed");
9495 wpas_p2p_deinit_global(wpa_s->global);
9496 }
9497}
9498
9499
9500void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9501{
9502 if (wpa_s->ap_iface->bss)
9503 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9504 wpas_p2p_group_deinit(wpa_s);
9505}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009506
9507
9508int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9509 unsigned int period, unsigned int interval,
9510 unsigned int count)
9511{
9512 struct p2p_data *p2p = wpa_s->global->p2p;
9513 u8 *device_types;
9514 size_t dev_types_len;
9515 struct wpabuf *buf;
9516 int ret;
9517
9518 if (wpa_s->p2p_lo_started) {
9519 wpa_dbg(wpa_s, MSG_DEBUG,
9520 "P2P Listen offload is already started");
9521 return 0;
9522 }
9523
9524 if (wpa_s->global->p2p == NULL ||
9525 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9526 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9527 return -1;
9528 }
9529
9530 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9531 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9532 freq);
9533 return -1;
9534 }
9535
9536 /* Get device type */
9537 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9538 WPS_DEV_TYPE_LEN;
9539 device_types = os_malloc(dev_types_len);
9540 if (!device_types)
9541 return -1;
9542 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9543 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9544 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9545
9546 /* Get Probe Response IE(s) */
9547 buf = p2p_build_probe_resp_template(p2p, freq);
9548 if (!buf) {
9549 os_free(device_types);
9550 return -1;
9551 }
9552
9553 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9554 device_types, dev_types_len,
9555 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9556 if (ret < 0)
9557 wpa_dbg(wpa_s, MSG_DEBUG,
9558 "P2P: Failed to start P2P listen offload");
9559
9560 os_free(device_types);
9561 wpabuf_free(buf);
9562
9563 if (ret == 0) {
9564 wpa_s->p2p_lo_started = 1;
9565
9566 /* Stop current P2P listen if any */
9567 wpas_stop_listen(wpa_s);
9568 }
9569
9570 return ret;
9571}
9572
9573
9574int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9575{
9576 int ret;
9577
9578 if (!wpa_s->p2p_lo_started)
9579 return 0;
9580
9581 ret = wpa_drv_p2p_lo_stop(wpa_s);
9582 if (ret < 0)
9583 wpa_dbg(wpa_s, MSG_DEBUG,
9584 "P2P: Failed to stop P2P listen offload");
9585
9586 wpa_s->p2p_lo_started = 0;
9587 return ret;
9588}