blob: 78011c67c1e7ed8bb4d31a765a7863b3e828799e [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;
983
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800984 wpa_s->waiting_presence_resp = 0;
985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
987 if (ssid && (ssid->p2p_group ||
988 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
989 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
990 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700991 if (ssid == wpa_s->current_ssid) {
992 wpa_sm_set_config(wpa_s->wpa, NULL);
993 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700995 }
996 /*
997 * Networks objects created during any P2P activities are not
998 * exposed out as they might/will confuse certain non-P2P aware
999 * applications since these network objects won't behave like
1000 * regular ones.
1001 *
1002 * Likewise, we don't send out network removed signals for such
1003 * network objects.
1004 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 wpa_config_remove_network(wpa_s->conf, id);
1006 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001007 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 } else {
1009 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1010 "found");
1011 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001012 if (wpa_s->ap_iface)
1013 wpa_supplicant_ap_deinit(wpa_s);
1014 else
1015 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001016
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1018
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001019 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020}
1021
1022
1023static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1024 u8 *go_dev_addr,
1025 const u8 *ssid, size_t ssid_len)
1026{
1027 struct wpa_bss *bss;
1028 const u8 *bssid;
1029 struct wpabuf *p2p;
1030 u8 group_capab;
1031 const u8 *addr;
1032
1033 if (wpa_s->go_params)
1034 bssid = wpa_s->go_params->peer_interface_addr;
1035 else
1036 bssid = wpa_s->bssid;
1037
1038 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001039 if (bss == NULL && wpa_s->go_params &&
1040 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1041 bss = wpa_bss_get_p2p_dev_addr(
1042 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 if (bss == NULL) {
1044 u8 iface_addr[ETH_ALEN];
1045 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1046 iface_addr) == 0)
1047 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1048 }
1049 if (bss == NULL) {
1050 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1051 "group is persistent - BSS " MACSTR " not found",
1052 MAC2STR(bssid));
1053 return 0;
1054 }
1055
1056 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001057 if (p2p == NULL)
1058 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1059 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 if (p2p == NULL) {
1061 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1062 "group is persistent - BSS " MACSTR
1063 " did not include P2P IE", MAC2STR(bssid));
1064 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1065 (u8 *) (bss + 1), bss->ie_len);
1066 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1067 ((u8 *) bss + 1) + bss->ie_len,
1068 bss->beacon_ie_len);
1069 return 0;
1070 }
1071
1072 group_capab = p2p_get_group_capab(p2p);
1073 addr = p2p_get_go_dev_addr(p2p);
1074 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1075 "group_capab=0x%x", group_capab);
1076 if (addr) {
1077 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1078 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1079 MAC2STR(addr));
1080 } else
1081 os_memset(go_dev_addr, 0, ETH_ALEN);
1082 wpabuf_free(p2p);
1083
1084 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1085 "go_dev_addr=" MACSTR,
1086 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1087
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001088 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089}
1090
1091
Jouni Malinen75ecf522011-06-27 15:19:46 -07001092static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1093 struct wpa_ssid *ssid,
1094 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095{
1096 struct wpa_ssid *s;
1097 int changed = 0;
1098
1099 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1100 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1101 for (s = wpa_s->conf->ssid; s; s = s->next) {
1102 if (s->disabled == 2 &&
1103 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1104 s->ssid_len == ssid->ssid_len &&
1105 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1106 break;
1107 }
1108
1109 if (s) {
1110 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1111 "entry");
1112 if (ssid->passphrase && !s->passphrase)
1113 changed = 1;
1114 else if (ssid->passphrase && s->passphrase &&
1115 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1116 changed = 1;
1117 } else {
1118 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1119 "entry");
1120 changed = 1;
1121 s = wpa_config_add_network(wpa_s->conf);
1122 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001123 return -1;
1124
1125 /*
1126 * Instead of network_added we emit persistent_group_added
1127 * notification. Also to keep the defense checks in
1128 * persistent_group obj registration method, we set the
1129 * relevant flags in s to designate it as a persistent group.
1130 */
1131 s->p2p_group = 1;
1132 s->p2p_persistent_group = 1;
1133 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 wpa_config_set_network_defaults(s);
1135 }
1136
1137 s->p2p_group = 1;
1138 s->p2p_persistent_group = 1;
1139 s->disabled = 2;
1140 s->bssid_set = 1;
1141 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1142 s->mode = ssid->mode;
1143 s->auth_alg = WPA_AUTH_ALG_OPEN;
1144 s->key_mgmt = WPA_KEY_MGMT_PSK;
1145 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001146 s->pbss = ssid->pbss;
1147 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 s->export_keys = 1;
1149 if (ssid->passphrase) {
1150 os_free(s->passphrase);
1151 s->passphrase = os_strdup(ssid->passphrase);
1152 }
1153 if (ssid->psk_set) {
1154 s->psk_set = 1;
1155 os_memcpy(s->psk, ssid->psk, 32);
1156 }
1157 if (s->passphrase && !s->psk_set)
1158 wpa_config_update_psk(s);
1159 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1160 os_free(s->ssid);
1161 s->ssid = os_malloc(ssid->ssid_len);
1162 }
1163 if (s->ssid) {
1164 s->ssid_len = ssid->ssid_len;
1165 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1166 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001167 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1168 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1169 wpa_s->global->add_psk = NULL;
1170 changed = 1;
1171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 if (changed && wpa_s->conf->update_config &&
1174 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1175 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1176 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001177
1178 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179}
1180
1181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001182static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1183 const u8 *addr)
1184{
1185 struct wpa_ssid *ssid, *s;
1186 u8 *n;
1187 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001188 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001189 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190
1191 ssid = wpa_s->current_ssid;
1192 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1193 !ssid->p2p_persistent_group)
1194 return;
1195
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001196 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1198 continue;
1199
1200 if (s->ssid_len == ssid->ssid_len &&
1201 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1202 break;
1203 }
1204
1205 if (s == NULL)
1206 return;
1207
1208 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001209 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001210 ETH_ALEN) != 0)
1211 continue;
1212
1213 if (i == s->num_p2p_clients - 1)
1214 return; /* already the most recent entry */
1215
1216 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001217 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1218 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1219 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001220 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001221 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1222 ETH_ALEN);
1223 os_memset(s->p2p_client_list +
1224 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1225 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 found = 1;
1227 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 }
1229
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001230 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1231 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001232 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001233 if (n == NULL)
1234 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001235 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1236 ETH_ALEN);
1237 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1238 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001239 s->p2p_client_list = n;
1240 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001241 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 /* Not enough room for an additional entry - drop the oldest
1243 * entry */
1244 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001245 s->p2p_client_list + 2 * ETH_ALEN,
1246 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001247 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001248 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001249 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001250 os_memset(s->p2p_client_list +
1251 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1252 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001253 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001255 if (p2p_wpa_s->conf->update_config &&
1256 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001258}
1259
1260
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001261static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1262 int go, struct wpa_ssid *ssid, int freq,
1263 const u8 *psk, const char *passphrase,
1264 const u8 *go_dev_addr, int persistent,
1265 const char *extra)
1266{
1267 const char *ssid_txt;
1268 char psk_txt[65];
1269
1270 if (psk)
1271 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1272 else
1273 psk_txt[0] = '\0';
1274
1275 if (ssid)
1276 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1277 else
1278 ssid_txt = "";
1279
1280 if (passphrase && passphrase[0] == '\0')
1281 passphrase = NULL;
1282
1283 /*
1284 * Include PSK/passphrase only in the control interface message and
1285 * leave it out from the debug log entry.
1286 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001287 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001288 P2P_EVENT_GROUP_STARTED
1289 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1290 MACSTR "%s%s",
1291 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1292 psk ? " psk=" : "", psk_txt,
1293 passphrase ? " passphrase=\"" : "",
1294 passphrase ? passphrase : "",
1295 passphrase ? "\"" : "",
1296 MAC2STR(go_dev_addr),
1297 persistent ? " [PERSISTENT]" : "", extra);
1298 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1299 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1300 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1301 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1302 extra);
1303}
1304
1305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001307 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308{
1309 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 int client;
1311 int persistent;
1312 u8 go_dev_addr[ETH_ALEN];
1313
1314 /*
1315 * This callback is likely called for the main interface. Update wpa_s
1316 * to use the group interface if a new interface was created for the
1317 * group.
1318 */
1319 if (wpa_s->global->p2p_group_formation)
1320 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001321 if (wpa_s->p2p_go_group_formation_completed) {
1322 wpa_s->global->p2p_group_formation = NULL;
1323 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001324 } else if (wpa_s->p2p_in_provisioning && !success) {
1325 wpa_msg(wpa_s, MSG_DEBUG,
1326 "P2P: Stop provisioning state due to failure");
1327 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001328 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001329 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001330 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331
1332 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001333 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001334 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001335 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001336 if (already_deleted)
1337 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001338 wpas_p2p_group_delete(wpa_s,
1339 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 return;
1341 }
1342
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001343 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001344 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345
1346 ssid = wpa_s->current_ssid;
1347 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1348 ssid->mode = WPAS_MODE_P2P_GO;
1349 p2p_group_notif_formation_done(wpa_s->p2p_group);
1350 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1351 }
1352
1353 persistent = 0;
1354 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 client = ssid->mode == WPAS_MODE_INFRA;
1356 if (ssid->mode == WPAS_MODE_P2P_GO) {
1357 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001358 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1359 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 } else
1361 persistent = wpas_p2p_persistent_group(wpa_s,
1362 go_dev_addr,
1363 ssid->ssid,
1364 ssid->ssid_len);
1365 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 client = wpa_s->p2p_group_interface ==
1367 P2P_GROUP_INTERFACE_CLIENT;
1368 os_memset(go_dev_addr, 0, ETH_ALEN);
1369 }
1370
1371 wpa_s->show_group_started = 0;
1372 if (client) {
1373 /*
1374 * Indicate event only after successfully completed 4-way
1375 * handshake, i.e., when the interface is ready for data
1376 * packets.
1377 */
1378 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001380 wpas_p2p_group_started(wpa_s, 1, ssid,
1381 ssid ? ssid->frequency : 0,
1382 ssid && ssid->passphrase == NULL &&
1383 ssid->psk_set ? ssid->psk : NULL,
1384 ssid ? ssid->passphrase : NULL,
1385 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 wpas_p2p_cross_connect_setup(wpa_s);
1387 wpas_p2p_set_group_idle_timeout(wpa_s);
1388 }
1389
1390 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001391 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1392 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001393 else {
1394 os_free(wpa_s->global->add_psk);
1395 wpa_s->global->add_psk = NULL;
1396 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001397
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001398 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001399 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001400 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402}
1403
1404
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001405struct send_action_work {
1406 unsigned int freq;
1407 u8 dst[ETH_ALEN];
1408 u8 src[ETH_ALEN];
1409 u8 bssid[ETH_ALEN];
1410 size_t len;
1411 unsigned int wait_time;
1412 u8 buf[0];
1413};
1414
1415
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001416static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1417{
1418 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1419
1420 wpa_printf(MSG_DEBUG,
1421 "P2P: Free Action frame radio work @%p (freq=%u dst="
1422 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1423 wpa_s->p2p_send_action_work, awork->freq,
1424 MAC2STR(awork->dst), MAC2STR(awork->src),
1425 MAC2STR(awork->bssid), awork->wait_time);
1426 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1427 awork->buf, awork->len);
1428 os_free(awork);
1429 wpa_s->p2p_send_action_work->ctx = NULL;
1430 radio_work_done(wpa_s->p2p_send_action_work);
1431 wpa_s->p2p_send_action_work = NULL;
1432}
1433
1434
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001435static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1436 void *timeout_ctx)
1437{
1438 struct wpa_supplicant *wpa_s = eloop_ctx;
1439
1440 if (!wpa_s->p2p_send_action_work)
1441 return;
1442
1443 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001444 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001445}
1446
1447
Dmitry Shmidt03658832014-08-13 11:03:49 -07001448static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450 if (wpa_s->p2p_send_action_work) {
1451 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001452
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001453 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001454 wpa_printf(MSG_DEBUG,
1455 "P2P: Clear Action TX work @%p (wait_time=%u)",
1456 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001457 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001458 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001459 } else {
1460 /*
1461 * In theory, this should not be needed, but number of
1462 * places in the P2P code is still using non-zero wait
1463 * time for the last Action frame in the sequence and
1464 * some of these do not call send_action_done().
1465 */
1466 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1467 wpa_s, NULL);
1468 eloop_register_timeout(
1469 0, awork->wait_time * 1000,
1470 wpas_p2p_send_action_work_timeout,
1471 wpa_s, NULL);
1472 }
1473 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001474}
1475
1476
1477static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1478 unsigned int freq,
1479 const u8 *dst, const u8 *src,
1480 const u8 *bssid,
1481 const u8 *data, size_t data_len,
1482 enum offchannel_send_action_result
1483 result)
1484{
1485 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1486
1487 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1490 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492 switch (result) {
1493 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1494 res = P2P_SEND_ACTION_SUCCESS;
1495 break;
1496 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1497 res = P2P_SEND_ACTION_NO_ACK;
1498 break;
1499 case OFFCHANNEL_SEND_ACTION_FAILED:
1500 res = P2P_SEND_ACTION_FAILED;
1501 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 }
1503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001506 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1507 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001508 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001509 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1510 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001512 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1513 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001514 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001515 P2P_EVENT_FALLBACK_TO_GO_NEG
1516 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001517 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1518 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 }
1520}
1521
1522
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001523static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1524{
1525 struct wpa_supplicant *wpa_s = work->wpa_s;
1526 struct send_action_work *awork = work->ctx;
1527
1528 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001529 if (work->started) {
1530 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1531 wpa_s, NULL);
1532 wpa_s->p2p_send_action_work = NULL;
1533 offchannel_send_action_done(wpa_s);
1534 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001535 os_free(awork);
1536 return;
1537 }
1538
1539 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1540 awork->bssid, awork->buf, awork->len,
1541 awork->wait_time,
1542 wpas_p2p_send_action_tx_status, 1) < 0) {
1543 os_free(awork);
1544 radio_work_done(work);
1545 return;
1546 }
1547 wpa_s->p2p_send_action_work = work;
1548}
1549
1550
1551static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1552 unsigned int freq, const u8 *dst,
1553 const u8 *src, const u8 *bssid, const u8 *buf,
1554 size_t len, unsigned int wait_time)
1555{
1556 struct send_action_work *awork;
1557
1558 if (wpa_s->p2p_send_action_work) {
1559 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1560 return -1;
1561 }
1562
1563 awork = os_zalloc(sizeof(*awork) + len);
1564 if (awork == NULL)
1565 return -1;
1566
1567 awork->freq = freq;
1568 os_memcpy(awork->dst, dst, ETH_ALEN);
1569 os_memcpy(awork->src, src, ETH_ALEN);
1570 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1571 awork->len = len;
1572 awork->wait_time = wait_time;
1573 os_memcpy(awork->buf, buf, len);
1574
1575 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1576 wpas_send_action_cb, awork) < 0) {
1577 os_free(awork);
1578 return -1;
1579 }
1580
1581 return 0;
1582}
1583
1584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1586 const u8 *src, const u8 *bssid, const u8 *buf,
1587 size_t len, unsigned int wait_time)
1588{
1589 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001590 int listen_freq = -1, send_freq = -1;
1591
1592 if (wpa_s->p2p_listen_work)
1593 listen_freq = wpa_s->p2p_listen_work->freq;
1594 if (wpa_s->p2p_send_action_work)
1595 send_freq = wpa_s->p2p_send_action_work->freq;
1596 if (listen_freq != (int) freq && send_freq != (int) freq) {
1597 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1598 listen_freq, send_freq);
1599 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1600 len, wait_time);
1601 }
1602
1603 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1605 wait_time,
1606 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607}
1608
1609
1610static void wpas_send_action_done(void *ctx)
1611{
1612 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613
1614 if (wpa_s->p2p_send_action_work) {
1615 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1616 wpa_s, NULL);
1617 os_free(wpa_s->p2p_send_action_work->ctx);
1618 radio_work_done(wpa_s->p2p_send_action_work);
1619 wpa_s->p2p_send_action_work = NULL;
1620 }
1621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001622 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623}
1624
1625
1626static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1627 struct p2p_go_neg_results *params)
1628{
1629 if (wpa_s->go_params == NULL) {
1630 wpa_s->go_params = os_malloc(sizeof(*params));
1631 if (wpa_s->go_params == NULL)
1632 return -1;
1633 }
1634 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1635 return 0;
1636}
1637
1638
1639static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1640 struct p2p_go_neg_results *res)
1641{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001642 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001643 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1644 " dev_addr " MACSTR " wps_method %d",
1645 MAC2STR(res->peer_interface_addr),
1646 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1648 res->ssid, res->ssid_len);
1649 wpa_supplicant_ap_deinit(wpa_s);
1650 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001653#ifdef CONFIG_WPS_NFC
1654 } else if (res->wps_method == WPS_NFC) {
1655 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1656 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001657 wpa_s->p2pdev->p2p_oob_dev_pw,
1658 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1659 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001660 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001661 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001662 NULL,
1663 NULL, 0, 0);
1664#endif /* CONFIG_WPS_NFC */
1665 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001667 if (wpa_s->p2p_wps_method == WPS_P2PS)
1668 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1670 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1671 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1672 wpa_s->p2p_pin, 1, dev_pw_id);
1673 }
1674}
1675
1676
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001677static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1678 struct wpa_ssid *ssid)
1679{
1680 struct wpa_ssid *persistent;
1681 struct psk_list_entry *psk;
1682 struct hostapd_data *hapd;
1683
1684 if (!wpa_s->ap_iface)
1685 return;
1686
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001687 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001688 ssid->ssid_len);
1689 if (persistent == NULL)
1690 return;
1691
1692 hapd = wpa_s->ap_iface->bss[0];
1693
1694 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1695 list) {
1696 struct hostapd_wpa_psk *hpsk;
1697
1698 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1699 MACSTR " psk=%d",
1700 MAC2STR(psk->addr), psk->p2p);
1701 hpsk = os_zalloc(sizeof(*hpsk));
1702 if (hpsk == NULL)
1703 break;
1704 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1705 if (psk->p2p)
1706 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1707 else
1708 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1709 hpsk->next = hapd->conf->ssid.wpa_psk;
1710 hapd->conf->ssid.wpa_psk = hpsk;
1711 }
1712}
1713
1714
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001715static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1716{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001717 char buf[20 + P2P_MAX_CHANNELS * 6];
1718 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001719 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001720 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001721
Roshan Pius3a1667e2018-07-03 15:17:14 -07001722 pos = buf;
1723 end = pos + sizeof(buf);
1724 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1725 res = os_snprintf(pos, end - pos, " %d",
1726 wpa_s->p2p_group_common_freqs[i]);
1727 if (os_snprintf_error(end - pos, res))
1728 break;
1729 pos += res;
1730 }
1731 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001732
Roshan Pius3a1667e2018-07-03 15:17:14 -07001733 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001734}
1735
1736
1737static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1738 struct p2p_go_neg_results *params)
1739{
1740 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1741
1742 wpa_s->p2p_group_common_freqs_num = 0;
1743 os_free(wpa_s->p2p_group_common_freqs);
1744 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1745 if (!wpa_s->p2p_group_common_freqs)
1746 return;
1747
1748 for (i = 0; i < len; i++) {
1749 if (!wpa_s->go_params->freq_list[i])
1750 break;
1751 wpa_s->p2p_group_common_freqs[i] =
1752 wpa_s->go_params->freq_list[i];
1753 }
1754 wpa_s->p2p_group_common_freqs_num = i;
1755}
1756
1757
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001758static void p2p_config_write(struct wpa_supplicant *wpa_s)
1759{
1760#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001761 if (wpa_s->p2pdev->conf->update_config &&
1762 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001763 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1764#endif /* CONFIG_NO_CONFIG_WRITE */
1765}
1766
1767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768static void p2p_go_configured(void *ctx, void *data)
1769{
1770 struct wpa_supplicant *wpa_s = ctx;
1771 struct p2p_go_neg_results *params = data;
1772 struct wpa_ssid *ssid;
1773
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001774 wpa_s->ap_configured_cb = NULL;
1775 wpa_s->ap_configured_cb_ctx = NULL;
1776 wpa_s->ap_configured_cb_data = NULL;
1777 if (!wpa_s->go_params) {
1778 wpa_printf(MSG_ERROR,
1779 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1780 return;
1781 }
1782
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001783 p2p_go_save_group_common_freqs(wpa_s, params);
1784 p2p_go_dump_common_freqs(wpa_s);
1785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 ssid = wpa_s->current_ssid;
1787 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1788 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1789 if (wpa_s->global->p2p_group_formation == wpa_s)
1790 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001791 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1792 params->passphrase[0] == '\0' ?
1793 params->psk : NULL,
1794 params->passphrase,
1795 wpa_s->global->p2p_dev_addr,
1796 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001797 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001799 if (wpa_s->p2pdev->p2ps_method_config_any) {
1800 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001801 wpa_dbg(wpa_s, MSG_DEBUG,
1802 "P2PS: Setting default PIN for ANY");
1803 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1804 "12345670", NULL, 0,
1805 0);
1806 } else {
1807 wpa_dbg(wpa_s, MSG_DEBUG,
1808 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001809 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001810 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001811 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001812 "12345670", NULL, 0, 0);
1813 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001814 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001815 }
1816
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001817 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001818 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001819 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001820 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001821 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001822 wpas_p2p_add_psk_list(wpa_s, ssid);
1823 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001824
1825 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001826 params->persistent_group, 0,
1827 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 wpas_p2p_cross_connect_setup(wpa_s);
1829 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001830
1831 if (wpa_s->p2p_first_connection_timeout) {
1832 wpa_dbg(wpa_s, MSG_DEBUG,
1833 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1834 wpa_s->p2p_first_connection_timeout);
1835 wpa_s->p2p_go_group_formation_completed = 0;
1836 wpa_s->global->p2p_group_formation = wpa_s;
1837 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001838 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001839 eloop_register_timeout(
1840 wpa_s->p2p_first_connection_timeout, 0,
1841 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001842 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001843 }
1844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 return;
1846 }
1847
1848 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1849 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1850 params->peer_interface_addr)) {
1851 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1852 "filtering");
1853 return;
1854 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001855 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001857 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001858#ifdef CONFIG_WPS_NFC
1859 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001860 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001861 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001862 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001863 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1864 return;
1865 }
1866 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001867 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1868 wpa_s->p2pdev->p2p_oob_dev_pw,
1869 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1870 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001871#endif /* CONFIG_WPS_NFC */
1872 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001874 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875 os_free(wpa_s->go_params);
1876 wpa_s->go_params = NULL;
1877}
1878
1879
1880static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1881 struct p2p_go_neg_results *params,
1882 int group_formation)
1883{
1884 struct wpa_ssid *ssid;
1885
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1887 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1888 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1889 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892
1893 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001894 if (ssid == NULL) {
1895 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001899 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001900 wpa_s->p2p_go_group_formation_completed = 0;
1901 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001902 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 wpa_config_set_network_defaults(ssid);
1905 ssid->temporary = 1;
1906 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001907 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1909 WPAS_MODE_P2P_GO;
1910 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001911 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001912 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001913 ssid->max_oper_chwidth = params->max_oper_chwidth;
1914 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08001915 ssid->he = params->he;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 ssid->ssid = os_zalloc(params->ssid_len + 1);
1917 if (ssid->ssid) {
1918 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1919 ssid->ssid_len = params->ssid_len;
1920 }
1921 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1922 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1923 ssid->proto = WPA_PROTO_RSN;
1924 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001925 ssid->group_cipher = WPA_CIPHER_CCMP;
1926 if (params->freq > 56160) {
1927 /*
1928 * Enable GCMP instead of CCMP as pairwise_cipher and
1929 * group_cipher in 60 GHz.
1930 */
1931 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1932 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001933 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1934 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001935 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001936 if (os_strlen(params->passphrase) > 0) {
1937 ssid->passphrase = os_strdup(params->passphrase);
1938 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001939 wpa_msg_global(wpa_s, MSG_ERROR,
1940 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001941 wpa_config_remove_network(wpa_s->conf, ssid->id);
1942 return;
1943 }
1944 } else
1945 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001946 ssid->psk_set = params->psk_set;
1947 if (ssid->psk_set)
1948 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001949 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001950 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001951 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952
1953 wpa_s->ap_configured_cb = p2p_go_configured;
1954 wpa_s->ap_configured_cb_ctx = wpa_s;
1955 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001956 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001957 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958 wpa_s->reassociate = 1;
1959 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1961 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 wpa_supplicant_req_scan(wpa_s, 0, 0);
1963}
1964
1965
1966static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1967 const struct wpa_supplicant *src)
1968{
1969 struct wpa_config *d;
1970 const struct wpa_config *s;
1971
1972 d = dst->conf;
1973 s = src->conf;
1974
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001975#define C(n) \
1976do { \
1977 if (s->n && !d->n) \
1978 d->n = os_strdup(s->n); \
1979} while (0)
1980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 C(device_name);
1982 C(manufacturer);
1983 C(model_name);
1984 C(model_number);
1985 C(serial_number);
1986 C(config_methods);
1987#undef C
1988
1989 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1990 os_memcpy(d->sec_device_type, s->sec_device_type,
1991 sizeof(d->sec_device_type));
1992 d->num_sec_device_types = s->num_sec_device_types;
1993
1994 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001995 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001998 d->max_num_sta = s->max_num_sta;
1999 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002000 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002001 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002002 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002003 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002004 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002005 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002006 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002007
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002008 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2009 !d->wps_nfc_pw_from_config) {
2010 wpabuf_free(d->wps_nfc_dh_privkey);
2011 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002012 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2013 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2014 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002015 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002016 d->go_interworking = s->go_interworking;
2017 d->go_access_network_type = s->go_access_network_type;
2018 d->go_internet = s->go_internet;
2019 d->go_venue_group = s->go_venue_group;
2020 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021}
2022
2023
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002024static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2025 char *ifname, size_t len)
2026{
2027 char *ifname_ptr = wpa_s->ifname;
2028
2029 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2030 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2031 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2032 }
2033
2034 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2035 if (os_strlen(ifname) >= IFNAMSIZ &&
2036 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002037 int res;
2038
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002039 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002040 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2041 if (os_snprintf_error(len, res) && len)
2042 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002043 }
2044}
2045
2046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2048 enum wpa_driver_if_type type)
2049{
2050 char ifname[120], force_ifname[120];
2051
2052 if (wpa_s->pending_interface_name[0]) {
2053 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2054 "- skip creation of a new one");
2055 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2056 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2057 "unknown?! ifname='%s'",
2058 wpa_s->pending_interface_name);
2059 return -1;
2060 }
2061 return 0;
2062 }
2063
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002064 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 force_ifname[0] = '\0';
2066
2067 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2068 ifname);
2069 wpa_s->p2p_group_idx++;
2070
2071 wpa_s->pending_interface_type = type;
2072 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2073 wpa_s->pending_interface_addr, NULL) < 0) {
2074 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2075 "interface");
2076 return -1;
2077 }
2078
Jimmy Chen36c21992018-11-29 16:46:43 +08002079 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002080 random_mac_addr(wpa_s->pending_interface_addr);
2081 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2082 " for the group",
2083 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002084 }
2085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 if (force_ifname[0]) {
2087 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2088 force_ifname);
2089 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2090 sizeof(wpa_s->pending_interface_name));
2091 } else
2092 os_strlcpy(wpa_s->pending_interface_name, ifname,
2093 sizeof(wpa_s->pending_interface_name));
2094 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2095 MACSTR, wpa_s->pending_interface_name,
2096 MAC2STR(wpa_s->pending_interface_addr));
2097
2098 return 0;
2099}
2100
2101
2102static void wpas_p2p_remove_pending_group_interface(
2103 struct wpa_supplicant *wpa_s)
2104{
2105 if (!wpa_s->pending_interface_name[0] ||
2106 is_zero_ether_addr(wpa_s->pending_interface_addr))
2107 return; /* No pending virtual interface */
2108
2109 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2110 wpa_s->pending_interface_name);
2111 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2112 wpa_s->pending_interface_name);
2113 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2114 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002115 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116}
2117
2118
2119static struct wpa_supplicant *
2120wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2121{
2122 struct wpa_interface iface;
2123 struct wpa_supplicant *group_wpa_s;
2124
2125 if (!wpa_s->pending_interface_name[0]) {
2126 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2127 if (!wpas_p2p_create_iface(wpa_s))
2128 return NULL;
2129 /*
2130 * Something has forced us to remove the pending interface; try
2131 * to create a new one and hope for the best that we will get
2132 * the same local address.
2133 */
2134 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2135 WPA_IF_P2P_CLIENT) < 0)
2136 return NULL;
2137 }
2138
2139 os_memset(&iface, 0, sizeof(iface));
2140 iface.ifname = wpa_s->pending_interface_name;
2141 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002142 if (wpa_s->conf->ctrl_interface == NULL &&
2143 wpa_s->parent != wpa_s &&
2144 wpa_s->p2p_mgmt &&
2145 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2146 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2147 else
2148 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002150 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 if (group_wpa_s == NULL) {
2152 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2153 "wpa_supplicant interface");
2154 return NULL;
2155 }
2156 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2158 P2P_GROUP_INTERFACE_CLIENT;
2159 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002160 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161
2162 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2163
Jimmy Chen36c21992018-11-29 16:46:43 +08002164 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002165 if (wpa_drv_set_mac_addr(group_wpa_s,
2166 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002167 wpa_msg(group_wpa_s, MSG_INFO,
2168 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002169 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2170 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002171 return NULL;
2172 }
2173
2174 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2175 wpa_msg(group_wpa_s, MSG_INFO,
2176 "Could not update MAC address information");
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
Hai Shalom74f70d42019-02-11 14:42:39 -08002182 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2183 " for the group",
2184 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002185 }
2186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 return group_wpa_s;
2188}
2189
2190
2191static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2192 void *timeout_ctx)
2193{
2194 struct wpa_supplicant *wpa_s = eloop_ctx;
2195 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002196 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002197}
2198
2199
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002200static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2201 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002202{
2203 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002204 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 if (wpa_s->global->p2p)
2206 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002207 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208}
2209
2210
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002211static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2212{
2213 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2214 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002215 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002216 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002217 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002218 wpa_s->global->p2p_fail_on_wps_complete = 0;
2219}
2220
2221
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002222void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2223{
2224 if (wpa_s->global->p2p_group_formation != wpa_s)
2225 return;
2226 /* Speed up group formation timeout since this cannot succeed */
2227 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002228 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002229 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002230 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002231}
2232
2233
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002234static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235{
2236 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002237 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238
2239 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2240 wpa_drv_cancel_remain_on_channel(wpa_s);
2241 wpa_s->off_channel_freq = 0;
2242 wpa_s->roc_waiting_drv_freq = 0;
2243 }
2244
2245 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002246 wpa_msg_global(wpa_s, MSG_INFO,
2247 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2248 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002249 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 wpas_p2p_remove_pending_group_interface(wpa_s);
2251 return;
2252 }
2253
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002254 if (!res->role_go) {
2255 /* Inform driver of the operating channel of GO. */
2256 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2257 }
2258
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002259 if (wpa_s->p2p_go_ht40)
2260 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002261 if (wpa_s->p2p_go_vht)
2262 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002263 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2264 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002265
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002266 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2267 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2268 " wps_method=%s",
2269 res->role_go ? "GO" : "client", res->freq, res->ht40,
2270 MAC2STR(res->peer_device_addr),
2271 MAC2STR(res->peer_interface_addr),
2272 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002273 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002274
Dmitry Shmidt04949592012-07-19 12:16:46 -07002275 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2276 struct wpa_ssid *ssid;
2277 ssid = wpa_config_get_network(wpa_s->conf,
2278 wpa_s->p2p_persistent_id);
2279 if (ssid && ssid->disabled == 2 &&
2280 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2281 size_t len = os_strlen(ssid->passphrase);
2282 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2283 "on requested persistent group");
2284 os_memcpy(res->passphrase, ssid->passphrase, len);
2285 res->passphrase[len] = '\0';
2286 }
2287 }
2288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002290 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2292 if (group_wpa_s == NULL) {
2293 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002294 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2295 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002296 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 return;
2298 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2300 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002302 group_wpa_s = wpa_s->parent;
2303 wpa_s->global->p2p_group_formation = group_wpa_s;
2304 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002305 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002308 group_wpa_s->p2p_in_provisioning = 1;
2309 group_wpa_s->p2pdev = wpa_s;
2310 if (group_wpa_s != wpa_s) {
2311 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2312 sizeof(group_wpa_s->p2p_pin));
2313 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2314 }
2315 if (res->role_go) {
2316 wpas_start_wps_go(group_wpa_s, res, 1);
2317 } else {
2318 os_get_reltime(&group_wpa_s->scan_min_time);
2319 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 }
2321
2322 wpa_s->p2p_long_listen = 0;
2323 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2324
2325 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2326 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2327 (res->peer_config_timeout % 100) * 10000,
2328 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2329}
2330
2331
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002332static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2333 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334{
2335 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002336 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002337 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2338 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002339
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002340 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341}
2342
2343
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002344static void wpas_dev_found(void *ctx, const u8 *addr,
2345 const struct p2p_peer_info *info,
2346 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002348 u8 *wfd_dev_info = NULL;
2349 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002350#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351 struct wpa_supplicant *wpa_s = ctx;
2352 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002353 char *wfd_dev_info_hex = NULL;
2354
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002355#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002356 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2357 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002358 if (wfd_dev_info_hex) {
2359 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2360 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2361 // Only used for notification, so not handling error.
2362 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2363 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002364#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002365
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002366 if (info->p2ps_instance) {
2367 char str[256];
2368 const u8 *buf = wpabuf_head(info->p2ps_instance);
2369 size_t len = wpabuf_len(info->p2ps_instance);
2370
2371 while (len) {
2372 u32 id;
2373 u16 methods;
2374 u8 str_len;
2375
2376 if (len < 4 + 2 + 1)
2377 break;
2378 id = WPA_GET_LE32(buf);
2379 buf += sizeof(u32);
2380 methods = WPA_GET_BE16(buf);
2381 buf += sizeof(u16);
2382 str_len = *buf++;
2383 if (str_len > len - 4 - 2 - 1)
2384 break;
2385 os_memcpy(str, buf, str_len);
2386 str[str_len] = '\0';
2387 buf += str_len;
2388 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2389
2390 wpa_msg_global(wpa_s, MSG_INFO,
2391 P2P_EVENT_DEVICE_FOUND MACSTR
2392 " p2p_dev_addr=" MACSTR
2393 " pri_dev_type=%s name='%s'"
2394 " config_methods=0x%x"
2395 " dev_capab=0x%x"
2396 " group_capab=0x%x"
2397 " adv_id=%x asp_svc=%s%s",
2398 MAC2STR(addr),
2399 MAC2STR(info->p2p_device_addr),
2400 wps_dev_type_bin2str(
2401 info->pri_dev_type,
2402 devtype, sizeof(devtype)),
2403 info->device_name, methods,
2404 info->dev_capab, info->group_capab,
2405 id, str,
2406 info->vendor_elems ?
2407 " vendor_elems=1" : "");
2408 }
2409 goto done;
2410 }
2411
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002412 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2413 " p2p_dev_addr=" MACSTR
2414 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002415 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002416 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2417 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2418 sizeof(devtype)),
2419 info->device_name, info->config_methods,
2420 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002421 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002422 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002423 info->vendor_elems ? " vendor_elems=1" : "",
2424 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002426done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002427 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002428#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002429
Roshan Piusfd2fd662017-01-23 13:41:57 -08002430 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2431 wfd_dev_info_len, new_device);
2432 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433}
2434
2435
2436static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2437{
2438 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002440 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2441 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2444}
2445
2446
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002447static void wpas_find_stopped(void *ctx)
2448{
2449 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002450
2451 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2452 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2453
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002454 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002455 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002456}
2457
2458
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002459struct wpas_p2p_listen_work {
2460 unsigned int freq;
2461 unsigned int duration;
2462 struct wpabuf *probe_resp_ie;
2463};
2464
2465
2466static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2467{
2468 if (lwork == NULL)
2469 return;
2470 wpabuf_free(lwork->probe_resp_ie);
2471 os_free(lwork);
2472}
2473
2474
2475static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2476{
2477 struct wpas_p2p_listen_work *lwork;
2478
2479 if (!wpa_s->p2p_listen_work)
2480 return;
2481
2482 lwork = wpa_s->p2p_listen_work->ctx;
2483 wpas_p2p_listen_work_free(lwork);
2484 radio_work_done(wpa_s->p2p_listen_work);
2485 wpa_s->p2p_listen_work = NULL;
2486}
2487
2488
2489static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2490{
2491 struct wpa_supplicant *wpa_s = work->wpa_s;
2492 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002493 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002494
2495 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002496 if (work->started) {
2497 wpa_s->p2p_listen_work = NULL;
2498 wpas_stop_listen(wpa_s);
2499 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002500 wpas_p2p_listen_work_free(lwork);
2501 return;
2502 }
2503
2504 wpa_s->p2p_listen_work = work;
2505
2506 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2507
2508 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2509 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2510 "report received Probe Request frames");
2511 wpas_p2p_listen_work_done(wpa_s);
2512 return;
2513 }
2514
2515 wpa_s->pending_listen_freq = lwork->freq;
2516 wpa_s->pending_listen_duration = lwork->duration;
2517
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002518 duration = lwork->duration;
2519#ifdef CONFIG_TESTING_OPTIONS
2520 if (wpa_s->extra_roc_dur) {
2521 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2522 duration, duration + wpa_s->extra_roc_dur);
2523 duration += wpa_s->extra_roc_dur;
2524 }
2525#endif /* CONFIG_TESTING_OPTIONS */
2526
2527 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002528 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2529 "to remain on channel (%u MHz) for Listen "
2530 "state", lwork->freq);
2531 wpas_p2p_listen_work_done(wpa_s);
2532 wpa_s->pending_listen_freq = 0;
2533 return;
2534 }
2535 wpa_s->off_channel_freq = 0;
2536 wpa_s->roc_waiting_drv_freq = lwork->freq;
2537}
2538
2539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540static int wpas_start_listen(void *ctx, unsigned int freq,
2541 unsigned int duration,
2542 const struct wpabuf *probe_resp_ie)
2543{
2544 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002545 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002547 if (wpa_s->p2p_listen_work) {
2548 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 return -1;
2550 }
2551
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002552 lwork = os_zalloc(sizeof(*lwork));
2553 if (lwork == NULL)
2554 return -1;
2555 lwork->freq = freq;
2556 lwork->duration = duration;
2557 if (probe_resp_ie) {
2558 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2559 if (lwork->probe_resp_ie == NULL) {
2560 wpas_p2p_listen_work_free(lwork);
2561 return -1;
2562 }
2563 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002565 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2566 lwork) < 0) {
2567 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 return -1;
2569 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570
2571 return 0;
2572}
2573
2574
2575static void wpas_stop_listen(void *ctx)
2576{
2577 struct wpa_supplicant *wpa_s = ctx;
2578 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2579 wpa_drv_cancel_remain_on_channel(wpa_s);
2580 wpa_s->off_channel_freq = 0;
2581 wpa_s->roc_waiting_drv_freq = 0;
2582 }
2583 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002584
2585 /*
2586 * Don't cancel Probe Request RX reporting for a connected P2P Client
2587 * handling Probe Request frames.
2588 */
2589 if (!wpa_s->p2p_cli_probe)
2590 wpa_drv_probe_req_report(wpa_s, 0);
2591
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002592 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593}
2594
2595
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002596static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2597 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598{
2599 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002600 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2601 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602}
2603
2604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2606 const u8 *peer, const char *params,
2607 unsigned int generated_pin)
2608{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002609 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2610 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611}
2612
2613
2614static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2615 const u8 *peer, const char *params)
2616{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002617 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2618 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619}
2620
2621
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002622static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2623 const u8 *dev_addr, const u8 *pri_dev_type,
2624 const char *dev_name, u16 supp_config_methods,
2625 u8 dev_capab, u8 group_capab, const u8 *group_id,
2626 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627{
2628 struct wpa_supplicant *wpa_s = ctx;
2629 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002630 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 u8 empty_dev_type[8];
2632 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002633 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002634 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635
2636 if (group_id) {
2637 for (group = wpa_s->global->ifaces; group; group = group->next)
2638 {
2639 struct wpa_ssid *s = group->current_ssid;
2640 if (s != NULL &&
2641 s->mode == WPAS_MODE_P2P_GO &&
2642 group_id_len - ETH_ALEN == s->ssid_len &&
2643 os_memcmp(group_id + ETH_ALEN, s->ssid,
2644 s->ssid_len) == 0)
2645 break;
2646 }
2647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648
2649 if (pri_dev_type == NULL) {
2650 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2651 pri_dev_type = empty_dev_type;
2652 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002653 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2654 " pri_dev_type=%s name='%s' config_methods=0x%x "
2655 "dev_capab=0x%x group_capab=0x%x%s%s",
2656 MAC2STR(dev_addr),
2657 wps_dev_type_bin2str(pri_dev_type, devtype,
2658 sizeof(devtype)),
2659 dev_name, supp_config_methods, dev_capab, group_capab,
2660 group ? " group=" : "",
2661 group ? group->ifname : "");
2662 if (os_snprintf_error(sizeof(params), res))
2663 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002667 if (wps_generate_pin(&generated_pin) < 0) {
2668 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2669 wpas_notify_p2p_provision_discovery(
2670 wpa_s, peer, 0 /* response */,
2671 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2672 return;
2673 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 wpas_prov_disc_local_display(wpa_s, peer, params,
2675 generated_pin);
2676 } else if (config_methods & WPS_CONFIG_KEYPAD)
2677 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2678 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002679 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2680 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002681
2682 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2683 P2P_PROV_DISC_SUCCESS,
2684 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685}
2686
2687
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002688static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689{
2690 struct wpa_supplicant *wpa_s = ctx;
2691 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002692 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002694 if (wpa_s->pending_pd_before_join &&
2695 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2696 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2697 wpa_s->pending_pd_before_join = 0;
2698 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2699 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002700 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002701 return;
2702 }
2703
Dmitry Shmidt04949592012-07-19 12:16:46 -07002704 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002705 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2706 int res;
2707
2708 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2709 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2710 if (os_snprintf_error(sizeof(params), res))
2711 params[sizeof(params) - 1] = '\0';
2712 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002713 params[0] = '\0';
2714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002716 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002718 if (wps_generate_pin(&generated_pin) < 0) {
2719 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2720 wpas_notify_p2p_provision_discovery(
2721 wpa_s, peer, 0 /* response */,
2722 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2723 return;
2724 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002725 wpas_prov_disc_local_display(wpa_s, peer, params,
2726 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002728 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2729 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730
Jouni Malinen75ecf522011-06-27 15:19:46 -07002731 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2732 P2P_PROV_DISC_SUCCESS,
2733 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734}
2735
2736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002737static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002738 enum p2p_prov_disc_status status,
2739 u32 adv_id, const u8 *adv_mac,
2740 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002741{
2742 struct wpa_supplicant *wpa_s = ctx;
2743
Dmitry Shmidt04949592012-07-19 12:16:46 -07002744 if (wpa_s->p2p_fallback_to_go_neg) {
2745 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2746 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002747 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002748 P2P_EVENT_FALLBACK_TO_GO_NEG
2749 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002750 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2751 return;
2752 }
2753
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002754 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002755 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002756 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2757 "join-existing-group operation (no ACK for PD "
2758 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002759 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002760 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002761 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002762
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002763 if (adv_id && adv_mac && deferred_session_resp) {
2764 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2765 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2766 " deferred_session_resp='%s'",
2767 MAC2STR(peer), status, adv_id,
2768 deferred_session_resp);
2769 } else if (adv_id && adv_mac) {
2770 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2771 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2772 MAC2STR(peer), status, adv_id);
2773 } else {
2774 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2775 " p2p_dev_addr=" MACSTR " status=%d",
2776 MAC2STR(peer), status);
2777 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002778
Jouni Malinen75ecf522011-06-27 15:19:46 -07002779 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2780 status, 0, 0);
2781}
2782
2783
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002784static int freq_included(struct wpa_supplicant *wpa_s,
2785 const struct p2p_channels *channels,
2786 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002787{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002788 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2789 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2790 return 1;
2791 return 0;
2792}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002793
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002794
2795static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2796{
2797 unsigned int num = P2P_MAX_CHANNELS;
2798 int *common_freqs;
2799 int ret;
2800
2801 p2p_go_dump_common_freqs(wpa_s);
2802 common_freqs = os_calloc(num, sizeof(int));
2803 if (!common_freqs)
2804 return;
2805
2806 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2807 if (ret < 0) {
2808 wpa_dbg(wpa_s, MSG_DEBUG,
2809 "P2P: Failed to get group common freqs");
2810 os_free(common_freqs);
2811 return;
2812 }
2813
2814 os_free(wpa_s->p2p_group_common_freqs);
2815 wpa_s->p2p_group_common_freqs = common_freqs;
2816 wpa_s->p2p_group_common_freqs_num = num;
2817 p2p_go_dump_common_freqs(wpa_s);
2818}
2819
2820
2821/*
2822 * Check if the given frequency is one of the possible operating frequencies
2823 * set after the completion of the GO Negotiation.
2824 */
2825static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2826{
2827 unsigned int i;
2828
2829 p2p_go_dump_common_freqs(wpa_s);
2830
2831 /* assume no restrictions */
2832 if (!wpa_s->p2p_group_common_freqs_num)
2833 return 1;
2834
2835 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2836 if (wpa_s->p2p_group_common_freqs[i] == freq)
2837 return 1;
2838 }
2839 return 0;
2840}
2841
2842
2843static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2844 struct sta_info *sta, void *ctx)
2845{
2846 int *ecsa_support = ctx;
2847
2848 *ecsa_support &= sta->ecsa_supported;
2849
2850 return 0;
2851}
2852
2853
2854/* Check if all the peers support eCSA */
2855static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2856{
2857 int ecsa_support = 1;
2858
2859 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2860 &ecsa_support);
2861
2862 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002863}
2864
2865
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002866/**
2867 * Pick the best frequency to use from all the currently used frequencies.
2868 */
2869static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2870 struct wpa_used_freq_data *freqs,
2871 unsigned int num)
2872{
2873 unsigned int i, c;
2874
2875 /* find a candidate freq that is supported by P2P */
2876 for (c = 0; c < num; c++)
2877 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2878 break;
2879
2880 if (c == num)
2881 return 0;
2882
2883 /* once we have a candidate, try to find a 'better' one */
2884 for (i = c + 1; i < num; i++) {
2885 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2886 continue;
2887
2888 /*
2889 * 1. Infrastructure station interfaces have higher preference.
2890 * 2. P2P Clients have higher preference.
2891 * 3. All others.
2892 */
2893 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2894 c = i;
2895 break;
2896 }
2897
2898 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2899 c = i;
2900 }
2901 return freqs[c].freq;
2902}
2903
2904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2906 const u8 *go_dev_addr, const u8 *ssid,
2907 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002908 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002909 const struct p2p_channels *channels,
2910 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911{
2912 struct wpa_supplicant *wpa_s = ctx;
2913 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002914 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002916 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917
2918 if (!persistent_group) {
2919 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002920 " to join an active group (SSID: %s)",
2921 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2923 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2924 == 0 ||
2925 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2926 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2927 "authorized invitation");
2928 goto accept_inv;
2929 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002930
2931#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002932 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2933 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002934 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002935 wpa_s->p2p_wps_method = WPS_NFC;
2936 wpa_s->pending_join_wps_method = WPS_NFC;
2937 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002938 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002939 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002940 bssid, ETH_ALEN);
2941 goto accept_inv;
2942 }
2943#endif /* CONFIG_WPS_NFC */
2944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945 /*
2946 * Do not accept the invitation automatically; notify user and
2947 * request approval.
2948 */
2949 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2950 }
2951
2952 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2953 if (grp) {
2954 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2955 "running persistent group");
2956 if (*go)
2957 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2958 goto accept_inv;
2959 }
2960
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002961 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2962 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2963 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2964 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002965 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002966 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2968
2969 for (s = wpa_s->conf->ssid; s; s = s->next) {
2970 if (s->disabled == 2 &&
2971 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2972 s->ssid_len == ssid_len &&
2973 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2974 break;
2975 }
2976
2977 if (!s) {
2978 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2979 " requested reinvocation of an unknown group",
2980 MAC2STR(sa));
2981 return P2P_SC_FAIL_UNKNOWN_GROUP;
2982 }
2983
2984 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2985 *go = 1;
2986 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2987 wpa_printf(MSG_DEBUG, "P2P: The only available "
2988 "interface is already in use - reject "
2989 "invitation");
2990 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2991 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002992 if (wpa_s->p2p_mgmt)
2993 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2994 ETH_ALEN);
2995 else
2996 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 } else if (s->mode == WPAS_MODE_P2P_GO) {
2998 *go = 1;
2999 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3000 {
3001 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3002 "interface address for the group");
3003 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3004 }
3005 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3006 ETH_ALEN);
3007 }
3008
3009accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003010 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3011
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003012 best_freq = 0;
3013 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3014 sizeof(struct wpa_used_freq_data));
3015 if (freqs) {
3016 int num_channels = wpa_s->num_multichan_concurrent;
3017 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3018 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3019 os_free(freqs);
3020 }
3021
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003022 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003023 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003024 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003025 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003026
3027 if (wpa_s->num_multichan_concurrent < 2 ||
3028 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3029 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 -07003030 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 }
3033
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003034 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3035 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003036 if (*go == 0) {
3037 /* We are the client */
3038 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3039 "running a GO but we are capable of MCC, "
3040 "figure out the best channel to use");
3041 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003042 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003043 /* We are the GO, and *force_freq is not in the
3044 * intersection */
3045 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3046 "in intersection but we are capable of MCC, "
3047 "figure out the best channel to use",
3048 *force_freq);
3049 *force_freq = 0;
3050 }
3051 }
3052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 return P2P_SC_SUCCESS;
3054}
3055
3056
3057static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3058 const u8 *ssid, size_t ssid_len,
3059 const u8 *go_dev_addr, u8 status,
3060 int op_freq)
3061{
3062 struct wpa_supplicant *wpa_s = ctx;
3063 struct wpa_ssid *s;
3064
3065 for (s = wpa_s->conf->ssid; s; s = s->next) {
3066 if (s->disabled == 2 &&
3067 s->ssid_len == ssid_len &&
3068 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3069 break;
3070 }
3071
3072 if (status == P2P_SC_SUCCESS) {
3073 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003074 " was accepted; op_freq=%d MHz, SSID=%s",
3075 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003077 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003078 if (go) {
3079 wpa_msg_global(wpa_s, MSG_INFO,
3080 P2P_EVENT_INVITATION_ACCEPTED
3081 "sa=" MACSTR
3082 " persistent=%d freq=%d",
3083 MAC2STR(sa), s->id, op_freq);
3084 } else {
3085 wpa_msg_global(wpa_s, MSG_INFO,
3086 P2P_EVENT_INVITATION_ACCEPTED
3087 "sa=" MACSTR
3088 " persistent=%d",
3089 MAC2STR(sa), s->id);
3090 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 wpas_p2p_group_add_persistent(
Hai Shalom74f70d42019-02-11 14:42:39 -08003092 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003093 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3094 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003096 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003097 wpa_msg_global(wpa_s, MSG_INFO,
3098 P2P_EVENT_INVITATION_ACCEPTED
3099 "sa=" MACSTR " go_dev_addr=" MACSTR
3100 " bssid=" MACSTR " unknown-network",
3101 MAC2STR(sa), MAC2STR(go_dev_addr),
3102 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003104 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003105 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 }
3107 return;
3108 }
3109
3110 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3111 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3112 " was rejected (status %u)", MAC2STR(sa), status);
3113 return;
3114 }
3115
3116 if (!s) {
3117 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003118 wpa_msg_global(wpa_s, MSG_INFO,
3119 P2P_EVENT_INVITATION_RECEIVED
3120 "sa=" MACSTR " go_dev_addr=" MACSTR
3121 " bssid=" MACSTR " unknown-network",
3122 MAC2STR(sa), MAC2STR(go_dev_addr),
3123 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003125 wpa_msg_global(wpa_s, MSG_INFO,
3126 P2P_EVENT_INVITATION_RECEIVED
3127 "sa=" MACSTR " go_dev_addr=" MACSTR
3128 " unknown-network",
3129 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003131 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3132 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 return;
3134 }
3135
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003136 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003137 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3138 "sa=" MACSTR " persistent=%d freq=%d",
3139 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003140 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003141 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3142 "sa=" MACSTR " persistent=%d",
3143 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003144 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003145 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3146 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147}
3148
3149
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003150static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3151 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003152 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003153{
3154 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003155 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003156
3157 if (ssid == NULL)
3158 return;
3159
3160 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003161 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003162 ETH_ALEN) == 0)
3163 break;
3164 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003165 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003166 if (ssid->mode != WPAS_MODE_P2P_GO &&
3167 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3168 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3169 "due to invitation result", ssid->id);
3170 wpas_notify_network_removed(wpa_s, ssid);
3171 wpa_config_remove_network(wpa_s->conf, ssid->id);
3172 return;
3173 }
3174 return; /* Peer not found in client list */
3175 }
3176
3177 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003178 "group %d client list%s",
3179 MAC2STR(peer), ssid->id,
3180 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003181 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3182 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3183 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003184 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003185 if (p2p_wpa_s->conf->update_config &&
3186 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003187 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003188}
3189
3190
3191static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3192 const u8 *peer)
3193{
3194 struct wpa_ssid *ssid;
3195
3196 wpa_s = wpa_s->global->p2p_invite_group;
3197 if (wpa_s == NULL)
3198 return; /* No known invitation group */
3199 ssid = wpa_s->current_ssid;
3200 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3201 !ssid->p2p_persistent_group)
3202 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003203 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003204 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003205 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003206}
3207
3208
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003209static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003210 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003211 const u8 *peer, int neg_freq,
3212 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213{
3214 struct wpa_supplicant *wpa_s = ctx;
3215 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003216 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217
3218 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003219 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3220 "status=%d " MACSTR,
3221 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003223 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3224 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 }
3226 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3227
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003228 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3229 status, MAC2STR(peer));
3230 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003231 struct wpa_supplicant *group_if =
3232 wpa_s->global->p2p_invite_group;
3233
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003234 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3235 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003236
3237 /*
3238 * Invitation to an active group. If this is successful and we
3239 * are the GO, set the client wait to postpone some concurrent
3240 * operations and to allow provisioning and connection to happen
3241 * more quickly.
3242 */
3243 if (status == P2P_SC_SUCCESS &&
3244 group_if && group_if->current_ssid &&
3245 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3246 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3247#ifdef CONFIG_TESTING_OPTIONS
3248 if (group_if->p2p_go_csa_on_inv) {
3249 wpa_printf(MSG_DEBUG,
3250 "Testing: force P2P GO CSA after invitation");
3251 eloop_cancel_timeout(
3252 wpas_p2p_reconsider_moving_go,
3253 wpa_s, NULL);
3254 eloop_register_timeout(
3255 0, 50000,
3256 wpas_p2p_reconsider_moving_go,
3257 wpa_s, NULL);
3258 }
3259#endif /* CONFIG_TESTING_OPTIONS */
3260 }
3261 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003262 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003264 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3265 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3266 "invitation exchange to indicate readiness for "
3267 "re-invocation");
3268 }
3269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003271 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3272 ssid = wpa_config_get_network(
3273 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003274 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003275 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276 wpas_p2p_remove_pending_group_interface(wpa_s);
3277 return;
3278 }
3279
3280 ssid = wpa_config_get_network(wpa_s->conf,
3281 wpa_s->pending_invite_ssid_id);
3282 if (ssid == NULL) {
3283 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3284 "data matching with invitation");
3285 return;
3286 }
3287
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003288 /*
3289 * The peer could have missed our ctrl::ack frame for Invitation
3290 * Response and continue retransmitting the frame. To reduce the
3291 * likelihood of the peer not getting successful TX status for the
3292 * Invitation Response frame, wait a short time here before starting
3293 * the persistent group so that we will remain on the current channel to
3294 * acknowledge any possible retransmission from the peer.
3295 */
3296 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3297 "starting persistent group");
3298 os_sleep(0, 50000);
3299
Dmitry Shmidt15907092014-03-25 10:42:57 -07003300 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003301 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003302 freq = neg_freq;
3303 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003304 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003305 freq = peer_oper_freq;
3306 else
3307 freq = 0;
3308
3309 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3310 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003312 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003313 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003314 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003315 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003316 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003317 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003318 wpa_s->p2p_go_he,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003319 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003320 ssid->mode == WPAS_MODE_P2P_GO ?
3321 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003322 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323}
3324
3325
Dmitry Shmidt04949592012-07-19 12:16:46 -07003326static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3327 unsigned int freq)
3328{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003329 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3330 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003331 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003332}
3333
3334
3335static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3336{
3337 reg->channel[reg->channels] = chan;
3338 reg->channels++;
3339}
3340
3341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003343 struct p2p_channels *chan,
3344 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345{
3346 int i, cla = 0;
3347
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003348 wpa_s->global->p2p_24ghz_social_channels = 1;
3349
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003350 os_memset(cli_chan, 0, sizeof(*cli_chan));
3351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003352 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3353 "band");
3354
3355 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3356 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003357 chan->reg_class[cla].channels = 0;
3358 for (i = 0; i < 11; i++) {
3359 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3360 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3361 }
3362 if (chan->reg_class[cla].channels)
3363 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364
3365 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3366 "band");
3367
3368 /* Operating class 115 - 5 GHz, channels 36-48 */
3369 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003370 chan->reg_class[cla].channels = 0;
3371 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3372 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3373 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3374 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3375 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3376 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3377 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3378 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3379 if (chan->reg_class[cla].channels)
3380 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003381
3382 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3383 "band");
3384
3385 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3386 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003387 chan->reg_class[cla].channels = 0;
3388 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3389 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3390 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3391 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3392 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3393 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3394 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3395 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3396 if (chan->reg_class[cla].channels)
3397 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398
3399 chan->reg_classes = cla;
3400 return 0;
3401}
3402
3403
Dmitry Shmidt04949592012-07-19 12:16:46 -07003404static int has_channel(struct wpa_global *global,
3405 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406{
3407 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003408 unsigned int freq;
3409
3410 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3411 chan * 5;
3412 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003413 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414
3415 for (i = 0; i < mode->num_channels; i++) {
3416 if (mode->channels[i].chan == chan) {
3417 if (flags)
3418 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003419 if (mode->channels[i].flag &
3420 (HOSTAPD_CHAN_DISABLED |
3421 HOSTAPD_CHAN_RADAR))
3422 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003423 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3424 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003425 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 }
3427 }
3428
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003429 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430}
3431
3432
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003433static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3434 struct hostapd_hw_modes *mode,
3435 u8 channel)
3436{
3437 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003438 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003439
3440 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3441 return 0;
3442
3443 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3444 /*
3445 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3446 * so the center channel is 6 channels away from the start/end.
3447 */
3448 if (channel >= center_channels[i] - 6 &&
3449 channel <= center_channels[i] + 6)
3450 return center_channels[i];
3451
3452 return 0;
3453}
3454
3455
3456static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3457 struct hostapd_hw_modes *mode,
3458 u8 channel, u8 bw)
3459{
3460 u8 center_chan;
3461 int i, flags;
3462 enum chan_allowed res, ret = ALLOWED;
3463
3464 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3465 if (!center_chan)
3466 return NOT_ALLOWED;
3467 if (center_chan >= 58 && center_chan <= 138)
3468 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3469
3470 /* check all the channels are available */
3471 for (i = 0; i < 4; i++) {
3472 int adj_chan = center_chan - 6 + i * 4;
3473
3474 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3475 if (res == NOT_ALLOWED)
3476 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003477 if (res == NO_IR)
3478 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003479
3480 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3481 return NOT_ALLOWED;
3482 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3483 return NOT_ALLOWED;
3484 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3485 return NOT_ALLOWED;
3486 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3487 return NOT_ALLOWED;
3488 }
3489
3490 return ret;
3491}
3492
3493
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003494static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3495 struct hostapd_hw_modes *mode,
3496 u8 channel)
3497{
3498 u8 center_channels[] = { 50, 114 };
3499 unsigned int i;
3500
3501 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3502 return 0;
3503
3504 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3505 /*
3506 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3507 * so the center channel is 14 channels away from the start/end.
3508 */
3509 if (channel >= center_channels[i] - 14 &&
3510 channel <= center_channels[i] + 14)
3511 return center_channels[i];
3512
3513 return 0;
3514}
3515
3516
3517static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3518 struct hostapd_hw_modes *mode,
3519 u8 channel, u8 bw)
3520{
3521 u8 center_chan;
3522 int i, flags;
3523 enum chan_allowed res, ret = ALLOWED;
3524
3525 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3526 if (!center_chan)
3527 return NOT_ALLOWED;
3528 /* VHT 160 MHz uses DFS channels in most countries. */
3529
3530 /* Check all the channels are available */
3531 for (i = 0; i < 8; i++) {
3532 int adj_chan = center_chan - 14 + i * 4;
3533
3534 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3535 if (res == NOT_ALLOWED)
3536 return NOT_ALLOWED;
3537
3538 if (res == NO_IR)
3539 ret = NO_IR;
3540
3541 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3542 return NOT_ALLOWED;
3543 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3544 return NOT_ALLOWED;
3545 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3546 return NOT_ALLOWED;
3547 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3548 return NOT_ALLOWED;
3549 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3550 return NOT_ALLOWED;
3551 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3552 return NOT_ALLOWED;
3553 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3554 return NOT_ALLOWED;
3555 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3556 return NOT_ALLOWED;
3557 }
3558
3559 return ret;
3560}
3561
3562
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003563static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3564 struct hostapd_hw_modes *mode,
3565 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003566{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003567 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003568 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003569
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003570 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3571 if (bw == BW40MINUS) {
3572 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3573 return NOT_ALLOWED;
3574 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3575 } else if (bw == BW40PLUS) {
3576 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3577 return NOT_ALLOWED;
3578 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3579 } else if (bw == BW80) {
3580 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003581 } else if (bw == BW160) {
3582 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003583 }
3584
3585 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3586 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003587 if (res == NO_IR || res2 == NO_IR)
3588 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003589 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003590}
3591
3592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003594 struct p2p_channels *chan,
3595 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003597 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003598 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003600 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3602 "of all supported channels; assume dualband "
3603 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003604 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 }
3606
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003607 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003609 for (op = 0; global_op_class[op].op_class; op++) {
3610 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003612 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003614 if (o->p2p == NO_P2P_SUPP)
3615 continue;
3616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003617 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 if (mode == NULL)
3619 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003620 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3621 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003623 enum chan_allowed res;
3624 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3625 if (res == ALLOWED) {
3626 if (reg == NULL) {
3627 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3628 o->op_class);
3629 reg = &chan->reg_class[cla];
3630 cla++;
3631 reg->reg_class = o->op_class;
3632 }
3633 reg->channel[reg->channels] = ch;
3634 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003635 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003636 wpa_s->conf->p2p_add_cli_chan) {
3637 if (cli_reg == NULL) {
3638 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3639 o->op_class);
3640 cli_reg = &cli_chan->reg_class[cli_cla];
3641 cli_cla++;
3642 cli_reg->reg_class = o->op_class;
3643 }
3644 cli_reg->channel[cli_reg->channels] = ch;
3645 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647 }
3648 if (reg) {
3649 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3650 reg->channel, reg->channels);
3651 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003652 if (cli_reg) {
3653 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3654 cli_reg->channel, cli_reg->channels);
3655 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656 }
3657
3658 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003659 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003661 return 0;
3662}
3663
3664
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003665int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3666 struct hostapd_hw_modes *mode, u8 channel)
3667{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003668 int op;
3669 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003670
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003671 for (op = 0; global_op_class[op].op_class; op++) {
3672 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003673 u8 ch;
3674
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003675 if (o->p2p == NO_P2P_SUPP)
3676 continue;
3677
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003678 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3679 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003680 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3681 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003682 continue;
3683 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003684 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003685 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003686 }
3687 }
3688 return 0;
3689}
3690
3691
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003692int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3693 struct hostapd_hw_modes *mode, u8 channel)
3694{
3695 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3696 return 0;
3697
3698 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3699}
3700
3701
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003702int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3703 struct hostapd_hw_modes *mode, u8 channel)
3704{
3705 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3706 return 0;
3707 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3708}
3709
3710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3712 size_t buf_len)
3713{
3714 struct wpa_supplicant *wpa_s = ctx;
3715
3716 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3717 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3718 break;
3719 }
3720 if (wpa_s == NULL)
3721 return -1;
3722
3723 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3724}
3725
3726
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003727struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3728 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003729{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003730 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3731 struct wpa_ssid *s = wpa_s->current_ssid;
3732 if (s == NULL)
3733 continue;
3734 if (s->mode != WPAS_MODE_P2P_GO &&
3735 s->mode != WPAS_MODE_AP &&
3736 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3737 continue;
3738 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003739 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003740 continue;
3741 return wpa_s;
3742 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003743
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003744 return NULL;
3745
3746}
3747
3748
3749struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3750 const u8 *peer_dev_addr)
3751{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003752 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3753 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003754 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003755 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003756 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3757 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003758 }
3759
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003760 return NULL;
3761}
3762
3763
3764static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3765{
3766 struct wpa_supplicant *wpa_s = ctx;
3767
3768 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003769}
3770
3771
Dmitry Shmidt18463232014-01-24 12:29:41 -08003772static int wpas_is_concurrent_session_active(void *ctx)
3773{
3774 struct wpa_supplicant *wpa_s = ctx;
3775 struct wpa_supplicant *ifs;
3776
3777 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3778 if (ifs == wpa_s)
3779 continue;
3780 if (ifs->wpa_state > WPA_ASSOCIATED)
3781 return 1;
3782 }
3783 return 0;
3784}
3785
3786
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003787static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3788{
3789 struct wpa_supplicant *wpa_s = ctx;
3790 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3791}
3792
3793
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003794int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3795 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003796{
3797 struct wpa_interface iface;
3798 struct wpa_supplicant *p2pdev_wpa_s;
3799 char ifname[100];
3800 char force_name[100];
3801 int ret;
3802
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003803 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3804 wpa_s->ifname);
3805 if (os_snprintf_error(sizeof(ifname), ret))
3806 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003807 force_name[0] = '\0';
3808 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3809 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3810 force_name, wpa_s->pending_interface_addr, NULL);
3811 if (ret < 0) {
3812 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3813 return ret;
3814 }
3815 os_strlcpy(wpa_s->pending_interface_name, ifname,
3816 sizeof(wpa_s->pending_interface_name));
3817
3818 os_memset(&iface, 0, sizeof(iface));
3819 iface.p2p_mgmt = 1;
3820 iface.ifname = wpa_s->pending_interface_name;
3821 iface.driver = wpa_s->driver->name;
3822 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003823
3824 /*
3825 * If a P2P Device configuration file was given, use it as the interface
3826 * configuration file (instead of using parent's configuration file.
3827 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003828 if (conf_p2p_dev) {
3829 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003830 iface.ctrl_interface = NULL;
3831 } else {
3832 iface.confname = wpa_s->confname;
3833 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3834 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003835
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003836 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003837 if (!p2pdev_wpa_s) {
3838 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3839 return -1;
3840 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003841
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003842 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003843 wpa_s->pending_interface_name[0] = '\0';
3844 return 0;
3845}
3846
3847
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003848static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3849 const u8 *noa, size_t noa_len)
3850{
3851 struct wpa_supplicant *wpa_s, *intf = ctx;
3852 char hex[100];
3853
3854 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3855 if (wpa_s->waiting_presence_resp)
3856 break;
3857 }
3858 if (!wpa_s) {
3859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3860 return;
3861 }
3862 wpa_s->waiting_presence_resp = 0;
3863
3864 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3865 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3866 " status=%u noa=%s", MAC2STR(src), status, hex);
3867}
3868
3869
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003870static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3871 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003872 u8 *ret_ssid, size_t *ret_ssid_len,
3873 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003874{
3875 struct wpa_supplicant *wpa_s = ctx;
3876 struct wpa_ssid *s;
3877
3878 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3879 if (s) {
3880 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3881 *ret_ssid_len = s->ssid_len;
3882 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003883
3884 if (s->mode != WPAS_MODE_P2P_GO) {
3885 os_memset(intended_iface_addr, 0, ETH_ALEN);
3886 } else if (wpas_p2p_create_iface(wpa_s)) {
3887 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3888 return 0;
3889
3890 os_memcpy(intended_iface_addr,
3891 wpa_s->pending_interface_addr, ETH_ALEN);
3892 } else {
3893 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3894 ETH_ALEN);
3895 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003896 return 1;
3897 }
3898
3899 return 0;
3900}
3901
3902
3903static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003904 u8 *ssid, size_t *ssid_len, int *group_iface,
3905 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003906{
3907 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003908 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003909 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003910
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003911 /*
3912 * group_iface will be set to 1 only if a dedicated interface for P2P
3913 * role is required. First, we try to reuse an active GO. However,
3914 * if it is not present, we will try to reactivate an existing
3915 * persistent group and set group_iface to 1, so the caller will know
3916 * that the pending interface should be used.
3917 */
3918 *group_iface = 0;
3919
3920 if (freq)
3921 *freq = 0;
3922
3923 go = wpas_p2p_get_go_group(wpa_s);
3924 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003925 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003926 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003927 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003928 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3929 else
3930 return 0;
3931 } else {
3932 s = go->current_ssid;
3933 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3934 if (freq)
3935 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003936 }
3937
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003938 os_memcpy(ssid, s->ssid, s->ssid_len);
3939 *ssid_len = s->ssid_len;
3940
3941 return 1;
3942}
3943
3944
3945static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3946 const u8 *ssid, size_t ssid_len)
3947{
3948 struct wpa_supplicant *wpa_s = ctx;
3949 struct wpa_ssid *s;
3950 int save_config = 0;
3951 size_t i;
3952
3953 /* Start with our first choice of Persistent Groups */
3954 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3955 if (go && ssid && ssid_len &&
3956 s->ssid_len == ssid_len &&
3957 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3958 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3959 break;
3960
3961 /* Remove stale persistent group */
3962 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003963 wpa_dbg(wpa_s, MSG_DEBUG,
3964 "P2P: Remove stale persistent group id=%d",
3965 s->id);
3966 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003967 wpa_config_remove_network(wpa_s->conf, s->id);
3968 save_config = 1;
3969 continue;
3970 }
3971
3972 for (i = 0; i < s->num_p2p_clients; i++) {
3973 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3974 peer, ETH_ALEN) != 0)
3975 continue;
3976
3977 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3978 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3979 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3980 break;
3981 }
3982 s->num_p2p_clients--;
3983 save_config = 1;
3984 }
3985
3986 if (save_config)
3987 p2p_config_write(wpa_s);
3988
3989 /* Return TRUE if valid SSID remains */
3990 return s != NULL;
3991}
3992
3993
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003994static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3995 const u8 *feat_cap, size_t feat_cap_len)
3996{
3997 static const char pref[] = " feature_cap=";
3998 int ret;
3999
4000 buf[0] = '\0';
4001
4002 /*
4003 * We expect a feature capability to contain at least one byte to be
4004 * reported. The string buffer provided by the caller function is
4005 * expected to be big enough to contain all bytes of the attribute for
4006 * known specifications. This function truncates the reported bytes if
4007 * the feature capability data exceeds the string buffer size.
4008 */
4009 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4010 return;
4011
4012 os_memcpy(buf, pref, sizeof(pref));
4013 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4014 buf_len - sizeof(pref) + 1,
4015 feat_cap, feat_cap_len);
4016
4017 if (ret != (2 * (int) feat_cap_len))
4018 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4019}
4020
4021
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004022static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4023 const u8 *adv_mac, const u8 *ses_mac,
4024 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4025 u8 conncap, int passwd_id,
4026 const u8 *persist_ssid,
4027 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004028 int prov_start, const char *session_info,
4029 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004030 unsigned int freq,
4031 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004032{
4033 struct wpa_supplicant *wpa_s = ctx;
4034 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004035 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004036 int save_config = 0;
4037 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004038 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004039
4040 if (!dev)
4041 return;
4042
4043 os_memset(mac, 0, ETH_ALEN);
4044 if (!adv_mac)
4045 adv_mac = mac;
4046 if (!ses_mac)
4047 ses_mac = mac;
4048 if (!grp_mac)
4049 grp_mac = mac;
4050
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004051 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4052 feat_cap, feat_cap_len);
4053
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004054 if (prov_start) {
4055 if (session_info == NULL) {
4056 wpa_msg_global(wpa_s, MSG_INFO,
4057 P2P_EVENT_P2PS_PROVISION_START MACSTR
4058 " adv_id=%x conncap=%x"
4059 " adv_mac=" MACSTR
4060 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004061 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004062 MAC2STR(dev), adv_id, conncap,
4063 MAC2STR(adv_mac),
4064 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004065 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004066 } else {
4067 wpa_msg_global(wpa_s, MSG_INFO,
4068 P2P_EVENT_P2PS_PROVISION_START MACSTR
4069 " adv_id=%x conncap=%x"
4070 " adv_mac=" MACSTR
4071 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004072 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004073 MAC2STR(dev), adv_id, conncap,
4074 MAC2STR(adv_mac),
4075 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004076 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004077 }
4078 return;
4079 }
4080
4081 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4082 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4083
4084 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4085 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4086 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4087
4088 if (persistent_go && !persistent_go->num_p2p_clients) {
4089 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004090 wpa_dbg(wpa_s, MSG_DEBUG,
4091 "P2P: Remove empty persistent group id=%d",
4092 persistent_go->id);
4093 wpas_notify_persistent_group_removed(wpa_s,
4094 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004095 wpa_config_remove_network(wpa_s->conf,
4096 persistent_go->id);
4097 }
4098
4099 wpa_msg_global(wpa_s, MSG_INFO,
4100 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4101 " status=%d"
4102 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004103 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004104 MAC2STR(dev), status,
4105 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004106 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004107 return;
4108 }
4109
4110 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004111 if (persist_ssid && persist_ssid_size)
4112 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4113 persist_ssid_size);
4114
4115 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4116 is_zero_ether_addr(grp_mac)) {
4117 wpa_dbg(wpa_s, MSG_ERROR,
4118 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4119 return;
4120 }
4121
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004122 for (;;) {
4123 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4124 if (!stale)
4125 break;
4126
4127 if (s && s->ssid_len == stale->ssid_len &&
4128 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4129 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4130 break;
4131
4132 /* Remove stale persistent group */
4133 if (stale->mode != WPAS_MODE_P2P_GO ||
4134 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004135 wpa_dbg(wpa_s, MSG_DEBUG,
4136 "P2P: Remove stale persistent group id=%d",
4137 stale->id);
4138 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004139 wpa_config_remove_network(wpa_s->conf, stale->id);
4140 } else {
4141 size_t i;
4142
4143 for (i = 0; i < stale->num_p2p_clients; i++) {
4144 if (os_memcmp(stale->p2p_client_list +
4145 i * ETH_ALEN,
4146 dev, ETH_ALEN) == 0) {
4147 os_memmove(stale->p2p_client_list +
4148 i * ETH_ALEN,
4149 stale->p2p_client_list +
4150 (i + 1) * ETH_ALEN,
4151 (stale->num_p2p_clients -
4152 i - 1) * ETH_ALEN);
4153 break;
4154 }
4155 }
4156 stale->num_p2p_clients--;
4157 }
4158 save_config = 1;
4159 }
4160
4161 if (save_config)
4162 p2p_config_write(wpa_s);
4163
4164 if (s) {
4165 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4166 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4167
4168 if (persistent_go && s != persistent_go &&
4169 !persistent_go->num_p2p_clients) {
4170 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004171 wpa_dbg(wpa_s, MSG_DEBUG,
4172 "P2P: Remove empty persistent group id=%d",
4173 persistent_go->id);
4174 wpas_notify_persistent_group_removed(wpa_s,
4175 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004176 wpa_config_remove_network(wpa_s->conf,
4177 persistent_go->id);
4178 /* Save config */
4179 }
4180
4181 wpa_msg_global(wpa_s, MSG_INFO,
4182 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4183 " status=%d"
4184 " adv_id=%x adv_mac=" MACSTR
4185 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004186 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004187 MAC2STR(dev), status,
4188 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004189 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004190 return;
4191 }
4192
4193 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004194 /*
4195 * We need to copy the interface name. Simply saving a
4196 * pointer isn't enough, since if we use pending_interface_name
4197 * it will be overwritten when the group is added.
4198 */
4199 char go_ifname[100];
4200
4201 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004202 if (!go_wpa_s) {
4203 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004204 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004205
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004206 if (!wpas_p2p_create_iface(wpa_s))
4207 os_memcpy(go_ifname, wpa_s->ifname,
4208 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004209 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004210 os_memcpy(go_ifname,
4211 wpa_s->pending_interface_name,
4212 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004213
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004214 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004215 wpas_p2ps_prov_complete(
4216 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4217 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004218 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004219 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4220 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004221 return;
4222 }
4223
4224 /* If PD Resp complete, start up the GO */
4225 if (response_done && persistent_go) {
4226 wpas_p2p_group_add_persistent(
4227 wpa_s, persistent_go,
Hai Shalom74f70d42019-02-11 14:42:39 -08004228 0, 0, freq, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004229 persistent_go->mode ==
4230 WPAS_MODE_P2P_GO ?
4231 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004232 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004233 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004234 wpas_p2p_group_add(wpa_s, 1, freq,
4235 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004236 }
4237
4238 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004239 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4240 ETH_ALEN);
4241 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004242 }
4243 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004244 os_memcpy(go_ifname, go_wpa_s->ifname,
4245 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004246
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004247 if (is_zero_ether_addr(grp_mac)) {
4248 wpa_dbg(go_wpa_s, MSG_DEBUG,
4249 "P2P: Setting PIN-1 for ANY");
4250 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4251 "12345670", NULL, 0,
4252 0);
4253 } else {
4254 wpa_dbg(go_wpa_s, MSG_DEBUG,
4255 "P2P: Setting PIN-1 for " MACSTR,
4256 MAC2STR(grp_mac));
4257 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4258 "12345670", NULL, 0,
4259 0);
4260 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004261
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004262 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4263 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004264 }
4265
4266 wpa_msg_global(wpa_s, MSG_INFO,
4267 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4268 " status=%d conncap=%x"
4269 " adv_id=%x adv_mac=" MACSTR
4270 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004271 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004272 MAC2STR(dev), status, conncap,
4273 adv_id, MAC2STR(adv_mac),
4274 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004275 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004276 return;
4277 }
4278
4279 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4280 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4281
4282 if (persistent_go && !persistent_go->num_p2p_clients) {
4283 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004284 wpa_dbg(wpa_s, MSG_DEBUG,
4285 "P2P: Remove empty persistent group id=%d",
4286 persistent_go->id);
4287 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004288 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4289 }
4290
4291 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004292 char ssid_hex[32 * 2 + 1];
4293
4294 if (group_ssid)
4295 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4296 group_ssid, group_ssid_len);
4297 else
4298 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004299 wpa_msg_global(wpa_s, MSG_INFO,
4300 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4301 " status=%d conncap=%x"
4302 " adv_id=%x adv_mac=" MACSTR
4303 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004304 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004305 MAC2STR(dev), status, conncap,
4306 adv_id, MAC2STR(adv_mac),
4307 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004308 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4309 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004310 } else {
4311 wpa_msg_global(wpa_s, MSG_INFO,
4312 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4313 " status=%d conncap=%x"
4314 " adv_id=%x adv_mac=" MACSTR
4315 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004316 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004317 MAC2STR(dev), status, conncap,
4318 adv_id, MAC2STR(adv_mac),
4319 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004320 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004321 }
4322}
4323
4324
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004325static int _wpas_p2p_in_progress(void *ctx)
4326{
4327 struct wpa_supplicant *wpa_s = ctx;
4328 return wpas_p2p_in_progress(wpa_s);
4329}
4330
4331
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004332static int wpas_prov_disc_resp_cb(void *ctx)
4333{
4334 struct wpa_supplicant *wpa_s = ctx;
4335 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004336 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004337
4338 if (!wpa_s->global->pending_p2ps_group)
4339 return 0;
4340
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004341 freq = wpa_s->global->pending_p2ps_group_freq;
4342 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004343 wpa_s->global->pending_p2ps_group = 0;
4344
4345 if (wpas_p2p_get_go_group(wpa_s))
4346 return 0;
4347 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4348
4349 if (persistent_go) {
4350 wpas_p2p_group_add_persistent(
Hai Shalom74f70d42019-02-11 14:42:39 -08004351 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004352 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004353 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004354 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08004355 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004356 }
4357
4358 return 1;
4359}
4360
4361
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004362static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4363 unsigned int *len,
4364 unsigned int *freq_list)
4365{
4366 struct wpa_supplicant *wpa_s = ctx;
4367
4368 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4369 WPA_IF_P2P_CLIENT, len, freq_list);
4370}
4371
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004372int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4373{
4374 u8 addr[ETH_ALEN] = {0};
4375
4376 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4377 return 0;
4378
4379 if (wpa_s->conf->ssid == NULL) {
4380 if (random_mac_addr(addr) < 0) {
4381 wpa_msg(wpa_s, MSG_INFO,
4382 "Failed to generate random MAC address");
4383 return -EINVAL;
4384 }
4385
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004386 /* Store generated MAC address. */
4387 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4388 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004389 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004390 /* If there are existing saved groups, restore last MAC address.
4391 * if there is no last used MAC address, the last one is
4392 * factory MAC. */
4393 if (is_zero_ether_addr(
4394 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004395 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004396 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4397 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004398 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4399 }
4400
4401 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4402 wpa_msg(wpa_s, MSG_INFO,
4403 "Failed to set random MAC address");
4404 return -EINVAL;
4405 }
4406
4407 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4408 wpa_msg(wpa_s, MSG_INFO,
4409 "Could not update MAC address information");
4410 return -EINVAL;
4411 }
4412
4413 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4414 MAC2STR(addr));
4415
4416 return 0;
4417}
4418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419/**
4420 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4421 * @global: Pointer to global data from wpa_supplicant_init()
4422 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4423 * Returns: 0 on success, -1 on failure
4424 */
4425int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4426{
4427 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 int i;
4429
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004430 if (wpa_s->conf->p2p_disabled)
4431 return 0;
4432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4434 return 0;
4435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436 if (global->p2p)
4437 return 0;
4438
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004439 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4440 wpa_msg(wpa_s, MSG_ERROR,
4441 "Failed to initialize P2P random MAC address.");
4442 return -1;
4443 }
4444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004447 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 p2p.p2p_scan = wpas_p2p_scan;
4449 p2p.send_action = wpas_send_action;
4450 p2p.send_action_done = wpas_send_action_done;
4451 p2p.go_neg_completed = wpas_go_neg_completed;
4452 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4453 p2p.dev_found = wpas_dev_found;
4454 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004455 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004456 p2p.start_listen = wpas_start_listen;
4457 p2p.stop_listen = wpas_stop_listen;
4458 p2p.send_probe_resp = wpas_send_probe_resp;
4459 p2p.sd_request = wpas_sd_request;
4460 p2p.sd_response = wpas_sd_response;
4461 p2p.prov_disc_req = wpas_prov_disc_req;
4462 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004463 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 p2p.invitation_process = wpas_invitation_process;
4465 p2p.invitation_received = wpas_invitation_received;
4466 p2p.invitation_result = wpas_invitation_result;
4467 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004468 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004469 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004470 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004471 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004472 p2p.get_persistent_group = wpas_get_persistent_group;
4473 p2p.get_go_info = wpas_get_go_info;
4474 p2p.remove_stale_groups = wpas_remove_stale_groups;
4475 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4476 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4477 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004478 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479
4480 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004481 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 p2p.dev_name = wpa_s->conf->device_name;
4483 p2p.manufacturer = wpa_s->conf->manufacturer;
4484 p2p.model_name = wpa_s->conf->model_name;
4485 p2p.model_number = wpa_s->conf->model_number;
4486 p2p.serial_number = wpa_s->conf->serial_number;
4487 if (wpa_s->wps) {
4488 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4489 p2p.config_methods = wpa_s->wps->config_methods;
4490 }
4491
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004492 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4493 wpa_printf(MSG_ERROR,
4494 "P2P: Failed to configure supported channel list");
4495 return -1;
4496 }
4497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 if (wpa_s->conf->p2p_listen_reg_class &&
4499 wpa_s->conf->p2p_listen_channel) {
4500 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4501 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004502 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 /*
4505 * Pick one of the social channels randomly as the listen
4506 * channel.
4507 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004508 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004509 &p2p.channel,
4510 &global->p2p_go_avoid_freq,
4511 &global->p2p_disallow_freq) !=
4512 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004513 wpa_printf(MSG_INFO,
4514 "P2P: No social channels supported by the driver - do not enable P2P");
4515 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004516 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004517 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004519 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4520 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521
4522 if (wpa_s->conf->p2p_oper_reg_class &&
4523 wpa_s->conf->p2p_oper_channel) {
4524 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4525 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4526 p2p.cfg_op_channel = 1;
4527 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4528 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4529
4530 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004532 * Use random operation channel from 2.4 GHz band social
4533 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4534 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004536 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004537 &p2p.op_channel, NULL,
4538 NULL) != 0) {
4539 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004540 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004541 p2p.op_reg_class = 0;
4542 p2p.op_channel = 0;
4543 /* This will be overridden during group setup in
4544 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546 p2p.cfg_op_channel = 0;
4547 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4548 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4549 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004550
4551 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4552 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4553 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4554 }
4555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4557 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4558 p2p.country[2] = 0x04;
4559 } else
4560 os_memcpy(p2p.country, "XX\x04", 3);
4561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4563 WPS_DEV_TYPE_LEN);
4564
4565 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4566 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4567 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4568
4569 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4570 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4571
4572 p2p.max_peers = 100;
4573
4574 if (wpa_s->conf->p2p_ssid_postfix) {
4575 p2p.ssid_postfix_len =
4576 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4577 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4578 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4579 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4580 p2p.ssid_postfix_len);
4581 }
4582
4583 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4584
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004585 p2p.max_listen = wpa_s->max_remain_on_chan;
4586
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004587 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4588 wpa_s->conf->p2p_passphrase_len <= 63)
4589 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4590 else
4591 p2p.passphrase_len = 8;
4592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 global->p2p = p2p_init(&p2p);
4594 if (global->p2p == NULL)
4595 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004596 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597
4598 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4599 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4600 continue;
4601 p2p_add_wps_vendor_extension(
4602 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4603 }
4604
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004605 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 return 0;
4608}
4609
4610
4611/**
4612 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4613 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4614 *
4615 * This function deinitialize per-interface P2P data.
4616 */
4617void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4618{
4619 if (wpa_s->driver && wpa_s->drv_priv)
4620 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004621
4622 if (wpa_s->go_params) {
4623 /* Clear any stored provisioning info */
4624 p2p_clear_provisioning_info(
4625 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004626 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004627 }
4628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 os_free(wpa_s->go_params);
4630 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004631 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4633 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4634 wpa_s->p2p_long_listen = 0;
4635 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4636 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4637 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004638 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004639 wpas_p2p_listen_work_done(wpa_s);
4640 if (wpa_s->p2p_send_action_work) {
4641 os_free(wpa_s->p2p_send_action_work->ctx);
4642 radio_work_done(wpa_s->p2p_send_action_work);
4643 wpa_s->p2p_send_action_work = NULL;
4644 }
4645 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004647 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4648 wpa_s->p2p_oob_dev_pw = NULL;
4649
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004650 os_free(wpa_s->p2p_group_common_freqs);
4651 wpa_s->p2p_group_common_freqs = NULL;
4652 wpa_s->p2p_group_common_freqs_num = 0;
4653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654 /* TODO: remove group interface from the driver if this wpa_s instance
4655 * is on top of a P2P group interface */
4656}
4657
4658
4659/**
4660 * wpas_p2p_deinit_global - Deinitialize global P2P module
4661 * @global: Pointer to global data from wpa_supplicant_init()
4662 *
4663 * This function deinitializes the global (per device) P2P module.
4664 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004665static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666{
4667 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004669 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004670
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004671 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672
4673 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4675 wpa_s = wpa_s->next;
4676 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677 tmp = global->ifaces;
4678 while (tmp &&
4679 (tmp == wpa_s ||
4680 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4681 tmp = tmp->next;
4682 }
4683 if (tmp == NULL)
4684 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004685 /* Disconnect from the P2P group and deinit the interface */
4686 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687 }
4688
4689 /*
4690 * Deinit GO data on any possibly remaining interface (if main
4691 * interface is used as GO).
4692 */
4693 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4694 if (wpa_s->ap_iface)
4695 wpas_p2p_group_deinit(wpa_s);
4696 }
4697
4698 p2p_deinit(global->p2p);
4699 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004700 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701}
4702
4703
4704static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4705{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004706 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004707 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708 if (wpa_s->drv_flags &
4709 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4710 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4711 return 1; /* P2P group requires a new interface in every case
4712 */
4713 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4714 return 0; /* driver does not support concurrent operations */
4715 if (wpa_s->global->ifaces->next)
4716 return 1; /* more that one interface already in use */
4717 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4718 return 1; /* this interface is already in use */
4719 return 0;
4720}
4721
4722
4723static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4724 const u8 *peer_addr,
4725 enum p2p_wps_method wps_method,
4726 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004727 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004728 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004730 if (persistent_group && wpa_s->conf->persistent_reconnect)
4731 persistent_group = 2;
4732
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004733 /*
4734 * Increase GO config timeout if HT40 is used since it takes some time
4735 * to scan channels for coex purposes before the BSS can be started.
4736 */
4737 p2p_set_config_timeout(wpa_s->global->p2p,
4738 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4741 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742 persistent_group, ssid ? ssid->ssid : NULL,
4743 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004744 wpa_s->p2p_pd_before_go_neg, pref_freq,
4745 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4746 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747}
4748
4749
4750static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4751 const u8 *peer_addr,
4752 enum p2p_wps_method wps_method,
4753 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004754 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004755 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004757 if (persistent_group && wpa_s->conf->persistent_reconnect)
4758 persistent_group = 2;
4759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4761 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004762 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004763 ssid ? ssid->ssid_len : 0, pref_freq,
4764 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4765 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004766}
4767
4768
4769static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4770{
4771 wpa_s->p2p_join_scan_count++;
4772 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4773 wpa_s->p2p_join_scan_count);
4774 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4775 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4776 " for join operationg - stop join attempt",
4777 MAC2STR(wpa_s->pending_join_iface_addr));
4778 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004779 if (wpa_s->p2p_auto_pd) {
4780 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004781 wpa_msg_global(wpa_s, MSG_INFO,
4782 P2P_EVENT_PROV_DISC_FAILURE
4783 " p2p_dev_addr=" MACSTR " status=N/A",
4784 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004785 return;
4786 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004787 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004788 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004789 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 }
4791}
4792
4793
Dmitry Shmidt04949592012-07-19 12:16:46 -07004794static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4795{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004796 int res;
4797 unsigned int num, i;
4798 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004799
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004800 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4801 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004802 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004803 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004804
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004805 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4806 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004807 if (!freqs)
4808 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004809
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004810 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4811 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004812
4813 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004814 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004815 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4816 freq);
4817 res = 0;
4818 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004819 }
4820 }
4821
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004822 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004823 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004824
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004825exit_free:
4826 os_free(freqs);
4827 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004828}
4829
4830
4831static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4832 const u8 *peer_dev_addr)
4833{
4834 struct wpa_bss *bss;
4835 int updated;
4836
4837 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4838 if (bss == NULL)
4839 return -1;
4840 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4841 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4842 "last scan");
4843 return 0;
4844 }
4845
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004846 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4847 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004848 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4849 "%ld.%06ld (%supdated in last scan)",
4850 bss->last_update.sec, bss->last_update.usec,
4851 updated ? "": "not ");
4852
4853 return updated;
4854}
4855
4856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4858 struct wpa_scan_results *scan_res)
4859{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004860 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004861 int freq;
4862 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4865
4866 if (wpa_s->global->p2p_disabled)
4867 return;
4868
Dmitry Shmidt04949592012-07-19 12:16:46 -07004869 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4870 scan_res ? (int) scan_res->num : -1,
4871 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872
4873 if (scan_res)
4874 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4875
Dmitry Shmidt04949592012-07-19 12:16:46 -07004876 if (wpa_s->p2p_auto_pd) {
4877 int join = wpas_p2p_peer_go(wpa_s,
4878 wpa_s->pending_join_dev_addr);
4879 if (join == 0 &&
4880 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4881 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004882 bss = wpa_bss_get_bssid_latest(
4883 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004884 if (bss) {
4885 freq = bss->freq;
4886 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4887 "the peer " MACSTR " at %d MHz",
4888 wpa_s->auto_pd_scan_retry,
4889 MAC2STR(wpa_s->
4890 pending_join_dev_addr),
4891 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004892 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004893 return;
4894 }
4895 }
4896
4897 if (join < 0)
4898 join = 0;
4899
4900 wpa_s->p2p_auto_pd = 0;
4901 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4902 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4903 MAC2STR(wpa_s->pending_join_dev_addr), join);
4904 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004905 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004906 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004907 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004908 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004909 wpa_msg_global(wpa_s, MSG_INFO,
4910 P2P_EVENT_PROV_DISC_FAILURE
4911 " p2p_dev_addr=" MACSTR " status=N/A",
4912 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004913 }
4914 return;
4915 }
4916
4917 if (wpa_s->p2p_auto_join) {
4918 int join = wpas_p2p_peer_go(wpa_s,
4919 wpa_s->pending_join_dev_addr);
4920 if (join < 0) {
4921 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4922 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004923 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004924 P2P_EVENT_FALLBACK_TO_GO_NEG
4925 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004926 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4927 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4928 wpa_s->p2p_persistent_group, 0, 0, 0,
4929 wpa_s->p2p_go_intent,
4930 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004931 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004932 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004933 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004934 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004935 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004936 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08004937 wpa_s->p2p_go_he,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004938 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004939 return;
4940 }
4941
4942 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4943 "try to join the group", join ? "" :
4944 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004945 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004946 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004947 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004948 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004949 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004950 }
4951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4953 wpa_s->pending_join_iface_addr);
4954 if (freq < 0 &&
4955 p2p_get_interface_addr(wpa_s->global->p2p,
4956 wpa_s->pending_join_dev_addr,
4957 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004958 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4959 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4961 "address for join from " MACSTR " to " MACSTR
4962 " based on newly discovered P2P peer entry",
4963 MAC2STR(wpa_s->pending_join_iface_addr),
4964 MAC2STR(iface_addr));
4965 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4966 ETH_ALEN);
4967
4968 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4969 wpa_s->pending_join_iface_addr);
4970 }
4971 if (freq >= 0) {
4972 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4973 "from P2P peer table: %d MHz", freq);
4974 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004975 if (wpa_s->p2p_join_ssid_len) {
4976 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4977 MACSTR " and SSID %s",
4978 MAC2STR(wpa_s->pending_join_iface_addr),
4979 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4980 wpa_s->p2p_join_ssid_len));
4981 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4982 wpa_s->p2p_join_ssid,
4983 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004984 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004985 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4986 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4987 bss = wpa_bss_get_bssid_latest(wpa_s,
4988 wpa_s->pending_join_iface_addr);
4989 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004991 u8 dev_addr[ETH_ALEN];
4992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 freq = bss->freq;
4994 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004995 "from BSS table: %d MHz (SSID %s)", freq,
4996 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004997 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4998 dev_addr) == 0 &&
4999 os_memcmp(wpa_s->pending_join_dev_addr,
5000 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5001 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5002 ETH_ALEN) != 0) {
5003 wpa_printf(MSG_DEBUG,
5004 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5005 MAC2STR(dev_addr),
5006 MAC2STR(wpa_s->pending_join_dev_addr));
5007 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5008 ETH_ALEN);
5009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 }
5011 if (freq > 0) {
5012 u16 method;
5013
Dmitry Shmidt04949592012-07-19 12:16:46 -07005014 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005015 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005016 P2P_EVENT_GROUP_FORMATION_FAILURE
5017 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005018 wpas_notify_p2p_group_formation_failure(
5019 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005020 return;
5021 }
5022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005023 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5024 "prior to joining an existing group (GO " MACSTR
5025 " freq=%u MHz)",
5026 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5027 wpa_s->pending_pd_before_join = 1;
5028
5029 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 case WPS_PIN_DISPLAY:
5031 method = WPS_CONFIG_KEYPAD;
5032 break;
5033 case WPS_PIN_KEYPAD:
5034 method = WPS_CONFIG_DISPLAY;
5035 break;
5036 case WPS_PBC:
5037 method = WPS_CONFIG_PUSHBUTTON;
5038 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005039 case WPS_P2PS:
5040 method = WPS_CONFIG_P2PS;
5041 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 default:
5043 method = 0;
5044 break;
5045 }
5046
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005047 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5048 wpa_s->pending_join_dev_addr) ==
5049 method)) {
5050 /*
5051 * We have already performed provision discovery for
5052 * joining the group. Proceed directly to join
5053 * operation without duplicated provision discovery. */
5054 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5055 "with " MACSTR " already done - proceed to "
5056 "join",
5057 MAC2STR(wpa_s->pending_join_dev_addr));
5058 wpa_s->pending_pd_before_join = 0;
5059 goto start;
5060 }
5061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005063 wpa_s->pending_join_dev_addr,
5064 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005065 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5067 "Discovery Request before joining an "
5068 "existing group");
5069 wpa_s->pending_pd_before_join = 0;
5070 goto start;
5071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072 return;
5073 }
5074
5075 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5076 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5077 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5078 wpas_p2p_check_join_scan_limit(wpa_s);
5079 return;
5080
5081start:
5082 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005083 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5084 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085}
5086
5087
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005088static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5089 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091 int ret;
5092 struct wpa_driver_scan_params params;
5093 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005094 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005095 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005096 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097
5098 os_memset(&params, 0, sizeof(params));
5099
5100 /* P2P Wildcard SSID */
5101 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005102 if (ssid && ssid_len) {
5103 params.ssids[0].ssid = ssid;
5104 params.ssids[0].ssid_len = ssid_len;
5105 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5106 wpa_s->p2p_join_ssid_len = ssid_len;
5107 } else {
5108 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5109 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5110 wpa_s->p2p_join_ssid_len = 0;
5111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112
5113 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005114 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5115 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5116 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005117 if (wps_ie == NULL) {
5118 wpas_p2p_scan_res_join(wpa_s, NULL);
5119 return;
5120 }
5121
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005122 if (!freq) {
5123 int oper_freq;
5124 /*
5125 * If freq is not provided, check the operating freq of the GO
5126 * and use a single channel scan on if possible.
5127 */
5128 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5129 wpa_s->pending_join_iface_addr);
5130 if (oper_freq > 0)
5131 freq = oper_freq;
5132 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005133 if (freq > 0) {
5134 freqs[0] = freq;
5135 params.freqs = freqs;
5136 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005138 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5139 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5140 if (ies == NULL) {
5141 wpabuf_free(wps_ie);
5142 wpas_p2p_scan_res_join(wpa_s, NULL);
5143 return;
5144 }
5145 wpabuf_put_buf(ies, wps_ie);
5146 wpabuf_free(wps_ie);
5147
5148 bands = wpas_get_bands(wpa_s, freqs);
5149 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5150
5151 params.p2p_probe = 1;
5152 params.extra_ies = wpabuf_head(ies);
5153 params.extra_ies_len = wpabuf_len(ies);
5154
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005155 if (wpa_s->clear_driver_scan_cache) {
5156 wpa_printf(MSG_DEBUG,
5157 "Request driver to clear scan cache due to local BSS flush");
5158 params.only_new_results = 1;
5159 }
5160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 /*
5162 * Run a scan to update BSS table and start Provision Discovery once
5163 * the new scan results become available.
5164 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005165 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005166 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005167 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005168 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005169 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005170 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172
5173 wpabuf_free(ies);
5174
5175 if (ret) {
5176 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5177 "try again later");
5178 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5179 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5180 wpas_p2p_check_join_scan_limit(wpa_s);
5181 }
5182}
5183
5184
Dmitry Shmidt04949592012-07-19 12:16:46 -07005185static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5186{
5187 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005188 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005189}
5190
5191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005192static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005193 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005194 int auto_join, int op_freq,
5195 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005196{
5197 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005198 MACSTR " dev " MACSTR " op_freq=%d)%s",
5199 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005200 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005201 if (ssid && ssid_len) {
5202 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5203 wpa_ssid_txt(ssid, ssid_len));
5204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005205
Dmitry Shmidt04949592012-07-19 12:16:46 -07005206 wpa_s->p2p_auto_pd = 0;
5207 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5209 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5210 wpa_s->pending_join_wps_method = wps_method;
5211
5212 /* Make sure we are not running find during connection establishment */
5213 wpas_p2p_stop_find(wpa_s);
5214
5215 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005216 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217 return 0;
5218}
5219
5220
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005221static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5222 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223{
5224 struct wpa_supplicant *group;
5225 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005226 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005227
5228 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5229 if (group == NULL)
5230 return -1;
5231 if (group != wpa_s) {
5232 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5233 sizeof(group->p2p_pin));
5234 group->p2p_wps_method = wpa_s->p2p_wps_method;
5235 }
5236
Hai Shalom74f70d42019-02-11 14:42:39 -08005237 /*
5238 * Need to mark the current interface for p2p_group_formation
5239 * when a separate group interface is not used. This is needed
5240 * to allow p2p_cancel stop a pending p2p_connect-join.
5241 * wpas_p2p_init_group_interface() addresses this for the case
5242 * where a separate group interface is used.
5243 */
5244 if (group == wpa_s->parent)
5245 wpa_s->global->p2p_group_formation = group;
5246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005247 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005248 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249
5250 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005251 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005252 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5253 ETH_ALEN);
5254 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005255 if (freq && ssid && ssid_len) {
5256 res.freq = freq;
5257 res.ssid_len = ssid_len;
5258 os_memcpy(res.ssid, ssid, ssid_len);
5259 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005260 if (ssid && ssid_len) {
5261 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5262 ssid, ssid_len);
5263 } else {
5264 bss = wpa_bss_get_bssid_latest(
5265 wpa_s, wpa_s->pending_join_iface_addr);
5266 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005267 if (bss) {
5268 res.freq = bss->freq;
5269 res.ssid_len = bss->ssid_len;
5270 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5271 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5272 bss->freq,
5273 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005274 } else if (ssid && ssid_len) {
5275 res.ssid_len = ssid_len;
5276 os_memcpy(res.ssid, ssid, ssid_len);
5277 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5278 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005279 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005280 }
5281
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005282 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5283 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5284 "starting client");
5285 wpa_drv_cancel_remain_on_channel(wpa_s);
5286 wpa_s->off_channel_freq = 0;
5287 wpa_s->roc_waiting_drv_freq = 0;
5288 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005289 wpas_start_wps_enrollee(group, &res);
5290
5291 /*
5292 * Allow a longer timeout for join-a-running-group than normal 15
5293 * second group formation timeout since the GO may not have authorized
5294 * our connection yet.
5295 */
5296 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5297 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5298 wpa_s, NULL);
5299
5300 return 0;
5301}
5302
5303
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005304static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005305 int *force_freq, int *pref_freq, int go,
5306 unsigned int *pref_freq_list,
5307 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005308{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005309 struct wpa_used_freq_data *freqs;
5310 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005311 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5312
5313 max_pref_freq = *num_pref_freq;
5314 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005315
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005316 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5317 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005318 if (!freqs)
5319 return -1;
5320
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005321 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5322 wpa_s->num_multichan_concurrent);
5323
5324 /*
5325 * It is possible that the total number of used frequencies is bigger
5326 * than the number of frequencies used for P2P, so get the system wide
5327 * number of unused frequencies.
5328 */
5329 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5330
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005331 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005332 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5333 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005334
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005335 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005336 int ret;
5337 if (go)
5338 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5339 else
5340 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5341 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005342 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005343 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5344 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005345 /*
5346 * If freq is a DFS channel and DFS is offloaded
5347 * to the driver, allow P2P GO to use it.
5348 */
5349 wpa_printf(MSG_DEBUG,
5350 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5351 freq);
5352 } else {
5353 wpa_printf(MSG_DEBUG,
5354 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5355 freq);
5356 res = -3;
5357 goto exit_free;
5358 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005359 }
5360
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005361 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005362 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005363 freq_in_use = 1;
5364 }
5365
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005366 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005367 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5368 freq);
5369 res = -2;
5370 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005371 }
5372 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5373 "requested channel (%u MHz)", freq);
5374 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005375 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005376 }
5377
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005378 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005379
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005380 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5381 enum wpa_driver_if_type iface_type;
5382
5383 if (go)
5384 iface_type = WPA_IF_P2P_GO;
5385 else
5386 iface_type = WPA_IF_P2P_CLIENT;
5387
5388 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5389 best_freq, go);
5390
5391 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5392 &max_pref_freq,
5393 pref_freq_list);
5394 if (!res && max_pref_freq > 0) {
5395 *num_pref_freq = max_pref_freq;
5396 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005397 while (i < *num_pref_freq &&
5398 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005399 pref_freq_list[i]) ||
5400 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005401 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005402 wpa_printf(MSG_DEBUG,
5403 "P2P: preferred_freq_list[%d]=%d is disallowed",
5404 i, pref_freq_list[i]);
5405 i++;
5406 }
5407 if (i != *num_pref_freq) {
5408 best_freq = pref_freq_list[i];
5409 wpa_printf(MSG_DEBUG,
5410 "P2P: Using preferred_freq_list[%d]=%d",
5411 i, best_freq);
5412 } else {
5413 wpa_printf(MSG_DEBUG,
5414 "P2P: All driver preferred frequencies are disallowed for P2P use");
5415 *num_pref_freq = 0;
5416 }
5417 } else {
5418 wpa_printf(MSG_DEBUG,
5419 "P2P: No preferred frequency list available");
5420 }
5421 }
5422
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005423 /* We have a candidate frequency to use */
5424 if (best_freq > 0) {
5425 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005426 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005427 best_freq);
5428 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005429 } else {
5430 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 -07005431 best_freq);
5432 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005433 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005434 } else if (num_unused > 0) {
5435 wpa_printf(MSG_DEBUG,
5436 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5437 *force_freq = 0;
5438 } else {
5439 wpa_printf(MSG_DEBUG,
5440 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5441 res = -2;
5442 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005443 }
5444
5445exit_ok:
5446 res = 0;
5447exit_free:
5448 os_free(freqs);
5449 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005450}
5451
5452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005453/**
5454 * wpas_p2p_connect - Request P2P Group Formation to be started
5455 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5456 * @peer_addr: Address of the peer P2P Device
5457 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5458 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005459 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460 * @join: Whether to join an existing group (as a client) instead of starting
5461 * Group Owner negotiation; @peer_addr is BSSID in that case
5462 * @auth: Whether to only authorize the connection instead of doing that and
5463 * initiating Group Owner negotiation
5464 * @go_intent: GO Intent or -1 to use default
5465 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005466 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005467 * @persistent_id: Persistent group credentials to use for forcing GO
5468 * parameters or -1 to generate new values (SSID/passphrase)
5469 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5470 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005471 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005472 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005473 * @vht_chwidth: Channel width supported by GO operating with VHT support
5474 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005475 * @group_ssid: Specific Group SSID for join or %NULL if not set
5476 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5478 * failure, -2 on failure due to channel not currently available,
5479 * -3 if forced channel is not supported
5480 */
5481int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5482 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005483 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005484 int go_intent, int freq, unsigned int vht_center_freq2,
5485 int persistent_id, int pd, int ht40, int vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08005486 unsigned int vht_chwidth, int he, const u8 *group_ssid,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005487 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005489 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005490 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005491 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005492 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005493 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005494 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005495
5496 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5497 return -1;
5498
Dmitry Shmidt04949592012-07-19 12:16:46 -07005499 if (persistent_id >= 0) {
5500 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5501 if (ssid == NULL || ssid->disabled != 2 ||
5502 ssid->mode != WPAS_MODE_P2P_GO)
5503 return -1;
5504 }
5505
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005506 os_free(wpa_s->global->add_psk);
5507 wpa_s->global->add_psk = NULL;
5508
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005509 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005510 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005511 wpa_s->global->pending_p2ps_group_freq = 0;
5512 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514 if (go_intent < 0)
5515 go_intent = wpa_s->conf->p2p_go_intent;
5516
5517 if (!auth)
5518 wpa_s->p2p_long_listen = 0;
5519
5520 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005521 wpa_s->p2p_persistent_group = !!persistent_group;
5522 wpa_s->p2p_persistent_id = persistent_id;
5523 wpa_s->p2p_go_intent = go_intent;
5524 wpa_s->p2p_connect_freq = freq;
5525 wpa_s->p2p_fallback_to_go_neg = 0;
5526 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005527 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005528 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005529 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5530 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005531 wpa_s->p2p_go_he = !!he;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005532
5533 if (pin)
5534 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5535 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005536 if (wps_generate_pin((unsigned int *) &ret) < 0)
5537 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005538 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5539 "%08d", ret);
5540 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5541 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5543 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005544 } else if (wps_method == WPS_P2PS) {
5545 /* Force the P2Ps default PIN to be used */
5546 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 } else
5548 wpa_s->p2p_pin[0] = '\0';
5549
Dmitry Shmidt04949592012-07-19 12:16:46 -07005550 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005551 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5552 if (auth) {
5553 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5554 "connect a running group from " MACSTR,
5555 MAC2STR(peer_addr));
5556 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5557 return ret;
5558 }
5559 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5560 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5561 iface_addr) < 0) {
5562 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5563 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5564 dev_addr);
5565 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005566 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005567 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005568 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5569 "%ld.%06ld",
5570 wpa_s->p2p_auto_started.sec,
5571 wpa_s->p2p_auto_started.usec);
5572 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005573 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005574 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005575 auto_join, freq,
5576 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577 return -1;
5578 return ret;
5579 }
5580
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005581 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005582 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005583 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005584 if (res)
5585 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005586 wpas_p2p_set_own_freq_preference(wpa_s,
5587 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005589 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005593 if (wpa_s->create_p2p_iface) {
5594 /* Prepare to add a new interface for the group */
5595 iftype = WPA_IF_P2P_GROUP;
5596 if (go_intent == 15)
5597 iftype = WPA_IF_P2P_GO;
5598 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5599 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5600 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005602 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005604 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005605 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005606 if (wpa_s->p2p_mgmt)
5607 if_addr = wpa_s->parent->own_addr;
5608 else
5609 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005610 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5611 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612
5613 if (auth) {
5614 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005615 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005616 force_freq, persistent_group, ssid,
5617 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 return -1;
5619 return ret;
5620 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005621
5622 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5623 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005624 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005625 if (wpa_s->create_p2p_iface)
5626 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005627 return -1;
5628 }
5629 return ret;
5630}
5631
5632
5633/**
5634 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5635 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5636 * @freq: Frequency of the channel in MHz
5637 * @duration: Duration of the stay on the channel in milliseconds
5638 *
5639 * This callback is called when the driver indicates that it has started the
5640 * requested remain-on-channel duration.
5641 */
5642void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5643 unsigned int freq, unsigned int duration)
5644{
5645 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5646 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005647 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)",
5648 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5649 wpa_s->roc_waiting_drv_freq, freq, duration);
5650 if (wpa_s->off_channel_freq &&
5651 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5653 wpa_s->pending_listen_duration);
5654 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005655 } else {
5656 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5657 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5658 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659 }
5660}
5661
5662
Jithu Jance57cea1a2014-08-20 10:22:04 -07005663int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005664{
5665 /* Limit maximum Listen state time based on driver limitation. */
5666 if (timeout > wpa_s->max_remain_on_chan)
5667 timeout = wpa_s->max_remain_on_chan;
5668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005669 return p2p_listen(wpa_s->global->p2p, timeout);
5670}
5671
5672
5673/**
5674 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5675 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5676 * @freq: Frequency of the channel in MHz
5677 *
5678 * This callback is called when the driver indicates that a remain-on-channel
5679 * operation has been completed, i.e., the duration on the requested channel
5680 * has timed out.
5681 */
5682void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5683 unsigned int freq)
5684{
5685 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5686 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005687 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005688 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5690 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005691 if (wpa_s->p2p_long_listen > 0)
5692 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5694 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005695 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005696 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697 if (wpa_s->p2p_long_listen > 0) {
5698 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5699 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005700 } else {
5701 /*
5702 * When listen duration is over, stop listen & update p2p_state
5703 * to IDLE.
5704 */
5705 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 }
5707}
5708
5709
5710/**
5711 * wpas_p2p_group_remove - Remove a P2P group
5712 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5713 * @ifname: Network interface name of the group interface or "*" to remove all
5714 * groups
5715 * Returns: 0 on success, -1 on failure
5716 *
5717 * This function is used to remove a P2P group. This can be used to disconnect
5718 * from a group in which the local end is a P2P Client or to end a P2P Group in
5719 * case the local end is the Group Owner. If a virtual network interface was
5720 * created for this group, that interface will be removed. Otherwise, only the
5721 * configured P2P group network will be removed from the interface.
5722 */
5723int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5724{
5725 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005726 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727
5728 if (os_strcmp(ifname, "*") == 0) {
5729 struct wpa_supplicant *prev;
5730 wpa_s = global->ifaces;
5731 while (wpa_s) {
5732 prev = wpa_s;
5733 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005734 if (prev->p2p_group_interface !=
5735 NOT_P2P_GROUP_INTERFACE ||
5736 (prev->current_ssid &&
5737 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005738 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739 }
5740 return 0;
5741 }
5742
5743 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5744 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5745 break;
5746 }
5747
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005748 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749}
5750
5751
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005752static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5753{
5754 unsigned int r;
5755
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005756 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5757 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5758 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5759 int res;
5760
5761 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5762 &size, pref_freq_list);
5763 if (!res && size > 0) {
5764 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005765 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005766 (!p2p_supported_freq(wpa_s->global->p2p,
5767 pref_freq_list[i]) ||
5768 wpas_p2p_disallowed_freq(wpa_s->global,
5769 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005770 wpa_printf(MSG_DEBUG,
5771 "P2P: preferred_freq_list[%d]=%d is disallowed",
5772 i, pref_freq_list[i]);
5773 i++;
5774 }
5775 if (i != size) {
5776 freq = pref_freq_list[i];
5777 wpa_printf(MSG_DEBUG,
5778 "P2P: Using preferred_freq_list[%d]=%d",
5779 i, freq);
5780 } else {
5781 wpa_printf(MSG_DEBUG,
5782 "P2P: All driver preferred frequencies are disallowed for P2P use");
5783 }
5784 } else {
5785 wpa_printf(MSG_DEBUG,
5786 "P2P: No preferred frequency list available");
5787 }
5788 }
5789
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005790 if (freq == 2) {
5791 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5792 "band");
5793 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005794 p2p_supported_freq_go(wpa_s->global->p2p,
5795 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005796 freq = wpa_s->best_24_freq;
5797 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5798 "channel: %d MHz", freq);
5799 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005800 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5801 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005802 freq = 2412 + (r % 3) * 25;
5803 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5804 "channel: %d MHz", freq);
5805 }
5806 }
5807
5808 if (freq == 5) {
5809 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5810 "band");
5811 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005812 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005813 wpa_s->best_5_freq)) {
5814 freq = wpa_s->best_5_freq;
5815 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5816 "channel: %d MHz", freq);
5817 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005818 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5819 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005820 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005821 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005822 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5823 "5 GHz channel for P2P group");
5824 return -1;
5825 }
5826 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5827 "channel: %d MHz", freq);
5828 }
5829 }
5830
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005831 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005832 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005833 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5834 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005835 /*
5836 * If freq is a DFS channel and DFS is offloaded to the
5837 * driver, allow P2P GO to use it.
5838 */
5839 wpa_printf(MSG_DEBUG, "P2P: "
5840 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5841 __func__, freq);
5842 return freq;
5843 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005844 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5845 "(%u MHz) is not supported for P2P uses",
5846 freq);
5847 return -1;
5848 }
5849
5850 return freq;
5851}
5852
5853
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005854static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5855 const struct p2p_channels *channels,
5856 int freq)
5857{
5858 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5859 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5860 freq_included(wpa_s, channels, freq))
5861 return 1;
5862 return 0;
5863}
5864
5865
5866static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5867 struct p2p_go_neg_results *params,
5868 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005869{
5870 unsigned int i, r;
5871
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005872 /* try all channels in operating class 115 */
5873 for (i = 0; i < 4; i++) {
5874 params->freq = 5180 + i * 20;
5875 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005876 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005877 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5878 goto out;
5879 }
5880
5881 /* try all channels in operating class 124 */
5882 for (i = 0; i < 4; i++) {
5883 params->freq = 5745 + i * 20;
5884 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005885 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005886 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5887 goto out;
5888 }
5889
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005890 /* try social channel class 180 channel 2 */
5891 params->freq = 58320 + 1 * 2160;
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 Shmidt661b4f72014-09-29 14:58:27 -07005894 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5895 goto out;
5896
5897 /* try all channels in reg. class 180 */
5898 for (i = 0; i < 4; i++) {
5899 params->freq = 58320 + i * 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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005906 /* try some random selection of the social channels */
5907 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5908 return;
5909
5910 for (i = 0; i < 3; i++) {
5911 params->freq = 2412 + ((r + i) % 3) * 25;
5912 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5913 goto out;
5914 }
5915
5916 /* try all other channels in operating class 81 */
5917 for (i = 0; i < 11; i++) {
5918 params->freq = 2412 + i * 5;
5919
5920 /* skip social channels; covered in the previous loop */
5921 if (params->freq == 2412 ||
5922 params->freq == 2437 ||
5923 params->freq == 2462)
5924 continue;
5925
5926 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5927 goto out;
5928 }
5929
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005930 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005931 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005932 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005933out:
5934 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5935 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005936}
5937
5938
Roshan Pius3a1667e2018-07-03 15:17:14 -07005939static int wpas_same_band(int freq1, int freq2)
5940{
5941 enum hostapd_hw_mode mode1, mode2;
5942 u8 chan1, chan2;
5943
5944 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5945 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5946 if (mode1 == NUM_HOSTAPD_MODES)
5947 return 0;
5948 return mode1 == mode2;
5949}
5950
5951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005952static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5953 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005954 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08005955 int vht, int max_oper_chwidth, int he,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005956 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005958 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005959 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005960 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005961 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005962 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005963
5964 os_memset(params, 0, sizeof(*params));
5965 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005966 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005967 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08005968 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005969 params->max_oper_chwidth = max_oper_chwidth;
5970 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005972 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5973 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005974 if (!freqs)
5975 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005977 num = get_shared_radio_freqs_data(wpa_s, freqs,
5978 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005979
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005980 if (wpa_s->current_ssid &&
5981 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5982 wpa_s->wpa_state == WPA_COMPLETED) {
5983 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5984 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005985
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005986 /*
5987 * If the frequency selection is done for an active P2P GO that
5988 * is not sharing a frequency, allow to select a new frequency
5989 * even if there are no unused frequencies as we are about to
5990 * move the P2P GO so its frequency can be re-used.
5991 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005992 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005993 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5994 freqs[i].flags == 0) {
5995 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005996 break;
5997 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005998 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005999 }
6000
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006001 /* try using the forced freq */
6002 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006003 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6004 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006005 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006006 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006007 freq);
6008 goto fail;
6009 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006010 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6011 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006012 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6013 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006014 /*
6015 * If freq is a DFS channel and DFS is offloaded
6016 * to the driver, allow P2P GO to use it.
6017 */
6018 wpa_printf(MSG_DEBUG,
6019 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6020 __func__, freq);
6021 } else {
6022 wpa_printf(MSG_DEBUG,
6023 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6024 freq);
6025 goto fail;
6026 }
6027 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006028
6029 for (i = 0; i < num; i++) {
6030 if (freqs[i].freq == freq) {
6031 wpa_printf(MSG_DEBUG,
6032 "P2P: forced freq (%d MHz) is also shared",
6033 freq);
6034 params->freq = freq;
6035 goto success;
6036 }
6037 }
6038
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006039 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006040 wpa_printf(MSG_DEBUG,
6041 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6042 freq);
6043 goto fail;
6044 }
6045
6046 wpa_printf(MSG_DEBUG,
6047 "P2P: force GO freq (%d MHz) on a free channel",
6048 freq);
6049 params->freq = freq;
6050 goto success;
6051 }
6052
6053 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006054 if (num &&
6055 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006056 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6057 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6058 wpa_printf(MSG_DEBUG,
6059 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006060 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006061 params->freq = cand;
6062 goto success;
6063 }
6064
6065 /* try using one of the shared freqs */
6066 for (i = 0; i < num; i++) {
6067 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6068 freqs[i].freq)) {
6069 wpa_printf(MSG_DEBUG,
6070 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006071 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006072 params->freq = freqs[i].freq;
6073 goto success;
6074 }
6075 }
6076 }
6077
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006078 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006079 wpa_printf(MSG_DEBUG,
6080 "P2P: Cannot force GO on any of the channels we are already using");
6081 goto fail;
6082 }
6083
6084 /* try using the setting from the configuration file */
6085 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6086 wpa_s->conf->p2p_oper_channel >= 1 &&
6087 wpa_s->conf->p2p_oper_channel <= 11 &&
6088 wpas_p2p_supported_freq_go(
6089 wpa_s, channels,
6090 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6091 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6092 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6093 "frequency %d MHz", params->freq);
6094 goto success;
6095 }
6096
6097 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6098 wpa_s->conf->p2p_oper_reg_class == 116 ||
6099 wpa_s->conf->p2p_oper_reg_class == 117 ||
6100 wpa_s->conf->p2p_oper_reg_class == 124 ||
6101 wpa_s->conf->p2p_oper_reg_class == 125 ||
6102 wpa_s->conf->p2p_oper_reg_class == 126 ||
6103 wpa_s->conf->p2p_oper_reg_class == 127) &&
6104 wpas_p2p_supported_freq_go(wpa_s, channels,
6105 5000 +
6106 5 * wpa_s->conf->p2p_oper_channel)) {
6107 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6108 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6109 "frequency %d MHz", params->freq);
6110 goto success;
6111 }
6112
6113 /* Try using best channels */
6114 if (wpa_s->conf->p2p_oper_channel == 0 &&
6115 wpa_s->best_overall_freq > 0 &&
6116 wpas_p2p_supported_freq_go(wpa_s, channels,
6117 wpa_s->best_overall_freq)) {
6118 params->freq = wpa_s->best_overall_freq;
6119 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6120 "channel %d MHz", params->freq);
6121 goto success;
6122 }
6123
6124 if (wpa_s->conf->p2p_oper_channel == 0 &&
6125 wpa_s->best_24_freq > 0 &&
6126 wpas_p2p_supported_freq_go(wpa_s, channels,
6127 wpa_s->best_24_freq)) {
6128 params->freq = wpa_s->best_24_freq;
6129 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6130 "channel %d MHz", params->freq);
6131 goto success;
6132 }
6133
6134 if (wpa_s->conf->p2p_oper_channel == 0 &&
6135 wpa_s->best_5_freq > 0 &&
6136 wpas_p2p_supported_freq_go(wpa_s, channels,
6137 wpa_s->best_5_freq)) {
6138 params->freq = wpa_s->best_5_freq;
6139 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6140 "channel %d MHz", params->freq);
6141 goto success;
6142 }
6143
6144 /* try using preferred channels */
6145 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6146 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6147 params->freq = cand;
6148 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6149 "channels", params->freq);
6150 goto success;
6151 }
6152
Roshan Pius3a1667e2018-07-03 15:17:14 -07006153 /* Try using a channel that allows VHT to be used with 80 MHz */
6154 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6155 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6156 enum hostapd_hw_mode mode;
6157 struct hostapd_hw_modes *hwmode;
6158 u8 chan;
6159
6160 cand = wpa_s->p2p_group_common_freqs[i];
6161 mode = ieee80211_freq_to_chan(cand, &chan);
6162 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6163 mode);
6164 if (!hwmode ||
6165 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6166 BW80) != ALLOWED)
6167 continue;
6168 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6169 params->freq = cand;
6170 wpa_printf(MSG_DEBUG,
6171 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6172 params->freq);
6173 goto success;
6174 }
6175 }
6176 }
6177
6178 /* Try using a channel that allows HT to be used with 40 MHz on the same
6179 * band so that CSA can be used */
6180 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6181 wpa_s->p2p_group_common_freqs) {
6182 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6183 enum hostapd_hw_mode mode;
6184 struct hostapd_hw_modes *hwmode;
6185 u8 chan;
6186
6187 cand = wpa_s->p2p_group_common_freqs[i];
6188 mode = ieee80211_freq_to_chan(cand, &chan);
6189 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6190 mode);
6191 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6192 cand) ||
6193 !hwmode ||
6194 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6195 BW40MINUS) != ALLOWED &&
6196 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6197 BW40PLUS) != ALLOWED))
6198 continue;
6199 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6200 params->freq = cand;
6201 wpa_printf(MSG_DEBUG,
6202 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6203 params->freq);
6204 goto success;
6205 }
6206 }
6207 }
6208
6209 /* Try using one of the group common freqs on the same band so that CSA
6210 * can be used */
6211 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6212 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6213 cand = wpa_s->p2p_group_common_freqs[i];
6214 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6215 cand))
6216 continue;
6217 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6218 params->freq = cand;
6219 wpa_printf(MSG_DEBUG,
6220 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6221 params->freq);
6222 goto success;
6223 }
6224 }
6225 }
6226
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006227 /* Try using one of the group common freqs */
6228 if (wpa_s->p2p_group_common_freqs) {
6229 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6230 cand = wpa_s->p2p_group_common_freqs[i];
6231 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6232 params->freq = cand;
6233 wpa_printf(MSG_DEBUG,
6234 "P2P: Use freq %d MHz common with the peer",
6235 params->freq);
6236 goto success;
6237 }
6238 }
6239 }
6240
6241 /* no preference, select some channel */
6242 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6243
6244 if (params->freq == 0) {
6245 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6246 goto fail;
6247 }
6248
6249success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006250 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006251 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006252fail:
6253 os_free(freqs);
6254 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255}
6256
6257
6258static struct wpa_supplicant *
6259wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6260 int go)
6261{
6262 struct wpa_supplicant *group_wpa_s;
6263
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006264 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006265 if (wpa_s->p2p_mgmt) {
6266 /*
6267 * We may be called on the p2p_dev interface which
6268 * cannot be used for group operations, so always use
6269 * the primary interface.
6270 */
6271 wpa_s->parent->p2pdev = wpa_s;
6272 wpa_s = wpa_s->parent;
6273 }
6274 wpa_dbg(wpa_s, MSG_DEBUG,
6275 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006276 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006277 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006278 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006280 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281
6282 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006283 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006284 wpa_msg_global(wpa_s, MSG_ERROR,
6285 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006286 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006288 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6289 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006290 wpa_msg_global(wpa_s, MSG_ERROR,
6291 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006292 wpas_p2p_remove_pending_group_interface(wpa_s);
6293 return NULL;
6294 }
6295
Roshan Pius3a1667e2018-07-03 15:17:14 -07006296 if (go && wpa_s->p2p_go_do_acs) {
6297 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6298 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6299 wpa_s->p2p_go_do_acs = 0;
6300 }
6301
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6303 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006304 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006305 return group_wpa_s;
6306}
6307
6308
6309/**
6310 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6311 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6312 * @persistent_group: Whether to create a persistent group
6313 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006314 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006315 * @ht40: Start GO with 40 MHz channel width
6316 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006317 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318 * Returns: 0 on success, -1 on failure
6319 *
6320 * This function creates a new P2P group with the local end as the Group Owner,
6321 * i.e., without using Group Owner Negotiation.
6322 */
6323int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006324 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08006325 int max_oper_chwidth, int he)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006326{
6327 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006328
6329 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6330 return -1;
6331
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006332 os_free(wpa_s->global->add_psk);
6333 wpa_s->global->add_psk = NULL;
6334
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006335 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006336 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006337 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006338
Roshan Pius3a1667e2018-07-03 15:17:14 -07006339 if (!wpa_s->p2p_go_do_acs) {
6340 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6341 if (freq < 0)
6342 return -1;
6343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006344
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006345 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Hai Shalom74f70d42019-02-11 14:42:39 -08006346 ht40, vht, max_oper_chwidth, he, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006347 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349 p2p_go_params(wpa_s->global->p2p, &params);
6350 params.persistent_group = persistent_group;
6351
6352 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6353 if (wpa_s == NULL)
6354 return -1;
6355 wpas_start_wps_go(wpa_s, &params, 0);
6356
6357 return 0;
6358}
6359
6360
6361static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006362 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006363 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364{
6365 struct wpa_ssid *ssid;
6366
6367 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6368 if (wpa_s == NULL)
6369 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006370 if (force_scan)
6371 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006372 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006373
6374 wpa_supplicant_ap_deinit(wpa_s);
6375
6376 ssid = wpa_config_add_network(wpa_s->conf);
6377 if (ssid == NULL)
6378 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006379 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380 wpa_config_set_network_defaults(ssid);
6381 ssid->temporary = 1;
6382 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006383 ssid->pbss = params->pbss;
6384 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6385 WPA_CIPHER_CCMP;
6386 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006387 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6388 ssid->ssid = os_malloc(params->ssid_len);
6389 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006390 wpa_config_remove_network(wpa_s->conf, ssid->id);
6391 return -1;
6392 }
6393 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6394 ssid->ssid_len = params->ssid_len;
6395 ssid->p2p_group = 1;
6396 ssid->export_keys = 1;
6397 if (params->psk_set) {
6398 os_memcpy(ssid->psk, params->psk, 32);
6399 ssid->psk_set = 1;
6400 }
6401 if (params->passphrase)
6402 ssid->passphrase = os_strdup(params->passphrase);
6403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006405 wpa_s->p2p_in_invitation = 1;
6406 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006407 wpa_s->p2p_go_group_formation_completed = 0;
6408 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006409
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006410 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006411 NULL);
6412 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6413 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006414 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006415 wpa_supplicant_select_network(wpa_s, ssid);
6416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417 return 0;
6418}
6419
6420
6421int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6422 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006423 int force_freq, int neg_freq,
6424 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006425 int vht, int max_oper_chwidth, int he,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006426 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006427 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006428{
6429 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006430 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006431
6432 if (ssid->disabled != 2 || ssid->ssid == NULL)
6433 return -1;
6434
6435 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6436 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6437 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6438 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006439 if (go == 0 &&
6440 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006441 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006442 /*
6443 * This can happen if Invitation Response frame was lost
6444 * and the peer (GO of a persistent group) tries to
6445 * invite us again. Reschedule the timeout to avoid
6446 * terminating the wait for the connection too early
6447 * since we now know that the peer is still trying to
6448 * invite us instead of having already started the GO.
6449 */
6450 wpa_printf(MSG_DEBUG,
6451 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6452 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6453 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006454 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006456 return 0;
6457 }
6458
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006459 os_free(wpa_s->global->add_psk);
6460 wpa_s->global->add_psk = NULL;
6461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006463 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006464
Dmitry Shmidt04949592012-07-19 12:16:46 -07006465 wpa_s->p2p_fallback_to_go_neg = 0;
6466
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006467 if (ssid->mode == WPAS_MODE_P2P_GO) {
6468 if (force_freq > 0) {
6469 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6470 if (freq < 0)
6471 return -1;
6472 } else {
6473 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6474 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006475 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006476 freq = 0;
6477 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006478 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006479 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006480 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006481 struct os_reltime now;
6482 struct wpa_bss *bss =
6483 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006484
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006485 os_get_reltime(&now);
6486 if (bss &&
6487 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006488 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006489 freq = bss->freq;
6490 else
6491 freq = 0;
6492 }
6493
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006494 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6495 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006496 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006497 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006498 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006499
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006500 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Hai Shalom74f70d42019-02-11 14:42:39 -08006501 ht40, vht, max_oper_chwidth, he, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006502 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006503
6504 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006505 params.psk_set = ssid->psk_set;
6506 if (params.psk_set)
6507 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006508 if (ssid->passphrase) {
6509 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6510 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6511 "persistent group");
6512 return -1;
6513 }
6514 os_strlcpy(params.passphrase, ssid->passphrase,
6515 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006516 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006517 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6518 params.ssid_len = ssid->ssid_len;
6519 params.persistent_group = 1;
6520
6521 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6522 if (wpa_s == NULL)
6523 return -1;
6524
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006525 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6526
Dmitry Shmidt56052862013-10-04 10:23:25 -07006527 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528 wpas_start_wps_go(wpa_s, &params, 0);
6529
6530 return 0;
6531}
6532
6533
6534static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6535 struct wpabuf *proberesp_ies)
6536{
6537 struct wpa_supplicant *wpa_s = ctx;
6538 if (wpa_s->ap_iface) {
6539 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006540 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6541 wpabuf_free(beacon_ies);
6542 wpabuf_free(proberesp_ies);
6543 return;
6544 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006545 if (beacon_ies) {
6546 wpabuf_free(hapd->p2p_beacon_ie);
6547 hapd->p2p_beacon_ie = beacon_ies;
6548 }
6549 wpabuf_free(hapd->p2p_probe_resp_ie);
6550 hapd->p2p_probe_resp_ie = proberesp_ies;
6551 } else {
6552 wpabuf_free(beacon_ies);
6553 wpabuf_free(proberesp_ies);
6554 }
6555 wpa_supplicant_ap_update_beacon(wpa_s);
6556}
6557
6558
6559static void wpas_p2p_idle_update(void *ctx, int idle)
6560{
6561 struct wpa_supplicant *wpa_s = ctx;
6562 if (!wpa_s->ap_iface)
6563 return;
6564 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006565 if (idle) {
6566 if (wpa_s->global->p2p_fail_on_wps_complete &&
6567 wpa_s->p2p_in_provisioning) {
6568 wpas_p2p_grpform_fail_after_wps(wpa_s);
6569 return;
6570 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006571 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006572 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6574}
6575
6576
6577struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006578 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006579{
6580 struct p2p_group *group;
6581 struct p2p_group_config *cfg;
6582
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006583 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6584 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006585 return NULL;
6586
6587 cfg = os_zalloc(sizeof(*cfg));
6588 if (cfg == NULL)
6589 return NULL;
6590
Dmitry Shmidt04949592012-07-19 12:16:46 -07006591 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006592 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006593 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006594 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006595 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6596 if (wpa_s->max_stations &&
6597 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6598 cfg->max_clients = wpa_s->max_stations;
6599 else
6600 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006601 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6602 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006603 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604 cfg->cb_ctx = wpa_s;
6605 cfg->ie_update = wpas_p2p_ie_update;
6606 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006607 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6608 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006609
6610 group = p2p_group_init(wpa_s->global->p2p, cfg);
6611 if (group == NULL)
6612 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006613 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006614 p2p_group_notif_formation_done(group);
6615 wpa_s->p2p_group = group;
6616 return group;
6617}
6618
6619
6620void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6621 int registrar)
6622{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006623 struct wpa_ssid *ssid = wpa_s->current_ssid;
6624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006625 if (!wpa_s->p2p_in_provisioning) {
6626 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6627 "provisioning not in progress");
6628 return;
6629 }
6630
Dmitry Shmidt04949592012-07-19 12:16:46 -07006631 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6632 u8 go_dev_addr[ETH_ALEN];
6633 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6634 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6635 ssid->ssid_len);
6636 /* Clear any stored provisioning info */
6637 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6638 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006639
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006640 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006641 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006642 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006643 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6644 /*
6645 * Use a separate timeout for initial data connection to
6646 * complete to allow the group to be removed automatically if
6647 * something goes wrong in this step before the P2P group idle
6648 * timeout mechanism is taken into use.
6649 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006650 wpa_dbg(wpa_s, MSG_DEBUG,
6651 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6652 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006653 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6654 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006655 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006656 /* Complete group formation on successful data connection. */
6657 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006658 } else if (ssid) {
6659 /*
6660 * Use a separate timeout for initial data connection to
6661 * complete to allow the group to be removed automatically if
6662 * the client does not complete data connection successfully.
6663 */
6664 wpa_dbg(wpa_s, MSG_DEBUG,
6665 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6666 P2P_MAX_INITIAL_CONN_WAIT_GO);
6667 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6668 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006669 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006670 /*
6671 * Complete group formation on first successful data connection
6672 */
6673 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006674 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006675 if (wpa_s->global->p2p)
6676 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006677 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006678}
6679
6680
Jouni Malinen75ecf522011-06-27 15:19:46 -07006681void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6682 struct wps_event_fail *fail)
6683{
6684 if (!wpa_s->p2p_in_provisioning) {
6685 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6686 "provisioning not in progress");
6687 return;
6688 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006689
6690 if (wpa_s->go_params) {
6691 p2p_clear_provisioning_info(
6692 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006693 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006694 }
6695
Jouni Malinen75ecf522011-06-27 15:19:46 -07006696 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006697
6698 if (wpa_s == wpa_s->global->p2p_group_formation) {
6699 /*
6700 * Allow some time for the failed WPS negotiation exchange to
6701 * complete, but remove the group since group formation cannot
6702 * succeed after provisioning failure.
6703 */
6704 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6705 wpa_s->global->p2p_fail_on_wps_complete = 1;
6706 eloop_deplete_timeout(0, 50000,
6707 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006708 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006709 }
6710}
6711
6712
6713int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6714{
6715 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6716 !wpa_s->p2p_in_provisioning)
6717 return 0;
6718
6719 wpas_p2p_grpform_fail_after_wps(wpa_s);
6720
6721 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006722}
6723
6724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006726 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006727 enum wpas_p2p_prov_disc_use use,
6728 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006729{
6730 u16 config_methods;
6731
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006732 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006733 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006734 wpa_s->p2p_fallback_to_go_neg = 0;
6735 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006736 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6737 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006738 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6739 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6740
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006741 wpa_printf(MSG_DEBUG,
6742 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6743 __func__, p2ps_prov->conncap,
6744 p2ps_prov->adv_id, p2ps_prov->conncap,
6745 p2ps_prov->status, p2ps_prov->info);
6746
6747 config_methods = 0;
6748 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006749 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006750 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006752 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6753 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006754 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006755 else {
6756 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006757 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006758 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006759 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006760
Dmitry Shmidt04949592012-07-19 12:16:46 -07006761 if (use == WPAS_P2P_PD_AUTO) {
6762 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6763 wpa_s->pending_pd_config_methods = config_methods;
6764 wpa_s->p2p_auto_pd = 1;
6765 wpa_s->p2p_auto_join = 0;
6766 wpa_s->pending_pd_before_join = 0;
6767 wpa_s->auto_pd_scan_retry = 0;
6768 wpas_p2p_stop_find(wpa_s);
6769 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006770 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006771 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6772 wpa_s->p2p_auto_started.sec,
6773 wpa_s->p2p_auto_started.usec);
6774 wpas_p2p_join_scan(wpa_s, NULL);
6775 return 0;
6776 }
6777
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006778 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6779 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006780 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006781 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006782
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006783 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006784 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006785 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006786}
6787
6788
6789int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6790 char *end)
6791{
6792 return p2p_scan_result_text(ies, ies_len, buf, end);
6793}
6794
6795
6796static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6797{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006798 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006799 return;
6800
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006801 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006802 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006803 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6804 wpa_s, NULL);
6805 offchannel_send_action_done(wpa_s);
6806 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006808 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6809 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006810 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006811}
6812
6813
6814int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6815 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006816 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006817 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006818 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819{
6820 wpas_p2p_clear_pending_action_tx(wpa_s);
6821 wpa_s->p2p_long_listen = 0;
6822
Dmitry Shmidt04949592012-07-19 12:16:46 -07006823 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006824 wpa_s->p2p_in_provisioning) {
6825 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6826 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6827 " (P2P disabled)" : "",
6828 wpa_s->p2p_in_provisioning ?
6829 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006830 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006832
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006833 wpa_supplicant_cancel_sched_scan(wpa_s);
6834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006836 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006837 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006838}
6839
6840
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006841static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6842 struct wpa_scan_results *scan_res)
6843{
6844 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6845
6846 if (wpa_s->p2p_scan_work) {
6847 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6848 wpa_s->p2p_scan_work = NULL;
6849 radio_work_done(work);
6850 }
6851
6852 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6853 return;
6854
6855 /*
6856 * Indicate that results have been processed so that the P2P module can
6857 * continue pending tasks.
6858 */
6859 p2p_scan_res_handled(wpa_s->global->p2p);
6860}
6861
6862
6863static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006864{
6865 wpas_p2p_clear_pending_action_tx(wpa_s);
6866 wpa_s->p2p_long_listen = 0;
6867 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6868 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006869
6870 if (wpa_s->global->p2p)
6871 p2p_stop_find(wpa_s->global->p2p);
6872
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006873 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6874 wpa_printf(MSG_DEBUG,
6875 "P2P: Do not consider the scan results after stop_find");
6876 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6877 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006878}
6879
6880
6881void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6882{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006883 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006884 if (!wpa_s->global->pending_group_iface_for_p2ps)
6885 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886}
6887
6888
6889static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6890{
6891 struct wpa_supplicant *wpa_s = eloop_ctx;
6892 wpa_s->p2p_long_listen = 0;
6893}
6894
6895
6896int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6897{
6898 int res;
6899
6900 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6901 return -1;
6902
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006903 if (wpa_s->p2p_lo_started) {
6904 wpa_printf(MSG_DEBUG,
6905 "P2P: Cannot start P2P listen, it is offloaded");
6906 return -1;
6907 }
6908
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006909 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006910 wpas_p2p_clear_pending_action_tx(wpa_s);
6911
6912 if (timeout == 0) {
6913 /*
6914 * This is a request for unlimited Listen state. However, at
6915 * least for now, this is mapped to a Listen state for one
6916 * hour.
6917 */
6918 timeout = 3600;
6919 }
6920 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6921 wpa_s->p2p_long_listen = 0;
6922
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006923 /*
6924 * Stop previous find/listen operation to avoid trying to request a new
6925 * remain-on-channel operation while the driver is still running the
6926 * previous one.
6927 */
6928 if (wpa_s->global->p2p)
6929 p2p_stop_find(wpa_s->global->p2p);
6930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6932 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6933 wpa_s->p2p_long_listen = timeout * 1000;
6934 eloop_register_timeout(timeout, 0,
6935 wpas_p2p_long_listen_timeout,
6936 wpa_s, NULL);
6937 }
6938
6939 return res;
6940}
6941
6942
6943int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6944 u8 *buf, size_t len, int p2p_group)
6945{
6946 struct wpabuf *p2p_ie;
6947 int ret;
6948
6949 if (wpa_s->global->p2p_disabled)
6950 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006951 /*
6952 * Advertize mandatory cross connection capability even on
6953 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6954 */
6955 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006956 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006957 if (wpa_s->global->p2p == NULL)
6958 return -1;
6959 if (bss == NULL)
6960 return -1;
6961
6962 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6963 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6964 p2p_group, p2p_ie);
6965 wpabuf_free(p2p_ie);
6966
6967 return ret;
6968}
6969
6970
6971int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006972 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006973 const u8 *ie, size_t ie_len,
6974 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975{
6976 if (wpa_s->global->p2p_disabled)
6977 return 0;
6978 if (wpa_s->global->p2p == NULL)
6979 return 0;
6980
Dmitry Shmidt04949592012-07-19 12:16:46 -07006981 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006982 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006983 case P2P_PREQ_NOT_P2P:
6984 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6985 ssi_signal);
6986 /* fall through */
6987 case P2P_PREQ_MALFORMED:
6988 case P2P_PREQ_NOT_LISTEN:
6989 case P2P_PREQ_NOT_PROCESSED:
6990 default: /* make gcc happy */
6991 return 0;
6992 case P2P_PREQ_PROCESSED:
6993 return 1;
6994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995}
6996
6997
6998void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6999 const u8 *sa, const u8 *bssid,
7000 u8 category, const u8 *data, size_t len, int freq)
7001{
7002 if (wpa_s->global->p2p_disabled)
7003 return;
7004 if (wpa_s->global->p2p == NULL)
7005 return;
7006
7007 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7008 freq);
7009}
7010
7011
7012void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7013{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007014 unsigned int bands;
7015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007016 if (wpa_s->global->p2p_disabled)
7017 return;
7018 if (wpa_s->global->p2p == NULL)
7019 return;
7020
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007021 bands = wpas_get_bands(wpa_s, NULL);
7022 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023}
7024
7025
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007026static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007027{
7028 p2p_group_deinit(wpa_s->p2p_group);
7029 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007030
7031 wpa_s->ap_configured_cb = NULL;
7032 wpa_s->ap_configured_cb_ctx = NULL;
7033 wpa_s->ap_configured_cb_data = NULL;
7034 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007035}
7036
7037
7038int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7039{
7040 wpa_s->p2p_long_listen = 0;
7041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7043 return -1;
7044
7045 return p2p_reject(wpa_s->global->p2p, addr);
7046}
7047
7048
7049/* Invite to reinvoke a persistent group */
7050int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007051 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007052 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08007053 int pref_freq, int he)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007054{
7055 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007056 u8 *bssid = NULL;
7057 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007058 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007059 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007060 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007061
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007062 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007063 if (peer_addr)
7064 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7065 else
7066 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067
Jouni Malinen31be0a42012-08-31 21:20:51 +03007068 wpa_s->p2p_persistent_go_freq = freq;
7069 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007070 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007071 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007072 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7073 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007074 if (ssid->mode == WPAS_MODE_P2P_GO) {
7075 role = P2P_INVITE_ROLE_GO;
7076 if (peer_addr == NULL) {
7077 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7078 "address in invitation command");
7079 return -1;
7080 }
7081 if (wpas_p2p_create_iface(wpa_s)) {
7082 if (wpas_p2p_add_group_interface(wpa_s,
7083 WPA_IF_P2P_GO) < 0) {
7084 wpa_printf(MSG_ERROR, "P2P: Failed to "
7085 "allocate a new interface for the "
7086 "group");
7087 return -1;
7088 }
7089 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007090 } else if (wpa_s->p2p_mgmt)
7091 bssid = wpa_s->parent->own_addr;
7092 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007093 bssid = wpa_s->own_addr;
7094 } else {
7095 role = P2P_INVITE_ROLE_CLIENT;
7096 peer_addr = ssid->bssid;
7097 }
7098 wpa_s->pending_invite_ssid_id = ssid->id;
7099
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007100 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007101 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007102 role == P2P_INVITE_ROLE_GO,
7103 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007104 if (res)
7105 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7108 return -1;
7109
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007110 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7111
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007112 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7113 no_pref_freq_given && pref_freq > 0 &&
7114 wpa_s->num_multichan_concurrent > 1 &&
7115 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7116 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7117 pref_freq);
7118 pref_freq = 0;
7119 }
7120
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007121 /*
7122 * Stop any find/listen operations before invitation and possibly
7123 * connection establishment.
7124 */
7125 wpas_p2p_stop_find_oper(wpa_s);
7126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007127 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007128 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007129 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007130}
7131
7132
7133/* Invite to join an active group */
7134int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7135 const u8 *peer_addr, const u8 *go_dev_addr)
7136{
7137 struct wpa_global *global = wpa_s->global;
7138 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007139 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007141 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007142 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007143 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007144 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007145
Jouni Malinen31be0a42012-08-31 21:20:51 +03007146 wpa_s->p2p_persistent_go_freq = 0;
7147 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007148 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007149 wpa_s->p2p_go_vht_center_freq2 = 0;
7150 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007152 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7153 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7154 break;
7155 }
7156 if (wpa_s == NULL) {
7157 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7158 return -1;
7159 }
7160
7161 ssid = wpa_s->current_ssid;
7162 if (ssid == NULL) {
7163 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7164 "invitation");
7165 return -1;
7166 }
7167
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007168 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007169 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007170 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007171 ssid->ssid, ssid->ssid_len);
7172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173 if (ssid->mode == WPAS_MODE_P2P_GO) {
7174 role = P2P_INVITE_ROLE_ACTIVE_GO;
7175 bssid = wpa_s->own_addr;
7176 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007177 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007178 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007179 } else {
7180 role = P2P_INVITE_ROLE_CLIENT;
7181 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7182 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7183 "invite to current group");
7184 return -1;
7185 }
7186 bssid = wpa_s->bssid;
7187 if (go_dev_addr == NULL &&
7188 !is_zero_ether_addr(wpa_s->go_dev_addr))
7189 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007190 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7191 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007192 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007193 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007195 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7196 return -1;
7197
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007198 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007199 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007200 role == P2P_INVITE_ROLE_ACTIVE_GO,
7201 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007202 if (res)
7203 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007204 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007206 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007207 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007208 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209}
7210
7211
7212void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7213{
7214 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007215 u8 go_dev_addr[ETH_ALEN];
7216 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007217 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007218 u8 ip[3 * 4];
7219 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220
Dmitry Shmidt04949592012-07-19 12:16:46 -07007221 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7222 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007223 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007224 }
7225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007227 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007228
7229 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007230 if (!wpa_s->p2p_go_group_formation_completed &&
7231 wpa_s->global->p2p_group_formation == wpa_s) {
7232 wpa_dbg(wpa_s, MSG_DEBUG,
7233 "P2P: Marking group formation completed on client on data connection");
7234 wpa_s->p2p_go_group_formation_completed = 1;
7235 wpa_s->global->p2p_group_formation = NULL;
7236 wpa_s->p2p_in_provisioning = 0;
7237 wpa_s->p2p_in_invitation = 0;
7238 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007240 os_memset(go_dev_addr, 0, ETH_ALEN);
7241 if (ssid->bssid_set)
7242 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7243 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7244 ssid->ssid_len);
7245 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7246
7247 if (wpa_s->global->p2p_group_formation == wpa_s)
7248 wpa_s->global->p2p_group_formation = NULL;
7249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007250 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7251 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007252
7253 ip_addr[0] = '\0';
7254 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007255 int res;
7256
7257 res = os_snprintf(ip_addr, sizeof(ip_addr),
7258 " ip_addr=%u.%u.%u.%u "
7259 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7260 ip[0], ip[1], ip[2], ip[3],
7261 ip[4], ip[5], ip[6], ip[7],
7262 ip[8], ip[9], ip[10], ip[11]);
7263 if (os_snprintf_error(sizeof(ip_addr), res))
7264 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007265 }
7266
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007267 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7268 ssid->passphrase == NULL && ssid->psk_set ?
7269 ssid->psk : NULL,
7270 ssid->passphrase, go_dev_addr, persistent,
7271 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007272
7273 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007274 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7275 ssid, go_dev_addr);
7276
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007277 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007278}
7279
7280
7281int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7282 u32 interval1, u32 duration2, u32 interval2)
7283{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007284 int ret;
7285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7287 return -1;
7288
7289 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7290 wpa_s->current_ssid == NULL ||
7291 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7292 return -1;
7293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007294 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7295 wpa_s->own_addr, wpa_s->assoc_freq,
7296 duration1, interval1, duration2, interval2);
7297 if (ret == 0)
7298 wpa_s->waiting_presence_resp = 1;
7299
7300 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007301}
7302
7303
7304int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7305 unsigned int interval)
7306{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007307 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7308 return -1;
7309
7310 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7311}
7312
7313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007314static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7315{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007316 if (wpa_s->current_ssid == NULL) {
7317 /*
7318 * current_ssid can be cleared when P2P client interface gets
7319 * disconnected, so assume this interface was used as P2P
7320 * client.
7321 */
7322 return 1;
7323 }
7324 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007325 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7326}
7327
7328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007329static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7330{
7331 struct wpa_supplicant *wpa_s = eloop_ctx;
7332
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007333 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007334 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7335 "disabled");
7336 return;
7337 }
7338
Dmitry Shmidt04949592012-07-19 12:16:46 -07007339 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7340 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007341 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007342}
7343
7344
7345static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7346{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007347 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007348
Dmitry Shmidt04949592012-07-19 12:16:46 -07007349 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7350 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7353 return;
7354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007355 timeout = wpa_s->conf->p2p_group_idle;
7356 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7357 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7358 timeout = P2P_MAX_CLIENT_IDLE;
7359
7360 if (timeout == 0)
7361 return;
7362
Dmitry Shmidt04949592012-07-19 12:16:46 -07007363 if (timeout < 0) {
7364 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7365 timeout = 0; /* special client mode no-timeout */
7366 else
7367 return;
7368 }
7369
7370 if (wpa_s->p2p_in_provisioning) {
7371 /*
7372 * Use the normal group formation timeout during the
7373 * provisioning phase to avoid terminating this process too
7374 * early due to group idle timeout.
7375 */
7376 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7377 "during provisioning");
7378 return;
7379 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307380
Dmitry Shmidt04949592012-07-19 12:16:46 -07007381 if (wpa_s->show_group_started) {
7382 /*
7383 * Use the normal group formation timeout between the end of
7384 * the provisioning phase and completion of 4-way handshake to
7385 * avoid terminating this process too early due to group idle
7386 * timeout.
7387 */
7388 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7389 "while waiting for initial 4-way handshake to "
7390 "complete");
7391 return;
7392 }
7393
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007395 timeout);
7396 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7397 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007398}
7399
7400
Jouni Malinen2b89da82012-08-31 22:04:41 +03007401/* Returns 1 if the interface was removed */
7402int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7403 u16 reason_code, const u8 *ie, size_t ie_len,
7404 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007405{
7406 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007407 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007408
Dmitry Shmidt04949592012-07-19 12:16:46 -07007409 if (!locally_generated)
7410 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7411 ie_len);
7412
7413 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7414 wpa_s->current_ssid &&
7415 wpa_s->current_ssid->p2p_group &&
7416 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7417 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7418 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007419 if (wpas_p2p_group_delete(wpa_s,
7420 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7421 > 0)
7422 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007423 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007424
7425 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007426}
7427
7428
7429void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007430 u16 reason_code, const u8 *ie, size_t ie_len,
7431 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007432{
7433 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7434 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007435
Dmitry Shmidt04949592012-07-19 12:16:46 -07007436 if (!locally_generated)
7437 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7438 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007439}
7440
7441
7442void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7443{
7444 struct p2p_data *p2p = wpa_s->global->p2p;
7445
7446 if (p2p == NULL)
7447 return;
7448
7449 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7450 return;
7451
7452 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7453 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7454
7455 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7456 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7457
7458 if (wpa_s->wps &&
7459 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7460 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7461
7462 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7463 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7464
7465 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7466 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7467 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7468 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7469 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7470 }
7471
7472 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7473 p2p_set_sec_dev_types(p2p,
7474 (void *) wpa_s->conf->sec_device_type,
7475 wpa_s->conf->num_sec_device_types);
7476
7477 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7478 int i;
7479 p2p_remove_wps_vendor_extensions(p2p);
7480 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7481 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7482 continue;
7483 p2p_add_wps_vendor_extension(
7484 p2p, wpa_s->conf->wps_vendor_ext[i]);
7485 }
7486 }
7487
7488 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7489 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7490 char country[3];
7491 country[0] = wpa_s->conf->country[0];
7492 country[1] = wpa_s->conf->country[1];
7493 country[2] = 0x04;
7494 p2p_set_country(p2p, country);
7495 }
7496
7497 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7498 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7499 wpa_s->conf->p2p_ssid_postfix ?
7500 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7501 0);
7502 }
7503
7504 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7505 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007506
7507 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7508 u8 reg_class, channel;
7509 int ret;
7510 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007511 u8 channel_forced;
7512
Jouni Malinen75ecf522011-06-27 15:19:46 -07007513 if (wpa_s->conf->p2p_listen_reg_class &&
7514 wpa_s->conf->p2p_listen_channel) {
7515 reg_class = wpa_s->conf->p2p_listen_reg_class;
7516 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007517 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007518 } else {
7519 reg_class = 81;
7520 /*
7521 * Pick one of the social channels randomly as the
7522 * listen channel.
7523 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007524 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7525 channel = 1;
7526 else
7527 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007528 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007529 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007530 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7531 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007532 if (ret)
7533 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7534 "failed: %d", ret);
7535 }
7536 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7537 u8 op_reg_class, op_channel, cfg_op_channel;
7538 int ret = 0;
7539 unsigned int r;
7540 if (wpa_s->conf->p2p_oper_reg_class &&
7541 wpa_s->conf->p2p_oper_channel) {
7542 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7543 op_channel = wpa_s->conf->p2p_oper_channel;
7544 cfg_op_channel = 1;
7545 } else {
7546 op_reg_class = 81;
7547 /*
7548 * Use random operation channel from (1, 6, 11)
7549 *if no other preference is indicated.
7550 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007551 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7552 op_channel = 1;
7553 else
7554 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007555 cfg_op_channel = 0;
7556 }
7557 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7558 cfg_op_channel);
7559 if (ret)
7560 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7561 "failed: %d", ret);
7562 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007563
7564 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7565 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7566 wpa_s->conf->p2p_pref_chan) < 0) {
7567 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7568 "update failed");
7569 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007570
7571 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7572 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7573 "update failed");
7574 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007575 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007576
7577 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7578 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007579}
7580
7581
7582int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7583 int duration)
7584{
7585 if (!wpa_s->ap_iface)
7586 return -1;
7587 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7588 duration);
7589}
7590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007591
7592int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7593{
7594 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7595 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007596
7597 wpa_s->global->cross_connection = enabled;
7598 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7599
7600 if (!enabled) {
7601 struct wpa_supplicant *iface;
7602
7603 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7604 {
7605 if (iface->cross_connect_enabled == 0)
7606 continue;
7607
7608 iface->cross_connect_enabled = 0;
7609 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007610 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007611 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7612 iface->ifname,
7613 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007614 }
7615 }
7616
7617 return 0;
7618}
7619
7620
7621static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7622{
7623 struct wpa_supplicant *iface;
7624
7625 if (!uplink->global->cross_connection)
7626 return;
7627
7628 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7629 if (!iface->cross_connect_enabled)
7630 continue;
7631 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7632 0)
7633 continue;
7634 if (iface->ap_iface == NULL)
7635 continue;
7636 if (iface->cross_connect_in_use)
7637 continue;
7638
7639 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007640 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007641 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7642 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007643 }
7644}
7645
7646
7647static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7648{
7649 struct wpa_supplicant *iface;
7650
7651 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7652 if (!iface->cross_connect_enabled)
7653 continue;
7654 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7655 0)
7656 continue;
7657 if (!iface->cross_connect_in_use)
7658 continue;
7659
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007660 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007661 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7662 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007663 iface->cross_connect_in_use = 0;
7664 }
7665}
7666
7667
7668void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7669{
7670 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7671 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7672 wpa_s->cross_connect_disallowed)
7673 wpas_p2p_disable_cross_connect(wpa_s);
7674 else
7675 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007676 if (!wpa_s->ap_iface &&
7677 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7678 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007679}
7680
7681
7682void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7683{
7684 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007685 if (!wpa_s->ap_iface &&
7686 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7687 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688 wpas_p2p_set_group_idle_timeout(wpa_s);
7689}
7690
7691
7692static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7693{
7694 struct wpa_supplicant *iface;
7695
7696 if (!wpa_s->global->cross_connection)
7697 return;
7698
7699 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7700 if (iface == wpa_s)
7701 continue;
7702 if (iface->drv_flags &
7703 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7704 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007705 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7706 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007707 continue;
7708
7709 wpa_s->cross_connect_enabled = 1;
7710 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7711 sizeof(wpa_s->cross_connect_uplink));
7712 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7713 "%s to %s whenever uplink is available",
7714 wpa_s->ifname, wpa_s->cross_connect_uplink);
7715
7716 if (iface->ap_iface || iface->current_ssid == NULL ||
7717 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7718 iface->cross_connect_disallowed ||
7719 iface->wpa_state != WPA_COMPLETED)
7720 break;
7721
7722 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007723 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007724 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7725 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007726 break;
7727 }
7728}
7729
7730
7731int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7732{
7733 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7734 !wpa_s->p2p_in_provisioning)
7735 return 0; /* not P2P client operation */
7736
7737 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7738 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007739 if (wpa_s != wpa_s->p2pdev)
7740 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007741 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007742 return 1;
7743}
7744
7745
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007746void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7747{
7748 struct wpa_supplicant *wpa_s = eloop_ctx;
7749 wpas_p2p_notif_pbc_overlap(wpa_s);
7750}
7751
7752
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007753void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7754 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007756 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007757 struct wpa_used_freq_data *freqs = NULL;
7758 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007759
7760 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7761 return;
7762
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007763 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7764 if (!freqs)
7765 return;
7766
7767 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007769 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007770 os_memset(&cli_chan, 0, sizeof(cli_chan));
7771 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007772 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7773 "channel list");
7774 return;
7775 }
7776
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007777 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007778
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007779 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007780
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007781 /*
7782 * The used frequencies map changed, so it is possible that a GO is
7783 * using a channel that is no longer valid for P2P use. It is also
7784 * possible that due to policy consideration, it would be preferable to
7785 * move it to a frequency already used by other station interfaces.
7786 */
7787 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7788
7789 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007790}
7791
7792
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007793static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7794 struct wpa_scan_results *scan_res)
7795{
7796 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7797}
7798
7799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007800int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7801{
7802 struct wpa_global *global = wpa_s->global;
7803 int found = 0;
7804 const u8 *peer;
7805
7806 if (global->p2p == NULL)
7807 return -1;
7808
7809 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7810
7811 if (wpa_s->pending_interface_name[0] &&
7812 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7813 found = 1;
7814
7815 peer = p2p_get_go_neg_peer(global->p2p);
7816 if (peer) {
7817 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7818 MACSTR, MAC2STR(peer));
7819 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007820 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007821 }
7822
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007823 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7824 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7825 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7826 found = 1;
7827 }
7828
7829 if (wpa_s->pending_pd_before_join) {
7830 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7831 wpa_s->pending_pd_before_join = 0;
7832 found = 1;
7833 }
7834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007835 wpas_p2p_stop_find(wpa_s);
7836
7837 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7838 if (wpa_s == global->p2p_group_formation &&
7839 (wpa_s->p2p_in_provisioning ||
7840 wpa_s->parent->pending_interface_type ==
7841 WPA_IF_P2P_CLIENT)) {
7842 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7843 "formation found - cancelling",
7844 wpa_s->ifname);
7845 found = 1;
7846 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007847 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007848 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007849 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007850 break;
7851 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007852 wpas_p2p_group_delete(wpa_s,
7853 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007854 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007855 } else if (wpa_s->p2p_in_invitation) {
7856 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7857 wpa_s->ifname);
7858 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007859 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007860 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007861 }
7862 }
7863
7864 if (!found) {
7865 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7866 return -1;
7867 }
7868
7869 return 0;
7870}
7871
7872
7873void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7874{
7875 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7876 return;
7877
7878 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7879 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007880 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007881}
7882
7883
7884void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7885 int freq_24, int freq_5, int freq_overall)
7886{
7887 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007888 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889 return;
7890 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7891}
7892
7893
7894int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7895{
7896 u8 peer[ETH_ALEN];
7897 struct p2p_data *p2p = wpa_s->global->p2p;
7898
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007899 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007900 return -1;
7901
7902 if (hwaddr_aton(addr, peer))
7903 return -1;
7904
7905 return p2p_unauthorize(p2p, peer);
7906}
7907
7908
7909/**
7910 * wpas_p2p_disconnect - Disconnect from a P2P Group
7911 * @wpa_s: Pointer to wpa_supplicant data
7912 * Returns: 0 on success, -1 on failure
7913 *
7914 * This can be used to disconnect from a group in which the local end is a P2P
7915 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7916 * virtual network interface was created for this group, that interface will be
7917 * removed. Otherwise, only the configured P2P group network will be removed
7918 * from the interface.
7919 */
7920int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7921{
7922
7923 if (wpa_s == NULL)
7924 return -1;
7925
Jouni Malinen2b89da82012-08-31 22:04:41 +03007926 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7927 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007929
7930
7931int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7932{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007933 int ret;
7934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007935 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7936 return 0;
7937
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007938 ret = p2p_in_progress(wpa_s->global->p2p);
7939 if (ret == 0) {
7940 /*
7941 * Check whether there is an ongoing WPS provisioning step (or
7942 * other parts of group formation) on another interface since
7943 * p2p_in_progress() does not report this to avoid issues for
7944 * scans during such provisioning step.
7945 */
7946 if (wpa_s->global->p2p_group_formation &&
7947 wpa_s->global->p2p_group_formation != wpa_s) {
7948 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7949 "in group formation",
7950 wpa_s->global->p2p_group_formation->ifname);
7951 ret = 1;
7952 }
7953 }
7954
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007955 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007956 struct os_reltime now;
7957 os_get_reltime(&now);
7958 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7959 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007960 /* Wait for the first client has expired */
7961 wpa_s->global->p2p_go_wait_client.sec = 0;
7962 } else {
7963 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7964 ret = 1;
7965 }
7966 }
7967
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007968 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007969}
7970
7971
7972void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7973 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007974{
7975 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7976 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007977 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007978 /**
7979 * Remove the network by scheduling the group formation
7980 * timeout to happen immediately. The teardown code
7981 * needs to be scheduled to run asynch later so that we
7982 * don't delete data from under ourselves unexpectedly.
7983 * Calling wpas_p2p_group_formation_timeout directly
7984 * causes a series of crashes in WPS failure scenarios.
7985 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007986 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7987 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007988 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007989 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007990 }
7991}
7992
7993
7994struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007995 const u8 *addr, const u8 *ssid,
7996 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007997{
7998 struct wpa_ssid *s;
7999 size_t i;
8000
8001 for (s = wpa_s->conf->ssid; s; s = s->next) {
8002 if (s->disabled != 2)
8003 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008004 if (ssid &&
8005 (ssid_len != s->ssid_len ||
8006 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8007 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008008 if (addr == NULL) {
8009 if (s->mode == WPAS_MODE_P2P_GO)
8010 return s;
8011 continue;
8012 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008013 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8014 return s; /* peer is GO in the persistent group */
8015 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8016 continue;
8017 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008018 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008019 addr, ETH_ALEN) == 0)
8020 return s; /* peer is P2P client in persistent
8021 * group */
8022 }
8023 }
8024
8025 return NULL;
8026}
8027
8028
8029void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8030 const u8 *addr)
8031{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008032 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008033 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008034 /*
8035 * This can happen if WPS provisioning step is not terminated
8036 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8037 * peer was able to connect, there is no need to time out group
8038 * formation after this, though. In addition, this is used with
8039 * the initial connection wait on the GO as a separate formation
8040 * timeout and as such, expected to be hit after the initial WPS
8041 * provisioning step.
8042 */
8043 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008044
8045 if (!wpa_s->p2p_go_group_formation_completed &&
8046 !wpa_s->group_formation_reported) {
8047 /*
8048 * GO has not yet notified group formation success since
8049 * the WPS step was not completed cleanly. Do that
8050 * notification now since the P2P Client was able to
8051 * connect and as such, must have received the
8052 * credential from the WPS step.
8053 */
8054 if (wpa_s->global->p2p)
8055 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008056 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008057 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008058 }
8059 if (!wpa_s->p2p_go_group_formation_completed) {
8060 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8061 wpa_s->p2p_go_group_formation_completed = 1;
8062 wpa_s->global->p2p_group_formation = NULL;
8063 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008064 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008065 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008066 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008067 if (addr == NULL)
8068 return;
8069 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8070}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008071
Dmitry Shmidt04949592012-07-19 12:16:46 -07008072
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008073static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8074 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008075{
8076 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008077 int ret = 0;
8078
Dmitry Shmidt04949592012-07-19 12:16:46 -07008079 if (wpa_s->global->p2p_group_formation)
8080 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008081 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008082 offchannel_send_action_done(wpa_s);
8083 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008084 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008085 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8086 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8087 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8088 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008089 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008090 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008091 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008092 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008093 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008094 wpa_s->p2p_go_max_oper_chwidth,
8095 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008096 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008097}
8098
8099
8100int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8101{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008102 int res;
8103
Dmitry Shmidt04949592012-07-19 12:16:46 -07008104 if (!wpa_s->p2p_fallback_to_go_neg ||
8105 wpa_s->p2p_in_provisioning <= 5)
8106 return 0;
8107
8108 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8109 return 0; /* peer operating as a GO */
8110
8111 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8112 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008113 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008114 "reason=GO-not-found");
8115 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008116
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008117 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008118}
8119
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008120
8121unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8122{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008123 struct wpa_supplicant *ifs;
8124
8125 if (wpa_s->wpa_state > WPA_SCANNING) {
8126 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8127 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008128 wpa_s->conf->p2p_search_delay);
8129 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008130 }
8131
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008132 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8133 radio_list) {
8134 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008135 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8136 "delay due to concurrent operation on "
8137 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008138 wpa_s->conf->p2p_search_delay,
8139 ifs->ifname);
8140 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008141 }
8142 }
8143
8144 return 0;
8145}
8146
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008147
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008148static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8149 struct wpa_ssid *s, const u8 *addr,
8150 int iface_addr)
8151{
8152 struct psk_list_entry *psk, *tmp;
8153 int changed = 0;
8154
8155 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8156 list) {
8157 if ((iface_addr && !psk->p2p &&
8158 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8159 (!iface_addr && psk->p2p &&
8160 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8161 wpa_dbg(wpa_s, MSG_DEBUG,
8162 "P2P: Remove persistent group PSK list entry for "
8163 MACSTR " p2p=%u",
8164 MAC2STR(psk->addr), psk->p2p);
8165 dl_list_del(&psk->list);
8166 os_free(psk);
8167 changed++;
8168 }
8169 }
8170
8171 return changed;
8172}
8173
8174
8175void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8176 const u8 *p2p_dev_addr,
8177 const u8 *psk, size_t psk_len)
8178{
8179 struct wpa_ssid *ssid = wpa_s->current_ssid;
8180 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008181 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008182
8183 if (psk_len != sizeof(p->psk))
8184 return;
8185
8186 if (p2p_dev_addr) {
8187 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8188 " p2p_dev_addr=" MACSTR,
8189 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8190 if (is_zero_ether_addr(p2p_dev_addr))
8191 p2p_dev_addr = NULL;
8192 } else {
8193 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8194 MAC2STR(mac_addr));
8195 }
8196
8197 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8198 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8199 /* To be added to persistent group once created */
8200 if (wpa_s->global->add_psk == NULL) {
8201 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8202 if (wpa_s->global->add_psk == NULL)
8203 return;
8204 }
8205 p = wpa_s->global->add_psk;
8206 if (p2p_dev_addr) {
8207 p->p2p = 1;
8208 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8209 } else {
8210 p->p2p = 0;
8211 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8212 }
8213 os_memcpy(p->psk, psk, psk_len);
8214 return;
8215 }
8216
8217 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8218 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8219 return;
8220 }
8221
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008222 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008223 ssid->ssid_len);
8224 if (!persistent) {
8225 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8226 return;
8227 }
8228
8229 p = os_zalloc(sizeof(*p));
8230 if (p == NULL)
8231 return;
8232 if (p2p_dev_addr) {
8233 p->p2p = 1;
8234 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8235 } else {
8236 p->p2p = 0;
8237 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8238 }
8239 os_memcpy(p->psk, psk, psk_len);
8240
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008241 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8242 (last = dl_list_last(&persistent->psk_list,
8243 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008244 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8245 MACSTR " (p2p=%u) to make room for a new one",
8246 MAC2STR(last->addr), last->p2p);
8247 dl_list_del(&last->list);
8248 os_free(last);
8249 }
8250
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008251 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008252 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8253 p2p_dev_addr == NULL);
8254 if (p2p_dev_addr) {
8255 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8256 MACSTR, MAC2STR(p2p_dev_addr));
8257 } else {
8258 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8259 MAC2STR(mac_addr));
8260 }
8261 dl_list_add(&persistent->psk_list, &p->list);
8262
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008263 if (wpa_s->p2pdev->conf->update_config &&
8264 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008265 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008266}
8267
8268
8269static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8270 struct wpa_ssid *s, const u8 *addr,
8271 int iface_addr)
8272{
8273 int res;
8274
8275 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008276 if (res > 0 && wpa_s->conf->update_config &&
8277 wpa_config_write(wpa_s->confname, wpa_s->conf))
8278 wpa_dbg(wpa_s, MSG_DEBUG,
8279 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008280}
8281
8282
8283static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8284 const u8 *peer, int iface_addr)
8285{
8286 struct hostapd_data *hapd;
8287 struct hostapd_wpa_psk *psk, *prev, *rem;
8288 struct sta_info *sta;
8289
8290 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8291 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8292 return;
8293
8294 /* Remove per-station PSK entry */
8295 hapd = wpa_s->ap_iface->bss[0];
8296 prev = NULL;
8297 psk = hapd->conf->ssid.wpa_psk;
8298 while (psk) {
8299 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8300 (!iface_addr &&
8301 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8303 MACSTR " iface_addr=%d",
8304 MAC2STR(peer), iface_addr);
8305 if (prev)
8306 prev->next = psk->next;
8307 else
8308 hapd->conf->ssid.wpa_psk = psk->next;
8309 rem = psk;
8310 psk = psk->next;
8311 os_free(rem);
8312 } else {
8313 prev = psk;
8314 psk = psk->next;
8315 }
8316 }
8317
8318 /* Disconnect from group */
8319 if (iface_addr)
8320 sta = ap_get_sta(hapd, peer);
8321 else
8322 sta = ap_get_sta_p2p(hapd, peer);
8323 if (sta) {
8324 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8325 " (iface_addr=%d) from group",
8326 MAC2STR(peer), iface_addr);
8327 hostapd_drv_sta_deauth(hapd, sta->addr,
8328 WLAN_REASON_DEAUTH_LEAVING);
8329 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8330 }
8331}
8332
8333
8334void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8335 int iface_addr)
8336{
8337 struct wpa_ssid *s;
8338 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008339 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008340
8341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8342
8343 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008344 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008345 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8346 continue;
8347 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008348 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8349 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008350 }
8351
8352 /* Remove from any operating group */
8353 for (w = wpa_s->global->ifaces; w; w = w->next)
8354 wpas_p2p_remove_client_go(w, peer, iface_addr);
8355}
8356
8357
8358static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8359{
8360 struct wpa_supplicant *wpa_s = eloop_ctx;
8361 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8362}
8363
8364
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008365static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8366{
8367 struct wpa_supplicant *wpa_s = eloop_ctx;
8368
8369 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8370 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8371}
8372
8373
8374int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8375 struct wpa_ssid *ssid)
8376{
8377 struct wpa_supplicant *iface;
8378
8379 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8380 if (!iface->current_ssid ||
8381 iface->current_ssid->frequency == freq ||
8382 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8383 !iface->current_ssid->p2p_group))
8384 continue;
8385
8386 /* Remove the connection with least priority */
8387 if (!wpas_is_p2p_prioritized(iface)) {
8388 /* STA connection has priority over existing
8389 * P2P connection, so remove the interface. */
8390 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8391 eloop_register_timeout(0, 0,
8392 wpas_p2p_group_freq_conflict,
8393 iface, NULL);
8394 /* If connection in progress is P2P connection, do not
8395 * proceed for the connection. */
8396 if (wpa_s == iface)
8397 return -1;
8398 else
8399 return 0;
8400 } else {
8401 /* P2P connection has priority, disable the STA network
8402 */
8403 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8404 ssid);
8405 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8406 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8407 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8408 ETH_ALEN);
8409 /* If P2P connection is in progress, continue
8410 * connecting...*/
8411 if (wpa_s == iface)
8412 return 0;
8413 else
8414 return -1;
8415 }
8416 }
8417
8418 return 0;
8419}
8420
8421
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008422int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8423{
8424 struct wpa_ssid *ssid = wpa_s->current_ssid;
8425
8426 if (ssid == NULL || !ssid->p2p_group)
8427 return 0;
8428
8429 if (wpa_s->p2p_last_4way_hs_fail &&
8430 wpa_s->p2p_last_4way_hs_fail == ssid) {
8431 u8 go_dev_addr[ETH_ALEN];
8432 struct wpa_ssid *persistent;
8433
8434 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8435 ssid->ssid,
8436 ssid->ssid_len) <= 0) {
8437 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8438 goto disconnect;
8439 }
8440
8441 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8442 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008443 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008444 ssid->ssid,
8445 ssid->ssid_len);
8446 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8447 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8448 goto disconnect;
8449 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008450 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008451 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8452 persistent->id);
8453 disconnect:
8454 wpa_s->p2p_last_4way_hs_fail = NULL;
8455 /*
8456 * Remove the group from a timeout to avoid issues with caller
8457 * continuing to use the interface if this is on a P2P group
8458 * interface.
8459 */
8460 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8461 wpa_s, NULL);
8462 return 1;
8463 }
8464
8465 wpa_s->p2p_last_4way_hs_fail = ssid;
8466 return 0;
8467}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008468
8469
8470#ifdef CONFIG_WPS_NFC
8471
8472static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8473 struct wpabuf *p2p)
8474{
8475 struct wpabuf *ret;
8476 size_t wsc_len;
8477
8478 if (p2p == NULL) {
8479 wpabuf_free(wsc);
8480 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8481 return NULL;
8482 }
8483
8484 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8485 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8486 if (ret == NULL) {
8487 wpabuf_free(wsc);
8488 wpabuf_free(p2p);
8489 return NULL;
8490 }
8491
8492 wpabuf_put_be16(ret, wsc_len);
8493 if (wsc)
8494 wpabuf_put_buf(ret, wsc);
8495 wpabuf_put_be16(ret, wpabuf_len(p2p));
8496 wpabuf_put_buf(ret, p2p);
8497
8498 wpabuf_free(wsc);
8499 wpabuf_free(p2p);
8500 wpa_hexdump_buf(MSG_DEBUG,
8501 "P2P: Generated NFC connection handover message", ret);
8502
8503 if (ndef && ret) {
8504 struct wpabuf *tmp;
8505 tmp = ndef_build_p2p(ret);
8506 wpabuf_free(ret);
8507 if (tmp == NULL) {
8508 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8509 return NULL;
8510 }
8511 ret = tmp;
8512 }
8513
8514 return ret;
8515}
8516
8517
8518static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8519 struct wpa_ssid **ssid, u8 *go_dev_addr)
8520{
8521 struct wpa_supplicant *iface;
8522
8523 if (go_dev_addr)
8524 os_memset(go_dev_addr, 0, ETH_ALEN);
8525 if (ssid)
8526 *ssid = NULL;
8527 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8528 if (iface->wpa_state < WPA_ASSOCIATING ||
8529 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8530 !iface->current_ssid->p2p_group ||
8531 iface->current_ssid->mode != WPAS_MODE_INFRA)
8532 continue;
8533 if (ssid)
8534 *ssid = iface->current_ssid;
8535 if (go_dev_addr)
8536 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8537 return iface->assoc_freq;
8538 }
8539 return 0;
8540}
8541
8542
8543struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8544 int ndef)
8545{
8546 struct wpabuf *wsc, *p2p;
8547 struct wpa_ssid *ssid;
8548 u8 go_dev_addr[ETH_ALEN];
8549 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8550
8551 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8552 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8553 return NULL;
8554 }
8555
8556 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8557 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8558 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8559 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8560 return NULL;
8561 }
8562
8563 if (cli_freq == 0) {
8564 wsc = wps_build_nfc_handover_req_p2p(
8565 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8566 } else
8567 wsc = NULL;
8568 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8569 go_dev_addr, ssid ? ssid->ssid : NULL,
8570 ssid ? ssid->ssid_len : 0);
8571
8572 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8573}
8574
8575
8576struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8577 int ndef, int tag)
8578{
8579 struct wpabuf *wsc, *p2p;
8580 struct wpa_ssid *ssid;
8581 u8 go_dev_addr[ETH_ALEN];
8582 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8583
8584 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8585 return NULL;
8586
8587 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8588 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8589 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8590 return NULL;
8591
8592 if (cli_freq == 0) {
8593 wsc = wps_build_nfc_handover_sel_p2p(
8594 wpa_s->parent->wps,
8595 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8596 DEV_PW_NFC_CONNECTION_HANDOVER,
8597 wpa_s->conf->wps_nfc_dh_pubkey,
8598 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8599 } else
8600 wsc = NULL;
8601 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8602 go_dev_addr, ssid ? ssid->ssid : NULL,
8603 ssid ? ssid->ssid_len : 0);
8604
8605 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8606}
8607
8608
8609static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8610 struct p2p_nfc_params *params)
8611{
8612 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8613 "connection handover (freq=%d)",
8614 params->go_freq);
8615
8616 if (params->go_freq && params->go_ssid_len) {
8617 wpa_s->p2p_wps_method = WPS_NFC;
8618 wpa_s->pending_join_wps_method = WPS_NFC;
8619 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8620 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8621 ETH_ALEN);
8622 return wpas_p2p_join_start(wpa_s, params->go_freq,
8623 params->go_ssid,
8624 params->go_ssid_len);
8625 }
8626
8627 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8628 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008629 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008630 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008631 wpa_s->p2p_go_he,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008632 params->go_ssid_len ? params->go_ssid : NULL,
8633 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008634}
8635
8636
8637static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8638 struct p2p_nfc_params *params, int tag)
8639{
8640 int res, persistent;
8641 struct wpa_ssid *ssid;
8642
8643 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8644 "connection handover");
8645 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8646 ssid = wpa_s->current_ssid;
8647 if (ssid == NULL)
8648 continue;
8649 if (ssid->mode != WPAS_MODE_P2P_GO)
8650 continue;
8651 if (wpa_s->ap_iface == NULL)
8652 continue;
8653 break;
8654 }
8655 if (wpa_s == NULL) {
8656 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8657 return -1;
8658 }
8659
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008660 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008661 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008662 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008663 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8664 return -1;
8665 }
8666 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008667 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8668 wpa_s->p2pdev->p2p_oob_dev_pw,
8669 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8670 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008671 if (res)
8672 return res;
8673
8674 if (!tag) {
8675 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8676 return 0;
8677 }
8678
8679 if (!params->peer ||
8680 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8681 return 0;
8682
8683 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8684 " to join", MAC2STR(params->peer->p2p_device_addr));
8685
8686 wpa_s->global->p2p_invite_group = wpa_s;
8687 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008688 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008689 params->peer->p2p_device_addr,
8690 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008691 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008692
8693 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8694 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8695 ssid->ssid, ssid->ssid_len, ssid->frequency,
8696 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008697 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008698}
8699
8700
8701static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8702 struct p2p_nfc_params *params,
8703 int forced_freq)
8704{
8705 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8706 "connection handover");
8707 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8708 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008709 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008710 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008711 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008712}
8713
8714
8715static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8716 struct p2p_nfc_params *params,
8717 int forced_freq)
8718{
8719 int res;
8720
8721 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8722 "connection handover");
8723 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8724 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008725 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008726 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08008727 wpa_s->p2p_go_he, NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008728 if (res)
8729 return res;
8730
8731 res = wpas_p2p_listen(wpa_s, 60);
8732 if (res) {
8733 p2p_unauthorize(wpa_s->global->p2p,
8734 params->peer->p2p_device_addr);
8735 }
8736
8737 return res;
8738}
8739
8740
8741static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8742 const struct wpabuf *data,
8743 int sel, int tag, int forced_freq)
8744{
8745 const u8 *pos, *end;
8746 u16 len, id;
8747 struct p2p_nfc_params params;
8748 int res;
8749
8750 os_memset(&params, 0, sizeof(params));
8751 params.sel = sel;
8752
8753 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8754
8755 pos = wpabuf_head(data);
8756 end = pos + wpabuf_len(data);
8757
8758 if (end - pos < 2) {
8759 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8760 "attributes");
8761 return -1;
8762 }
8763 len = WPA_GET_BE16(pos);
8764 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008765 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008766 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8767 "attributes");
8768 return -1;
8769 }
8770 params.wsc_attr = pos;
8771 params.wsc_len = len;
8772 pos += len;
8773
8774 if (end - pos < 2) {
8775 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8776 "attributes");
8777 return -1;
8778 }
8779 len = WPA_GET_BE16(pos);
8780 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008781 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008782 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8783 "attributes");
8784 return -1;
8785 }
8786 params.p2p_attr = pos;
8787 params.p2p_len = len;
8788 pos += len;
8789
8790 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8791 params.wsc_attr, params.wsc_len);
8792 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8793 params.p2p_attr, params.p2p_len);
8794 if (pos < end) {
8795 wpa_hexdump(MSG_DEBUG,
8796 "P2P: Ignored extra data after P2P attributes",
8797 pos, end - pos);
8798 }
8799
8800 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8801 if (res)
8802 return res;
8803
8804 if (params.next_step == NO_ACTION)
8805 return 0;
8806
8807 if (params.next_step == BOTH_GO) {
8808 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8809 MAC2STR(params.peer->p2p_device_addr));
8810 return 0;
8811 }
8812
8813 if (params.next_step == PEER_CLIENT) {
8814 if (!is_zero_ether_addr(params.go_dev_addr)) {
8815 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8816 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8817 " ssid=\"%s\"",
8818 MAC2STR(params.peer->p2p_device_addr),
8819 params.go_freq,
8820 MAC2STR(params.go_dev_addr),
8821 wpa_ssid_txt(params.go_ssid,
8822 params.go_ssid_len));
8823 } else {
8824 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8825 "peer=" MACSTR " freq=%d",
8826 MAC2STR(params.peer->p2p_device_addr),
8827 params.go_freq);
8828 }
8829 return 0;
8830 }
8831
8832 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8833 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8834 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8835 return 0;
8836 }
8837
8838 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8839 wpa_s->p2p_oob_dev_pw = NULL;
8840
8841 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8842 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8843 "received");
8844 return -1;
8845 }
8846
8847 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8848 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8849 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8850 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8851 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8852 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8853 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8854
8855 if (tag) {
8856 if (id < 0x10) {
8857 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8858 "peer OOB Device Password Id %u", id);
8859 return -1;
8860 }
8861 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8862 "Device Password Id %u", id);
8863 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8864 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8865 params.oob_dev_pw_len -
8866 WPS_OOB_PUBKEY_HASH_LEN - 2);
8867 wpa_s->p2p_oob_dev_pw_id = id;
8868 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8869 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8870 params.oob_dev_pw_len -
8871 WPS_OOB_PUBKEY_HASH_LEN - 2);
8872 if (wpa_s->p2p_oob_dev_pw == NULL)
8873 return -1;
8874
8875 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8876 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8877 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8878 return -1;
8879 } else {
8880 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8881 "without Device Password");
8882 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8883 }
8884
8885 switch (params.next_step) {
8886 case NO_ACTION:
8887 case BOTH_GO:
8888 case PEER_CLIENT:
8889 /* already covered above */
8890 return 0;
8891 case JOIN_GROUP:
8892 return wpas_p2p_nfc_join_group(wpa_s, &params);
8893 case AUTH_JOIN:
8894 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8895 case INIT_GO_NEG:
8896 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8897 case RESP_GO_NEG:
8898 /* TODO: use own OOB Dev Pw */
8899 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8900 }
8901
8902 return -1;
8903}
8904
8905
8906int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8907 const struct wpabuf *data, int forced_freq)
8908{
8909 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8910 return -1;
8911
8912 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8913}
8914
8915
8916int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8917 const struct wpabuf *req,
8918 const struct wpabuf *sel, int forced_freq)
8919{
8920 struct wpabuf *tmp;
8921 int ret;
8922
8923 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8924 return -1;
8925
8926 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8927
8928 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8929 wpabuf_head(req), wpabuf_len(req));
8930 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8931 wpabuf_head(sel), wpabuf_len(sel));
8932 if (forced_freq)
8933 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8934 tmp = ndef_parse_p2p(init ? sel : req);
8935 if (tmp == NULL) {
8936 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8937 return -1;
8938 }
8939
8940 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8941 forced_freq);
8942 wpabuf_free(tmp);
8943
8944 return ret;
8945}
8946
8947
8948int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8949{
8950 const u8 *if_addr;
8951 int go_intent = wpa_s->conf->p2p_go_intent;
8952 struct wpa_supplicant *iface;
8953
8954 if (wpa_s->global->p2p == NULL)
8955 return -1;
8956
8957 if (!enabled) {
8958 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8959 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8960 {
8961 if (!iface->ap_iface)
8962 continue;
8963 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8964 }
8965 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8966 0, NULL);
8967 if (wpa_s->p2p_nfc_tag_enabled)
8968 wpas_p2p_remove_pending_group_interface(wpa_s);
8969 wpa_s->p2p_nfc_tag_enabled = 0;
8970 return 0;
8971 }
8972
8973 if (wpa_s->global->p2p_disabled)
8974 return -1;
8975
8976 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8977 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8978 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8979 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8980 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8981 "to allow static handover cases");
8982 return -1;
8983 }
8984
8985 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8986
8987 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8988 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8989 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8990 if (wpa_s->p2p_oob_dev_pw == NULL)
8991 return -1;
8992 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8993
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008994 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8995 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8996 /*
8997 * P2P Group Interface present and the command came on group
8998 * interface, so enable the token for the current interface.
8999 */
9000 wpa_s->create_p2p_iface = 0;
9001 } else {
9002 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9003 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009004
9005 if (wpa_s->create_p2p_iface) {
9006 enum wpa_driver_if_type iftype;
9007 /* Prepare to add a new interface for the group */
9008 iftype = WPA_IF_P2P_GROUP;
9009 if (go_intent == 15)
9010 iftype = WPA_IF_P2P_GO;
9011 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9012 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9013 "interface for the group");
9014 return -1;
9015 }
9016
9017 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009018 } else if (wpa_s->p2p_mgmt)
9019 if_addr = wpa_s->parent->own_addr;
9020 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009021 if_addr = wpa_s->own_addr;
9022
9023 wpa_s->p2p_nfc_tag_enabled = enabled;
9024
9025 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9026 struct hostapd_data *hapd;
9027 if (iface->ap_iface == NULL)
9028 continue;
9029 hapd = iface->ap_iface->bss[0];
9030 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9031 hapd->conf->wps_nfc_dh_pubkey =
9032 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9033 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9034 hapd->conf->wps_nfc_dh_privkey =
9035 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9036 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9037 hapd->conf->wps_nfc_dev_pw =
9038 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9039 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9040
9041 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9042 wpa_dbg(iface, MSG_DEBUG,
9043 "P2P: Failed to enable NFC Tag for GO");
9044 }
9045 }
9046 p2p_set_authorized_oob_dev_pw_id(
9047 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9048 if_addr);
9049
9050 return 0;
9051}
9052
9053#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009054
9055
9056static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9057 struct wpa_used_freq_data *freqs,
9058 unsigned int num)
9059{
9060 u8 curr_chan, cand, chan;
9061 unsigned int i;
9062
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009063 /*
9064 * If possible, optimize the Listen channel to be a channel that is
9065 * already used by one of the other interfaces.
9066 */
9067 if (!wpa_s->conf->p2p_optimize_listen_chan)
9068 return;
9069
9070 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9071 return;
9072
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009073 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9074 for (i = 0, cand = 0; i < num; i++) {
9075 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9076 if (curr_chan == chan) {
9077 cand = 0;
9078 break;
9079 }
9080
9081 if (chan == 1 || chan == 6 || chan == 11)
9082 cand = chan;
9083 }
9084
9085 if (cand) {
9086 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009087 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009088 cand);
9089 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9090 }
9091}
9092
9093
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009094static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009095{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009096 struct hostapd_config *conf;
9097 struct p2p_go_neg_results params;
9098 struct csa_settings csa_settings;
9099 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9100 int old_freq = current_ssid->frequency;
9101 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009103 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9104 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9105 return -1;
9106 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009107
9108 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009109 * TODO: This function may not always work correctly. For example,
9110 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009111 */
Hai Shalom74f70d42019-02-11 14:42:39 -08009112 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009113 wpa_dbg(wpa_s, MSG_DEBUG,
9114 "P2P CSA: Failed to select new frequency for GO");
9115 return -1;
9116 }
9117
9118 if (current_ssid->frequency == params.freq) {
9119 wpa_dbg(wpa_s, MSG_DEBUG,
9120 "P2P CSA: Selected same frequency - not moving GO");
9121 return 0;
9122 }
9123
9124 conf = hostapd_config_defaults();
9125 if (!conf) {
9126 wpa_dbg(wpa_s, MSG_DEBUG,
9127 "P2P CSA: Failed to allocate default config");
9128 return -1;
9129 }
9130
9131 current_ssid->frequency = params.freq;
9132 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9133 wpa_dbg(wpa_s, MSG_DEBUG,
9134 "P2P CSA: Failed to create new GO config");
9135 ret = -1;
9136 goto out;
9137 }
9138
9139 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9140 wpa_dbg(wpa_s, MSG_DEBUG,
9141 "P2P CSA: CSA to a different band is not supported");
9142 ret = -1;
9143 goto out;
9144 }
9145
9146 os_memset(&csa_settings, 0, sizeof(csa_settings));
9147 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9148 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9149 csa_settings.freq_params.freq = params.freq;
9150 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9151 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9152 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9153
9154 if (conf->ieee80211ac) {
9155 int freq1 = 0, freq2 = 0;
9156 u8 chan, opclass;
9157
9158 if (ieee80211_freq_to_channel_ext(params.freq,
9159 conf->secondary_channel,
9160 conf->vht_oper_chwidth,
9161 &opclass, &chan) ==
9162 NUM_HOSTAPD_MODES) {
9163 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9164 ret = -1;
9165 goto out;
9166 }
9167
9168 if (conf->vht_oper_centr_freq_seg0_idx)
9169 freq1 = ieee80211_chan_to_freq(
9170 NULL, opclass,
9171 conf->vht_oper_centr_freq_seg0_idx);
9172
9173 if (conf->vht_oper_centr_freq_seg1_idx)
9174 freq2 = ieee80211_chan_to_freq(
9175 NULL, opclass,
9176 conf->vht_oper_centr_freq_seg1_idx);
9177
9178 if (freq1 < 0 || freq2 < 0) {
9179 wpa_dbg(wpa_s, MSG_DEBUG,
9180 "P2P CSA: Selected invalid VHT center freqs");
9181 ret = -1;
9182 goto out;
9183 }
9184
9185 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9186 csa_settings.freq_params.center_freq1 = freq1;
9187 csa_settings.freq_params.center_freq2 = freq2;
9188
9189 switch (conf->vht_oper_chwidth) {
9190 case VHT_CHANWIDTH_80MHZ:
9191 case VHT_CHANWIDTH_80P80MHZ:
9192 csa_settings.freq_params.bandwidth = 80;
9193 break;
9194 case VHT_CHANWIDTH_160MHZ:
9195 csa_settings.freq_params.bandwidth = 160;
9196 break;
9197 }
9198 }
9199
9200 ret = ap_switch_channel(wpa_s, &csa_settings);
9201out:
9202 current_ssid->frequency = old_freq;
9203 hostapd_config_free(conf);
9204 return ret;
9205}
9206
9207
9208static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9209{
9210 struct p2p_go_neg_results params;
9211 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9212
9213 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9214
9215 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9216 current_ssid->frequency);
9217
9218 /* Stop the AP functionality */
9219 /* TODO: Should do this in a way that does not indicated to possible
9220 * P2P Clients in the group that the group is terminated. */
9221 wpa_supplicant_ap_deinit(wpa_s);
9222
9223 /* Reselect the GO frequency */
Hai Shalom74f70d42019-02-11 14:42:39 -08009224 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009225 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9226 wpas_p2p_group_delete(wpa_s,
9227 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9228 return;
9229 }
9230 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9231 params.freq);
9232
9233 if (params.freq &&
9234 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9235 wpa_printf(MSG_DEBUG,
9236 "P2P: Selected freq (%u MHz) is not valid for P2P",
9237 params.freq);
9238 wpas_p2p_group_delete(wpa_s,
9239 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9240 return;
9241 }
9242
9243 /* Update the frequency */
9244 current_ssid->frequency = params.freq;
9245 wpa_s->connect_without_scan = current_ssid;
9246 wpa_s->reassociate = 1;
9247 wpa_s->disconnected = 0;
9248 wpa_supplicant_req_scan(wpa_s, 0, 0);
9249}
9250
9251
9252static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9253{
9254 struct wpa_supplicant *wpa_s = eloop_ctx;
9255
9256 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009257 return;
9258
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009259 wpas_p2p_go_update_common_freqs(wpa_s);
9260
9261 /* Do not move GO in the middle of a CSA */
9262 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9263 wpa_printf(MSG_DEBUG,
9264 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009265 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009266 }
9267
9268 /*
9269 * First, try a channel switch flow. If it is not supported or fails,
9270 * take down the GO and bring it up again.
9271 */
9272 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9273 wpas_p2p_move_go_no_csa(wpa_s);
9274}
9275
9276
9277static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9278{
9279 struct wpa_supplicant *wpa_s = eloop_ctx;
9280 struct wpa_used_freq_data *freqs = NULL;
9281 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009282
9283 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9284 if (!freqs)
9285 return;
9286
9287 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9288
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009289 /* Previous attempt to move a GO was not possible -- try again. */
9290 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9291 WPAS_P2P_CHANNEL_UPDATE_ANY);
9292
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009293 os_free(freqs);
9294}
9295
9296
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009297/*
9298 * Consider moving a GO from its currently used frequency:
9299 * 1. It is possible that due to regulatory consideration the frequency
9300 * can no longer be used and there is a need to evacuate the GO.
9301 * 2. It is possible that due to MCC considerations, it would be preferable
9302 * to move the GO to a channel that is currently used by some other
9303 * station interface.
9304 *
9305 * In case a frequency that became invalid is once again valid, cancel a
9306 * previously initiated GO frequency change.
9307 */
9308static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9309 struct wpa_used_freq_data *freqs,
9310 unsigned int num)
9311{
9312 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9313 unsigned int timeout;
9314 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009315 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009316
9317 wpas_p2p_go_update_common_freqs(wpa_s);
9318
9319 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009320 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009321 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009322 for (i = 0, invalid_freq = 0; i < num; i++) {
9323 if (freqs[i].freq == freq) {
9324 flags = freqs[i].flags;
9325
9326 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009327 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9328 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009329 wpa_dbg(wpa_s, MSG_DEBUG,
9330 "P2P: Freq=%d MHz no longer valid for GO",
9331 freq);
9332 invalid_freq = 1;
9333 }
9334 } else if (freqs[i].flags == 0) {
9335 /* Freq is not used by any other station interface */
9336 continue;
9337 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009338 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009339 /* Freq is not valid for P2P use cases */
9340 continue;
9341 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9342 P2P_GO_FREQ_MOVE_SCM) {
9343 policy_move = 1;
9344 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9345 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9346 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9347 policy_move = 1;
9348 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9349 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9350 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9351 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9352 policy_move = 1;
9353 } else if ((wpa_s->drv_flags &
9354 WPA_DRIVER_FLAGS_AP_CSA) &&
9355 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9356 u8 chan;
9357
9358 /*
9359 * We do not support CSA between bands, so move
9360 * GO only within the same band.
9361 */
9362 if (wpa_s->ap_iface->current_mode->mode ==
9363 ieee80211_freq_to_chan(freqs[i].freq,
9364 &chan))
9365 policy_move = 1;
9366 }
9367 }
9368 }
9369
9370 wpa_dbg(wpa_s, MSG_DEBUG,
9371 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9372 invalid_freq, policy_move, flags);
9373
9374 /*
9375 * The channel is valid, or we are going to have a policy move, so
9376 * cancel timeout.
9377 */
9378 if (!invalid_freq || policy_move) {
9379 wpa_dbg(wpa_s, MSG_DEBUG,
9380 "P2P: Cancel a GO move from freq=%d MHz", freq);
9381 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9382
9383 if (wpas_p2p_in_progress(wpa_s)) {
9384 wpa_dbg(wpa_s, MSG_DEBUG,
9385 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9386 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9387 wpa_s, NULL);
9388 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9389 wpas_p2p_reconsider_moving_go,
9390 wpa_s, NULL);
9391 return;
9392 }
9393 }
9394
9395 if (!invalid_freq && (!policy_move || flags != 0)) {
9396 wpa_dbg(wpa_s, MSG_DEBUG,
9397 "P2P: Not initiating a GO frequency change");
9398 return;
9399 }
9400
9401 /*
9402 * Do not consider moving GO if it is in the middle of a CSA. When the
9403 * CSA is finished this flow should be retriggered.
9404 */
9405 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9406 wpa_dbg(wpa_s, MSG_DEBUG,
9407 "P2P: Not initiating a GO frequency change - CSA is in progress");
9408 return;
9409 }
9410
9411 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9412 timeout = P2P_GO_FREQ_CHANGE_TIME;
9413 else
9414 timeout = 0;
9415
9416 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9417 freq, timeout);
9418 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9419 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9420}
9421
9422
9423static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9424 struct wpa_used_freq_data *freqs,
9425 unsigned int num,
9426 enum wpas_p2p_channel_update_trig trig)
9427{
9428 struct wpa_supplicant *ifs;
9429
9430 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9431 NULL);
9432
9433 /*
9434 * Travers all the radio interfaces, and for each GO interface, check
9435 * if there is a need to move the GO from the frequency it is using,
9436 * or in case the frequency is valid again, cancel the evacuation flow.
9437 */
9438 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9439 radio_list) {
9440 if (ifs->current_ssid == NULL ||
9441 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9442 continue;
9443
9444 /*
9445 * The GO was just started or completed channel switch, no need
9446 * to move it.
9447 */
9448 if (wpa_s == ifs &&
9449 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9450 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9451 wpa_dbg(wpa_s, MSG_DEBUG,
9452 "P2P: GO move - schedule re-consideration");
9453 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9454 wpas_p2p_reconsider_moving_go,
9455 wpa_s, NULL);
9456 continue;
9457 }
9458
9459 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9460 }
9461}
9462
9463
9464void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9465{
9466 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9467 return;
9468
9469 wpas_p2p_update_channel_list(wpa_s,
9470 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9471}
9472
9473
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009474void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9475{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009476 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9477 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9478 "the management interface is being removed");
9479 wpas_p2p_deinit_global(wpa_s->global);
9480 }
9481}
9482
9483
9484void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9485{
9486 if (wpa_s->ap_iface->bss)
9487 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9488 wpas_p2p_group_deinit(wpa_s);
9489}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009490
9491
9492int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9493 unsigned int period, unsigned int interval,
9494 unsigned int count)
9495{
9496 struct p2p_data *p2p = wpa_s->global->p2p;
9497 u8 *device_types;
9498 size_t dev_types_len;
9499 struct wpabuf *buf;
9500 int ret;
9501
9502 if (wpa_s->p2p_lo_started) {
9503 wpa_dbg(wpa_s, MSG_DEBUG,
9504 "P2P Listen offload is already started");
9505 return 0;
9506 }
9507
9508 if (wpa_s->global->p2p == NULL ||
9509 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9510 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9511 return -1;
9512 }
9513
9514 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9515 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9516 freq);
9517 return -1;
9518 }
9519
9520 /* Get device type */
9521 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9522 WPS_DEV_TYPE_LEN;
9523 device_types = os_malloc(dev_types_len);
9524 if (!device_types)
9525 return -1;
9526 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9527 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9528 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9529
9530 /* Get Probe Response IE(s) */
9531 buf = p2p_build_probe_resp_template(p2p, freq);
9532 if (!buf) {
9533 os_free(device_types);
9534 return -1;
9535 }
9536
9537 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9538 device_types, dev_types_len,
9539 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9540 if (ret < 0)
9541 wpa_dbg(wpa_s, MSG_DEBUG,
9542 "P2P: Failed to start P2P listen offload");
9543
9544 os_free(device_types);
9545 wpabuf_free(buf);
9546
9547 if (ret == 0) {
9548 wpa_s->p2p_lo_started = 1;
9549
9550 /* Stop current P2P listen if any */
9551 wpas_stop_listen(wpa_s);
9552 }
9553
9554 return ret;
9555}
9556
9557
9558int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9559{
9560 int ret;
9561
9562 if (!wpa_s->p2p_lo_started)
9563 return 0;
9564
9565 ret = wpa_drv_p2p_lo_stop(wpa_s);
9566 if (ret < 0)
9567 wpa_dbg(wpa_s, MSG_DEBUG,
9568 "P2P: Failed to stop P2P listen offload");
9569
9570 wpa_s->p2p_lo_started = 0;
9571 return ret;
9572}