blob: 562caad508313b7dab31c3c70f12343a6c297bdb [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
1559 if (wpa_s->p2p_send_action_work) {
1560 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
1576 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1577 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;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002271 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2272 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002273
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002274 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2275 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2276 " wps_method=%s",
2277 res->role_go ? "GO" : "client", res->freq, res->ht40,
2278 MAC2STR(res->peer_device_addr),
2279 MAC2STR(res->peer_interface_addr),
2280 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002281 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282
Dmitry Shmidt04949592012-07-19 12:16:46 -07002283 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2284 struct wpa_ssid *ssid;
2285 ssid = wpa_config_get_network(wpa_s->conf,
2286 wpa_s->p2p_persistent_id);
2287 if (ssid && ssid->disabled == 2 &&
2288 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2289 size_t len = os_strlen(ssid->passphrase);
2290 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2291 "on requested persistent group");
2292 os_memcpy(res->passphrase, ssid->passphrase, len);
2293 res->passphrase[len] = '\0';
2294 }
2295 }
2296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002298 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2300 if (group_wpa_s == NULL) {
2301 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002302 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2303 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002304 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002305 return;
2306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2308 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002310 group_wpa_s = wpa_s->parent;
2311 wpa_s->global->p2p_group_formation = group_wpa_s;
2312 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002313 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002316 group_wpa_s->p2p_in_provisioning = 1;
2317 group_wpa_s->p2pdev = wpa_s;
2318 if (group_wpa_s != wpa_s) {
2319 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2320 sizeof(group_wpa_s->p2p_pin));
2321 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2322 }
2323 if (res->role_go) {
2324 wpas_start_wps_go(group_wpa_s, res, 1);
2325 } else {
2326 os_get_reltime(&group_wpa_s->scan_min_time);
2327 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 }
2329
2330 wpa_s->p2p_long_listen = 0;
2331 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2332
2333 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2334 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2335 (res->peer_config_timeout % 100) * 10000,
2336 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2337}
2338
2339
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002340static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2341 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342{
2343 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002344 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002345 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2346 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002348 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349}
2350
2351
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002352static void wpas_dev_found(void *ctx, const u8 *addr,
2353 const struct p2p_peer_info *info,
2354 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002356 u8 *wfd_dev_info = NULL;
2357 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002358#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359 struct wpa_supplicant *wpa_s = ctx;
2360 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002361 char *wfd_dev_info_hex = NULL;
2362
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002363#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002364 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2365 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002366 if (wfd_dev_info_hex) {
2367 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2368 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2369 // Only used for notification, so not handling error.
2370 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2371 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002372#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002373
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002374 if (info->p2ps_instance) {
2375 char str[256];
2376 const u8 *buf = wpabuf_head(info->p2ps_instance);
2377 size_t len = wpabuf_len(info->p2ps_instance);
2378
2379 while (len) {
2380 u32 id;
2381 u16 methods;
2382 u8 str_len;
2383
2384 if (len < 4 + 2 + 1)
2385 break;
2386 id = WPA_GET_LE32(buf);
2387 buf += sizeof(u32);
2388 methods = WPA_GET_BE16(buf);
2389 buf += sizeof(u16);
2390 str_len = *buf++;
2391 if (str_len > len - 4 - 2 - 1)
2392 break;
2393 os_memcpy(str, buf, str_len);
2394 str[str_len] = '\0';
2395 buf += str_len;
2396 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2397
2398 wpa_msg_global(wpa_s, MSG_INFO,
2399 P2P_EVENT_DEVICE_FOUND MACSTR
2400 " p2p_dev_addr=" MACSTR
2401 " pri_dev_type=%s name='%s'"
2402 " config_methods=0x%x"
2403 " dev_capab=0x%x"
2404 " group_capab=0x%x"
2405 " adv_id=%x asp_svc=%s%s",
2406 MAC2STR(addr),
2407 MAC2STR(info->p2p_device_addr),
2408 wps_dev_type_bin2str(
2409 info->pri_dev_type,
2410 devtype, sizeof(devtype)),
2411 info->device_name, methods,
2412 info->dev_capab, info->group_capab,
2413 id, str,
2414 info->vendor_elems ?
2415 " vendor_elems=1" : "");
2416 }
2417 goto done;
2418 }
2419
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002420 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2421 " p2p_dev_addr=" MACSTR
2422 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002423 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002424 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2425 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2426 sizeof(devtype)),
2427 info->device_name, info->config_methods,
2428 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002429 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002430 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002431 info->vendor_elems ? " vendor_elems=1" : "",
2432 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002434done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002435 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002436#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002437
Roshan Piusfd2fd662017-01-23 13:41:57 -08002438 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2439 wfd_dev_info_len, new_device);
2440 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441}
2442
2443
2444static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2445{
2446 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002447
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002448 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2449 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2452}
2453
2454
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002455static void wpas_find_stopped(void *ctx)
2456{
2457 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002458
2459 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2460 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2461
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002462 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002463 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002464}
2465
2466
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002467struct wpas_p2p_listen_work {
2468 unsigned int freq;
2469 unsigned int duration;
2470 struct wpabuf *probe_resp_ie;
2471};
2472
2473
2474static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2475{
2476 if (lwork == NULL)
2477 return;
2478 wpabuf_free(lwork->probe_resp_ie);
2479 os_free(lwork);
2480}
2481
2482
2483static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2484{
2485 struct wpas_p2p_listen_work *lwork;
2486
2487 if (!wpa_s->p2p_listen_work)
2488 return;
2489
2490 lwork = wpa_s->p2p_listen_work->ctx;
2491 wpas_p2p_listen_work_free(lwork);
2492 radio_work_done(wpa_s->p2p_listen_work);
2493 wpa_s->p2p_listen_work = NULL;
2494}
2495
2496
2497static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2498{
2499 struct wpa_supplicant *wpa_s = work->wpa_s;
2500 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002501 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002502
2503 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002504 if (work->started) {
2505 wpa_s->p2p_listen_work = NULL;
2506 wpas_stop_listen(wpa_s);
2507 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002508 wpas_p2p_listen_work_free(lwork);
2509 return;
2510 }
2511
2512 wpa_s->p2p_listen_work = work;
2513
2514 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2515
2516 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2517 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2518 "report received Probe Request frames");
2519 wpas_p2p_listen_work_done(wpa_s);
2520 return;
2521 }
2522
2523 wpa_s->pending_listen_freq = lwork->freq;
2524 wpa_s->pending_listen_duration = lwork->duration;
2525
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002526 duration = lwork->duration;
2527#ifdef CONFIG_TESTING_OPTIONS
2528 if (wpa_s->extra_roc_dur) {
2529 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2530 duration, duration + wpa_s->extra_roc_dur);
2531 duration += wpa_s->extra_roc_dur;
2532 }
2533#endif /* CONFIG_TESTING_OPTIONS */
2534
2535 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002536 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2537 "to remain on channel (%u MHz) for Listen "
2538 "state", lwork->freq);
2539 wpas_p2p_listen_work_done(wpa_s);
2540 wpa_s->pending_listen_freq = 0;
2541 return;
2542 }
2543 wpa_s->off_channel_freq = 0;
2544 wpa_s->roc_waiting_drv_freq = lwork->freq;
2545}
2546
2547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548static int wpas_start_listen(void *ctx, unsigned int freq,
2549 unsigned int duration,
2550 const struct wpabuf *probe_resp_ie)
2551{
2552 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002553 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002555 if (wpa_s->p2p_listen_work) {
2556 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 return -1;
2558 }
2559
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002560 lwork = os_zalloc(sizeof(*lwork));
2561 if (lwork == NULL)
2562 return -1;
2563 lwork->freq = freq;
2564 lwork->duration = duration;
2565 if (probe_resp_ie) {
2566 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2567 if (lwork->probe_resp_ie == NULL) {
2568 wpas_p2p_listen_work_free(lwork);
2569 return -1;
2570 }
2571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002573 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2574 lwork) < 0) {
2575 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 return -1;
2577 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578
2579 return 0;
2580}
2581
2582
2583static void wpas_stop_listen(void *ctx)
2584{
2585 struct wpa_supplicant *wpa_s = ctx;
2586 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2587 wpa_drv_cancel_remain_on_channel(wpa_s);
2588 wpa_s->off_channel_freq = 0;
2589 wpa_s->roc_waiting_drv_freq = 0;
2590 }
2591 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002592
2593 /*
2594 * Don't cancel Probe Request RX reporting for a connected P2P Client
2595 * handling Probe Request frames.
2596 */
2597 if (!wpa_s->p2p_cli_probe)
2598 wpa_drv_probe_req_report(wpa_s, 0);
2599
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002600 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002601}
2602
2603
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002604static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2605 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606{
2607 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002608 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2609 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610}
2611
2612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2614 const u8 *peer, const char *params,
2615 unsigned int generated_pin)
2616{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002617 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2618 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619}
2620
2621
2622static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2623 const u8 *peer, const char *params)
2624{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002625 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2626 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627}
2628
2629
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002630static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2631 const u8 *dev_addr, const u8 *pri_dev_type,
2632 const char *dev_name, u16 supp_config_methods,
2633 u8 dev_capab, u8 group_capab, const u8 *group_id,
2634 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635{
2636 struct wpa_supplicant *wpa_s = ctx;
2637 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 u8 empty_dev_type[8];
2640 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002641 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002642 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002643
2644 if (group_id) {
2645 for (group = wpa_s->global->ifaces; group; group = group->next)
2646 {
2647 struct wpa_ssid *s = group->current_ssid;
2648 if (s != NULL &&
2649 s->mode == WPAS_MODE_P2P_GO &&
2650 group_id_len - ETH_ALEN == s->ssid_len &&
2651 os_memcmp(group_id + ETH_ALEN, s->ssid,
2652 s->ssid_len) == 0)
2653 break;
2654 }
2655 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656
2657 if (pri_dev_type == NULL) {
2658 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2659 pri_dev_type = empty_dev_type;
2660 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002661 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2662 " pri_dev_type=%s name='%s' config_methods=0x%x "
2663 "dev_capab=0x%x group_capab=0x%x%s%s",
2664 MAC2STR(dev_addr),
2665 wps_dev_type_bin2str(pri_dev_type, devtype,
2666 sizeof(devtype)),
2667 dev_name, supp_config_methods, dev_capab, group_capab,
2668 group ? " group=" : "",
2669 group ? group->ifname : "");
2670 if (os_snprintf_error(sizeof(params), res))
2671 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002675 if (wps_generate_pin(&generated_pin) < 0) {
2676 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2677 wpas_notify_p2p_provision_discovery(
2678 wpa_s, peer, 0 /* response */,
2679 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2680 return;
2681 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 wpas_prov_disc_local_display(wpa_s, peer, params,
2683 generated_pin);
2684 } else if (config_methods & WPS_CONFIG_KEYPAD)
2685 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2686 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002687 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2688 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002689
2690 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2691 P2P_PROV_DISC_SUCCESS,
2692 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693}
2694
2695
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002696static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697{
2698 struct wpa_supplicant *wpa_s = ctx;
2699 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002700 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002702 if (wpa_s->pending_pd_before_join &&
2703 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2704 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2705 wpa_s->pending_pd_before_join = 0;
2706 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2707 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002708 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002709 return;
2710 }
2711
Dmitry Shmidt04949592012-07-19 12:16:46 -07002712 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002713 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2714 int res;
2715
2716 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2717 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2718 if (os_snprintf_error(sizeof(params), res))
2719 params[sizeof(params) - 1] = '\0';
2720 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002721 params[0] = '\0';
2722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002724 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002726 if (wps_generate_pin(&generated_pin) < 0) {
2727 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2728 wpas_notify_p2p_provision_discovery(
2729 wpa_s, peer, 0 /* response */,
2730 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2731 return;
2732 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002733 wpas_prov_disc_local_display(wpa_s, peer, params,
2734 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002736 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2737 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738
Jouni Malinen75ecf522011-06-27 15:19:46 -07002739 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2740 P2P_PROV_DISC_SUCCESS,
2741 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742}
2743
2744
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002745static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002746 enum p2p_prov_disc_status status,
2747 u32 adv_id, const u8 *adv_mac,
2748 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002749{
2750 struct wpa_supplicant *wpa_s = ctx;
2751
Dmitry Shmidt04949592012-07-19 12:16:46 -07002752 if (wpa_s->p2p_fallback_to_go_neg) {
2753 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2754 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002755 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002756 P2P_EVENT_FALLBACK_TO_GO_NEG
2757 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002758 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2759 return;
2760 }
2761
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002762 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002763 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002764 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2765 "join-existing-group operation (no ACK for PD "
2766 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002767 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002768 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002769 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002770
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002771 if (adv_id && adv_mac && deferred_session_resp) {
2772 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2773 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2774 " deferred_session_resp='%s'",
2775 MAC2STR(peer), status, adv_id,
2776 deferred_session_resp);
2777 } else if (adv_id && adv_mac) {
2778 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2779 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2780 MAC2STR(peer), status, adv_id);
2781 } else {
2782 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2783 " p2p_dev_addr=" MACSTR " status=%d",
2784 MAC2STR(peer), status);
2785 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002786
Jouni Malinen75ecf522011-06-27 15:19:46 -07002787 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2788 status, 0, 0);
2789}
2790
2791
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002792static int freq_included(struct wpa_supplicant *wpa_s,
2793 const struct p2p_channels *channels,
2794 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002795{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002796 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2797 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2798 return 1;
2799 return 0;
2800}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002801
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002802
2803static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2804{
2805 unsigned int num = P2P_MAX_CHANNELS;
2806 int *common_freqs;
2807 int ret;
2808
2809 p2p_go_dump_common_freqs(wpa_s);
2810 common_freqs = os_calloc(num, sizeof(int));
2811 if (!common_freqs)
2812 return;
2813
2814 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2815 if (ret < 0) {
2816 wpa_dbg(wpa_s, MSG_DEBUG,
2817 "P2P: Failed to get group common freqs");
2818 os_free(common_freqs);
2819 return;
2820 }
2821
2822 os_free(wpa_s->p2p_group_common_freqs);
2823 wpa_s->p2p_group_common_freqs = common_freqs;
2824 wpa_s->p2p_group_common_freqs_num = num;
2825 p2p_go_dump_common_freqs(wpa_s);
2826}
2827
2828
2829/*
2830 * Check if the given frequency is one of the possible operating frequencies
2831 * set after the completion of the GO Negotiation.
2832 */
2833static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2834{
2835 unsigned int i;
2836
2837 p2p_go_dump_common_freqs(wpa_s);
2838
2839 /* assume no restrictions */
2840 if (!wpa_s->p2p_group_common_freqs_num)
2841 return 1;
2842
2843 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2844 if (wpa_s->p2p_group_common_freqs[i] == freq)
2845 return 1;
2846 }
2847 return 0;
2848}
2849
2850
2851static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2852 struct sta_info *sta, void *ctx)
2853{
2854 int *ecsa_support = ctx;
2855
2856 *ecsa_support &= sta->ecsa_supported;
2857
2858 return 0;
2859}
2860
2861
2862/* Check if all the peers support eCSA */
2863static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2864{
2865 int ecsa_support = 1;
2866
2867 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2868 &ecsa_support);
2869
2870 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002871}
2872
2873
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002874/**
2875 * Pick the best frequency to use from all the currently used frequencies.
2876 */
2877static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2878 struct wpa_used_freq_data *freqs,
2879 unsigned int num)
2880{
2881 unsigned int i, c;
2882
2883 /* find a candidate freq that is supported by P2P */
2884 for (c = 0; c < num; c++)
2885 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2886 break;
2887
2888 if (c == num)
2889 return 0;
2890
2891 /* once we have a candidate, try to find a 'better' one */
2892 for (i = c + 1; i < num; i++) {
2893 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2894 continue;
2895
2896 /*
2897 * 1. Infrastructure station interfaces have higher preference.
2898 * 2. P2P Clients have higher preference.
2899 * 3. All others.
2900 */
2901 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2902 c = i;
2903 break;
2904 }
2905
2906 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2907 c = i;
2908 }
2909 return freqs[c].freq;
2910}
2911
2912
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2914 const u8 *go_dev_addr, const u8 *ssid,
2915 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002916 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002917 const struct p2p_channels *channels,
2918 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919{
2920 struct wpa_supplicant *wpa_s = ctx;
2921 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002922 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002924 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925
2926 if (!persistent_group) {
2927 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002928 " to join an active group (SSID: %s)",
2929 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2931 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2932 == 0 ||
2933 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2934 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2935 "authorized invitation");
2936 goto accept_inv;
2937 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002938
2939#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002940 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2941 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002942 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002943 wpa_s->p2p_wps_method = WPS_NFC;
2944 wpa_s->pending_join_wps_method = WPS_NFC;
2945 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002946 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002947 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002948 bssid, ETH_ALEN);
2949 goto accept_inv;
2950 }
2951#endif /* CONFIG_WPS_NFC */
2952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953 /*
2954 * Do not accept the invitation automatically; notify user and
2955 * request approval.
2956 */
2957 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2958 }
2959
2960 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2961 if (grp) {
2962 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2963 "running persistent group");
2964 if (*go)
2965 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2966 goto accept_inv;
2967 }
2968
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002969 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2970 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2971 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2972 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002973 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002974 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2976
2977 for (s = wpa_s->conf->ssid; s; s = s->next) {
2978 if (s->disabled == 2 &&
2979 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2980 s->ssid_len == ssid_len &&
2981 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2982 break;
2983 }
2984
2985 if (!s) {
2986 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2987 " requested reinvocation of an unknown group",
2988 MAC2STR(sa));
2989 return P2P_SC_FAIL_UNKNOWN_GROUP;
2990 }
2991
2992 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2993 *go = 1;
2994 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2995 wpa_printf(MSG_DEBUG, "P2P: The only available "
2996 "interface is already in use - reject "
2997 "invitation");
2998 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2999 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003000 if (wpa_s->p2p_mgmt)
3001 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3002 ETH_ALEN);
3003 else
3004 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 } else if (s->mode == WPAS_MODE_P2P_GO) {
3006 *go = 1;
3007 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3008 {
3009 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3010 "interface address for the group");
3011 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3012 }
3013 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3014 ETH_ALEN);
3015 }
3016
3017accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003018 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3019
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003020 best_freq = 0;
3021 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3022 sizeof(struct wpa_used_freq_data));
3023 if (freqs) {
3024 int num_channels = wpa_s->num_multichan_concurrent;
3025 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3026 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3027 os_free(freqs);
3028 }
3029
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003030 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003031 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003032 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003033 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003034
3035 if (wpa_s->num_multichan_concurrent < 2 ||
3036 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3037 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 -07003038 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 }
3041
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003042 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3043 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003044 if (*go == 0) {
3045 /* We are the client */
3046 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3047 "running a GO but we are capable of MCC, "
3048 "figure out the best channel to use");
3049 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003050 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003051 /* We are the GO, and *force_freq is not in the
3052 * intersection */
3053 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3054 "in intersection but we are capable of MCC, "
3055 "figure out the best channel to use",
3056 *force_freq);
3057 *force_freq = 0;
3058 }
3059 }
3060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 return P2P_SC_SUCCESS;
3062}
3063
3064
3065static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3066 const u8 *ssid, size_t ssid_len,
3067 const u8 *go_dev_addr, u8 status,
3068 int op_freq)
3069{
3070 struct wpa_supplicant *wpa_s = ctx;
3071 struct wpa_ssid *s;
3072
3073 for (s = wpa_s->conf->ssid; s; s = s->next) {
3074 if (s->disabled == 2 &&
3075 s->ssid_len == ssid_len &&
3076 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3077 break;
3078 }
3079
3080 if (status == P2P_SC_SUCCESS) {
3081 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003082 " was accepted; op_freq=%d MHz, SSID=%s",
3083 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003085 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003086 if (go) {
3087 wpa_msg_global(wpa_s, MSG_INFO,
3088 P2P_EVENT_INVITATION_ACCEPTED
3089 "sa=" MACSTR
3090 " persistent=%d freq=%d",
3091 MAC2STR(sa), s->id, op_freq);
3092 } else {
3093 wpa_msg_global(wpa_s, MSG_INFO,
3094 P2P_EVENT_INVITATION_ACCEPTED
3095 "sa=" MACSTR
3096 " persistent=%d",
3097 MAC2STR(sa), s->id);
3098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 wpas_p2p_group_add_persistent(
Hai Shalom74f70d42019-02-11 14:42:39 -08003100 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003101 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3102 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003104 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003105 wpa_msg_global(wpa_s, MSG_INFO,
3106 P2P_EVENT_INVITATION_ACCEPTED
3107 "sa=" MACSTR " go_dev_addr=" MACSTR
3108 " bssid=" MACSTR " unknown-network",
3109 MAC2STR(sa), MAC2STR(go_dev_addr),
3110 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003112 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003113 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 }
3115 return;
3116 }
3117
3118 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3119 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3120 " was rejected (status %u)", MAC2STR(sa), status);
3121 return;
3122 }
3123
3124 if (!s) {
3125 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003126 wpa_msg_global(wpa_s, MSG_INFO,
3127 P2P_EVENT_INVITATION_RECEIVED
3128 "sa=" MACSTR " go_dev_addr=" MACSTR
3129 " bssid=" MACSTR " unknown-network",
3130 MAC2STR(sa), MAC2STR(go_dev_addr),
3131 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003133 wpa_msg_global(wpa_s, MSG_INFO,
3134 P2P_EVENT_INVITATION_RECEIVED
3135 "sa=" MACSTR " go_dev_addr=" MACSTR
3136 " unknown-network",
3137 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003139 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3140 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 return;
3142 }
3143
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003144 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003145 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3146 "sa=" MACSTR " persistent=%d freq=%d",
3147 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003148 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003149 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3150 "sa=" MACSTR " persistent=%d",
3151 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003152 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003153 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3154 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155}
3156
3157
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003158static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3159 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003160 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003161{
3162 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003163 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003164
3165 if (ssid == NULL)
3166 return;
3167
3168 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003169 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003170 ETH_ALEN) == 0)
3171 break;
3172 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003173 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003174 if (ssid->mode != WPAS_MODE_P2P_GO &&
3175 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3176 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3177 "due to invitation result", ssid->id);
3178 wpas_notify_network_removed(wpa_s, ssid);
3179 wpa_config_remove_network(wpa_s->conf, ssid->id);
3180 return;
3181 }
3182 return; /* Peer not found in client list */
3183 }
3184
3185 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003186 "group %d client list%s",
3187 MAC2STR(peer), ssid->id,
3188 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003189 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3190 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3191 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003192 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003193 if (p2p_wpa_s->conf->update_config &&
3194 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003195 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003196}
3197
3198
3199static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3200 const u8 *peer)
3201{
3202 struct wpa_ssid *ssid;
3203
3204 wpa_s = wpa_s->global->p2p_invite_group;
3205 if (wpa_s == NULL)
3206 return; /* No known invitation group */
3207 ssid = wpa_s->current_ssid;
3208 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3209 !ssid->p2p_persistent_group)
3210 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003211 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003212 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003213 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003214}
3215
3216
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003217static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003218 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003219 const u8 *peer, int neg_freq,
3220 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221{
3222 struct wpa_supplicant *wpa_s = ctx;
3223 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003224 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225
3226 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003227 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3228 "status=%d " MACSTR,
3229 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003231 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3232 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233 }
3234 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3235
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003236 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3237 status, MAC2STR(peer));
3238 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003239 struct wpa_supplicant *group_if =
3240 wpa_s->global->p2p_invite_group;
3241
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003242 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3243 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003244
3245 /*
3246 * Invitation to an active group. If this is successful and we
3247 * are the GO, set the client wait to postpone some concurrent
3248 * operations and to allow provisioning and connection to happen
3249 * more quickly.
3250 */
3251 if (status == P2P_SC_SUCCESS &&
3252 group_if && group_if->current_ssid &&
3253 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3254 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3255#ifdef CONFIG_TESTING_OPTIONS
3256 if (group_if->p2p_go_csa_on_inv) {
3257 wpa_printf(MSG_DEBUG,
3258 "Testing: force P2P GO CSA after invitation");
3259 eloop_cancel_timeout(
3260 wpas_p2p_reconsider_moving_go,
3261 wpa_s, NULL);
3262 eloop_register_timeout(
3263 0, 50000,
3264 wpas_p2p_reconsider_moving_go,
3265 wpa_s, NULL);
3266 }
3267#endif /* CONFIG_TESTING_OPTIONS */
3268 }
3269 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003270 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003272 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3273 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3274 "invitation exchange to indicate readiness for "
3275 "re-invocation");
3276 }
3277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003279 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3280 ssid = wpa_config_get_network(
3281 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003282 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003283 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284 wpas_p2p_remove_pending_group_interface(wpa_s);
3285 return;
3286 }
3287
3288 ssid = wpa_config_get_network(wpa_s->conf,
3289 wpa_s->pending_invite_ssid_id);
3290 if (ssid == NULL) {
3291 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3292 "data matching with invitation");
3293 return;
3294 }
3295
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003296 /*
3297 * The peer could have missed our ctrl::ack frame for Invitation
3298 * Response and continue retransmitting the frame. To reduce the
3299 * likelihood of the peer not getting successful TX status for the
3300 * Invitation Response frame, wait a short time here before starting
3301 * the persistent group so that we will remain on the current channel to
3302 * acknowledge any possible retransmission from the peer.
3303 */
3304 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3305 "starting persistent group");
3306 os_sleep(0, 50000);
3307
Dmitry Shmidt15907092014-03-25 10:42:57 -07003308 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003309 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003310 freq = neg_freq;
3311 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003312 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003313 freq = peer_oper_freq;
3314 else
3315 freq = 0;
3316
3317 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3318 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003320 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003321 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003322 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003323 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003324 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003325 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003326 wpa_s->p2p_go_he,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003327 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003328 ssid->mode == WPAS_MODE_P2P_GO ?
3329 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003330 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331}
3332
3333
Dmitry Shmidt04949592012-07-19 12:16:46 -07003334static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3335 unsigned int freq)
3336{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003337 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3338 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003339 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003340}
3341
3342
3343static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3344{
3345 reg->channel[reg->channels] = chan;
3346 reg->channels++;
3347}
3348
3349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003351 struct p2p_channels *chan,
3352 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353{
3354 int i, cla = 0;
3355
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003356 wpa_s->global->p2p_24ghz_social_channels = 1;
3357
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003358 os_memset(cli_chan, 0, sizeof(*cli_chan));
3359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003360 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3361 "band");
3362
3363 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3364 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003365 chan->reg_class[cla].channels = 0;
3366 for (i = 0; i < 11; i++) {
3367 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3368 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3369 }
3370 if (chan->reg_class[cla].channels)
3371 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372
3373 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3374 "band");
3375
3376 /* Operating class 115 - 5 GHz, channels 36-48 */
3377 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003378 chan->reg_class[cla].channels = 0;
3379 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3380 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3381 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3382 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3383 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3384 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3385 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3386 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3387 if (chan->reg_class[cla].channels)
3388 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389
3390 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3391 "band");
3392
3393 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3394 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003395 chan->reg_class[cla].channels = 0;
3396 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3397 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3398 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3399 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3400 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3401 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3402 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3403 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3404 if (chan->reg_class[cla].channels)
3405 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406
3407 chan->reg_classes = cla;
3408 return 0;
3409}
3410
3411
Dmitry Shmidt04949592012-07-19 12:16:46 -07003412static int has_channel(struct wpa_global *global,
3413 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414{
3415 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003416 unsigned int freq;
3417
3418 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3419 chan * 5;
3420 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003421 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003422
3423 for (i = 0; i < mode->num_channels; i++) {
3424 if (mode->channels[i].chan == chan) {
3425 if (flags)
3426 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003427 if (mode->channels[i].flag &
3428 (HOSTAPD_CHAN_DISABLED |
3429 HOSTAPD_CHAN_RADAR))
3430 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003431 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3432 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003433 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 }
3435 }
3436
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003437 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438}
3439
3440
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003441static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3442 struct hostapd_hw_modes *mode,
3443 u8 channel)
3444{
3445 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003446 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003447
3448 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3449 return 0;
3450
3451 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3452 /*
3453 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3454 * so the center channel is 6 channels away from the start/end.
3455 */
3456 if (channel >= center_channels[i] - 6 &&
3457 channel <= center_channels[i] + 6)
3458 return center_channels[i];
3459
3460 return 0;
3461}
3462
3463
3464static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3465 struct hostapd_hw_modes *mode,
3466 u8 channel, u8 bw)
3467{
3468 u8 center_chan;
3469 int i, flags;
3470 enum chan_allowed res, ret = ALLOWED;
3471
3472 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3473 if (!center_chan)
3474 return NOT_ALLOWED;
3475 if (center_chan >= 58 && center_chan <= 138)
3476 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3477
3478 /* check all the channels are available */
3479 for (i = 0; i < 4; i++) {
3480 int adj_chan = center_chan - 6 + i * 4;
3481
3482 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3483 if (res == NOT_ALLOWED)
3484 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003485 if (res == NO_IR)
3486 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003487
3488 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3489 return NOT_ALLOWED;
3490 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3491 return NOT_ALLOWED;
3492 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3493 return NOT_ALLOWED;
3494 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3495 return NOT_ALLOWED;
3496 }
3497
3498 return ret;
3499}
3500
3501
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003502static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3503 struct hostapd_hw_modes *mode,
3504 u8 channel)
3505{
3506 u8 center_channels[] = { 50, 114 };
3507 unsigned int i;
3508
3509 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3510 return 0;
3511
3512 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3513 /*
3514 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3515 * so the center channel is 14 channels away from the start/end.
3516 */
3517 if (channel >= center_channels[i] - 14 &&
3518 channel <= center_channels[i] + 14)
3519 return center_channels[i];
3520
3521 return 0;
3522}
3523
3524
3525static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3526 struct hostapd_hw_modes *mode,
3527 u8 channel, u8 bw)
3528{
3529 u8 center_chan;
3530 int i, flags;
3531 enum chan_allowed res, ret = ALLOWED;
3532
3533 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3534 if (!center_chan)
3535 return NOT_ALLOWED;
3536 /* VHT 160 MHz uses DFS channels in most countries. */
3537
3538 /* Check all the channels are available */
3539 for (i = 0; i < 8; i++) {
3540 int adj_chan = center_chan - 14 + i * 4;
3541
3542 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3543 if (res == NOT_ALLOWED)
3544 return NOT_ALLOWED;
3545
3546 if (res == NO_IR)
3547 ret = NO_IR;
3548
3549 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3550 return NOT_ALLOWED;
3551 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3552 return NOT_ALLOWED;
3553 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3554 return NOT_ALLOWED;
3555 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3556 return NOT_ALLOWED;
3557 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3558 return NOT_ALLOWED;
3559 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3560 return NOT_ALLOWED;
3561 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3562 return NOT_ALLOWED;
3563 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3564 return NOT_ALLOWED;
3565 }
3566
3567 return ret;
3568}
3569
3570
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003571static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3572 struct hostapd_hw_modes *mode,
3573 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003574{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003575 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003576 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003577
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003578 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3579 if (bw == BW40MINUS) {
3580 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3581 return NOT_ALLOWED;
3582 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3583 } else if (bw == BW40PLUS) {
3584 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3585 return NOT_ALLOWED;
3586 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3587 } else if (bw == BW80) {
3588 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003589 } else if (bw == BW160) {
3590 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003591 }
3592
3593 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3594 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003595 if (res == NO_IR || res2 == NO_IR)
3596 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003597 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003598}
3599
3600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003602 struct p2p_channels *chan,
3603 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003605 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003606 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3610 "of all supported channels; assume dualband "
3611 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003612 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 }
3614
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003615 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003617 for (op = 0; global_op_class[op].op_class; op++) {
3618 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003620 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003622 if (o->p2p == NO_P2P_SUPP)
3623 continue;
3624
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003625 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003626 if (mode == NULL)
3627 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003628 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3629 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003631 enum chan_allowed res;
3632 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3633 if (res == ALLOWED) {
3634 if (reg == NULL) {
3635 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3636 o->op_class);
3637 reg = &chan->reg_class[cla];
3638 cla++;
3639 reg->reg_class = o->op_class;
3640 }
3641 reg->channel[reg->channels] = ch;
3642 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003643 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003644 wpa_s->conf->p2p_add_cli_chan) {
3645 if (cli_reg == NULL) {
3646 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3647 o->op_class);
3648 cli_reg = &cli_chan->reg_class[cli_cla];
3649 cli_cla++;
3650 cli_reg->reg_class = o->op_class;
3651 }
3652 cli_reg->channel[cli_reg->channels] = ch;
3653 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655 }
3656 if (reg) {
3657 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3658 reg->channel, reg->channels);
3659 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003660 if (cli_reg) {
3661 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3662 cli_reg->channel, cli_reg->channels);
3663 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664 }
3665
3666 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003667 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 return 0;
3670}
3671
3672
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003673int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3674 struct hostapd_hw_modes *mode, u8 channel)
3675{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003676 int op;
3677 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003678
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003679 for (op = 0; global_op_class[op].op_class; op++) {
3680 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003681 u8 ch;
3682
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003683 if (o->p2p == NO_P2P_SUPP)
3684 continue;
3685
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003686 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3687 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003688 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3689 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003690 continue;
3691 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003692 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003693 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003694 }
3695 }
3696 return 0;
3697}
3698
3699
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003700int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3701 struct hostapd_hw_modes *mode, u8 channel)
3702{
3703 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3704 return 0;
3705
3706 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3707}
3708
3709
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003710int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3711 struct hostapd_hw_modes *mode, u8 channel)
3712{
3713 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3714 return 0;
3715 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3716}
3717
3718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3720 size_t buf_len)
3721{
3722 struct wpa_supplicant *wpa_s = ctx;
3723
3724 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3725 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3726 break;
3727 }
3728 if (wpa_s == NULL)
3729 return -1;
3730
3731 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3732}
3733
3734
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003735struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3736 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003737{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003738 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3739 struct wpa_ssid *s = wpa_s->current_ssid;
3740 if (s == NULL)
3741 continue;
3742 if (s->mode != WPAS_MODE_P2P_GO &&
3743 s->mode != WPAS_MODE_AP &&
3744 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3745 continue;
3746 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003747 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003748 continue;
3749 return wpa_s;
3750 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003751
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003752 return NULL;
3753
3754}
3755
3756
3757struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3758 const u8 *peer_dev_addr)
3759{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003760 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3761 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003762 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003763 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003764 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3765 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003766 }
3767
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003768 return NULL;
3769}
3770
3771
3772static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3773{
3774 struct wpa_supplicant *wpa_s = ctx;
3775
3776 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003777}
3778
3779
Dmitry Shmidt18463232014-01-24 12:29:41 -08003780static int wpas_is_concurrent_session_active(void *ctx)
3781{
3782 struct wpa_supplicant *wpa_s = ctx;
3783 struct wpa_supplicant *ifs;
3784
3785 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3786 if (ifs == wpa_s)
3787 continue;
3788 if (ifs->wpa_state > WPA_ASSOCIATED)
3789 return 1;
3790 }
3791 return 0;
3792}
3793
3794
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003795static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3796{
3797 struct wpa_supplicant *wpa_s = ctx;
3798 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3799}
3800
3801
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003802int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3803 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003804{
3805 struct wpa_interface iface;
3806 struct wpa_supplicant *p2pdev_wpa_s;
3807 char ifname[100];
3808 char force_name[100];
3809 int ret;
3810
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003811 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3812 wpa_s->ifname);
3813 if (os_snprintf_error(sizeof(ifname), ret))
3814 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003815 force_name[0] = '\0';
3816 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3817 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3818 force_name, wpa_s->pending_interface_addr, NULL);
3819 if (ret < 0) {
3820 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3821 return ret;
3822 }
3823 os_strlcpy(wpa_s->pending_interface_name, ifname,
3824 sizeof(wpa_s->pending_interface_name));
3825
3826 os_memset(&iface, 0, sizeof(iface));
3827 iface.p2p_mgmt = 1;
3828 iface.ifname = wpa_s->pending_interface_name;
3829 iface.driver = wpa_s->driver->name;
3830 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003831
3832 /*
3833 * If a P2P Device configuration file was given, use it as the interface
3834 * configuration file (instead of using parent's configuration file.
3835 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003836 if (conf_p2p_dev) {
3837 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003838 iface.ctrl_interface = NULL;
3839 } else {
3840 iface.confname = wpa_s->confname;
3841 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3842 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003843
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003844 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003845 if (!p2pdev_wpa_s) {
3846 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3847 return -1;
3848 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003849
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003850 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003851 wpa_s->pending_interface_name[0] = '\0';
3852 return 0;
3853}
3854
3855
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003856static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3857 const u8 *noa, size_t noa_len)
3858{
3859 struct wpa_supplicant *wpa_s, *intf = ctx;
3860 char hex[100];
3861
3862 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3863 if (wpa_s->waiting_presence_resp)
3864 break;
3865 }
3866 if (!wpa_s) {
3867 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3868 return;
3869 }
3870 wpa_s->waiting_presence_resp = 0;
3871
3872 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3873 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3874 " status=%u noa=%s", MAC2STR(src), status, hex);
3875}
3876
3877
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003878static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3879 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003880 u8 *ret_ssid, size_t *ret_ssid_len,
3881 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003882{
3883 struct wpa_supplicant *wpa_s = ctx;
3884 struct wpa_ssid *s;
3885
3886 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3887 if (s) {
3888 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3889 *ret_ssid_len = s->ssid_len;
3890 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003891
3892 if (s->mode != WPAS_MODE_P2P_GO) {
3893 os_memset(intended_iface_addr, 0, ETH_ALEN);
3894 } else if (wpas_p2p_create_iface(wpa_s)) {
3895 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3896 return 0;
3897
3898 os_memcpy(intended_iface_addr,
3899 wpa_s->pending_interface_addr, ETH_ALEN);
3900 } else {
3901 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3902 ETH_ALEN);
3903 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003904 return 1;
3905 }
3906
3907 return 0;
3908}
3909
3910
3911static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003912 u8 *ssid, size_t *ssid_len, int *group_iface,
3913 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003914{
3915 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003916 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003917 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003919 /*
3920 * group_iface will be set to 1 only if a dedicated interface for P2P
3921 * role is required. First, we try to reuse an active GO. However,
3922 * if it is not present, we will try to reactivate an existing
3923 * persistent group and set group_iface to 1, so the caller will know
3924 * that the pending interface should be used.
3925 */
3926 *group_iface = 0;
3927
3928 if (freq)
3929 *freq = 0;
3930
3931 go = wpas_p2p_get_go_group(wpa_s);
3932 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003933 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003934 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003935 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003936 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3937 else
3938 return 0;
3939 } else {
3940 s = go->current_ssid;
3941 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3942 if (freq)
3943 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003944 }
3945
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003946 os_memcpy(ssid, s->ssid, s->ssid_len);
3947 *ssid_len = s->ssid_len;
3948
3949 return 1;
3950}
3951
3952
3953static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3954 const u8 *ssid, size_t ssid_len)
3955{
3956 struct wpa_supplicant *wpa_s = ctx;
3957 struct wpa_ssid *s;
3958 int save_config = 0;
3959 size_t i;
3960
3961 /* Start with our first choice of Persistent Groups */
3962 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3963 if (go && ssid && ssid_len &&
3964 s->ssid_len == ssid_len &&
3965 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3966 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3967 break;
3968
3969 /* Remove stale persistent group */
3970 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003971 wpa_dbg(wpa_s, MSG_DEBUG,
3972 "P2P: Remove stale persistent group id=%d",
3973 s->id);
3974 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003975 wpa_config_remove_network(wpa_s->conf, s->id);
3976 save_config = 1;
3977 continue;
3978 }
3979
3980 for (i = 0; i < s->num_p2p_clients; i++) {
3981 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3982 peer, ETH_ALEN) != 0)
3983 continue;
3984
3985 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3986 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3987 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3988 break;
3989 }
3990 s->num_p2p_clients--;
3991 save_config = 1;
3992 }
3993
3994 if (save_config)
3995 p2p_config_write(wpa_s);
3996
3997 /* Return TRUE if valid SSID remains */
3998 return s != NULL;
3999}
4000
4001
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004002static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4003 const u8 *feat_cap, size_t feat_cap_len)
4004{
4005 static const char pref[] = " feature_cap=";
4006 int ret;
4007
4008 buf[0] = '\0';
4009
4010 /*
4011 * We expect a feature capability to contain at least one byte to be
4012 * reported. The string buffer provided by the caller function is
4013 * expected to be big enough to contain all bytes of the attribute for
4014 * known specifications. This function truncates the reported bytes if
4015 * the feature capability data exceeds the string buffer size.
4016 */
4017 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4018 return;
4019
4020 os_memcpy(buf, pref, sizeof(pref));
4021 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4022 buf_len - sizeof(pref) + 1,
4023 feat_cap, feat_cap_len);
4024
4025 if (ret != (2 * (int) feat_cap_len))
4026 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4027}
4028
4029
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004030static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4031 const u8 *adv_mac, const u8 *ses_mac,
4032 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4033 u8 conncap, int passwd_id,
4034 const u8 *persist_ssid,
4035 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004036 int prov_start, const char *session_info,
4037 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004038 unsigned int freq,
4039 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004040{
4041 struct wpa_supplicant *wpa_s = ctx;
4042 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004043 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004044 int save_config = 0;
4045 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004046 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004047
4048 if (!dev)
4049 return;
4050
4051 os_memset(mac, 0, ETH_ALEN);
4052 if (!adv_mac)
4053 adv_mac = mac;
4054 if (!ses_mac)
4055 ses_mac = mac;
4056 if (!grp_mac)
4057 grp_mac = mac;
4058
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004059 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4060 feat_cap, feat_cap_len);
4061
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004062 if (prov_start) {
4063 if (session_info == NULL) {
4064 wpa_msg_global(wpa_s, MSG_INFO,
4065 P2P_EVENT_P2PS_PROVISION_START MACSTR
4066 " adv_id=%x conncap=%x"
4067 " adv_mac=" MACSTR
4068 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004069 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004070 MAC2STR(dev), adv_id, conncap,
4071 MAC2STR(adv_mac),
4072 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004073 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004074 } else {
4075 wpa_msg_global(wpa_s, MSG_INFO,
4076 P2P_EVENT_P2PS_PROVISION_START MACSTR
4077 " adv_id=%x conncap=%x"
4078 " adv_mac=" MACSTR
4079 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004080 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004081 MAC2STR(dev), adv_id, conncap,
4082 MAC2STR(adv_mac),
4083 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004084 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004085 }
4086 return;
4087 }
4088
4089 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4090 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4091
4092 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4093 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4094 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4095
4096 if (persistent_go && !persistent_go->num_p2p_clients) {
4097 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004098 wpa_dbg(wpa_s, MSG_DEBUG,
4099 "P2P: Remove empty persistent group id=%d",
4100 persistent_go->id);
4101 wpas_notify_persistent_group_removed(wpa_s,
4102 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004103 wpa_config_remove_network(wpa_s->conf,
4104 persistent_go->id);
4105 }
4106
4107 wpa_msg_global(wpa_s, MSG_INFO,
4108 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4109 " status=%d"
4110 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004111 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004112 MAC2STR(dev), status,
4113 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004114 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004115 return;
4116 }
4117
4118 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004119 if (persist_ssid && persist_ssid_size)
4120 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4121 persist_ssid_size);
4122
4123 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4124 is_zero_ether_addr(grp_mac)) {
4125 wpa_dbg(wpa_s, MSG_ERROR,
4126 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4127 return;
4128 }
4129
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004130 for (;;) {
4131 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4132 if (!stale)
4133 break;
4134
4135 if (s && s->ssid_len == stale->ssid_len &&
4136 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4137 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4138 break;
4139
4140 /* Remove stale persistent group */
4141 if (stale->mode != WPAS_MODE_P2P_GO ||
4142 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004143 wpa_dbg(wpa_s, MSG_DEBUG,
4144 "P2P: Remove stale persistent group id=%d",
4145 stale->id);
4146 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004147 wpa_config_remove_network(wpa_s->conf, stale->id);
4148 } else {
4149 size_t i;
4150
4151 for (i = 0; i < stale->num_p2p_clients; i++) {
4152 if (os_memcmp(stale->p2p_client_list +
4153 i * ETH_ALEN,
4154 dev, ETH_ALEN) == 0) {
4155 os_memmove(stale->p2p_client_list +
4156 i * ETH_ALEN,
4157 stale->p2p_client_list +
4158 (i + 1) * ETH_ALEN,
4159 (stale->num_p2p_clients -
4160 i - 1) * ETH_ALEN);
4161 break;
4162 }
4163 }
4164 stale->num_p2p_clients--;
4165 }
4166 save_config = 1;
4167 }
4168
4169 if (save_config)
4170 p2p_config_write(wpa_s);
4171
4172 if (s) {
4173 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4174 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4175
4176 if (persistent_go && s != persistent_go &&
4177 !persistent_go->num_p2p_clients) {
4178 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004179 wpa_dbg(wpa_s, MSG_DEBUG,
4180 "P2P: Remove empty persistent group id=%d",
4181 persistent_go->id);
4182 wpas_notify_persistent_group_removed(wpa_s,
4183 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004184 wpa_config_remove_network(wpa_s->conf,
4185 persistent_go->id);
4186 /* Save config */
4187 }
4188
4189 wpa_msg_global(wpa_s, MSG_INFO,
4190 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4191 " status=%d"
4192 " adv_id=%x adv_mac=" MACSTR
4193 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004194 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004195 MAC2STR(dev), status,
4196 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004197 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004198 return;
4199 }
4200
4201 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004202 /*
4203 * We need to copy the interface name. Simply saving a
4204 * pointer isn't enough, since if we use pending_interface_name
4205 * it will be overwritten when the group is added.
4206 */
4207 char go_ifname[100];
4208
4209 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004210 if (!go_wpa_s) {
4211 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004212 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004213
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004214 if (!wpas_p2p_create_iface(wpa_s))
4215 os_memcpy(go_ifname, wpa_s->ifname,
4216 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004217 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004218 os_memcpy(go_ifname,
4219 wpa_s->pending_interface_name,
4220 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004221
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004222 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004223 wpas_p2ps_prov_complete(
4224 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4225 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004226 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004227 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4228 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004229 return;
4230 }
4231
4232 /* If PD Resp complete, start up the GO */
4233 if (response_done && persistent_go) {
4234 wpas_p2p_group_add_persistent(
4235 wpa_s, persistent_go,
Hai Shalom74f70d42019-02-11 14:42:39 -08004236 0, 0, freq, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004237 persistent_go->mode ==
4238 WPAS_MODE_P2P_GO ?
4239 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004240 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004241 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004242 wpas_p2p_group_add(wpa_s, 1, freq,
4243 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004244 }
4245
4246 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004247 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4248 ETH_ALEN);
4249 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004250 }
4251 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004252 os_memcpy(go_ifname, go_wpa_s->ifname,
4253 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004254
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004255 if (is_zero_ether_addr(grp_mac)) {
4256 wpa_dbg(go_wpa_s, MSG_DEBUG,
4257 "P2P: Setting PIN-1 for ANY");
4258 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4259 "12345670", NULL, 0,
4260 0);
4261 } else {
4262 wpa_dbg(go_wpa_s, MSG_DEBUG,
4263 "P2P: Setting PIN-1 for " MACSTR,
4264 MAC2STR(grp_mac));
4265 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4266 "12345670", NULL, 0,
4267 0);
4268 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004269
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004270 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4271 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004272 }
4273
4274 wpa_msg_global(wpa_s, MSG_INFO,
4275 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4276 " status=%d conncap=%x"
4277 " adv_id=%x adv_mac=" MACSTR
4278 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004279 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004280 MAC2STR(dev), status, conncap,
4281 adv_id, MAC2STR(adv_mac),
4282 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004283 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004284 return;
4285 }
4286
4287 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4288 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4289
4290 if (persistent_go && !persistent_go->num_p2p_clients) {
4291 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004292 wpa_dbg(wpa_s, MSG_DEBUG,
4293 "P2P: Remove empty persistent group id=%d",
4294 persistent_go->id);
4295 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004296 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4297 }
4298
4299 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004300 char ssid_hex[32 * 2 + 1];
4301
4302 if (group_ssid)
4303 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4304 group_ssid, group_ssid_len);
4305 else
4306 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004307 wpa_msg_global(wpa_s, MSG_INFO,
4308 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4309 " status=%d conncap=%x"
4310 " adv_id=%x adv_mac=" MACSTR
4311 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004312 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004313 MAC2STR(dev), status, conncap,
4314 adv_id, MAC2STR(adv_mac),
4315 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004316 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4317 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004318 } else {
4319 wpa_msg_global(wpa_s, MSG_INFO,
4320 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4321 " status=%d conncap=%x"
4322 " adv_id=%x adv_mac=" MACSTR
4323 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004324 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004325 MAC2STR(dev), status, conncap,
4326 adv_id, MAC2STR(adv_mac),
4327 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004328 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004329 }
4330}
4331
4332
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004333static int _wpas_p2p_in_progress(void *ctx)
4334{
4335 struct wpa_supplicant *wpa_s = ctx;
4336 return wpas_p2p_in_progress(wpa_s);
4337}
4338
4339
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004340static int wpas_prov_disc_resp_cb(void *ctx)
4341{
4342 struct wpa_supplicant *wpa_s = ctx;
4343 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004344 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004345
4346 if (!wpa_s->global->pending_p2ps_group)
4347 return 0;
4348
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004349 freq = wpa_s->global->pending_p2ps_group_freq;
4350 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004351 wpa_s->global->pending_p2ps_group = 0;
4352
4353 if (wpas_p2p_get_go_group(wpa_s))
4354 return 0;
4355 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4356
4357 if (persistent_go) {
4358 wpas_p2p_group_add_persistent(
Hai Shalom74f70d42019-02-11 14:42:39 -08004359 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004360 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004361 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004362 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08004363 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004364 }
4365
4366 return 1;
4367}
4368
4369
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004370static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4371 unsigned int *len,
4372 unsigned int *freq_list)
4373{
4374 struct wpa_supplicant *wpa_s = ctx;
4375
4376 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4377 WPA_IF_P2P_CLIENT, len, freq_list);
4378}
4379
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004380int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4381{
4382 u8 addr[ETH_ALEN] = {0};
4383
4384 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4385 return 0;
4386
4387 if (wpa_s->conf->ssid == NULL) {
4388 if (random_mac_addr(addr) < 0) {
4389 wpa_msg(wpa_s, MSG_INFO,
4390 "Failed to generate random MAC address");
4391 return -EINVAL;
4392 }
4393
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004394 /* Store generated MAC address. */
4395 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4396 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004397 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004398 /* If there are existing saved groups, restore last MAC address.
4399 * if there is no last used MAC address, the last one is
4400 * factory MAC. */
4401 if (is_zero_ether_addr(
4402 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004403 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004404 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4405 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004406 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4407 }
4408
4409 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4410 wpa_msg(wpa_s, MSG_INFO,
4411 "Failed to set random MAC address");
4412 return -EINVAL;
4413 }
4414
4415 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4416 wpa_msg(wpa_s, MSG_INFO,
4417 "Could not update MAC address information");
4418 return -EINVAL;
4419 }
4420
4421 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4422 MAC2STR(addr));
4423
4424 return 0;
4425}
4426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427/**
4428 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4429 * @global: Pointer to global data from wpa_supplicant_init()
4430 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4431 * Returns: 0 on success, -1 on failure
4432 */
4433int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4434{
4435 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436 int i;
4437
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004438 if (wpa_s->conf->p2p_disabled)
4439 return 0;
4440
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004441 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4442 return 0;
4443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 if (global->p2p)
4445 return 0;
4446
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004447 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4448 wpa_msg(wpa_s, MSG_ERROR,
4449 "Failed to initialize P2P random MAC address.");
4450 return -1;
4451 }
4452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004455 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004456 p2p.p2p_scan = wpas_p2p_scan;
4457 p2p.send_action = wpas_send_action;
4458 p2p.send_action_done = wpas_send_action_done;
4459 p2p.go_neg_completed = wpas_go_neg_completed;
4460 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4461 p2p.dev_found = wpas_dev_found;
4462 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004463 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 p2p.start_listen = wpas_start_listen;
4465 p2p.stop_listen = wpas_stop_listen;
4466 p2p.send_probe_resp = wpas_send_probe_resp;
4467 p2p.sd_request = wpas_sd_request;
4468 p2p.sd_response = wpas_sd_response;
4469 p2p.prov_disc_req = wpas_prov_disc_req;
4470 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004471 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 p2p.invitation_process = wpas_invitation_process;
4473 p2p.invitation_received = wpas_invitation_received;
4474 p2p.invitation_result = wpas_invitation_result;
4475 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004476 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004477 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004478 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004479 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004480 p2p.get_persistent_group = wpas_get_persistent_group;
4481 p2p.get_go_info = wpas_get_go_info;
4482 p2p.remove_stale_groups = wpas_remove_stale_groups;
4483 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4484 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4485 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004486 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487
4488 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004489 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 p2p.dev_name = wpa_s->conf->device_name;
4491 p2p.manufacturer = wpa_s->conf->manufacturer;
4492 p2p.model_name = wpa_s->conf->model_name;
4493 p2p.model_number = wpa_s->conf->model_number;
4494 p2p.serial_number = wpa_s->conf->serial_number;
4495 if (wpa_s->wps) {
4496 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4497 p2p.config_methods = wpa_s->wps->config_methods;
4498 }
4499
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004500 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4501 wpa_printf(MSG_ERROR,
4502 "P2P: Failed to configure supported channel list");
4503 return -1;
4504 }
4505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506 if (wpa_s->conf->p2p_listen_reg_class &&
4507 wpa_s->conf->p2p_listen_channel) {
4508 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4509 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004510 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 /*
4513 * Pick one of the social channels randomly as the listen
4514 * channel.
4515 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004516 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004517 &p2p.channel,
4518 &global->p2p_go_avoid_freq,
4519 &global->p2p_disallow_freq) !=
4520 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004521 wpa_printf(MSG_INFO,
4522 "P2P: No social channels supported by the driver - do not enable P2P");
4523 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004524 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004525 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004527 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4528 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529
4530 if (wpa_s->conf->p2p_oper_reg_class &&
4531 wpa_s->conf->p2p_oper_channel) {
4532 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4533 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4534 p2p.cfg_op_channel = 1;
4535 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4536 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4537
4538 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004539 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004540 * Use random operation channel from 2.4 GHz band social
4541 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4542 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004544 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004545 &p2p.op_channel, NULL,
4546 NULL) != 0) {
4547 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004548 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004549 p2p.op_reg_class = 0;
4550 p2p.op_channel = 0;
4551 /* This will be overridden during group setup in
4552 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004553 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554 p2p.cfg_op_channel = 0;
4555 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4556 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4557 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004558
4559 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4560 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4561 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4562 }
4563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4565 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4566 p2p.country[2] = 0x04;
4567 } else
4568 os_memcpy(p2p.country, "XX\x04", 3);
4569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4571 WPS_DEV_TYPE_LEN);
4572
4573 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4574 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4575 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4576
4577 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4578 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4579
4580 p2p.max_peers = 100;
4581
4582 if (wpa_s->conf->p2p_ssid_postfix) {
4583 p2p.ssid_postfix_len =
4584 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4585 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4586 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4587 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4588 p2p.ssid_postfix_len);
4589 }
4590
4591 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4592
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004593 p2p.max_listen = wpa_s->max_remain_on_chan;
4594
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004595 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4596 wpa_s->conf->p2p_passphrase_len <= 63)
4597 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4598 else
4599 p2p.passphrase_len = 8;
4600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 global->p2p = p2p_init(&p2p);
4602 if (global->p2p == NULL)
4603 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004604 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605
4606 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4607 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4608 continue;
4609 p2p_add_wps_vendor_extension(
4610 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4611 }
4612
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004613 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 return 0;
4616}
4617
4618
4619/**
4620 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4621 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4622 *
4623 * This function deinitialize per-interface P2P data.
4624 */
4625void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4626{
4627 if (wpa_s->driver && wpa_s->drv_priv)
4628 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004629
4630 if (wpa_s->go_params) {
4631 /* Clear any stored provisioning info */
4632 p2p_clear_provisioning_info(
4633 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004634 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004635 }
4636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637 os_free(wpa_s->go_params);
4638 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004639 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4641 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4642 wpa_s->p2p_long_listen = 0;
4643 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4644 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4645 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004646 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004647 wpas_p2p_listen_work_done(wpa_s);
4648 if (wpa_s->p2p_send_action_work) {
4649 os_free(wpa_s->p2p_send_action_work->ctx);
4650 radio_work_done(wpa_s->p2p_send_action_work);
4651 wpa_s->p2p_send_action_work = NULL;
4652 }
4653 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004655 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4656 wpa_s->p2p_oob_dev_pw = NULL;
4657
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004658 os_free(wpa_s->p2p_group_common_freqs);
4659 wpa_s->p2p_group_common_freqs = NULL;
4660 wpa_s->p2p_group_common_freqs_num = 0;
4661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662 /* TODO: remove group interface from the driver if this wpa_s instance
4663 * is on top of a P2P group interface */
4664}
4665
4666
4667/**
4668 * wpas_p2p_deinit_global - Deinitialize global P2P module
4669 * @global: Pointer to global data from wpa_supplicant_init()
4670 *
4671 * This function deinitializes the global (per device) P2P module.
4672 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004673static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674{
4675 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004677 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004678
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004679 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004680
4681 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4683 wpa_s = wpa_s->next;
4684 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004685 tmp = global->ifaces;
4686 while (tmp &&
4687 (tmp == wpa_s ||
4688 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4689 tmp = tmp->next;
4690 }
4691 if (tmp == NULL)
4692 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004693 /* Disconnect from the P2P group and deinit the interface */
4694 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004695 }
4696
4697 /*
4698 * Deinit GO data on any possibly remaining interface (if main
4699 * interface is used as GO).
4700 */
4701 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4702 if (wpa_s->ap_iface)
4703 wpas_p2p_group_deinit(wpa_s);
4704 }
4705
4706 p2p_deinit(global->p2p);
4707 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004708 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709}
4710
4711
4712static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4713{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004714 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004715 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 if (wpa_s->drv_flags &
4717 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4718 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4719 return 1; /* P2P group requires a new interface in every case
4720 */
4721 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4722 return 0; /* driver does not support concurrent operations */
4723 if (wpa_s->global->ifaces->next)
4724 return 1; /* more that one interface already in use */
4725 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4726 return 1; /* this interface is already in use */
4727 return 0;
4728}
4729
4730
4731static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4732 const u8 *peer_addr,
4733 enum p2p_wps_method wps_method,
4734 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004735 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004736 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738 if (persistent_group && wpa_s->conf->persistent_reconnect)
4739 persistent_group = 2;
4740
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004741 /*
4742 * Increase GO config timeout if HT40 is used since it takes some time
4743 * to scan channels for coex purposes before the BSS can be started.
4744 */
4745 p2p_set_config_timeout(wpa_s->global->p2p,
4746 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4749 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004750 persistent_group, ssid ? ssid->ssid : NULL,
4751 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004752 wpa_s->p2p_pd_before_go_neg, pref_freq,
4753 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4754 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755}
4756
4757
4758static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4759 const u8 *peer_addr,
4760 enum p2p_wps_method wps_method,
4761 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004762 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004763 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004764{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004765 if (persistent_group && wpa_s->conf->persistent_reconnect)
4766 persistent_group = 2;
4767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004768 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4769 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004770 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004771 ssid ? ssid->ssid_len : 0, pref_freq,
4772 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4773 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004774}
4775
4776
4777static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4778{
4779 wpa_s->p2p_join_scan_count++;
4780 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4781 wpa_s->p2p_join_scan_count);
4782 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4783 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4784 " for join operationg - stop join attempt",
4785 MAC2STR(wpa_s->pending_join_iface_addr));
4786 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004787 if (wpa_s->p2p_auto_pd) {
4788 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004789 wpa_msg_global(wpa_s, MSG_INFO,
4790 P2P_EVENT_PROV_DISC_FAILURE
4791 " p2p_dev_addr=" MACSTR " status=N/A",
4792 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004793 return;
4794 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004795 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004796 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004797 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798 }
4799}
4800
4801
Dmitry Shmidt04949592012-07-19 12:16:46 -07004802static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4803{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004804 int res;
4805 unsigned int num, i;
4806 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004807
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004808 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4809 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004810 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004811 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004813 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4814 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004815 if (!freqs)
4816 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004817
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004818 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4819 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004820
4821 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004822 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004823 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4824 freq);
4825 res = 0;
4826 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004827 }
4828 }
4829
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004830 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004831 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004832
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004833exit_free:
4834 os_free(freqs);
4835 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836}
4837
4838
4839static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4840 const u8 *peer_dev_addr)
4841{
4842 struct wpa_bss *bss;
4843 int updated;
4844
4845 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4846 if (bss == NULL)
4847 return -1;
4848 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4849 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4850 "last scan");
4851 return 0;
4852 }
4853
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004854 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4855 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004856 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4857 "%ld.%06ld (%supdated in last scan)",
4858 bss->last_update.sec, bss->last_update.usec,
4859 updated ? "": "not ");
4860
4861 return updated;
4862}
4863
4864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4866 struct wpa_scan_results *scan_res)
4867{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004868 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 int freq;
4870 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4873
4874 if (wpa_s->global->p2p_disabled)
4875 return;
4876
Dmitry Shmidt04949592012-07-19 12:16:46 -07004877 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4878 scan_res ? (int) scan_res->num : -1,
4879 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880
4881 if (scan_res)
4882 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4883
Dmitry Shmidt04949592012-07-19 12:16:46 -07004884 if (wpa_s->p2p_auto_pd) {
4885 int join = wpas_p2p_peer_go(wpa_s,
4886 wpa_s->pending_join_dev_addr);
4887 if (join == 0 &&
4888 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4889 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004890 bss = wpa_bss_get_bssid_latest(
4891 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004892 if (bss) {
4893 freq = bss->freq;
4894 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4895 "the peer " MACSTR " at %d MHz",
4896 wpa_s->auto_pd_scan_retry,
4897 MAC2STR(wpa_s->
4898 pending_join_dev_addr),
4899 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004900 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004901 return;
4902 }
4903 }
4904
4905 if (join < 0)
4906 join = 0;
4907
4908 wpa_s->p2p_auto_pd = 0;
4909 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4910 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4911 MAC2STR(wpa_s->pending_join_dev_addr), join);
4912 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004913 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004914 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004915 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004916 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004917 wpa_msg_global(wpa_s, MSG_INFO,
4918 P2P_EVENT_PROV_DISC_FAILURE
4919 " p2p_dev_addr=" MACSTR " status=N/A",
4920 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004921 }
4922 return;
4923 }
4924
4925 if (wpa_s->p2p_auto_join) {
4926 int join = wpas_p2p_peer_go(wpa_s,
4927 wpa_s->pending_join_dev_addr);
4928 if (join < 0) {
4929 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4930 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004931 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004932 P2P_EVENT_FALLBACK_TO_GO_NEG
4933 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004934 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4935 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4936 wpa_s->p2p_persistent_group, 0, 0, 0,
4937 wpa_s->p2p_go_intent,
4938 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004939 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004940 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004941 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004942 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004943 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004944 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08004945 wpa_s->p2p_go_he,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004946 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004947 return;
4948 }
4949
4950 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4951 "try to join the group", join ? "" :
4952 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004953 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004954 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004955 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004956 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004957 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004958 }
4959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4961 wpa_s->pending_join_iface_addr);
4962 if (freq < 0 &&
4963 p2p_get_interface_addr(wpa_s->global->p2p,
4964 wpa_s->pending_join_dev_addr,
4965 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004966 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4967 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4969 "address for join from " MACSTR " to " MACSTR
4970 " based on newly discovered P2P peer entry",
4971 MAC2STR(wpa_s->pending_join_iface_addr),
4972 MAC2STR(iface_addr));
4973 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4974 ETH_ALEN);
4975
4976 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4977 wpa_s->pending_join_iface_addr);
4978 }
4979 if (freq >= 0) {
4980 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4981 "from P2P peer table: %d MHz", freq);
4982 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004983 if (wpa_s->p2p_join_ssid_len) {
4984 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4985 MACSTR " and SSID %s",
4986 MAC2STR(wpa_s->pending_join_iface_addr),
4987 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4988 wpa_s->p2p_join_ssid_len));
4989 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4990 wpa_s->p2p_join_ssid,
4991 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004992 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004993 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4994 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4995 bss = wpa_bss_get_bssid_latest(wpa_s,
4996 wpa_s->pending_join_iface_addr);
4997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004999 u8 dev_addr[ETH_ALEN];
5000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001 freq = bss->freq;
5002 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005003 "from BSS table: %d MHz (SSID %s)", freq,
5004 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005005 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
5006 dev_addr) == 0 &&
5007 os_memcmp(wpa_s->pending_join_dev_addr,
5008 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5009 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5010 ETH_ALEN) != 0) {
5011 wpa_printf(MSG_DEBUG,
5012 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5013 MAC2STR(dev_addr),
5014 MAC2STR(wpa_s->pending_join_dev_addr));
5015 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5016 ETH_ALEN);
5017 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005018 }
5019 if (freq > 0) {
5020 u16 method;
5021
Dmitry Shmidt04949592012-07-19 12:16:46 -07005022 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005023 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005024 P2P_EVENT_GROUP_FORMATION_FAILURE
5025 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005026 wpas_notify_p2p_group_formation_failure(
5027 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005028 return;
5029 }
5030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5032 "prior to joining an existing group (GO " MACSTR
5033 " freq=%u MHz)",
5034 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5035 wpa_s->pending_pd_before_join = 1;
5036
5037 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 case WPS_PIN_DISPLAY:
5039 method = WPS_CONFIG_KEYPAD;
5040 break;
5041 case WPS_PIN_KEYPAD:
5042 method = WPS_CONFIG_DISPLAY;
5043 break;
5044 case WPS_PBC:
5045 method = WPS_CONFIG_PUSHBUTTON;
5046 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005047 case WPS_P2PS:
5048 method = WPS_CONFIG_P2PS;
5049 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005050 default:
5051 method = 0;
5052 break;
5053 }
5054
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005055 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5056 wpa_s->pending_join_dev_addr) ==
5057 method)) {
5058 /*
5059 * We have already performed provision discovery for
5060 * joining the group. Proceed directly to join
5061 * operation without duplicated provision discovery. */
5062 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5063 "with " MACSTR " already done - proceed to "
5064 "join",
5065 MAC2STR(wpa_s->pending_join_dev_addr));
5066 wpa_s->pending_pd_before_join = 0;
5067 goto start;
5068 }
5069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005071 wpa_s->pending_join_dev_addr,
5072 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005073 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5075 "Discovery Request before joining an "
5076 "existing group");
5077 wpa_s->pending_pd_before_join = 0;
5078 goto start;
5079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 return;
5081 }
5082
5083 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5084 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5085 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5086 wpas_p2p_check_join_scan_limit(wpa_s);
5087 return;
5088
5089start:
5090 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005091 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5092 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093}
5094
5095
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005096static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5097 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005099 int ret;
5100 struct wpa_driver_scan_params params;
5101 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005102 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005103 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005104 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105
5106 os_memset(&params, 0, sizeof(params));
5107
5108 /* P2P Wildcard SSID */
5109 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005110 if (ssid && ssid_len) {
5111 params.ssids[0].ssid = ssid;
5112 params.ssids[0].ssid_len = ssid_len;
5113 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5114 wpa_s->p2p_join_ssid_len = ssid_len;
5115 } else {
5116 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5117 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5118 wpa_s->p2p_join_ssid_len = 0;
5119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120
5121 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005122 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5123 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5124 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 if (wps_ie == NULL) {
5126 wpas_p2p_scan_res_join(wpa_s, NULL);
5127 return;
5128 }
5129
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005130 if (!freq) {
5131 int oper_freq;
5132 /*
5133 * If freq is not provided, check the operating freq of the GO
5134 * and use a single channel scan on if possible.
5135 */
5136 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5137 wpa_s->pending_join_iface_addr);
5138 if (oper_freq > 0)
5139 freq = oper_freq;
5140 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005141 if (freq > 0) {
5142 freqs[0] = freq;
5143 params.freqs = freqs;
5144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005146 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5147 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5148 if (ies == NULL) {
5149 wpabuf_free(wps_ie);
5150 wpas_p2p_scan_res_join(wpa_s, NULL);
5151 return;
5152 }
5153 wpabuf_put_buf(ies, wps_ie);
5154 wpabuf_free(wps_ie);
5155
5156 bands = wpas_get_bands(wpa_s, freqs);
5157 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5158
5159 params.p2p_probe = 1;
5160 params.extra_ies = wpabuf_head(ies);
5161 params.extra_ies_len = wpabuf_len(ies);
5162
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005163 if (wpa_s->clear_driver_scan_cache) {
5164 wpa_printf(MSG_DEBUG,
5165 "Request driver to clear scan cache due to local BSS flush");
5166 params.only_new_results = 1;
5167 }
5168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169 /*
5170 * Run a scan to update BSS table and start Provision Discovery once
5171 * the new scan results become available.
5172 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005173 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005174 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005175 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005176 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005177 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005178 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005179 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180
5181 wpabuf_free(ies);
5182
5183 if (ret) {
5184 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5185 "try again later");
5186 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5187 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5188 wpas_p2p_check_join_scan_limit(wpa_s);
5189 }
5190}
5191
5192
Dmitry Shmidt04949592012-07-19 12:16:46 -07005193static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5194{
5195 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005196 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005197}
5198
5199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005201 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005202 int auto_join, int op_freq,
5203 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204{
5205 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005206 MACSTR " dev " MACSTR " op_freq=%d)%s",
5207 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005209 if (ssid && ssid_len) {
5210 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5211 wpa_ssid_txt(ssid, ssid_len));
5212 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213
Dmitry Shmidt04949592012-07-19 12:16:46 -07005214 wpa_s->p2p_auto_pd = 0;
5215 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5217 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5218 wpa_s->pending_join_wps_method = wps_method;
5219
5220 /* Make sure we are not running find during connection establishment */
5221 wpas_p2p_stop_find(wpa_s);
5222
5223 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005224 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005225 return 0;
5226}
5227
5228
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005229static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5230 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231{
5232 struct wpa_supplicant *group;
5233 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005234 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235
5236 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5237 if (group == NULL)
5238 return -1;
5239 if (group != wpa_s) {
5240 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5241 sizeof(group->p2p_pin));
5242 group->p2p_wps_method = wpa_s->p2p_wps_method;
5243 }
5244
Hai Shalom74f70d42019-02-11 14:42:39 -08005245 /*
5246 * Need to mark the current interface for p2p_group_formation
5247 * when a separate group interface is not used. This is needed
5248 * to allow p2p_cancel stop a pending p2p_connect-join.
5249 * wpas_p2p_init_group_interface() addresses this for the case
5250 * where a separate group interface is used.
5251 */
5252 if (group == wpa_s->parent)
5253 wpa_s->global->p2p_group_formation = group;
5254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005256 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257
5258 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005259 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5261 ETH_ALEN);
5262 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005263 if (freq && ssid && ssid_len) {
5264 res.freq = freq;
5265 res.ssid_len = ssid_len;
5266 os_memcpy(res.ssid, ssid, ssid_len);
5267 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005268 if (ssid && ssid_len) {
5269 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5270 ssid, ssid_len);
5271 } else {
5272 bss = wpa_bss_get_bssid_latest(
5273 wpa_s, wpa_s->pending_join_iface_addr);
5274 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005275 if (bss) {
5276 res.freq = bss->freq;
5277 res.ssid_len = bss->ssid_len;
5278 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5279 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5280 bss->freq,
5281 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005282 } else if (ssid && ssid_len) {
5283 res.ssid_len = ssid_len;
5284 os_memcpy(res.ssid, ssid, ssid_len);
5285 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5286 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005287 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005288 }
5289
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005290 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5291 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5292 "starting client");
5293 wpa_drv_cancel_remain_on_channel(wpa_s);
5294 wpa_s->off_channel_freq = 0;
5295 wpa_s->roc_waiting_drv_freq = 0;
5296 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005297 wpas_start_wps_enrollee(group, &res);
5298
5299 /*
5300 * Allow a longer timeout for join-a-running-group than normal 15
5301 * second group formation timeout since the GO may not have authorized
5302 * our connection yet.
5303 */
5304 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5305 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5306 wpa_s, NULL);
5307
5308 return 0;
5309}
5310
5311
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005312static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005313 int *force_freq, int *pref_freq, int go,
5314 unsigned int *pref_freq_list,
5315 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005316{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005317 struct wpa_used_freq_data *freqs;
5318 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005319 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5320
5321 max_pref_freq = *num_pref_freq;
5322 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005323
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005324 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5325 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005326 if (!freqs)
5327 return -1;
5328
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005329 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5330 wpa_s->num_multichan_concurrent);
5331
5332 /*
5333 * It is possible that the total number of used frequencies is bigger
5334 * than the number of frequencies used for P2P, so get the system wide
5335 * number of unused frequencies.
5336 */
5337 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5338
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005339 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005340 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5341 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005342
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005343 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005344 int ret;
5345 if (go)
5346 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5347 else
5348 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5349 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005350 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005351 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5352 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005353 /*
5354 * If freq is a DFS channel and DFS is offloaded
5355 * to the driver, allow P2P GO to use it.
5356 */
5357 wpa_printf(MSG_DEBUG,
5358 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5359 freq);
5360 } else {
5361 wpa_printf(MSG_DEBUG,
5362 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5363 freq);
5364 res = -3;
5365 goto exit_free;
5366 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005367 }
5368
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005369 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005370 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005371 freq_in_use = 1;
5372 }
5373
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005374 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005375 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5376 freq);
5377 res = -2;
5378 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005379 }
5380 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5381 "requested channel (%u MHz)", freq);
5382 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005383 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005384 }
5385
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005386 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005387
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005388 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5389 enum wpa_driver_if_type iface_type;
5390
5391 if (go)
5392 iface_type = WPA_IF_P2P_GO;
5393 else
5394 iface_type = WPA_IF_P2P_CLIENT;
5395
5396 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5397 best_freq, go);
5398
5399 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5400 &max_pref_freq,
5401 pref_freq_list);
5402 if (!res && max_pref_freq > 0) {
5403 *num_pref_freq = max_pref_freq;
5404 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005405 while (i < *num_pref_freq &&
5406 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005407 pref_freq_list[i]) ||
5408 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005409 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005410 wpa_printf(MSG_DEBUG,
5411 "P2P: preferred_freq_list[%d]=%d is disallowed",
5412 i, pref_freq_list[i]);
5413 i++;
5414 }
5415 if (i != *num_pref_freq) {
5416 best_freq = pref_freq_list[i];
5417 wpa_printf(MSG_DEBUG,
5418 "P2P: Using preferred_freq_list[%d]=%d",
5419 i, best_freq);
5420 } else {
5421 wpa_printf(MSG_DEBUG,
5422 "P2P: All driver preferred frequencies are disallowed for P2P use");
5423 *num_pref_freq = 0;
5424 }
5425 } else {
5426 wpa_printf(MSG_DEBUG,
5427 "P2P: No preferred frequency list available");
5428 }
5429 }
5430
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005431 /* We have a candidate frequency to use */
5432 if (best_freq > 0) {
5433 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005434 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005435 best_freq);
5436 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005437 } else {
5438 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 -07005439 best_freq);
5440 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005441 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005442 } else if (num_unused > 0) {
5443 wpa_printf(MSG_DEBUG,
5444 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5445 *force_freq = 0;
5446 } else {
5447 wpa_printf(MSG_DEBUG,
5448 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5449 res = -2;
5450 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005451 }
5452
5453exit_ok:
5454 res = 0;
5455exit_free:
5456 os_free(freqs);
5457 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005458}
5459
5460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461/**
5462 * wpas_p2p_connect - Request P2P Group Formation to be started
5463 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5464 * @peer_addr: Address of the peer P2P Device
5465 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5466 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005467 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 * @join: Whether to join an existing group (as a client) instead of starting
5469 * Group Owner negotiation; @peer_addr is BSSID in that case
5470 * @auth: Whether to only authorize the connection instead of doing that and
5471 * initiating Group Owner negotiation
5472 * @go_intent: GO Intent or -1 to use default
5473 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005474 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005475 * @persistent_id: Persistent group credentials to use for forcing GO
5476 * parameters or -1 to generate new values (SSID/passphrase)
5477 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5478 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005479 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005480 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005481 * @vht_chwidth: Channel width supported by GO operating with VHT support
5482 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005483 * @group_ssid: Specific Group SSID for join or %NULL if not set
5484 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5486 * failure, -2 on failure due to channel not currently available,
5487 * -3 if forced channel is not supported
5488 */
5489int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5490 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005491 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005492 int go_intent, int freq, unsigned int vht_center_freq2,
5493 int persistent_id, int pd, int ht40, int vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08005494 unsigned int vht_chwidth, int he, const u8 *group_ssid,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005495 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005497 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005498 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005500 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005501 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005502 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503
5504 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5505 return -1;
5506
Dmitry Shmidt04949592012-07-19 12:16:46 -07005507 if (persistent_id >= 0) {
5508 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5509 if (ssid == NULL || ssid->disabled != 2 ||
5510 ssid->mode != WPAS_MODE_P2P_GO)
5511 return -1;
5512 }
5513
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005514 os_free(wpa_s->global->add_psk);
5515 wpa_s->global->add_psk = NULL;
5516
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005517 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005518 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005519 wpa_s->global->pending_p2ps_group_freq = 0;
5520 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005522 if (go_intent < 0)
5523 go_intent = wpa_s->conf->p2p_go_intent;
5524
5525 if (!auth)
5526 wpa_s->p2p_long_listen = 0;
5527
5528 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005529 wpa_s->p2p_persistent_group = !!persistent_group;
5530 wpa_s->p2p_persistent_id = persistent_id;
5531 wpa_s->p2p_go_intent = go_intent;
5532 wpa_s->p2p_connect_freq = freq;
5533 wpa_s->p2p_fallback_to_go_neg = 0;
5534 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005535 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005536 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005537 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5538 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005539 wpa_s->p2p_go_he = !!he;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005540
5541 if (pin)
5542 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5543 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005544 if (wps_generate_pin((unsigned int *) &ret) < 0)
5545 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005546 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5547 "%08d", ret);
5548 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5549 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005550 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5551 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005552 } else if (wps_method == WPS_P2PS) {
5553 /* Force the P2Ps default PIN to be used */
5554 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005555 } else
5556 wpa_s->p2p_pin[0] = '\0';
5557
Dmitry Shmidt04949592012-07-19 12:16:46 -07005558 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005559 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5560 if (auth) {
5561 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5562 "connect a running group from " MACSTR,
5563 MAC2STR(peer_addr));
5564 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5565 return ret;
5566 }
5567 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5568 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5569 iface_addr) < 0) {
5570 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5571 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5572 dev_addr);
5573 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005574 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005575 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005576 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5577 "%ld.%06ld",
5578 wpa_s->p2p_auto_started.sec,
5579 wpa_s->p2p_auto_started.usec);
5580 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005581 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005582 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005583 auto_join, freq,
5584 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005585 return -1;
5586 return ret;
5587 }
5588
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005589 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005590 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005591 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005592 if (res)
5593 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005594 wpas_p2p_set_own_freq_preference(wpa_s,
5595 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005597 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005601 if (wpa_s->create_p2p_iface) {
5602 /* Prepare to add a new interface for the group */
5603 iftype = WPA_IF_P2P_GROUP;
5604 if (go_intent == 15)
5605 iftype = WPA_IF_P2P_GO;
5606 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5607 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5608 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005610 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005612 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005613 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005614 if (wpa_s->p2p_mgmt)
5615 if_addr = wpa_s->parent->own_addr;
5616 else
5617 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005618 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005620
5621 if (auth) {
5622 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005623 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005624 force_freq, persistent_group, ssid,
5625 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626 return -1;
5627 return ret;
5628 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005629
5630 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5631 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005632 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005633 if (wpa_s->create_p2p_iface)
5634 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 return -1;
5636 }
5637 return ret;
5638}
5639
5640
5641/**
5642 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5643 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5644 * @freq: Frequency of the channel in MHz
5645 * @duration: Duration of the stay on the channel in milliseconds
5646 *
5647 * This callback is called when the driver indicates that it has started the
5648 * requested remain-on-channel duration.
5649 */
5650void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5651 unsigned int freq, unsigned int duration)
5652{
5653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5654 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005655 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)",
5656 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5657 wpa_s->roc_waiting_drv_freq, freq, duration);
5658 if (wpa_s->off_channel_freq &&
5659 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005660 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5661 wpa_s->pending_listen_duration);
5662 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005663 } else {
5664 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5665 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5666 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667 }
5668}
5669
5670
Jithu Jance57cea1a2014-08-20 10:22:04 -07005671int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672{
5673 /* Limit maximum Listen state time based on driver limitation. */
5674 if (timeout > wpa_s->max_remain_on_chan)
5675 timeout = wpa_s->max_remain_on_chan;
5676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005677 return p2p_listen(wpa_s->global->p2p, timeout);
5678}
5679
5680
5681/**
5682 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5683 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5684 * @freq: Frequency of the channel in MHz
5685 *
5686 * This callback is called when the driver indicates that a remain-on-channel
5687 * operation has been completed, i.e., the duration on the requested channel
5688 * has timed out.
5689 */
5690void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5691 unsigned int freq)
5692{
5693 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5694 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005695 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005696 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5698 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005699 if (wpa_s->p2p_long_listen > 0)
5700 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5702 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005703 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705 if (wpa_s->p2p_long_listen > 0) {
5706 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5707 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005708 } else {
5709 /*
5710 * When listen duration is over, stop listen & update p2p_state
5711 * to IDLE.
5712 */
5713 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714 }
5715}
5716
5717
5718/**
5719 * wpas_p2p_group_remove - Remove a P2P group
5720 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5721 * @ifname: Network interface name of the group interface or "*" to remove all
5722 * groups
5723 * Returns: 0 on success, -1 on failure
5724 *
5725 * This function is used to remove a P2P group. This can be used to disconnect
5726 * from a group in which the local end is a P2P Client or to end a P2P Group in
5727 * case the local end is the Group Owner. If a virtual network interface was
5728 * created for this group, that interface will be removed. Otherwise, only the
5729 * configured P2P group network will be removed from the interface.
5730 */
5731int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5732{
5733 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005734 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005735
5736 if (os_strcmp(ifname, "*") == 0) {
5737 struct wpa_supplicant *prev;
5738 wpa_s = global->ifaces;
5739 while (wpa_s) {
5740 prev = wpa_s;
5741 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005742 if (prev->p2p_group_interface !=
5743 NOT_P2P_GROUP_INTERFACE ||
5744 (prev->current_ssid &&
5745 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005746 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005747 }
5748 return 0;
5749 }
5750
5751 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5752 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5753 break;
5754 }
5755
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005756 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757}
5758
5759
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005760static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5761{
5762 unsigned int r;
5763
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005764 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5765 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5766 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5767 int res;
5768
5769 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5770 &size, pref_freq_list);
5771 if (!res && size > 0) {
5772 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005773 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005774 (!p2p_supported_freq(wpa_s->global->p2p,
5775 pref_freq_list[i]) ||
5776 wpas_p2p_disallowed_freq(wpa_s->global,
5777 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005778 wpa_printf(MSG_DEBUG,
5779 "P2P: preferred_freq_list[%d]=%d is disallowed",
5780 i, pref_freq_list[i]);
5781 i++;
5782 }
5783 if (i != size) {
5784 freq = pref_freq_list[i];
5785 wpa_printf(MSG_DEBUG,
5786 "P2P: Using preferred_freq_list[%d]=%d",
5787 i, freq);
5788 } else {
5789 wpa_printf(MSG_DEBUG,
5790 "P2P: All driver preferred frequencies are disallowed for P2P use");
5791 }
5792 } else {
5793 wpa_printf(MSG_DEBUG,
5794 "P2P: No preferred frequency list available");
5795 }
5796 }
5797
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005798 if (freq == 2) {
5799 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5800 "band");
5801 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005802 p2p_supported_freq_go(wpa_s->global->p2p,
5803 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005804 freq = wpa_s->best_24_freq;
5805 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5806 "channel: %d MHz", freq);
5807 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005808 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5809 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005810 freq = 2412 + (r % 3) * 25;
5811 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5812 "channel: %d MHz", freq);
5813 }
5814 }
5815
5816 if (freq == 5) {
5817 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5818 "band");
5819 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005820 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005821 wpa_s->best_5_freq)) {
5822 freq = wpa_s->best_5_freq;
5823 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5824 "channel: %d MHz", freq);
5825 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005826 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5827 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005828 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005829 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005830 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5831 "5 GHz channel for P2P group");
5832 return -1;
5833 }
5834 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5835 "channel: %d MHz", freq);
5836 }
5837 }
5838
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005839 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005840 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005841 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5842 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005843 /*
5844 * If freq is a DFS channel and DFS is offloaded to the
5845 * driver, allow P2P GO to use it.
5846 */
5847 wpa_printf(MSG_DEBUG, "P2P: "
5848 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5849 __func__, freq);
5850 return freq;
5851 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005852 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5853 "(%u MHz) is not supported for P2P uses",
5854 freq);
5855 return -1;
5856 }
5857
5858 return freq;
5859}
5860
5861
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005862static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5863 const struct p2p_channels *channels,
5864 int freq)
5865{
5866 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5867 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5868 freq_included(wpa_s, channels, freq))
5869 return 1;
5870 return 0;
5871}
5872
5873
5874static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5875 struct p2p_go_neg_results *params,
5876 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005877{
5878 unsigned int i, r;
5879
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005880 /* try all channels in operating class 115 */
5881 for (i = 0; i < 4; i++) {
5882 params->freq = 5180 + i * 20;
5883 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005884 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005885 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5886 goto out;
5887 }
5888
5889 /* try all channels in operating class 124 */
5890 for (i = 0; i < 4; i++) {
5891 params->freq = 5745 + i * 20;
5892 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005893 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005894 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5895 goto out;
5896 }
5897
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005898 /* try social channel class 180 channel 2 */
5899 params->freq = 58320 + 1 * 2160;
5900 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005901 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005902 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5903 goto out;
5904
5905 /* try all channels in reg. class 180 */
5906 for (i = 0; i < 4; i++) {
5907 params->freq = 58320 + i * 2160;
5908 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005909 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005910 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5911 goto out;
5912 }
5913
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005914 /* try some random selection of the social channels */
5915 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5916 return;
5917
5918 for (i = 0; i < 3; i++) {
5919 params->freq = 2412 + ((r + i) % 3) * 25;
5920 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5921 goto out;
5922 }
5923
5924 /* try all other channels in operating class 81 */
5925 for (i = 0; i < 11; i++) {
5926 params->freq = 2412 + i * 5;
5927
5928 /* skip social channels; covered in the previous loop */
5929 if (params->freq == 2412 ||
5930 params->freq == 2437 ||
5931 params->freq == 2462)
5932 continue;
5933
5934 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5935 goto out;
5936 }
5937
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005938 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005939 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005940 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005941out:
5942 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5943 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005944}
5945
5946
Roshan Pius3a1667e2018-07-03 15:17:14 -07005947static int wpas_same_band(int freq1, int freq2)
5948{
5949 enum hostapd_hw_mode mode1, mode2;
5950 u8 chan1, chan2;
5951
5952 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5953 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5954 if (mode1 == NUM_HOSTAPD_MODES)
5955 return 0;
5956 return mode1 == mode2;
5957}
5958
5959
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005960static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5961 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005962 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08005963 int vht, int max_oper_chwidth, int he,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005964 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005966 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005967 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005968 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005969 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005970 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971
5972 os_memset(params, 0, sizeof(*params));
5973 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005974 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005975 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08005976 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005977 params->max_oper_chwidth = max_oper_chwidth;
5978 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005980 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5981 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005982 if (!freqs)
5983 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005985 num = get_shared_radio_freqs_data(wpa_s, freqs,
5986 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005987
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005988 if (wpa_s->current_ssid &&
5989 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5990 wpa_s->wpa_state == WPA_COMPLETED) {
5991 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5992 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005993
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005994 /*
5995 * If the frequency selection is done for an active P2P GO that
5996 * is not sharing a frequency, allow to select a new frequency
5997 * even if there are no unused frequencies as we are about to
5998 * move the P2P GO so its frequency can be re-used.
5999 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006000 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006001 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6002 freqs[i].flags == 0) {
6003 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006004 break;
6005 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006006 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006007 }
6008
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006009 /* try using the forced freq */
6010 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006011 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6012 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006013 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006014 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006015 freq);
6016 goto fail;
6017 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006018 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6019 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006020 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6021 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006022 /*
6023 * If freq is a DFS channel and DFS is offloaded
6024 * to the driver, allow P2P GO to use it.
6025 */
6026 wpa_printf(MSG_DEBUG,
6027 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6028 __func__, freq);
6029 } else {
6030 wpa_printf(MSG_DEBUG,
6031 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6032 freq);
6033 goto fail;
6034 }
6035 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006036
6037 for (i = 0; i < num; i++) {
6038 if (freqs[i].freq == freq) {
6039 wpa_printf(MSG_DEBUG,
6040 "P2P: forced freq (%d MHz) is also shared",
6041 freq);
6042 params->freq = freq;
6043 goto success;
6044 }
6045 }
6046
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006047 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006048 wpa_printf(MSG_DEBUG,
6049 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6050 freq);
6051 goto fail;
6052 }
6053
6054 wpa_printf(MSG_DEBUG,
6055 "P2P: force GO freq (%d MHz) on a free channel",
6056 freq);
6057 params->freq = freq;
6058 goto success;
6059 }
6060
6061 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006062 if (num &&
6063 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006064 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6065 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6066 wpa_printf(MSG_DEBUG,
6067 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006068 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006069 params->freq = cand;
6070 goto success;
6071 }
6072
6073 /* try using one of the shared freqs */
6074 for (i = 0; i < num; i++) {
6075 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6076 freqs[i].freq)) {
6077 wpa_printf(MSG_DEBUG,
6078 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006079 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006080 params->freq = freqs[i].freq;
6081 goto success;
6082 }
6083 }
6084 }
6085
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006086 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006087 wpa_printf(MSG_DEBUG,
6088 "P2P: Cannot force GO on any of the channels we are already using");
6089 goto fail;
6090 }
6091
6092 /* try using the setting from the configuration file */
6093 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6094 wpa_s->conf->p2p_oper_channel >= 1 &&
6095 wpa_s->conf->p2p_oper_channel <= 11 &&
6096 wpas_p2p_supported_freq_go(
6097 wpa_s, channels,
6098 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6099 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6100 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6101 "frequency %d MHz", params->freq);
6102 goto success;
6103 }
6104
6105 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6106 wpa_s->conf->p2p_oper_reg_class == 116 ||
6107 wpa_s->conf->p2p_oper_reg_class == 117 ||
6108 wpa_s->conf->p2p_oper_reg_class == 124 ||
6109 wpa_s->conf->p2p_oper_reg_class == 125 ||
6110 wpa_s->conf->p2p_oper_reg_class == 126 ||
6111 wpa_s->conf->p2p_oper_reg_class == 127) &&
6112 wpas_p2p_supported_freq_go(wpa_s, channels,
6113 5000 +
6114 5 * wpa_s->conf->p2p_oper_channel)) {
6115 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6116 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6117 "frequency %d MHz", params->freq);
6118 goto success;
6119 }
6120
6121 /* Try using best channels */
6122 if (wpa_s->conf->p2p_oper_channel == 0 &&
6123 wpa_s->best_overall_freq > 0 &&
6124 wpas_p2p_supported_freq_go(wpa_s, channels,
6125 wpa_s->best_overall_freq)) {
6126 params->freq = wpa_s->best_overall_freq;
6127 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6128 "channel %d MHz", params->freq);
6129 goto success;
6130 }
6131
6132 if (wpa_s->conf->p2p_oper_channel == 0 &&
6133 wpa_s->best_24_freq > 0 &&
6134 wpas_p2p_supported_freq_go(wpa_s, channels,
6135 wpa_s->best_24_freq)) {
6136 params->freq = wpa_s->best_24_freq;
6137 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6138 "channel %d MHz", params->freq);
6139 goto success;
6140 }
6141
6142 if (wpa_s->conf->p2p_oper_channel == 0 &&
6143 wpa_s->best_5_freq > 0 &&
6144 wpas_p2p_supported_freq_go(wpa_s, channels,
6145 wpa_s->best_5_freq)) {
6146 params->freq = wpa_s->best_5_freq;
6147 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6148 "channel %d MHz", params->freq);
6149 goto success;
6150 }
6151
6152 /* try using preferred channels */
6153 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6154 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6155 params->freq = cand;
6156 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6157 "channels", params->freq);
6158 goto success;
6159 }
6160
Roshan Pius3a1667e2018-07-03 15:17:14 -07006161 /* Try using a channel that allows VHT to be used with 80 MHz */
6162 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6163 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6164 enum hostapd_hw_mode mode;
6165 struct hostapd_hw_modes *hwmode;
6166 u8 chan;
6167
6168 cand = wpa_s->p2p_group_common_freqs[i];
6169 mode = ieee80211_freq_to_chan(cand, &chan);
6170 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6171 mode);
6172 if (!hwmode ||
6173 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6174 BW80) != ALLOWED)
6175 continue;
6176 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6177 params->freq = cand;
6178 wpa_printf(MSG_DEBUG,
6179 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6180 params->freq);
6181 goto success;
6182 }
6183 }
6184 }
6185
6186 /* Try using a channel that allows HT to be used with 40 MHz on the same
6187 * band so that CSA can be used */
6188 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6189 wpa_s->p2p_group_common_freqs) {
6190 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6191 enum hostapd_hw_mode mode;
6192 struct hostapd_hw_modes *hwmode;
6193 u8 chan;
6194
6195 cand = wpa_s->p2p_group_common_freqs[i];
6196 mode = ieee80211_freq_to_chan(cand, &chan);
6197 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6198 mode);
6199 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6200 cand) ||
6201 !hwmode ||
6202 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6203 BW40MINUS) != ALLOWED &&
6204 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6205 BW40PLUS) != ALLOWED))
6206 continue;
6207 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6208 params->freq = cand;
6209 wpa_printf(MSG_DEBUG,
6210 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6211 params->freq);
6212 goto success;
6213 }
6214 }
6215 }
6216
6217 /* Try using one of the group common freqs on the same band so that CSA
6218 * can be used */
6219 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6220 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6221 cand = wpa_s->p2p_group_common_freqs[i];
6222 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6223 cand))
6224 continue;
6225 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6226 params->freq = cand;
6227 wpa_printf(MSG_DEBUG,
6228 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6229 params->freq);
6230 goto success;
6231 }
6232 }
6233 }
6234
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006235 /* Try using one of the group common freqs */
6236 if (wpa_s->p2p_group_common_freqs) {
6237 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6238 cand = wpa_s->p2p_group_common_freqs[i];
6239 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6240 params->freq = cand;
6241 wpa_printf(MSG_DEBUG,
6242 "P2P: Use freq %d MHz common with the peer",
6243 params->freq);
6244 goto success;
6245 }
6246 }
6247 }
6248
6249 /* no preference, select some channel */
6250 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6251
6252 if (params->freq == 0) {
6253 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6254 goto fail;
6255 }
6256
6257success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006258 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006259 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006260fail:
6261 os_free(freqs);
6262 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006263}
6264
6265
6266static struct wpa_supplicant *
6267wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6268 int go)
6269{
6270 struct wpa_supplicant *group_wpa_s;
6271
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006272 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006273 if (wpa_s->p2p_mgmt) {
6274 /*
6275 * We may be called on the p2p_dev interface which
6276 * cannot be used for group operations, so always use
6277 * the primary interface.
6278 */
6279 wpa_s->parent->p2pdev = wpa_s;
6280 wpa_s = wpa_s->parent;
6281 }
6282 wpa_dbg(wpa_s, MSG_DEBUG,
6283 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006284 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006285 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006286 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006287 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006288 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006289
6290 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006291 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006292 wpa_msg_global(wpa_s, MSG_ERROR,
6293 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006294 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006296 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6297 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006298 wpa_msg_global(wpa_s, MSG_ERROR,
6299 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300 wpas_p2p_remove_pending_group_interface(wpa_s);
6301 return NULL;
6302 }
6303
Roshan Pius3a1667e2018-07-03 15:17:14 -07006304 if (go && wpa_s->p2p_go_do_acs) {
6305 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6306 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6307 wpa_s->p2p_go_do_acs = 0;
6308 }
6309
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6311 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006312 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313 return group_wpa_s;
6314}
6315
6316
6317/**
6318 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6319 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6320 * @persistent_group: Whether to create a persistent group
6321 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006322 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006323 * @ht40: Start GO with 40 MHz channel width
6324 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006325 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006326 * Returns: 0 on success, -1 on failure
6327 *
6328 * This function creates a new P2P group with the local end as the Group Owner,
6329 * i.e., without using Group Owner Negotiation.
6330 */
6331int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006332 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08006333 int max_oper_chwidth, int he)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334{
6335 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006336
6337 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6338 return -1;
6339
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006340 os_free(wpa_s->global->add_psk);
6341 wpa_s->global->add_psk = NULL;
6342
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006343 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006344 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006345 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006346
Roshan Pius3a1667e2018-07-03 15:17:14 -07006347 if (!wpa_s->p2p_go_do_acs) {
6348 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6349 if (freq < 0)
6350 return -1;
6351 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006353 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Hai Shalom74f70d42019-02-11 14:42:39 -08006354 ht40, vht, max_oper_chwidth, he, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006355 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006356
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357 p2p_go_params(wpa_s->global->p2p, &params);
6358 params.persistent_group = persistent_group;
6359
6360 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6361 if (wpa_s == NULL)
6362 return -1;
6363 wpas_start_wps_go(wpa_s, &params, 0);
6364
6365 return 0;
6366}
6367
6368
6369static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006370 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006371 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006372{
6373 struct wpa_ssid *ssid;
6374
6375 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6376 if (wpa_s == NULL)
6377 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006378 if (force_scan)
6379 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006380 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006381
6382 wpa_supplicant_ap_deinit(wpa_s);
6383
6384 ssid = wpa_config_add_network(wpa_s->conf);
6385 if (ssid == NULL)
6386 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006387 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388 wpa_config_set_network_defaults(ssid);
6389 ssid->temporary = 1;
6390 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006391 ssid->pbss = params->pbss;
6392 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6393 WPA_CIPHER_CCMP;
6394 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006395 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6396 ssid->ssid = os_malloc(params->ssid_len);
6397 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006398 wpa_config_remove_network(wpa_s->conf, ssid->id);
6399 return -1;
6400 }
6401 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6402 ssid->ssid_len = params->ssid_len;
6403 ssid->p2p_group = 1;
6404 ssid->export_keys = 1;
6405 if (params->psk_set) {
6406 os_memcpy(ssid->psk, params->psk, 32);
6407 ssid->psk_set = 1;
6408 }
6409 if (params->passphrase)
6410 ssid->passphrase = os_strdup(params->passphrase);
6411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006412 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006413 wpa_s->p2p_in_invitation = 1;
6414 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006415 wpa_s->p2p_go_group_formation_completed = 0;
6416 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006418 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006419 NULL);
6420 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6421 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006422 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006423 wpa_supplicant_select_network(wpa_s, ssid);
6424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006425 return 0;
6426}
6427
6428
6429int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6430 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006431 int force_freq, int neg_freq,
6432 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006433 int vht, int max_oper_chwidth, int he,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006434 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006435 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006436{
6437 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006438 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006439
6440 if (ssid->disabled != 2 || ssid->ssid == NULL)
6441 return -1;
6442
6443 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6444 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6445 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6446 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006447 if (go == 0 &&
6448 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006449 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006450 /*
6451 * This can happen if Invitation Response frame was lost
6452 * and the peer (GO of a persistent group) tries to
6453 * invite us again. Reschedule the timeout to avoid
6454 * terminating the wait for the connection too early
6455 * since we now know that the peer is still trying to
6456 * invite us instead of having already started the GO.
6457 */
6458 wpa_printf(MSG_DEBUG,
6459 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6460 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6461 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006462 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006464 return 0;
6465 }
6466
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006467 os_free(wpa_s->global->add_psk);
6468 wpa_s->global->add_psk = NULL;
6469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006470 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006471 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006472
Dmitry Shmidt04949592012-07-19 12:16:46 -07006473 wpa_s->p2p_fallback_to_go_neg = 0;
6474
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006475 if (ssid->mode == WPAS_MODE_P2P_GO) {
6476 if (force_freq > 0) {
6477 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6478 if (freq < 0)
6479 return -1;
6480 } else {
6481 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6482 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006483 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006484 freq = 0;
6485 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006486 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006487 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006488 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006489 struct os_reltime now;
6490 struct wpa_bss *bss =
6491 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006492
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006493 os_get_reltime(&now);
6494 if (bss &&
6495 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006496 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006497 freq = bss->freq;
6498 else
6499 freq = 0;
6500 }
6501
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006502 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6503 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006504 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006505 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006506 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006507
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006508 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Hai Shalom74f70d42019-02-11 14:42:39 -08006509 ht40, vht, max_oper_chwidth, he, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006510 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006511
6512 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006513 params.psk_set = ssid->psk_set;
6514 if (params.psk_set)
6515 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006516 if (ssid->passphrase) {
6517 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6518 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6519 "persistent group");
6520 return -1;
6521 }
6522 os_strlcpy(params.passphrase, ssid->passphrase,
6523 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006524 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6526 params.ssid_len = ssid->ssid_len;
6527 params.persistent_group = 1;
6528
6529 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6530 if (wpa_s == NULL)
6531 return -1;
6532
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006533 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6534
Dmitry Shmidt56052862013-10-04 10:23:25 -07006535 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006536 wpas_start_wps_go(wpa_s, &params, 0);
6537
6538 return 0;
6539}
6540
6541
6542static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6543 struct wpabuf *proberesp_ies)
6544{
6545 struct wpa_supplicant *wpa_s = ctx;
6546 if (wpa_s->ap_iface) {
6547 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006548 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6549 wpabuf_free(beacon_ies);
6550 wpabuf_free(proberesp_ies);
6551 return;
6552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006553 if (beacon_ies) {
6554 wpabuf_free(hapd->p2p_beacon_ie);
6555 hapd->p2p_beacon_ie = beacon_ies;
6556 }
6557 wpabuf_free(hapd->p2p_probe_resp_ie);
6558 hapd->p2p_probe_resp_ie = proberesp_ies;
6559 } else {
6560 wpabuf_free(beacon_ies);
6561 wpabuf_free(proberesp_ies);
6562 }
6563 wpa_supplicant_ap_update_beacon(wpa_s);
6564}
6565
6566
6567static void wpas_p2p_idle_update(void *ctx, int idle)
6568{
6569 struct wpa_supplicant *wpa_s = ctx;
6570 if (!wpa_s->ap_iface)
6571 return;
6572 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006573 if (idle) {
6574 if (wpa_s->global->p2p_fail_on_wps_complete &&
6575 wpa_s->p2p_in_provisioning) {
6576 wpas_p2p_grpform_fail_after_wps(wpa_s);
6577 return;
6578 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006579 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006580 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006581 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6582}
6583
6584
6585struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006586 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587{
6588 struct p2p_group *group;
6589 struct p2p_group_config *cfg;
6590
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006591 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6592 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006593 return NULL;
6594
6595 cfg = os_zalloc(sizeof(*cfg));
6596 if (cfg == NULL)
6597 return NULL;
6598
Dmitry Shmidt04949592012-07-19 12:16:46 -07006599 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006600 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006601 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006602 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006603 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6604 if (wpa_s->max_stations &&
6605 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6606 cfg->max_clients = wpa_s->max_stations;
6607 else
6608 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006609 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6610 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006611 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006612 cfg->cb_ctx = wpa_s;
6613 cfg->ie_update = wpas_p2p_ie_update;
6614 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006615 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6616 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617
6618 group = p2p_group_init(wpa_s->global->p2p, cfg);
6619 if (group == NULL)
6620 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006621 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006622 p2p_group_notif_formation_done(group);
6623 wpa_s->p2p_group = group;
6624 return group;
6625}
6626
6627
6628void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6629 int registrar)
6630{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006631 struct wpa_ssid *ssid = wpa_s->current_ssid;
6632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633 if (!wpa_s->p2p_in_provisioning) {
6634 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6635 "provisioning not in progress");
6636 return;
6637 }
6638
Dmitry Shmidt04949592012-07-19 12:16:46 -07006639 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6640 u8 go_dev_addr[ETH_ALEN];
6641 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6642 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6643 ssid->ssid_len);
6644 /* Clear any stored provisioning info */
6645 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6646 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006647
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006648 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006649 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006650 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006651 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6652 /*
6653 * Use a separate timeout for initial data connection to
6654 * complete to allow the group to be removed automatically if
6655 * something goes wrong in this step before the P2P group idle
6656 * timeout mechanism is taken into use.
6657 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006658 wpa_dbg(wpa_s, MSG_DEBUG,
6659 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6660 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006661 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6662 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006663 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006664 /* Complete group formation on successful data connection. */
6665 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006666 } else if (ssid) {
6667 /*
6668 * Use a separate timeout for initial data connection to
6669 * complete to allow the group to be removed automatically if
6670 * the client does not complete data connection successfully.
6671 */
6672 wpa_dbg(wpa_s, MSG_DEBUG,
6673 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6674 P2P_MAX_INITIAL_CONN_WAIT_GO);
6675 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6676 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006677 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006678 /*
6679 * Complete group formation on first successful data connection
6680 */
6681 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006682 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006683 if (wpa_s->global->p2p)
6684 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006685 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006686}
6687
6688
Jouni Malinen75ecf522011-06-27 15:19:46 -07006689void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6690 struct wps_event_fail *fail)
6691{
6692 if (!wpa_s->p2p_in_provisioning) {
6693 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6694 "provisioning not in progress");
6695 return;
6696 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006697
6698 if (wpa_s->go_params) {
6699 p2p_clear_provisioning_info(
6700 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006701 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006702 }
6703
Jouni Malinen75ecf522011-06-27 15:19:46 -07006704 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006705
6706 if (wpa_s == wpa_s->global->p2p_group_formation) {
6707 /*
6708 * Allow some time for the failed WPS negotiation exchange to
6709 * complete, but remove the group since group formation cannot
6710 * succeed after provisioning failure.
6711 */
6712 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6713 wpa_s->global->p2p_fail_on_wps_complete = 1;
6714 eloop_deplete_timeout(0, 50000,
6715 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006716 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006717 }
6718}
6719
6720
6721int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6722{
6723 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6724 !wpa_s->p2p_in_provisioning)
6725 return 0;
6726
6727 wpas_p2p_grpform_fail_after_wps(wpa_s);
6728
6729 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006730}
6731
6732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006733int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006734 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006735 enum wpas_p2p_prov_disc_use use,
6736 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006737{
6738 u16 config_methods;
6739
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006740 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006741 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006742 wpa_s->p2p_fallback_to_go_neg = 0;
6743 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006744 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6745 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006746 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6747 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6748
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006749 wpa_printf(MSG_DEBUG,
6750 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6751 __func__, p2ps_prov->conncap,
6752 p2ps_prov->adv_id, p2ps_prov->conncap,
6753 p2ps_prov->status, p2ps_prov->info);
6754
6755 config_methods = 0;
6756 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006757 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006758 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006760 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6761 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006762 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006763 else {
6764 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006765 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006766 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006767 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006768
Dmitry Shmidt04949592012-07-19 12:16:46 -07006769 if (use == WPAS_P2P_PD_AUTO) {
6770 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6771 wpa_s->pending_pd_config_methods = config_methods;
6772 wpa_s->p2p_auto_pd = 1;
6773 wpa_s->p2p_auto_join = 0;
6774 wpa_s->pending_pd_before_join = 0;
6775 wpa_s->auto_pd_scan_retry = 0;
6776 wpas_p2p_stop_find(wpa_s);
6777 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006778 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006779 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6780 wpa_s->p2p_auto_started.sec,
6781 wpa_s->p2p_auto_started.usec);
6782 wpas_p2p_join_scan(wpa_s, NULL);
6783 return 0;
6784 }
6785
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006786 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6787 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006788 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006789 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006790
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006791 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006792 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006793 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006794}
6795
6796
6797int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6798 char *end)
6799{
6800 return p2p_scan_result_text(ies, ies_len, buf, end);
6801}
6802
6803
6804static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6805{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006806 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006807 return;
6808
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006809 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006810 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006811 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6812 wpa_s, NULL);
6813 offchannel_send_action_done(wpa_s);
6814 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006816 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6817 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006818 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819}
6820
6821
6822int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6823 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006824 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006825 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006826 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006827{
6828 wpas_p2p_clear_pending_action_tx(wpa_s);
6829 wpa_s->p2p_long_listen = 0;
6830
Dmitry Shmidt04949592012-07-19 12:16:46 -07006831 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006832 wpa_s->p2p_in_provisioning) {
6833 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6834 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6835 " (P2P disabled)" : "",
6836 wpa_s->p2p_in_provisioning ?
6837 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006838 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006840
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006841 wpa_supplicant_cancel_sched_scan(wpa_s);
6842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006843 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006844 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006845 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006846}
6847
6848
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006849static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6850 struct wpa_scan_results *scan_res)
6851{
6852 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6853
6854 if (wpa_s->p2p_scan_work) {
6855 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6856 wpa_s->p2p_scan_work = NULL;
6857 radio_work_done(work);
6858 }
6859
6860 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6861 return;
6862
6863 /*
6864 * Indicate that results have been processed so that the P2P module can
6865 * continue pending tasks.
6866 */
6867 p2p_scan_res_handled(wpa_s->global->p2p);
6868}
6869
6870
6871static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872{
6873 wpas_p2p_clear_pending_action_tx(wpa_s);
6874 wpa_s->p2p_long_listen = 0;
6875 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6876 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006877
6878 if (wpa_s->global->p2p)
6879 p2p_stop_find(wpa_s->global->p2p);
6880
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006881 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6882 wpa_printf(MSG_DEBUG,
6883 "P2P: Do not consider the scan results after stop_find");
6884 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6885 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006886}
6887
6888
6889void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6890{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006891 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006892 if (!wpa_s->global->pending_group_iface_for_p2ps)
6893 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006894}
6895
6896
6897static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6898{
6899 struct wpa_supplicant *wpa_s = eloop_ctx;
6900 wpa_s->p2p_long_listen = 0;
6901}
6902
6903
6904int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6905{
6906 int res;
6907
6908 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6909 return -1;
6910
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006911 if (wpa_s->p2p_lo_started) {
6912 wpa_printf(MSG_DEBUG,
6913 "P2P: Cannot start P2P listen, it is offloaded");
6914 return -1;
6915 }
6916
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006917 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006918 wpas_p2p_clear_pending_action_tx(wpa_s);
6919
6920 if (timeout == 0) {
6921 /*
6922 * This is a request for unlimited Listen state. However, at
6923 * least for now, this is mapped to a Listen state for one
6924 * hour.
6925 */
6926 timeout = 3600;
6927 }
6928 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6929 wpa_s->p2p_long_listen = 0;
6930
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006931 /*
6932 * Stop previous find/listen operation to avoid trying to request a new
6933 * remain-on-channel operation while the driver is still running the
6934 * previous one.
6935 */
6936 if (wpa_s->global->p2p)
6937 p2p_stop_find(wpa_s->global->p2p);
6938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006939 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6940 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6941 wpa_s->p2p_long_listen = timeout * 1000;
6942 eloop_register_timeout(timeout, 0,
6943 wpas_p2p_long_listen_timeout,
6944 wpa_s, NULL);
6945 }
6946
6947 return res;
6948}
6949
6950
6951int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6952 u8 *buf, size_t len, int p2p_group)
6953{
6954 struct wpabuf *p2p_ie;
6955 int ret;
6956
6957 if (wpa_s->global->p2p_disabled)
6958 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006959 /*
6960 * Advertize mandatory cross connection capability even on
6961 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6962 */
6963 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006964 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006965 if (wpa_s->global->p2p == NULL)
6966 return -1;
6967 if (bss == NULL)
6968 return -1;
6969
6970 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6971 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6972 p2p_group, p2p_ie);
6973 wpabuf_free(p2p_ie);
6974
6975 return ret;
6976}
6977
6978
6979int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006980 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006981 const u8 *ie, size_t ie_len,
6982 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983{
6984 if (wpa_s->global->p2p_disabled)
6985 return 0;
6986 if (wpa_s->global->p2p == NULL)
6987 return 0;
6988
Dmitry Shmidt04949592012-07-19 12:16:46 -07006989 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006990 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006991 case P2P_PREQ_NOT_P2P:
6992 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6993 ssi_signal);
6994 /* fall through */
6995 case P2P_PREQ_MALFORMED:
6996 case P2P_PREQ_NOT_LISTEN:
6997 case P2P_PREQ_NOT_PROCESSED:
6998 default: /* make gcc happy */
6999 return 0;
7000 case P2P_PREQ_PROCESSED:
7001 return 1;
7002 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007003}
7004
7005
7006void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7007 const u8 *sa, const u8 *bssid,
7008 u8 category, const u8 *data, size_t len, int freq)
7009{
7010 if (wpa_s->global->p2p_disabled)
7011 return;
7012 if (wpa_s->global->p2p == NULL)
7013 return;
7014
7015 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7016 freq);
7017}
7018
7019
7020void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7021{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007022 unsigned int bands;
7023
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007024 if (wpa_s->global->p2p_disabled)
7025 return;
7026 if (wpa_s->global->p2p == NULL)
7027 return;
7028
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007029 bands = wpas_get_bands(wpa_s, NULL);
7030 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007031}
7032
7033
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007034static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007035{
7036 p2p_group_deinit(wpa_s->p2p_group);
7037 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007038
7039 wpa_s->ap_configured_cb = NULL;
7040 wpa_s->ap_configured_cb_ctx = NULL;
7041 wpa_s->ap_configured_cb_data = NULL;
7042 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043}
7044
7045
7046int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7047{
7048 wpa_s->p2p_long_listen = 0;
7049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7051 return -1;
7052
7053 return p2p_reject(wpa_s->global->p2p, addr);
7054}
7055
7056
7057/* Invite to reinvoke a persistent group */
7058int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007059 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007060 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08007061 int pref_freq, int he)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007062{
7063 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007064 u8 *bssid = NULL;
7065 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007066 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007067 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007068 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007069
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007070 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007071 if (peer_addr)
7072 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7073 else
7074 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007075
Jouni Malinen31be0a42012-08-31 21:20:51 +03007076 wpa_s->p2p_persistent_go_freq = freq;
7077 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007078 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007079 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007080 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7081 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082 if (ssid->mode == WPAS_MODE_P2P_GO) {
7083 role = P2P_INVITE_ROLE_GO;
7084 if (peer_addr == NULL) {
7085 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7086 "address in invitation command");
7087 return -1;
7088 }
7089 if (wpas_p2p_create_iface(wpa_s)) {
7090 if (wpas_p2p_add_group_interface(wpa_s,
7091 WPA_IF_P2P_GO) < 0) {
7092 wpa_printf(MSG_ERROR, "P2P: Failed to "
7093 "allocate a new interface for the "
7094 "group");
7095 return -1;
7096 }
7097 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007098 } else if (wpa_s->p2p_mgmt)
7099 bssid = wpa_s->parent->own_addr;
7100 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007101 bssid = wpa_s->own_addr;
7102 } else {
7103 role = P2P_INVITE_ROLE_CLIENT;
7104 peer_addr = ssid->bssid;
7105 }
7106 wpa_s->pending_invite_ssid_id = ssid->id;
7107
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007108 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007109 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007110 role == P2P_INVITE_ROLE_GO,
7111 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007112 if (res)
7113 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7116 return -1;
7117
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007118 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7119
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007120 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7121 no_pref_freq_given && pref_freq > 0 &&
7122 wpa_s->num_multichan_concurrent > 1 &&
7123 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7124 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7125 pref_freq);
7126 pref_freq = 0;
7127 }
7128
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007129 /*
7130 * Stop any find/listen operations before invitation and possibly
7131 * connection establishment.
7132 */
7133 wpas_p2p_stop_find_oper(wpa_s);
7134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007135 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007136 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007137 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007138}
7139
7140
7141/* Invite to join an active group */
7142int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7143 const u8 *peer_addr, const u8 *go_dev_addr)
7144{
7145 struct wpa_global *global = wpa_s->global;
7146 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007147 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007149 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007150 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007151 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007152 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007153
Jouni Malinen31be0a42012-08-31 21:20:51 +03007154 wpa_s->p2p_persistent_go_freq = 0;
7155 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007156 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007157 wpa_s->p2p_go_vht_center_freq2 = 0;
7158 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007160 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7161 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7162 break;
7163 }
7164 if (wpa_s == NULL) {
7165 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7166 return -1;
7167 }
7168
7169 ssid = wpa_s->current_ssid;
7170 if (ssid == NULL) {
7171 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7172 "invitation");
7173 return -1;
7174 }
7175
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007176 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007177 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007178 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007179 ssid->ssid, ssid->ssid_len);
7180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007181 if (ssid->mode == WPAS_MODE_P2P_GO) {
7182 role = P2P_INVITE_ROLE_ACTIVE_GO;
7183 bssid = wpa_s->own_addr;
7184 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007185 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007186 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007187 } else {
7188 role = P2P_INVITE_ROLE_CLIENT;
7189 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7190 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7191 "invite to current group");
7192 return -1;
7193 }
7194 bssid = wpa_s->bssid;
7195 if (go_dev_addr == NULL &&
7196 !is_zero_ether_addr(wpa_s->go_dev_addr))
7197 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007198 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7199 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007200 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007201 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007203 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7204 return -1;
7205
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007206 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007207 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007208 role == P2P_INVITE_ROLE_ACTIVE_GO,
7209 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007210 if (res)
7211 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007212 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007214 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007215 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007216 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007217}
7218
7219
7220void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7221{
7222 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007223 u8 go_dev_addr[ETH_ALEN];
7224 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007225 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007226 u8 ip[3 * 4];
7227 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007228
Dmitry Shmidt04949592012-07-19 12:16:46 -07007229 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7230 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007231 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007232 }
7233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007234 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007235 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007236
7237 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007238 if (!wpa_s->p2p_go_group_formation_completed &&
7239 wpa_s->global->p2p_group_formation == wpa_s) {
7240 wpa_dbg(wpa_s, MSG_DEBUG,
7241 "P2P: Marking group formation completed on client on data connection");
7242 wpa_s->p2p_go_group_formation_completed = 1;
7243 wpa_s->global->p2p_group_formation = NULL;
7244 wpa_s->p2p_in_provisioning = 0;
7245 wpa_s->p2p_in_invitation = 0;
7246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007248 os_memset(go_dev_addr, 0, ETH_ALEN);
7249 if (ssid->bssid_set)
7250 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7251 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7252 ssid->ssid_len);
7253 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7254
7255 if (wpa_s->global->p2p_group_formation == wpa_s)
7256 wpa_s->global->p2p_group_formation = NULL;
7257
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007258 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7259 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007260
7261 ip_addr[0] = '\0';
7262 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007263 int res;
7264
7265 res = os_snprintf(ip_addr, sizeof(ip_addr),
7266 " ip_addr=%u.%u.%u.%u "
7267 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7268 ip[0], ip[1], ip[2], ip[3],
7269 ip[4], ip[5], ip[6], ip[7],
7270 ip[8], ip[9], ip[10], ip[11]);
7271 if (os_snprintf_error(sizeof(ip_addr), res))
7272 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007273 }
7274
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007275 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7276 ssid->passphrase == NULL && ssid->psk_set ?
7277 ssid->psk : NULL,
7278 ssid->passphrase, go_dev_addr, persistent,
7279 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007280
7281 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007282 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7283 ssid, go_dev_addr);
7284
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007285 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286}
7287
7288
7289int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7290 u32 interval1, u32 duration2, u32 interval2)
7291{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007292 int ret;
7293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007294 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7295 return -1;
7296
7297 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7298 wpa_s->current_ssid == NULL ||
7299 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7300 return -1;
7301
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007302 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7303 wpa_s->own_addr, wpa_s->assoc_freq,
7304 duration1, interval1, duration2, interval2);
7305 if (ret == 0)
7306 wpa_s->waiting_presence_resp = 1;
7307
7308 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007309}
7310
7311
7312int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7313 unsigned int interval)
7314{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7316 return -1;
7317
7318 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7319}
7320
7321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007322static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7323{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007324 if (wpa_s->current_ssid == NULL) {
7325 /*
7326 * current_ssid can be cleared when P2P client interface gets
7327 * disconnected, so assume this interface was used as P2P
7328 * client.
7329 */
7330 return 1;
7331 }
7332 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007333 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7334}
7335
7336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007337static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7338{
7339 struct wpa_supplicant *wpa_s = eloop_ctx;
7340
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007341 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007342 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7343 "disabled");
7344 return;
7345 }
7346
Dmitry Shmidt04949592012-07-19 12:16:46 -07007347 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7348 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007349 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350}
7351
7352
7353static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7354{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007355 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007356
Dmitry Shmidt04949592012-07-19 12:16:46 -07007357 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7358 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007360 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7361 return;
7362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007363 timeout = wpa_s->conf->p2p_group_idle;
7364 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7365 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7366 timeout = P2P_MAX_CLIENT_IDLE;
7367
7368 if (timeout == 0)
7369 return;
7370
Dmitry Shmidt04949592012-07-19 12:16:46 -07007371 if (timeout < 0) {
7372 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7373 timeout = 0; /* special client mode no-timeout */
7374 else
7375 return;
7376 }
7377
7378 if (wpa_s->p2p_in_provisioning) {
7379 /*
7380 * Use the normal group formation timeout during the
7381 * provisioning phase to avoid terminating this process too
7382 * early due to group idle timeout.
7383 */
7384 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7385 "during provisioning");
7386 return;
7387 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307388
Dmitry Shmidt04949592012-07-19 12:16:46 -07007389 if (wpa_s->show_group_started) {
7390 /*
7391 * Use the normal group formation timeout between the end of
7392 * the provisioning phase and completion of 4-way handshake to
7393 * avoid terminating this process too early due to group idle
7394 * timeout.
7395 */
7396 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7397 "while waiting for initial 4-way handshake to "
7398 "complete");
7399 return;
7400 }
7401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007402 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007403 timeout);
7404 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7405 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007406}
7407
7408
Jouni Malinen2b89da82012-08-31 22:04:41 +03007409/* Returns 1 if the interface was removed */
7410int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7411 u16 reason_code, const u8 *ie, size_t ie_len,
7412 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007413{
7414 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007415 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007416
Dmitry Shmidt04949592012-07-19 12:16:46 -07007417 if (!locally_generated)
7418 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7419 ie_len);
7420
7421 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7422 wpa_s->current_ssid &&
7423 wpa_s->current_ssid->p2p_group &&
7424 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7425 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7426 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007427 if (wpas_p2p_group_delete(wpa_s,
7428 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7429 > 0)
7430 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007431 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007432
7433 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007434}
7435
7436
7437void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007438 u16 reason_code, const u8 *ie, size_t ie_len,
7439 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007440{
7441 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7442 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007443
Dmitry Shmidt04949592012-07-19 12:16:46 -07007444 if (!locally_generated)
7445 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7446 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007447}
7448
7449
7450void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7451{
7452 struct p2p_data *p2p = wpa_s->global->p2p;
7453
7454 if (p2p == NULL)
7455 return;
7456
7457 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7458 return;
7459
7460 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7461 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7462
7463 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7464 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7465
7466 if (wpa_s->wps &&
7467 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7468 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7469
7470 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7471 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7472
7473 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7474 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7475 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7476 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7477 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7478 }
7479
7480 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7481 p2p_set_sec_dev_types(p2p,
7482 (void *) wpa_s->conf->sec_device_type,
7483 wpa_s->conf->num_sec_device_types);
7484
7485 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7486 int i;
7487 p2p_remove_wps_vendor_extensions(p2p);
7488 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7489 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7490 continue;
7491 p2p_add_wps_vendor_extension(
7492 p2p, wpa_s->conf->wps_vendor_ext[i]);
7493 }
7494 }
7495
7496 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7497 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7498 char country[3];
7499 country[0] = wpa_s->conf->country[0];
7500 country[1] = wpa_s->conf->country[1];
7501 country[2] = 0x04;
7502 p2p_set_country(p2p, country);
7503 }
7504
7505 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7506 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7507 wpa_s->conf->p2p_ssid_postfix ?
7508 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7509 0);
7510 }
7511
7512 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7513 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007514
7515 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7516 u8 reg_class, channel;
7517 int ret;
7518 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007519 u8 channel_forced;
7520
Jouni Malinen75ecf522011-06-27 15:19:46 -07007521 if (wpa_s->conf->p2p_listen_reg_class &&
7522 wpa_s->conf->p2p_listen_channel) {
7523 reg_class = wpa_s->conf->p2p_listen_reg_class;
7524 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007525 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007526 } else {
7527 reg_class = 81;
7528 /*
7529 * Pick one of the social channels randomly as the
7530 * listen channel.
7531 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007532 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7533 channel = 1;
7534 else
7535 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007536 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007537 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007538 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7539 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007540 if (ret)
7541 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7542 "failed: %d", ret);
7543 }
7544 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7545 u8 op_reg_class, op_channel, cfg_op_channel;
7546 int ret = 0;
7547 unsigned int r;
7548 if (wpa_s->conf->p2p_oper_reg_class &&
7549 wpa_s->conf->p2p_oper_channel) {
7550 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7551 op_channel = wpa_s->conf->p2p_oper_channel;
7552 cfg_op_channel = 1;
7553 } else {
7554 op_reg_class = 81;
7555 /*
7556 * Use random operation channel from (1, 6, 11)
7557 *if no other preference is indicated.
7558 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007559 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7560 op_channel = 1;
7561 else
7562 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007563 cfg_op_channel = 0;
7564 }
7565 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7566 cfg_op_channel);
7567 if (ret)
7568 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7569 "failed: %d", ret);
7570 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007571
7572 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7573 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7574 wpa_s->conf->p2p_pref_chan) < 0) {
7575 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7576 "update failed");
7577 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007578
7579 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7580 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7581 "update failed");
7582 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007583 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007584
7585 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7586 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007587}
7588
7589
7590int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7591 int duration)
7592{
7593 if (!wpa_s->ap_iface)
7594 return -1;
7595 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7596 duration);
7597}
7598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599
7600int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7601{
7602 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7603 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007604
7605 wpa_s->global->cross_connection = enabled;
7606 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7607
7608 if (!enabled) {
7609 struct wpa_supplicant *iface;
7610
7611 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7612 {
7613 if (iface->cross_connect_enabled == 0)
7614 continue;
7615
7616 iface->cross_connect_enabled = 0;
7617 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007618 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007619 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7620 iface->ifname,
7621 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007622 }
7623 }
7624
7625 return 0;
7626}
7627
7628
7629static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7630{
7631 struct wpa_supplicant *iface;
7632
7633 if (!uplink->global->cross_connection)
7634 return;
7635
7636 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7637 if (!iface->cross_connect_enabled)
7638 continue;
7639 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7640 0)
7641 continue;
7642 if (iface->ap_iface == NULL)
7643 continue;
7644 if (iface->cross_connect_in_use)
7645 continue;
7646
7647 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007648 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007649 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7650 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007651 }
7652}
7653
7654
7655static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7656{
7657 struct wpa_supplicant *iface;
7658
7659 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7660 if (!iface->cross_connect_enabled)
7661 continue;
7662 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7663 0)
7664 continue;
7665 if (!iface->cross_connect_in_use)
7666 continue;
7667
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007668 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007669 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7670 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007671 iface->cross_connect_in_use = 0;
7672 }
7673}
7674
7675
7676void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7677{
7678 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7679 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7680 wpa_s->cross_connect_disallowed)
7681 wpas_p2p_disable_cross_connect(wpa_s);
7682 else
7683 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007684 if (!wpa_s->ap_iface &&
7685 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7686 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007687}
7688
7689
7690void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7691{
7692 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007693 if (!wpa_s->ap_iface &&
7694 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7695 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007696 wpas_p2p_set_group_idle_timeout(wpa_s);
7697}
7698
7699
7700static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7701{
7702 struct wpa_supplicant *iface;
7703
7704 if (!wpa_s->global->cross_connection)
7705 return;
7706
7707 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7708 if (iface == wpa_s)
7709 continue;
7710 if (iface->drv_flags &
7711 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7712 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007713 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7714 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007715 continue;
7716
7717 wpa_s->cross_connect_enabled = 1;
7718 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7719 sizeof(wpa_s->cross_connect_uplink));
7720 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7721 "%s to %s whenever uplink is available",
7722 wpa_s->ifname, wpa_s->cross_connect_uplink);
7723
7724 if (iface->ap_iface || iface->current_ssid == NULL ||
7725 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7726 iface->cross_connect_disallowed ||
7727 iface->wpa_state != WPA_COMPLETED)
7728 break;
7729
7730 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007731 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007732 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7733 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007734 break;
7735 }
7736}
7737
7738
7739int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7740{
7741 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7742 !wpa_s->p2p_in_provisioning)
7743 return 0; /* not P2P client operation */
7744
7745 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7746 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007747 if (wpa_s != wpa_s->p2pdev)
7748 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007749 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007750 return 1;
7751}
7752
7753
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007754void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7755{
7756 struct wpa_supplicant *wpa_s = eloop_ctx;
7757 wpas_p2p_notif_pbc_overlap(wpa_s);
7758}
7759
7760
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007761void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7762 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007763{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007764 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007765 struct wpa_used_freq_data *freqs = NULL;
7766 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007767
7768 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7769 return;
7770
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007771 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7772 if (!freqs)
7773 return;
7774
7775 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007777 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007778 os_memset(&cli_chan, 0, sizeof(cli_chan));
7779 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007780 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7781 "channel list");
7782 return;
7783 }
7784
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007785 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007786
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007787 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007788
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007789 /*
7790 * The used frequencies map changed, so it is possible that a GO is
7791 * using a channel that is no longer valid for P2P use. It is also
7792 * possible that due to policy consideration, it would be preferable to
7793 * move it to a frequency already used by other station interfaces.
7794 */
7795 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7796
7797 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007798}
7799
7800
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007801static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7802 struct wpa_scan_results *scan_res)
7803{
7804 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7805}
7806
7807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007808int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7809{
7810 struct wpa_global *global = wpa_s->global;
7811 int found = 0;
7812 const u8 *peer;
7813
7814 if (global->p2p == NULL)
7815 return -1;
7816
7817 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7818
7819 if (wpa_s->pending_interface_name[0] &&
7820 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7821 found = 1;
7822
7823 peer = p2p_get_go_neg_peer(global->p2p);
7824 if (peer) {
7825 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7826 MACSTR, MAC2STR(peer));
7827 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007828 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007829 }
7830
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007831 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7832 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7833 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7834 found = 1;
7835 }
7836
7837 if (wpa_s->pending_pd_before_join) {
7838 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7839 wpa_s->pending_pd_before_join = 0;
7840 found = 1;
7841 }
7842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007843 wpas_p2p_stop_find(wpa_s);
7844
7845 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7846 if (wpa_s == global->p2p_group_formation &&
7847 (wpa_s->p2p_in_provisioning ||
7848 wpa_s->parent->pending_interface_type ==
7849 WPA_IF_P2P_CLIENT)) {
7850 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7851 "formation found - cancelling",
7852 wpa_s->ifname);
7853 found = 1;
7854 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007855 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007856 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007857 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007858 break;
7859 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007860 wpas_p2p_group_delete(wpa_s,
7861 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007862 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007863 } else if (wpa_s->p2p_in_invitation) {
7864 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7865 wpa_s->ifname);
7866 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007867 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007868 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007869 }
7870 }
7871
7872 if (!found) {
7873 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7874 return -1;
7875 }
7876
7877 return 0;
7878}
7879
7880
7881void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7882{
7883 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7884 return;
7885
7886 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7887 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007888 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889}
7890
7891
7892void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7893 int freq_24, int freq_5, int freq_overall)
7894{
7895 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007896 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007897 return;
7898 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7899}
7900
7901
7902int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7903{
7904 u8 peer[ETH_ALEN];
7905 struct p2p_data *p2p = wpa_s->global->p2p;
7906
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007907 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007908 return -1;
7909
7910 if (hwaddr_aton(addr, peer))
7911 return -1;
7912
7913 return p2p_unauthorize(p2p, peer);
7914}
7915
7916
7917/**
7918 * wpas_p2p_disconnect - Disconnect from a P2P Group
7919 * @wpa_s: Pointer to wpa_supplicant data
7920 * Returns: 0 on success, -1 on failure
7921 *
7922 * This can be used to disconnect from a group in which the local end is a P2P
7923 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7924 * virtual network interface was created for this group, that interface will be
7925 * removed. Otherwise, only the configured P2P group network will be removed
7926 * from the interface.
7927 */
7928int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7929{
7930
7931 if (wpa_s == NULL)
7932 return -1;
7933
Jouni Malinen2b89da82012-08-31 22:04:41 +03007934 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7935 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007936}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007937
7938
7939int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7940{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007941 int ret;
7942
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007943 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7944 return 0;
7945
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007946 ret = p2p_in_progress(wpa_s->global->p2p);
7947 if (ret == 0) {
7948 /*
7949 * Check whether there is an ongoing WPS provisioning step (or
7950 * other parts of group formation) on another interface since
7951 * p2p_in_progress() does not report this to avoid issues for
7952 * scans during such provisioning step.
7953 */
7954 if (wpa_s->global->p2p_group_formation &&
7955 wpa_s->global->p2p_group_formation != wpa_s) {
7956 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7957 "in group formation",
7958 wpa_s->global->p2p_group_formation->ifname);
7959 ret = 1;
7960 }
7961 }
7962
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007963 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007964 struct os_reltime now;
7965 os_get_reltime(&now);
7966 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7967 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007968 /* Wait for the first client has expired */
7969 wpa_s->global->p2p_go_wait_client.sec = 0;
7970 } else {
7971 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7972 ret = 1;
7973 }
7974 }
7975
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007976 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007977}
7978
7979
7980void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7981 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007982{
7983 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7984 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007985 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007986 /**
7987 * Remove the network by scheduling the group formation
7988 * timeout to happen immediately. The teardown code
7989 * needs to be scheduled to run asynch later so that we
7990 * don't delete data from under ourselves unexpectedly.
7991 * Calling wpas_p2p_group_formation_timeout directly
7992 * causes a series of crashes in WPS failure scenarios.
7993 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007994 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7995 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007996 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007997 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007998 }
7999}
8000
8001
8002struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008003 const u8 *addr, const u8 *ssid,
8004 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008005{
8006 struct wpa_ssid *s;
8007 size_t i;
8008
8009 for (s = wpa_s->conf->ssid; s; s = s->next) {
8010 if (s->disabled != 2)
8011 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008012 if (ssid &&
8013 (ssid_len != s->ssid_len ||
8014 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8015 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008016 if (addr == NULL) {
8017 if (s->mode == WPAS_MODE_P2P_GO)
8018 return s;
8019 continue;
8020 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008021 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8022 return s; /* peer is GO in the persistent group */
8023 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8024 continue;
8025 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008026 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008027 addr, ETH_ALEN) == 0)
8028 return s; /* peer is P2P client in persistent
8029 * group */
8030 }
8031 }
8032
8033 return NULL;
8034}
8035
8036
8037void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8038 const u8 *addr)
8039{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008040 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008041 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008042 /*
8043 * This can happen if WPS provisioning step is not terminated
8044 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8045 * peer was able to connect, there is no need to time out group
8046 * formation after this, though. In addition, this is used with
8047 * the initial connection wait on the GO as a separate formation
8048 * timeout and as such, expected to be hit after the initial WPS
8049 * provisioning step.
8050 */
8051 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008052
8053 if (!wpa_s->p2p_go_group_formation_completed &&
8054 !wpa_s->group_formation_reported) {
8055 /*
8056 * GO has not yet notified group formation success since
8057 * the WPS step was not completed cleanly. Do that
8058 * notification now since the P2P Client was able to
8059 * connect and as such, must have received the
8060 * credential from the WPS step.
8061 */
8062 if (wpa_s->global->p2p)
8063 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008064 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008065 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008066 }
8067 if (!wpa_s->p2p_go_group_formation_completed) {
8068 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8069 wpa_s->p2p_go_group_formation_completed = 1;
8070 wpa_s->global->p2p_group_formation = NULL;
8071 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008072 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008073 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008074 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008075 if (addr == NULL)
8076 return;
8077 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8078}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008079
Dmitry Shmidt04949592012-07-19 12:16:46 -07008080
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008081static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8082 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008083{
8084 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008085 int ret = 0;
8086
Dmitry Shmidt04949592012-07-19 12:16:46 -07008087 if (wpa_s->global->p2p_group_formation)
8088 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008089 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008090 offchannel_send_action_done(wpa_s);
8091 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008092 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8094 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8095 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8096 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008097 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008098 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008099 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008100 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008101 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008102 wpa_s->p2p_go_max_oper_chwidth,
8103 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008104 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008105}
8106
8107
8108int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8109{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008110 int res;
8111
Dmitry Shmidt04949592012-07-19 12:16:46 -07008112 if (!wpa_s->p2p_fallback_to_go_neg ||
8113 wpa_s->p2p_in_provisioning <= 5)
8114 return 0;
8115
8116 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8117 return 0; /* peer operating as a GO */
8118
8119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8120 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008121 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008122 "reason=GO-not-found");
8123 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008124
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008125 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008126}
8127
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008128
8129unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8130{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008131 struct wpa_supplicant *ifs;
8132
8133 if (wpa_s->wpa_state > WPA_SCANNING) {
8134 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8135 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008136 wpa_s->conf->p2p_search_delay);
8137 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008138 }
8139
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008140 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8141 radio_list) {
8142 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008143 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8144 "delay due to concurrent operation on "
8145 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008146 wpa_s->conf->p2p_search_delay,
8147 ifs->ifname);
8148 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008149 }
8150 }
8151
8152 return 0;
8153}
8154
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008155
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008156static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8157 struct wpa_ssid *s, const u8 *addr,
8158 int iface_addr)
8159{
8160 struct psk_list_entry *psk, *tmp;
8161 int changed = 0;
8162
8163 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8164 list) {
8165 if ((iface_addr && !psk->p2p &&
8166 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8167 (!iface_addr && psk->p2p &&
8168 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8169 wpa_dbg(wpa_s, MSG_DEBUG,
8170 "P2P: Remove persistent group PSK list entry for "
8171 MACSTR " p2p=%u",
8172 MAC2STR(psk->addr), psk->p2p);
8173 dl_list_del(&psk->list);
8174 os_free(psk);
8175 changed++;
8176 }
8177 }
8178
8179 return changed;
8180}
8181
8182
8183void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8184 const u8 *p2p_dev_addr,
8185 const u8 *psk, size_t psk_len)
8186{
8187 struct wpa_ssid *ssid = wpa_s->current_ssid;
8188 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008189 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008190
8191 if (psk_len != sizeof(p->psk))
8192 return;
8193
8194 if (p2p_dev_addr) {
8195 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8196 " p2p_dev_addr=" MACSTR,
8197 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8198 if (is_zero_ether_addr(p2p_dev_addr))
8199 p2p_dev_addr = NULL;
8200 } else {
8201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8202 MAC2STR(mac_addr));
8203 }
8204
8205 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8206 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8207 /* To be added to persistent group once created */
8208 if (wpa_s->global->add_psk == NULL) {
8209 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8210 if (wpa_s->global->add_psk == NULL)
8211 return;
8212 }
8213 p = wpa_s->global->add_psk;
8214 if (p2p_dev_addr) {
8215 p->p2p = 1;
8216 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8217 } else {
8218 p->p2p = 0;
8219 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8220 }
8221 os_memcpy(p->psk, psk, psk_len);
8222 return;
8223 }
8224
8225 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8226 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8227 return;
8228 }
8229
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008230 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008231 ssid->ssid_len);
8232 if (!persistent) {
8233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8234 return;
8235 }
8236
8237 p = os_zalloc(sizeof(*p));
8238 if (p == NULL)
8239 return;
8240 if (p2p_dev_addr) {
8241 p->p2p = 1;
8242 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8243 } else {
8244 p->p2p = 0;
8245 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8246 }
8247 os_memcpy(p->psk, psk, psk_len);
8248
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008249 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8250 (last = dl_list_last(&persistent->psk_list,
8251 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008252 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8253 MACSTR " (p2p=%u) to make room for a new one",
8254 MAC2STR(last->addr), last->p2p);
8255 dl_list_del(&last->list);
8256 os_free(last);
8257 }
8258
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008259 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008260 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8261 p2p_dev_addr == NULL);
8262 if (p2p_dev_addr) {
8263 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8264 MACSTR, MAC2STR(p2p_dev_addr));
8265 } else {
8266 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8267 MAC2STR(mac_addr));
8268 }
8269 dl_list_add(&persistent->psk_list, &p->list);
8270
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008271 if (wpa_s->p2pdev->conf->update_config &&
8272 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008273 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008274}
8275
8276
8277static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8278 struct wpa_ssid *s, const u8 *addr,
8279 int iface_addr)
8280{
8281 int res;
8282
8283 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008284 if (res > 0 && wpa_s->conf->update_config &&
8285 wpa_config_write(wpa_s->confname, wpa_s->conf))
8286 wpa_dbg(wpa_s, MSG_DEBUG,
8287 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008288}
8289
8290
8291static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8292 const u8 *peer, int iface_addr)
8293{
8294 struct hostapd_data *hapd;
8295 struct hostapd_wpa_psk *psk, *prev, *rem;
8296 struct sta_info *sta;
8297
8298 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8299 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8300 return;
8301
8302 /* Remove per-station PSK entry */
8303 hapd = wpa_s->ap_iface->bss[0];
8304 prev = NULL;
8305 psk = hapd->conf->ssid.wpa_psk;
8306 while (psk) {
8307 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8308 (!iface_addr &&
8309 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8311 MACSTR " iface_addr=%d",
8312 MAC2STR(peer), iface_addr);
8313 if (prev)
8314 prev->next = psk->next;
8315 else
8316 hapd->conf->ssid.wpa_psk = psk->next;
8317 rem = psk;
8318 psk = psk->next;
8319 os_free(rem);
8320 } else {
8321 prev = psk;
8322 psk = psk->next;
8323 }
8324 }
8325
8326 /* Disconnect from group */
8327 if (iface_addr)
8328 sta = ap_get_sta(hapd, peer);
8329 else
8330 sta = ap_get_sta_p2p(hapd, peer);
8331 if (sta) {
8332 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8333 " (iface_addr=%d) from group",
8334 MAC2STR(peer), iface_addr);
8335 hostapd_drv_sta_deauth(hapd, sta->addr,
8336 WLAN_REASON_DEAUTH_LEAVING);
8337 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8338 }
8339}
8340
8341
8342void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8343 int iface_addr)
8344{
8345 struct wpa_ssid *s;
8346 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008347 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008348
8349 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8350
8351 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008352 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008353 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8354 continue;
8355 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008356 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8357 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008358 }
8359
8360 /* Remove from any operating group */
8361 for (w = wpa_s->global->ifaces; w; w = w->next)
8362 wpas_p2p_remove_client_go(w, peer, iface_addr);
8363}
8364
8365
8366static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8367{
8368 struct wpa_supplicant *wpa_s = eloop_ctx;
8369 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8370}
8371
8372
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008373static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8374{
8375 struct wpa_supplicant *wpa_s = eloop_ctx;
8376
8377 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8378 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8379}
8380
8381
8382int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8383 struct wpa_ssid *ssid)
8384{
8385 struct wpa_supplicant *iface;
8386
8387 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8388 if (!iface->current_ssid ||
8389 iface->current_ssid->frequency == freq ||
8390 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8391 !iface->current_ssid->p2p_group))
8392 continue;
8393
8394 /* Remove the connection with least priority */
8395 if (!wpas_is_p2p_prioritized(iface)) {
8396 /* STA connection has priority over existing
8397 * P2P connection, so remove the interface. */
8398 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8399 eloop_register_timeout(0, 0,
8400 wpas_p2p_group_freq_conflict,
8401 iface, NULL);
8402 /* If connection in progress is P2P connection, do not
8403 * proceed for the connection. */
8404 if (wpa_s == iface)
8405 return -1;
8406 else
8407 return 0;
8408 } else {
8409 /* P2P connection has priority, disable the STA network
8410 */
8411 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8412 ssid);
8413 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8414 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8415 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8416 ETH_ALEN);
8417 /* If P2P connection is in progress, continue
8418 * connecting...*/
8419 if (wpa_s == iface)
8420 return 0;
8421 else
8422 return -1;
8423 }
8424 }
8425
8426 return 0;
8427}
8428
8429
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008430int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8431{
8432 struct wpa_ssid *ssid = wpa_s->current_ssid;
8433
8434 if (ssid == NULL || !ssid->p2p_group)
8435 return 0;
8436
8437 if (wpa_s->p2p_last_4way_hs_fail &&
8438 wpa_s->p2p_last_4way_hs_fail == ssid) {
8439 u8 go_dev_addr[ETH_ALEN];
8440 struct wpa_ssid *persistent;
8441
8442 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8443 ssid->ssid,
8444 ssid->ssid_len) <= 0) {
8445 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8446 goto disconnect;
8447 }
8448
8449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8450 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008451 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008452 ssid->ssid,
8453 ssid->ssid_len);
8454 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8455 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8456 goto disconnect;
8457 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008458 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008459 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8460 persistent->id);
8461 disconnect:
8462 wpa_s->p2p_last_4way_hs_fail = NULL;
8463 /*
8464 * Remove the group from a timeout to avoid issues with caller
8465 * continuing to use the interface if this is on a P2P group
8466 * interface.
8467 */
8468 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8469 wpa_s, NULL);
8470 return 1;
8471 }
8472
8473 wpa_s->p2p_last_4way_hs_fail = ssid;
8474 return 0;
8475}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008476
8477
8478#ifdef CONFIG_WPS_NFC
8479
8480static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8481 struct wpabuf *p2p)
8482{
8483 struct wpabuf *ret;
8484 size_t wsc_len;
8485
8486 if (p2p == NULL) {
8487 wpabuf_free(wsc);
8488 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8489 return NULL;
8490 }
8491
8492 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8493 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8494 if (ret == NULL) {
8495 wpabuf_free(wsc);
8496 wpabuf_free(p2p);
8497 return NULL;
8498 }
8499
8500 wpabuf_put_be16(ret, wsc_len);
8501 if (wsc)
8502 wpabuf_put_buf(ret, wsc);
8503 wpabuf_put_be16(ret, wpabuf_len(p2p));
8504 wpabuf_put_buf(ret, p2p);
8505
8506 wpabuf_free(wsc);
8507 wpabuf_free(p2p);
8508 wpa_hexdump_buf(MSG_DEBUG,
8509 "P2P: Generated NFC connection handover message", ret);
8510
8511 if (ndef && ret) {
8512 struct wpabuf *tmp;
8513 tmp = ndef_build_p2p(ret);
8514 wpabuf_free(ret);
8515 if (tmp == NULL) {
8516 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8517 return NULL;
8518 }
8519 ret = tmp;
8520 }
8521
8522 return ret;
8523}
8524
8525
8526static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8527 struct wpa_ssid **ssid, u8 *go_dev_addr)
8528{
8529 struct wpa_supplicant *iface;
8530
8531 if (go_dev_addr)
8532 os_memset(go_dev_addr, 0, ETH_ALEN);
8533 if (ssid)
8534 *ssid = NULL;
8535 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8536 if (iface->wpa_state < WPA_ASSOCIATING ||
8537 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8538 !iface->current_ssid->p2p_group ||
8539 iface->current_ssid->mode != WPAS_MODE_INFRA)
8540 continue;
8541 if (ssid)
8542 *ssid = iface->current_ssid;
8543 if (go_dev_addr)
8544 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8545 return iface->assoc_freq;
8546 }
8547 return 0;
8548}
8549
8550
8551struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8552 int ndef)
8553{
8554 struct wpabuf *wsc, *p2p;
8555 struct wpa_ssid *ssid;
8556 u8 go_dev_addr[ETH_ALEN];
8557 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8558
8559 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8560 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8561 return NULL;
8562 }
8563
8564 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8565 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8566 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8567 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8568 return NULL;
8569 }
8570
8571 if (cli_freq == 0) {
8572 wsc = wps_build_nfc_handover_req_p2p(
8573 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8574 } else
8575 wsc = NULL;
8576 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8577 go_dev_addr, ssid ? ssid->ssid : NULL,
8578 ssid ? ssid->ssid_len : 0);
8579
8580 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8581}
8582
8583
8584struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8585 int ndef, int tag)
8586{
8587 struct wpabuf *wsc, *p2p;
8588 struct wpa_ssid *ssid;
8589 u8 go_dev_addr[ETH_ALEN];
8590 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8591
8592 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8593 return NULL;
8594
8595 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8596 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8597 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8598 return NULL;
8599
8600 if (cli_freq == 0) {
8601 wsc = wps_build_nfc_handover_sel_p2p(
8602 wpa_s->parent->wps,
8603 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8604 DEV_PW_NFC_CONNECTION_HANDOVER,
8605 wpa_s->conf->wps_nfc_dh_pubkey,
8606 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8607 } else
8608 wsc = NULL;
8609 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8610 go_dev_addr, ssid ? ssid->ssid : NULL,
8611 ssid ? ssid->ssid_len : 0);
8612
8613 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8614}
8615
8616
8617static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8618 struct p2p_nfc_params *params)
8619{
8620 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8621 "connection handover (freq=%d)",
8622 params->go_freq);
8623
8624 if (params->go_freq && params->go_ssid_len) {
8625 wpa_s->p2p_wps_method = WPS_NFC;
8626 wpa_s->pending_join_wps_method = WPS_NFC;
8627 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8628 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8629 ETH_ALEN);
8630 return wpas_p2p_join_start(wpa_s, params->go_freq,
8631 params->go_ssid,
8632 params->go_ssid_len);
8633 }
8634
8635 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8636 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008637 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008638 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008639 wpa_s->p2p_go_he,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008640 params->go_ssid_len ? params->go_ssid : NULL,
8641 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008642}
8643
8644
8645static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8646 struct p2p_nfc_params *params, int tag)
8647{
8648 int res, persistent;
8649 struct wpa_ssid *ssid;
8650
8651 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8652 "connection handover");
8653 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8654 ssid = wpa_s->current_ssid;
8655 if (ssid == NULL)
8656 continue;
8657 if (ssid->mode != WPAS_MODE_P2P_GO)
8658 continue;
8659 if (wpa_s->ap_iface == NULL)
8660 continue;
8661 break;
8662 }
8663 if (wpa_s == NULL) {
8664 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8665 return -1;
8666 }
8667
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008668 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008669 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008670 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008671 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8672 return -1;
8673 }
8674 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008675 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8676 wpa_s->p2pdev->p2p_oob_dev_pw,
8677 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8678 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008679 if (res)
8680 return res;
8681
8682 if (!tag) {
8683 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8684 return 0;
8685 }
8686
8687 if (!params->peer ||
8688 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8689 return 0;
8690
8691 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8692 " to join", MAC2STR(params->peer->p2p_device_addr));
8693
8694 wpa_s->global->p2p_invite_group = wpa_s;
8695 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008696 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008697 params->peer->p2p_device_addr,
8698 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008699 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008700
8701 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8702 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8703 ssid->ssid, ssid->ssid_len, ssid->frequency,
8704 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008705 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008706}
8707
8708
8709static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8710 struct p2p_nfc_params *params,
8711 int forced_freq)
8712{
8713 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8714 "connection handover");
8715 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8716 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008717 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008718 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008719 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008720}
8721
8722
8723static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8724 struct p2p_nfc_params *params,
8725 int forced_freq)
8726{
8727 int res;
8728
8729 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8730 "connection handover");
8731 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8732 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008733 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008734 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008735 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008736 if (res)
8737 return res;
8738
8739 res = wpas_p2p_listen(wpa_s, 60);
8740 if (res) {
8741 p2p_unauthorize(wpa_s->global->p2p,
8742 params->peer->p2p_device_addr);
8743 }
8744
8745 return res;
8746}
8747
8748
8749static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8750 const struct wpabuf *data,
8751 int sel, int tag, int forced_freq)
8752{
8753 const u8 *pos, *end;
8754 u16 len, id;
8755 struct p2p_nfc_params params;
8756 int res;
8757
8758 os_memset(&params, 0, sizeof(params));
8759 params.sel = sel;
8760
8761 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8762
8763 pos = wpabuf_head(data);
8764 end = pos + wpabuf_len(data);
8765
8766 if (end - pos < 2) {
8767 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8768 "attributes");
8769 return -1;
8770 }
8771 len = WPA_GET_BE16(pos);
8772 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008773 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008774 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8775 "attributes");
8776 return -1;
8777 }
8778 params.wsc_attr = pos;
8779 params.wsc_len = len;
8780 pos += len;
8781
8782 if (end - pos < 2) {
8783 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8784 "attributes");
8785 return -1;
8786 }
8787 len = WPA_GET_BE16(pos);
8788 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008789 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008790 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8791 "attributes");
8792 return -1;
8793 }
8794 params.p2p_attr = pos;
8795 params.p2p_len = len;
8796 pos += len;
8797
8798 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8799 params.wsc_attr, params.wsc_len);
8800 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8801 params.p2p_attr, params.p2p_len);
8802 if (pos < end) {
8803 wpa_hexdump(MSG_DEBUG,
8804 "P2P: Ignored extra data after P2P attributes",
8805 pos, end - pos);
8806 }
8807
8808 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8809 if (res)
8810 return res;
8811
8812 if (params.next_step == NO_ACTION)
8813 return 0;
8814
8815 if (params.next_step == BOTH_GO) {
8816 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8817 MAC2STR(params.peer->p2p_device_addr));
8818 return 0;
8819 }
8820
8821 if (params.next_step == PEER_CLIENT) {
8822 if (!is_zero_ether_addr(params.go_dev_addr)) {
8823 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8824 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8825 " ssid=\"%s\"",
8826 MAC2STR(params.peer->p2p_device_addr),
8827 params.go_freq,
8828 MAC2STR(params.go_dev_addr),
8829 wpa_ssid_txt(params.go_ssid,
8830 params.go_ssid_len));
8831 } else {
8832 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8833 "peer=" MACSTR " freq=%d",
8834 MAC2STR(params.peer->p2p_device_addr),
8835 params.go_freq);
8836 }
8837 return 0;
8838 }
8839
8840 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8841 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8842 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8843 return 0;
8844 }
8845
8846 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8847 wpa_s->p2p_oob_dev_pw = NULL;
8848
8849 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8850 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8851 "received");
8852 return -1;
8853 }
8854
8855 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8856 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8857 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8858 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8859 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8860 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8861 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8862
8863 if (tag) {
8864 if (id < 0x10) {
8865 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8866 "peer OOB Device Password Id %u", id);
8867 return -1;
8868 }
8869 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8870 "Device Password Id %u", id);
8871 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8872 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8873 params.oob_dev_pw_len -
8874 WPS_OOB_PUBKEY_HASH_LEN - 2);
8875 wpa_s->p2p_oob_dev_pw_id = id;
8876 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8877 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8878 params.oob_dev_pw_len -
8879 WPS_OOB_PUBKEY_HASH_LEN - 2);
8880 if (wpa_s->p2p_oob_dev_pw == NULL)
8881 return -1;
8882
8883 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8884 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8885 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8886 return -1;
8887 } else {
8888 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8889 "without Device Password");
8890 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8891 }
8892
8893 switch (params.next_step) {
8894 case NO_ACTION:
8895 case BOTH_GO:
8896 case PEER_CLIENT:
8897 /* already covered above */
8898 return 0;
8899 case JOIN_GROUP:
8900 return wpas_p2p_nfc_join_group(wpa_s, &params);
8901 case AUTH_JOIN:
8902 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8903 case INIT_GO_NEG:
8904 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8905 case RESP_GO_NEG:
8906 /* TODO: use own OOB Dev Pw */
8907 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8908 }
8909
8910 return -1;
8911}
8912
8913
8914int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8915 const struct wpabuf *data, int forced_freq)
8916{
8917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8918 return -1;
8919
8920 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8921}
8922
8923
8924int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8925 const struct wpabuf *req,
8926 const struct wpabuf *sel, int forced_freq)
8927{
8928 struct wpabuf *tmp;
8929 int ret;
8930
8931 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8932 return -1;
8933
8934 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8935
8936 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8937 wpabuf_head(req), wpabuf_len(req));
8938 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8939 wpabuf_head(sel), wpabuf_len(sel));
8940 if (forced_freq)
8941 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8942 tmp = ndef_parse_p2p(init ? sel : req);
8943 if (tmp == NULL) {
8944 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8945 return -1;
8946 }
8947
8948 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8949 forced_freq);
8950 wpabuf_free(tmp);
8951
8952 return ret;
8953}
8954
8955
8956int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8957{
8958 const u8 *if_addr;
8959 int go_intent = wpa_s->conf->p2p_go_intent;
8960 struct wpa_supplicant *iface;
8961
8962 if (wpa_s->global->p2p == NULL)
8963 return -1;
8964
8965 if (!enabled) {
8966 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8967 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8968 {
8969 if (!iface->ap_iface)
8970 continue;
8971 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8972 }
8973 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8974 0, NULL);
8975 if (wpa_s->p2p_nfc_tag_enabled)
8976 wpas_p2p_remove_pending_group_interface(wpa_s);
8977 wpa_s->p2p_nfc_tag_enabled = 0;
8978 return 0;
8979 }
8980
8981 if (wpa_s->global->p2p_disabled)
8982 return -1;
8983
8984 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8985 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8986 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8987 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8988 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8989 "to allow static handover cases");
8990 return -1;
8991 }
8992
8993 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8994
8995 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8996 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8997 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8998 if (wpa_s->p2p_oob_dev_pw == NULL)
8999 return -1;
9000 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9001
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009002 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9003 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9004 /*
9005 * P2P Group Interface present and the command came on group
9006 * interface, so enable the token for the current interface.
9007 */
9008 wpa_s->create_p2p_iface = 0;
9009 } else {
9010 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9011 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009012
9013 if (wpa_s->create_p2p_iface) {
9014 enum wpa_driver_if_type iftype;
9015 /* Prepare to add a new interface for the group */
9016 iftype = WPA_IF_P2P_GROUP;
9017 if (go_intent == 15)
9018 iftype = WPA_IF_P2P_GO;
9019 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9020 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9021 "interface for the group");
9022 return -1;
9023 }
9024
9025 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009026 } else if (wpa_s->p2p_mgmt)
9027 if_addr = wpa_s->parent->own_addr;
9028 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009029 if_addr = wpa_s->own_addr;
9030
9031 wpa_s->p2p_nfc_tag_enabled = enabled;
9032
9033 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9034 struct hostapd_data *hapd;
9035 if (iface->ap_iface == NULL)
9036 continue;
9037 hapd = iface->ap_iface->bss[0];
9038 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9039 hapd->conf->wps_nfc_dh_pubkey =
9040 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9041 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9042 hapd->conf->wps_nfc_dh_privkey =
9043 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9044 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9045 hapd->conf->wps_nfc_dev_pw =
9046 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9047 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9048
9049 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9050 wpa_dbg(iface, MSG_DEBUG,
9051 "P2P: Failed to enable NFC Tag for GO");
9052 }
9053 }
9054 p2p_set_authorized_oob_dev_pw_id(
9055 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9056 if_addr);
9057
9058 return 0;
9059}
9060
9061#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009062
9063
9064static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9065 struct wpa_used_freq_data *freqs,
9066 unsigned int num)
9067{
9068 u8 curr_chan, cand, chan;
9069 unsigned int i;
9070
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009071 /*
9072 * If possible, optimize the Listen channel to be a channel that is
9073 * already used by one of the other interfaces.
9074 */
9075 if (!wpa_s->conf->p2p_optimize_listen_chan)
9076 return;
9077
9078 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9079 return;
9080
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009081 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9082 for (i = 0, cand = 0; i < num; i++) {
9083 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9084 if (curr_chan == chan) {
9085 cand = 0;
9086 break;
9087 }
9088
9089 if (chan == 1 || chan == 6 || chan == 11)
9090 cand = chan;
9091 }
9092
9093 if (cand) {
9094 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009095 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009096 cand);
9097 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9098 }
9099}
9100
9101
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009102static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009103{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009104 struct hostapd_config *conf;
9105 struct p2p_go_neg_results params;
9106 struct csa_settings csa_settings;
9107 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9108 int old_freq = current_ssid->frequency;
9109 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009110
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009111 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9112 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9113 return -1;
9114 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009115
9116 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009117 * TODO: This function may not always work correctly. For example,
9118 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009119 */
Hai Shalom74f70d42019-02-11 14:42:39 -08009120 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009121 wpa_dbg(wpa_s, MSG_DEBUG,
9122 "P2P CSA: Failed to select new frequency for GO");
9123 return -1;
9124 }
9125
9126 if (current_ssid->frequency == params.freq) {
9127 wpa_dbg(wpa_s, MSG_DEBUG,
9128 "P2P CSA: Selected same frequency - not moving GO");
9129 return 0;
9130 }
9131
9132 conf = hostapd_config_defaults();
9133 if (!conf) {
9134 wpa_dbg(wpa_s, MSG_DEBUG,
9135 "P2P CSA: Failed to allocate default config");
9136 return -1;
9137 }
9138
9139 current_ssid->frequency = params.freq;
9140 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9141 wpa_dbg(wpa_s, MSG_DEBUG,
9142 "P2P CSA: Failed to create new GO config");
9143 ret = -1;
9144 goto out;
9145 }
9146
9147 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9148 wpa_dbg(wpa_s, MSG_DEBUG,
9149 "P2P CSA: CSA to a different band is not supported");
9150 ret = -1;
9151 goto out;
9152 }
9153
9154 os_memset(&csa_settings, 0, sizeof(csa_settings));
9155 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9156 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9157 csa_settings.freq_params.freq = params.freq;
9158 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9159 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9160 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9161
9162 if (conf->ieee80211ac) {
9163 int freq1 = 0, freq2 = 0;
9164 u8 chan, opclass;
9165
9166 if (ieee80211_freq_to_channel_ext(params.freq,
9167 conf->secondary_channel,
9168 conf->vht_oper_chwidth,
9169 &opclass, &chan) ==
9170 NUM_HOSTAPD_MODES) {
9171 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9172 ret = -1;
9173 goto out;
9174 }
9175
9176 if (conf->vht_oper_centr_freq_seg0_idx)
9177 freq1 = ieee80211_chan_to_freq(
9178 NULL, opclass,
9179 conf->vht_oper_centr_freq_seg0_idx);
9180
9181 if (conf->vht_oper_centr_freq_seg1_idx)
9182 freq2 = ieee80211_chan_to_freq(
9183 NULL, opclass,
9184 conf->vht_oper_centr_freq_seg1_idx);
9185
9186 if (freq1 < 0 || freq2 < 0) {
9187 wpa_dbg(wpa_s, MSG_DEBUG,
9188 "P2P CSA: Selected invalid VHT center freqs");
9189 ret = -1;
9190 goto out;
9191 }
9192
9193 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9194 csa_settings.freq_params.center_freq1 = freq1;
9195 csa_settings.freq_params.center_freq2 = freq2;
9196
9197 switch (conf->vht_oper_chwidth) {
9198 case VHT_CHANWIDTH_80MHZ:
9199 case VHT_CHANWIDTH_80P80MHZ:
9200 csa_settings.freq_params.bandwidth = 80;
9201 break;
9202 case VHT_CHANWIDTH_160MHZ:
9203 csa_settings.freq_params.bandwidth = 160;
9204 break;
9205 }
9206 }
9207
9208 ret = ap_switch_channel(wpa_s, &csa_settings);
9209out:
9210 current_ssid->frequency = old_freq;
9211 hostapd_config_free(conf);
9212 return ret;
9213}
9214
9215
9216static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9217{
9218 struct p2p_go_neg_results params;
9219 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9220
9221 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9222
9223 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9224 current_ssid->frequency);
9225
9226 /* Stop the AP functionality */
9227 /* TODO: Should do this in a way that does not indicated to possible
9228 * P2P Clients in the group that the group is terminated. */
9229 wpa_supplicant_ap_deinit(wpa_s);
9230
9231 /* Reselect the GO frequency */
Hai Shalom74f70d42019-02-11 14:42:39 -08009232 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9234 wpas_p2p_group_delete(wpa_s,
9235 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9236 return;
9237 }
9238 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9239 params.freq);
9240
9241 if (params.freq &&
9242 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9243 wpa_printf(MSG_DEBUG,
9244 "P2P: Selected freq (%u MHz) is not valid for P2P",
9245 params.freq);
9246 wpas_p2p_group_delete(wpa_s,
9247 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9248 return;
9249 }
9250
9251 /* Update the frequency */
9252 current_ssid->frequency = params.freq;
9253 wpa_s->connect_without_scan = current_ssid;
9254 wpa_s->reassociate = 1;
9255 wpa_s->disconnected = 0;
9256 wpa_supplicant_req_scan(wpa_s, 0, 0);
9257}
9258
9259
9260static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9261{
9262 struct wpa_supplicant *wpa_s = eloop_ctx;
9263
9264 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009265 return;
9266
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009267 wpas_p2p_go_update_common_freqs(wpa_s);
9268
9269 /* Do not move GO in the middle of a CSA */
9270 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9271 wpa_printf(MSG_DEBUG,
9272 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009273 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009274 }
9275
9276 /*
9277 * First, try a channel switch flow. If it is not supported or fails,
9278 * take down the GO and bring it up again.
9279 */
9280 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9281 wpas_p2p_move_go_no_csa(wpa_s);
9282}
9283
9284
9285static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9286{
9287 struct wpa_supplicant *wpa_s = eloop_ctx;
9288 struct wpa_used_freq_data *freqs = NULL;
9289 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009290
9291 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9292 if (!freqs)
9293 return;
9294
9295 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9296
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009297 /* Previous attempt to move a GO was not possible -- try again. */
9298 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9299 WPAS_P2P_CHANNEL_UPDATE_ANY);
9300
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009301 os_free(freqs);
9302}
9303
9304
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009305/*
9306 * Consider moving a GO from its currently used frequency:
9307 * 1. It is possible that due to regulatory consideration the frequency
9308 * can no longer be used and there is a need to evacuate the GO.
9309 * 2. It is possible that due to MCC considerations, it would be preferable
9310 * to move the GO to a channel that is currently used by some other
9311 * station interface.
9312 *
9313 * In case a frequency that became invalid is once again valid, cancel a
9314 * previously initiated GO frequency change.
9315 */
9316static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9317 struct wpa_used_freq_data *freqs,
9318 unsigned int num)
9319{
9320 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9321 unsigned int timeout;
9322 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009323 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009324
9325 wpas_p2p_go_update_common_freqs(wpa_s);
9326
9327 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009328 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009329 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009330 for (i = 0, invalid_freq = 0; i < num; i++) {
9331 if (freqs[i].freq == freq) {
9332 flags = freqs[i].flags;
9333
9334 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009335 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9336 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009337 wpa_dbg(wpa_s, MSG_DEBUG,
9338 "P2P: Freq=%d MHz no longer valid for GO",
9339 freq);
9340 invalid_freq = 1;
9341 }
9342 } else if (freqs[i].flags == 0) {
9343 /* Freq is not used by any other station interface */
9344 continue;
9345 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009346 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009347 /* Freq is not valid for P2P use cases */
9348 continue;
9349 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9350 P2P_GO_FREQ_MOVE_SCM) {
9351 policy_move = 1;
9352 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9353 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9354 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9355 policy_move = 1;
9356 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9357 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9358 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9359 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9360 policy_move = 1;
9361 } else if ((wpa_s->drv_flags &
9362 WPA_DRIVER_FLAGS_AP_CSA) &&
9363 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9364 u8 chan;
9365
9366 /*
9367 * We do not support CSA between bands, so move
9368 * GO only within the same band.
9369 */
9370 if (wpa_s->ap_iface->current_mode->mode ==
9371 ieee80211_freq_to_chan(freqs[i].freq,
9372 &chan))
9373 policy_move = 1;
9374 }
9375 }
9376 }
9377
9378 wpa_dbg(wpa_s, MSG_DEBUG,
9379 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9380 invalid_freq, policy_move, flags);
9381
9382 /*
9383 * The channel is valid, or we are going to have a policy move, so
9384 * cancel timeout.
9385 */
9386 if (!invalid_freq || policy_move) {
9387 wpa_dbg(wpa_s, MSG_DEBUG,
9388 "P2P: Cancel a GO move from freq=%d MHz", freq);
9389 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9390
9391 if (wpas_p2p_in_progress(wpa_s)) {
9392 wpa_dbg(wpa_s, MSG_DEBUG,
9393 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9394 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9395 wpa_s, NULL);
9396 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9397 wpas_p2p_reconsider_moving_go,
9398 wpa_s, NULL);
9399 return;
9400 }
9401 }
9402
9403 if (!invalid_freq && (!policy_move || flags != 0)) {
9404 wpa_dbg(wpa_s, MSG_DEBUG,
9405 "P2P: Not initiating a GO frequency change");
9406 return;
9407 }
9408
9409 /*
9410 * Do not consider moving GO if it is in the middle of a CSA. When the
9411 * CSA is finished this flow should be retriggered.
9412 */
9413 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9414 wpa_dbg(wpa_s, MSG_DEBUG,
9415 "P2P: Not initiating a GO frequency change - CSA is in progress");
9416 return;
9417 }
9418
9419 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9420 timeout = P2P_GO_FREQ_CHANGE_TIME;
9421 else
9422 timeout = 0;
9423
9424 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9425 freq, timeout);
9426 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9427 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9428}
9429
9430
9431static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9432 struct wpa_used_freq_data *freqs,
9433 unsigned int num,
9434 enum wpas_p2p_channel_update_trig trig)
9435{
9436 struct wpa_supplicant *ifs;
9437
9438 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9439 NULL);
9440
9441 /*
9442 * Travers all the radio interfaces, and for each GO interface, check
9443 * if there is a need to move the GO from the frequency it is using,
9444 * or in case the frequency is valid again, cancel the evacuation flow.
9445 */
9446 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9447 radio_list) {
9448 if (ifs->current_ssid == NULL ||
9449 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9450 continue;
9451
9452 /*
9453 * The GO was just started or completed channel switch, no need
9454 * to move it.
9455 */
9456 if (wpa_s == ifs &&
9457 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9458 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9459 wpa_dbg(wpa_s, MSG_DEBUG,
9460 "P2P: GO move - schedule re-consideration");
9461 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9462 wpas_p2p_reconsider_moving_go,
9463 wpa_s, NULL);
9464 continue;
9465 }
9466
9467 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9468 }
9469}
9470
9471
9472void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9473{
9474 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9475 return;
9476
9477 wpas_p2p_update_channel_list(wpa_s,
9478 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9479}
9480
9481
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009482void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9483{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009484 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9485 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9486 "the management interface is being removed");
9487 wpas_p2p_deinit_global(wpa_s->global);
9488 }
9489}
9490
9491
9492void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9493{
9494 if (wpa_s->ap_iface->bss)
9495 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9496 wpas_p2p_group_deinit(wpa_s);
9497}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009498
9499
9500int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9501 unsigned int period, unsigned int interval,
9502 unsigned int count)
9503{
9504 struct p2p_data *p2p = wpa_s->global->p2p;
9505 u8 *device_types;
9506 size_t dev_types_len;
9507 struct wpabuf *buf;
9508 int ret;
9509
9510 if (wpa_s->p2p_lo_started) {
9511 wpa_dbg(wpa_s, MSG_DEBUG,
9512 "P2P Listen offload is already started");
9513 return 0;
9514 }
9515
9516 if (wpa_s->global->p2p == NULL ||
9517 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9518 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9519 return -1;
9520 }
9521
9522 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9523 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9524 freq);
9525 return -1;
9526 }
9527
9528 /* Get device type */
9529 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9530 WPS_DEV_TYPE_LEN;
9531 device_types = os_malloc(dev_types_len);
9532 if (!device_types)
9533 return -1;
9534 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9535 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9536 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9537
9538 /* Get Probe Response IE(s) */
9539 buf = p2p_build_probe_resp_template(p2p, freq);
9540 if (!buf) {
9541 os_free(device_types);
9542 return -1;
9543 }
9544
9545 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9546 device_types, dev_types_len,
9547 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9548 if (ret < 0)
9549 wpa_dbg(wpa_s, MSG_DEBUG,
9550 "P2P: Failed to start P2P listen offload");
9551
9552 os_free(device_types);
9553 wpabuf_free(buf);
9554
9555 if (ret == 0) {
9556 wpa_s->p2p_lo_started = 1;
9557
9558 /* Stop current P2P listen if any */
9559 wpas_stop_listen(wpa_s);
9560 }
9561
9562 return ret;
9563}
9564
9565
9566int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9567{
9568 int ret;
9569
9570 if (!wpa_s->p2p_lo_started)
9571 return 0;
9572
9573 ret = wpa_drv_p2p_lo_stop(wpa_s);
9574 if (ret < 0)
9575 wpa_dbg(wpa_s, MSG_DEBUG,
9576 "P2P: Failed to stop P2P listen offload");
9577
9578 wpa_s->p2p_lo_started = 0;
9579 return ret;
9580}