blob: c68e5154e560d838cd8c7cc8668d9c25e17202ed [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
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
132 unsigned int *pref_freq_list,
133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
169
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170/*
171 * Get the number of concurrent channels that the HW can operate, but that are
172 * currently not in use by any of the wpa_supplicant interfaces.
173 */
174static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
175{
176 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800177 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700178
179 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
180 if (!freqs)
181 return -1;
182
183 num = get_shared_radio_freqs(wpa_s, freqs,
184 wpa_s->num_multichan_concurrent);
185 os_free(freqs);
186
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800187 unused = wpa_s->num_multichan_concurrent - num;
188 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
189 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700190}
191
192
193/*
194 * Get the frequencies that are currently in use by one or more of the virtual
195 * interfaces, and that are also valid for P2P operation.
196 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700197static unsigned int
198wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
199 struct wpa_used_freq_data *p2p_freqs,
200 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700201{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700203 unsigned int num, i, j;
204
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 freqs = os_calloc(wpa_s->num_multichan_concurrent,
206 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700207 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700208 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 num = get_shared_radio_freqs_data(wpa_s, freqs,
211 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700212
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214
215 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700216 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700217 p2p_freqs[j++] = freqs[i];
218 }
219
220 os_free(freqs);
221
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700222 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800223
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700224 return j;
225}
226
227
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700228static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
229 int freq)
230{
231 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
232 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700233
234 /* Use the wpa_s used to control the P2P Device operation */
235 wpa_s = wpa_s->global->p2p_init_wpa_s;
236
237 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800238 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
239 wpas_p2p_num_unused_channels(wpa_s) > 0) {
240 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
241 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700242 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800243 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700244 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
245}
246
247
Hai Shalom60840252021-02-19 19:02:11 -0800248static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
249{
250 unsigned int delay = wpas_p2p_search_delay(wpa_s);
251
252 /* In case of concurrent P2P and external scans, delay P2P search. */
253 if (external_scan_running(wpa_s->radio)) {
254 delay = wpa_s->conf->p2p_search_delay;
255 wpa_printf(MSG_DEBUG,
256 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
257 delay);
258 }
259
260 p2p_scan_res_handled(wpa_s->global->p2p, delay);
261}
262
263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
265 struct wpa_scan_results *scan_res)
266{
267 size_t i;
268
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800269 if (wpa_s->p2p_scan_work) {
270 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
271 wpa_s->p2p_scan_work = NULL;
272 radio_work_done(work);
273 }
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
276 return;
277
278 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
279 (int) scan_res->num);
280
281 for (i = 0; i < scan_res->num; i++) {
282 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700284 const u8 *ies;
285 size_t ies_len;
286
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800287 time_tmp_age.sec = bss->age / 1000;
288 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800289 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700290
291 ies = (const u8 *) (bss + 1);
292 ies_len = bss->ie_len;
293 if (bss->beacon_ie_len > 0 &&
294 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
295 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
296 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
297 MACSTR, MAC2STR(bss->bssid));
298 ies = ies + ies_len;
299 ies_len = bss->beacon_ie_len;
300 }
301
302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800304 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700305 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 break;
307 }
308
Hai Shalom60840252021-02-19 19:02:11 -0800309 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310}
311
312
Hai Shalom60840252021-02-19 19:02:11 -0800313static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700314{
Hai Shalom60840252021-02-19 19:02:11 -0800315 if (wpa_s->p2p_scan_work) {
316 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700317
Hai Shalom60840252021-02-19 19:02:11 -0800318 wpa_s->p2p_scan_work = NULL;
319 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700320 }
321
Hai Shalom60840252021-02-19 19:02:11 -0800322 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
323 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700324
Hai Shalom60840252021-02-19 19:02:11 -0800325 wpa_dbg(wpa_s, MSG_DEBUG,
326 "P2P: Failed to get scan results - try to continue");
327 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700328}
329
330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
332{
333 struct wpa_supplicant *wpa_s = work->wpa_s;
334 struct wpa_driver_scan_params *params = work->ctx;
335 int ret;
336
337 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800338 if (!work->started) {
339 wpa_scan_free_params(params);
340 return;
341 }
342
343 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800344 return;
345 }
346
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800347 if (wpa_s->clear_driver_scan_cache) {
348 wpa_printf(MSG_DEBUG,
349 "Request driver to clear scan cache due to local BSS flush");
350 params->only_new_results = 1;
351 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700352
353 if (wpa_s->conf->p2p_6ghz_disable && !params->freqs) {
354 wpa_printf(MSG_DEBUG,
355 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -0800356 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
357 0);
358 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
359 0);
Hai Shalom899fcc72020-10-19 14:38:18 -0700360 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800362 if (ret == 0)
363 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800364 wpa_scan_free_params(params);
365 work->ctx = NULL;
366 if (ret) {
367 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800368 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800369 return;
370 }
371
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800372 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800373 os_get_reltime(&wpa_s->scan_trigger_time);
374 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800375 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800376 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800377 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800378 wpa_s->p2p_scan_work = work;
379}
380
381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800382static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
383 int freq)
384{
385 if (wpa_s->global->p2p_24ghz_social_channels &&
386 (freq == 2412 || freq == 2437 || freq == 2462)) {
387 /*
388 * Search all social channels regardless of whether these have
389 * been disabled for P2P operating channel use to avoid missing
390 * peers.
391 */
392 return 1;
393 }
394 return p2p_supported_freq(wpa_s->global->p2p, freq);
395}
396
397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
399 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700400 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401{
402 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800403 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700405 unsigned int num_channels = 0;
406 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800407 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700408 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800409 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410
411 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
412 return -1;
413
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800414 if (wpa_s->p2p_scan_work) {
415 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
416 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 }
418
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800419 params = os_zalloc(sizeof(*params));
420 if (params == NULL)
421 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422
423 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800424 params->num_ssids = 1;
425 n = os_malloc(P2P_WILDCARD_SSID_LEN);
426 if (n == NULL)
427 goto fail;
428 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
429 params->ssids[0].ssid = n;
430 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
432 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700433 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
434 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 num_req_dev_types, req_dev_types);
436 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800437 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 switch (type) {
440 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700441 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
442 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800443 if (params->freqs == NULL)
444 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700445 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800446 if (wpas_p2p_search_social_channel(
447 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700448 params->freqs[num_channels++] =
449 social_channels_freq[i];
450 }
451 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 break;
453 case P2P_SCAN_FULL:
454 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800455 case P2P_SCAN_SPECIFIC:
456 params->freqs = os_calloc(2, sizeof(int));
457 if (params->freqs == NULL)
458 goto fail;
459 params->freqs[0] = freq;
460 params->freqs[1] = 0;
461 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700463 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
464 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800465 if (params->freqs == NULL)
466 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700467 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800468 if (wpas_p2p_search_social_channel(
469 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700470 params->freqs[num_channels++] =
471 social_channels_freq[i];
472 }
473 if (p2p_supported_freq(wpa_s->global->p2p, freq))
474 params->freqs[num_channels++] = freq;
475 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 break;
477 }
478
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800479 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
480 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
481 if (ies == NULL) {
482 wpabuf_free(wps_ie);
483 goto fail;
484 }
485 wpabuf_put_buf(ies, wps_ie);
486 wpabuf_free(wps_ie);
487
488 bands = wpas_get_bands(wpa_s, params->freqs);
489 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
490
491 params->p2p_probe = 1;
492 n = os_malloc(wpabuf_len(ies));
493 if (n == NULL) {
494 wpabuf_free(ies);
495 goto fail;
496 }
497 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
498 params->extra_ies = n;
499 params->extra_ies_len = wpabuf_len(ies);
500 wpabuf_free(ies);
501
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800502 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800503 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
504 params) < 0)
505 goto fail;
506 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800508fail:
509 wpa_scan_free_params(params);
510 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511}
512
513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
515{
516 switch (p2p_group_interface) {
517 case P2P_GROUP_INTERFACE_PENDING:
518 return WPA_IF_P2P_GROUP;
519 case P2P_GROUP_INTERFACE_GO:
520 return WPA_IF_P2P_GO;
521 case P2P_GROUP_INTERFACE_CLIENT:
522 return WPA_IF_P2P_CLIENT;
523 }
524
525 return WPA_IF_P2P_GROUP;
526}
527
528
529static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
530 const u8 *ssid,
531 size_t ssid_len, int *go)
532{
533 struct wpa_ssid *s;
534
535 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
536 for (s = wpa_s->conf->ssid; s; s = s->next) {
537 if (s->disabled != 0 || !s->p2p_group ||
538 s->ssid_len != ssid_len ||
539 os_memcmp(ssid, s->ssid, ssid_len) != 0)
540 continue;
541 if (s->mode == WPAS_MODE_P2P_GO &&
542 s != wpa_s->current_ssid)
543 continue;
544 if (go)
545 *go = s->mode == WPAS_MODE_P2P_GO;
546 return wpa_s;
547 }
548 }
549
550 return NULL;
551}
552
553
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800554static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
555{
556 struct wpa_supplicant *wpa_s = eloop_ctx;
557 wpa_printf(MSG_DEBUG,
558 "P2P: Complete previously requested removal of %s",
559 wpa_s->ifname);
560 wpas_p2p_disconnect(wpa_s);
561}
562
563
564static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
565 struct wpa_supplicant *calling_wpa_s)
566{
567 if (calling_wpa_s == wpa_s && wpa_s &&
568 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
569 /*
570 * The calling wpa_s instance is going to be removed. Do that
571 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700572 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800573 * control interface responses on the per-interface socket.
574 */
575 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
576 wpa_s, NULL) < 0)
577 return -1;
578 return 0;
579 }
580
581 return wpas_p2p_disconnect(wpa_s);
582}
583
584
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800585/* Determine total number of clients in active groups where we are the GO */
586static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
587{
588 unsigned int count = 0;
589 struct wpa_ssid *s;
590
591 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
592 for (s = wpa_s->conf->ssid; s; s = s->next) {
593 wpa_printf(MSG_DEBUG,
594 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
595 wpa_s, s, s->disabled, s->p2p_group,
596 s->mode);
597 if (!s->disabled && s->p2p_group &&
598 s->mode == WPAS_MODE_P2P_GO) {
599 count += p2p_get_group_num_members(
600 wpa_s->p2p_group);
601 }
602 }
603 }
604
605 return count;
606}
607
608
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800609static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
610{
611 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
612 !wpa_s->current_ssid->disabled &&
613 wpa_s->current_ssid->p2p_group &&
614 wpa_s->current_ssid->p2p_persistent_group;
615}
616
617
618static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
619{
620 return p2p_is_active_persistent_group(wpa_s) &&
621 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
622}
623
624
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800625/* Find an interface for a P2P group where we are the GO */
626static struct wpa_supplicant *
627wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
628{
629 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800630
631 if (!wpa_s)
632 return NULL;
633
634 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 if (!p2p_is_active_persistent_go(wpa_s))
636 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800637
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800638 /* Prefer a group with connected clients */
639 if (p2p_get_group_num_members(wpa_s->p2p_group))
640 return wpa_s;
641 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800642 }
643
644 /* No group with connected clients, so pick the one without (if any) */
645 return save;
646}
647
648
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800649static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800650{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800651 return p2p_is_active_persistent_group(wpa_s) &&
652 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
653}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800654
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800655
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800656/* Find an interface for a P2P group where we are the P2P Client */
657static struct wpa_supplicant *
658wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
659{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800661 if (p2p_is_active_persistent_cli(wpa_s))
662 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800663 }
664
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800665 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800666}
667
668
669/* Find a persistent group where we are the GO */
670static struct wpa_ssid *
671wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
672{
673 struct wpa_ssid *s;
674
675 for (s = wpa_s->conf->ssid; s; s = s->next) {
676 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
677 return s;
678 }
679
680 return NULL;
681}
682
683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800684static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
685 unsigned int *force_freq,
686 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800687{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800688 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800689 struct wpa_ssid *s;
690 u8 conncap = P2PS_SETUP_NONE;
691 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800692 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800693 struct wpa_ssid *persistent_go;
694 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800695 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800696
697 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
698
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800699 if (force_freq)
700 *force_freq = 0;
701 if (pref_freq)
702 *pref_freq = 0;
703
704 size = P2P_MAX_PREF_CHANNELS;
705 if (force_freq && pref_freq &&
706 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
707 (int *) pref_freq, 0, pref_freq_list, &size))
708 wpas_p2p_set_own_freq_preference(wpa_s,
709 *force_freq ? *force_freq :
710 *pref_freq);
711
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800712 /*
713 * For non-concurrent capable devices:
714 * If persistent_go, then no new.
715 * If GO, then no client.
716 * If client, then no GO.
717 */
718 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719 if (go_wpa_s)
720 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800721 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800722 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
723 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800725 wpa_printf(MSG_DEBUG,
726 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
727 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800728
729 /* If not concurrent, restrict our choices */
730 if (p2p_no_group_iface) {
731 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800733 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800734 return P2PS_SETUP_NONE;
735
736 if (go_wpa_s) {
737 if (role == P2PS_SETUP_CLIENT ||
738 incoming == P2PS_SETUP_GROUP_OWNER ||
739 p2p_client_limit_reached(go_wpa_s->p2p_group))
740 return P2PS_SETUP_NONE;
741
742 return P2PS_SETUP_GROUP_OWNER;
743 }
744
745 if (persistent_go) {
746 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
747 if (!incoming)
748 return P2PS_SETUP_GROUP_OWNER |
749 P2PS_SETUP_CLIENT;
750 if (incoming == P2PS_SETUP_NEW) {
751 u8 r;
752
753 if (os_get_random(&r, sizeof(r)) < 0 ||
754 (r & 1))
755 return P2PS_SETUP_CLIENT;
756 return P2PS_SETUP_GROUP_OWNER;
757 }
758 }
759 }
760 }
761
762 /* If a required role has been specified, handle it here */
763 if (role && role != P2PS_SETUP_NEW) {
764 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800765 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
766 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
767 /*
768 * Peer has an active GO, so if the role allows it and
769 * we do not have any active roles, become client.
770 */
771 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
772 !cli_wpa_s)
773 return P2PS_SETUP_CLIENT;
774
775 /* fall through */
776
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800777 case P2PS_SETUP_NONE:
778 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800779 conncap = role;
780 goto grp_owner;
781
782 case P2PS_SETUP_GROUP_OWNER:
783 /*
784 * Must be a complimentary role - cannot be a client to
785 * more than one peer.
786 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800787 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800788 return P2PS_SETUP_NONE;
789
790 return P2PS_SETUP_CLIENT;
791
792 case P2PS_SETUP_CLIENT:
793 /* Must be a complimentary role */
794 if (incoming != role) {
795 conncap = P2PS_SETUP_GROUP_OWNER;
796 goto grp_owner;
797 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700798 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800799
800 default:
801 return P2PS_SETUP_NONE;
802 }
803 }
804
805 /*
806 * For now, we only will support ownership of one group, and being a
807 * client of one group. Therefore, if we have either an existing GO
808 * group, or an existing client group, we will not do a new GO
809 * negotiation, but rather try to re-use the existing groups.
810 */
811 switch (incoming) {
812 case P2PS_SETUP_NONE:
813 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800814 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 conncap = P2PS_SETUP_GROUP_OWNER;
816 else if (!owned_members)
817 conncap = P2PS_SETUP_NEW;
818 else if (incoming == P2PS_SETUP_NONE)
819 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
820 else
821 conncap = P2PS_SETUP_CLIENT;
822 break;
823
824 case P2PS_SETUP_CLIENT:
825 conncap = P2PS_SETUP_GROUP_OWNER;
826 break;
827
828 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800829 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800830 conncap = P2PS_SETUP_CLIENT;
831 break;
832
833 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
834 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800835 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800836 conncap = P2PS_SETUP_GROUP_OWNER;
837 else {
838 u8 r;
839
840 if (os_get_random(&r, sizeof(r)) < 0 ||
841 (r & 1))
842 conncap = P2PS_SETUP_CLIENT;
843 else
844 conncap = P2PS_SETUP_GROUP_OWNER;
845 }
846 break;
847
848 default:
849 return P2PS_SETUP_NONE;
850 }
851
852grp_owner:
853 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
854 (!incoming && (conncap & P2PS_SETUP_NEW))) {
855 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
856 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800857
858 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800859 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800860 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800861 wpa_s->p2p_mgmt ?
862 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800863 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800864 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800865 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800866 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800867 wpa_printf(MSG_ERROR,
868 "P2P: Failed to allocate a new interface for the group");
869 return P2PS_SETUP_NONE;
870 }
871 wpa_s->global->pending_group_iface_for_p2ps = 1;
872 p2p_set_intended_addr(wpa_s->global->p2p,
873 wpa_s->pending_interface_addr);
874 }
875 }
876
877 return conncap;
878}
879
880
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700881static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
882 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883{
884 struct wpa_ssid *ssid;
885 char *gtype;
886 const char *reason;
887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 ssid = wpa_s->current_ssid;
889 if (ssid == NULL) {
890 /*
891 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700892 * pending P2P group interface waiting for provisioning or a
893 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 */
895 ssid = wpa_s->conf->ssid;
896 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700897 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 break;
899 ssid = ssid->next;
900 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300901 if (ssid == NULL &&
902 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
903 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700904 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
905 "not found");
906 return -1;
907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 }
909 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
910 gtype = "GO";
911 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
912 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
913 wpa_s->reassociate = 0;
914 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 gtype = "client";
916 } else
917 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700918
919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
920 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
921
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800922 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700923 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800924 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
925 wpa_s, NULL)) {
926 wpa_printf(MSG_DEBUG,
927 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
928 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
929 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
930 wpa_s, NULL);
931 }
932 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 if (wpa_s->cross_connect_in_use) {
935 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800936 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700937 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
938 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 case P2P_GROUP_REMOVAL_REQUESTED:
942 reason = " reason=REQUESTED";
943 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700944 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
945 reason = " reason=FORMATION_FAILED";
946 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
948 reason = " reason=IDLE";
949 break;
950 case P2P_GROUP_REMOVAL_UNAVAILABLE:
951 reason = " reason=UNAVAILABLE";
952 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700953 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
954 reason = " reason=GO_ENDING_SESSION";
955 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700956 case P2P_GROUP_REMOVAL_PSK_FAILURE:
957 reason = " reason=PSK_FAILURE";
958 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800959 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
960 reason = " reason=FREQ_CONFLICT";
961 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 default:
963 reason = "";
964 break;
965 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700966 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800967 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700968 P2P_EVENT_GROUP_REMOVED "%s %s%s",
969 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800972 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
973 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700974 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
975 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800976 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800977 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800978 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
979 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700980 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800981 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700982 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983
Dmitry Shmidt15907092014-03-25 10:42:57 -0700984 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800985 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
986 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700987
Dmitry Shmidt96571392013-10-14 12:54:46 -0700988 /*
989 * Make sure wait for the first client does not remain active after the
990 * group has been removed.
991 */
992 wpa_s->global->p2p_go_wait_client.sec = 0;
993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
995 struct wpa_global *global;
996 char *ifname;
997 enum wpa_driver_if_type type;
998 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
999 wpa_s->ifname);
1000 global = wpa_s->global;
1001 ifname = os_strdup(wpa_s->ifname);
1002 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001003 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001004 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 wpa_s = global->ifaces;
1006 if (wpa_s && ifname)
1007 wpa_drv_if_remove(wpa_s, type, ifname);
1008 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001009 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 }
1011
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001012 /*
1013 * The primary interface was used for P2P group operations, so
1014 * need to reset its p2pdev.
1015 */
1016 wpa_s->p2pdev = wpa_s->parent;
1017
Dmitry Shmidt96571392013-10-14 12:54:46 -07001018 if (!wpa_s->p2p_go_group_formation_completed) {
1019 wpa_s->global->p2p_group_formation = NULL;
1020 wpa_s->p2p_in_provisioning = 0;
1021 }
1022
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001023 wpa_s->show_group_started = 0;
1024 os_free(wpa_s->go_params);
1025 wpa_s->go_params = NULL;
1026
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001027 os_free(wpa_s->p2p_group_common_freqs);
1028 wpa_s->p2p_group_common_freqs = NULL;
1029 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001030 wpa_s->p2p_go_do_acs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001031
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001032 wpa_s->waiting_presence_resp = 0;
1033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1035 if (ssid && (ssid->p2p_group ||
1036 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1037 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1038 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001039 if (ssid == wpa_s->current_ssid) {
1040 wpa_sm_set_config(wpa_s->wpa, NULL);
1041 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001043 }
1044 /*
1045 * Networks objects created during any P2P activities are not
1046 * exposed out as they might/will confuse certain non-P2P aware
1047 * applications since these network objects won't behave like
1048 * regular ones.
1049 *
1050 * Likewise, we don't send out network removed signals for such
1051 * network objects.
1052 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 wpa_config_remove_network(wpa_s->conf, id);
1054 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001055 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 } else {
1057 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1058 "found");
1059 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001060 if (wpa_s->ap_iface)
1061 wpa_supplicant_ap_deinit(wpa_s);
1062 else
1063 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001064
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001065 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1066
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001067 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068}
1069
1070
1071static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1072 u8 *go_dev_addr,
1073 const u8 *ssid, size_t ssid_len)
1074{
1075 struct wpa_bss *bss;
1076 const u8 *bssid;
1077 struct wpabuf *p2p;
1078 u8 group_capab;
1079 const u8 *addr;
1080
1081 if (wpa_s->go_params)
1082 bssid = wpa_s->go_params->peer_interface_addr;
1083 else
1084 bssid = wpa_s->bssid;
1085
1086 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001087 if (bss == NULL && wpa_s->go_params &&
1088 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1089 bss = wpa_bss_get_p2p_dev_addr(
1090 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 if (bss == NULL) {
1092 u8 iface_addr[ETH_ALEN];
1093 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1094 iface_addr) == 0)
1095 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1096 }
1097 if (bss == NULL) {
1098 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1099 "group is persistent - BSS " MACSTR " not found",
1100 MAC2STR(bssid));
1101 return 0;
1102 }
1103
1104 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001105 if (p2p == NULL)
1106 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1107 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 if (p2p == NULL) {
1109 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1110 "group is persistent - BSS " MACSTR
1111 " did not include P2P IE", MAC2STR(bssid));
1112 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001113 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001115 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 bss->beacon_ie_len);
1117 return 0;
1118 }
1119
1120 group_capab = p2p_get_group_capab(p2p);
1121 addr = p2p_get_go_dev_addr(p2p);
1122 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1123 "group_capab=0x%x", group_capab);
1124 if (addr) {
1125 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1126 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1127 MAC2STR(addr));
1128 } else
1129 os_memset(go_dev_addr, 0, ETH_ALEN);
1130 wpabuf_free(p2p);
1131
1132 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1133 "go_dev_addr=" MACSTR,
1134 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1135
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001136 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137}
1138
1139
Jouni Malinen75ecf522011-06-27 15:19:46 -07001140static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1141 struct wpa_ssid *ssid,
1142 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143{
1144 struct wpa_ssid *s;
1145 int changed = 0;
1146
1147 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1148 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1149 for (s = wpa_s->conf->ssid; s; s = s->next) {
1150 if (s->disabled == 2 &&
1151 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1152 s->ssid_len == ssid->ssid_len &&
1153 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1154 break;
1155 }
1156
1157 if (s) {
1158 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1159 "entry");
1160 if (ssid->passphrase && !s->passphrase)
1161 changed = 1;
1162 else if (ssid->passphrase && s->passphrase &&
1163 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1164 changed = 1;
1165 } else {
1166 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1167 "entry");
1168 changed = 1;
1169 s = wpa_config_add_network(wpa_s->conf);
1170 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001171 return -1;
1172
1173 /*
1174 * Instead of network_added we emit persistent_group_added
1175 * notification. Also to keep the defense checks in
1176 * persistent_group obj registration method, we set the
1177 * relevant flags in s to designate it as a persistent group.
1178 */
1179 s->p2p_group = 1;
1180 s->p2p_persistent_group = 1;
1181 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 wpa_config_set_network_defaults(s);
1183 }
1184
1185 s->p2p_group = 1;
1186 s->p2p_persistent_group = 1;
1187 s->disabled = 2;
1188 s->bssid_set = 1;
1189 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1190 s->mode = ssid->mode;
1191 s->auth_alg = WPA_AUTH_ALG_OPEN;
1192 s->key_mgmt = WPA_KEY_MGMT_PSK;
1193 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001194 s->pbss = ssid->pbss;
1195 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 s->export_keys = 1;
1197 if (ssid->passphrase) {
1198 os_free(s->passphrase);
1199 s->passphrase = os_strdup(ssid->passphrase);
1200 }
1201 if (ssid->psk_set) {
1202 s->psk_set = 1;
1203 os_memcpy(s->psk, ssid->psk, 32);
1204 }
1205 if (s->passphrase && !s->psk_set)
1206 wpa_config_update_psk(s);
1207 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1208 os_free(s->ssid);
1209 s->ssid = os_malloc(ssid->ssid_len);
1210 }
1211 if (s->ssid) {
1212 s->ssid_len = ssid->ssid_len;
1213 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1214 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001215 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1216 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1217 wpa_s->global->add_psk = NULL;
1218 changed = 1;
1219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 if (changed && wpa_s->conf->update_config &&
1222 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1223 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1224 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001225
1226 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227}
1228
1229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001230static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1231 const u8 *addr)
1232{
1233 struct wpa_ssid *ssid, *s;
1234 u8 *n;
1235 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001236 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001237 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238
1239 ssid = wpa_s->current_ssid;
1240 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1241 !ssid->p2p_persistent_group)
1242 return;
1243
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001244 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1246 continue;
1247
1248 if (s->ssid_len == ssid->ssid_len &&
1249 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1250 break;
1251 }
1252
1253 if (s == NULL)
1254 return;
1255
1256 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001257 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001258 ETH_ALEN) != 0)
1259 continue;
1260
1261 if (i == s->num_p2p_clients - 1)
1262 return; /* already the most recent entry */
1263
1264 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001265 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1266 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1267 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001268 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001269 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1270 ETH_ALEN);
1271 os_memset(s->p2p_client_list +
1272 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1273 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001274 found = 1;
1275 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001276 }
1277
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001278 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1279 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001280 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001281 if (n == NULL)
1282 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001283 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1284 ETH_ALEN);
1285 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1286 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001287 s->p2p_client_list = n;
1288 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001289 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001290 /* Not enough room for an additional entry - drop the oldest
1291 * entry */
1292 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001293 s->p2p_client_list + 2 * ETH_ALEN,
1294 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001295 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001296 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001297 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001298 os_memset(s->p2p_client_list +
1299 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1300 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001301 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001302
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001303 if (p2p_wpa_s->conf->update_config &&
1304 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001306}
1307
1308
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001309static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1310 int go, struct wpa_ssid *ssid, int freq,
1311 const u8 *psk, const char *passphrase,
1312 const u8 *go_dev_addr, int persistent,
1313 const char *extra)
1314{
1315 const char *ssid_txt;
1316 char psk_txt[65];
1317
1318 if (psk)
1319 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1320 else
1321 psk_txt[0] = '\0';
1322
1323 if (ssid)
1324 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1325 else
1326 ssid_txt = "";
1327
1328 if (passphrase && passphrase[0] == '\0')
1329 passphrase = NULL;
1330
1331 /*
1332 * Include PSK/passphrase only in the control interface message and
1333 * leave it out from the debug log entry.
1334 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001335 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001336 P2P_EVENT_GROUP_STARTED
1337 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1338 MACSTR "%s%s",
1339 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1340 psk ? " psk=" : "", psk_txt,
1341 passphrase ? " passphrase=\"" : "",
1342 passphrase ? passphrase : "",
1343 passphrase ? "\"" : "",
1344 MAC2STR(go_dev_addr),
1345 persistent ? " [PERSISTENT]" : "", extra);
1346 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1347 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1348 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1349 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1350 extra);
1351}
1352
1353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001355 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356{
1357 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 int client;
1359 int persistent;
1360 u8 go_dev_addr[ETH_ALEN];
1361
1362 /*
1363 * This callback is likely called for the main interface. Update wpa_s
1364 * to use the group interface if a new interface was created for the
1365 * group.
1366 */
1367 if (wpa_s->global->p2p_group_formation)
1368 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001369 if (wpa_s->p2p_go_group_formation_completed) {
1370 wpa_s->global->p2p_group_formation = NULL;
1371 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001372 } else if (wpa_s->p2p_in_provisioning && !success) {
1373 wpa_msg(wpa_s, MSG_DEBUG,
1374 "P2P: Stop provisioning state due to failure");
1375 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001376 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001377 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001378 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379
1380 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001381 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001382 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001383 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001384 if (already_deleted)
1385 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001386 wpas_p2p_group_delete(wpa_s,
1387 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 return;
1389 }
1390
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001391 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001392 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393
1394 ssid = wpa_s->current_ssid;
1395 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1396 ssid->mode = WPAS_MODE_P2P_GO;
1397 p2p_group_notif_formation_done(wpa_s->p2p_group);
1398 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1399 }
1400
1401 persistent = 0;
1402 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 client = ssid->mode == WPAS_MODE_INFRA;
1404 if (ssid->mode == WPAS_MODE_P2P_GO) {
1405 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001406 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1407 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 } else
1409 persistent = wpas_p2p_persistent_group(wpa_s,
1410 go_dev_addr,
1411 ssid->ssid,
1412 ssid->ssid_len);
1413 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 client = wpa_s->p2p_group_interface ==
1415 P2P_GROUP_INTERFACE_CLIENT;
1416 os_memset(go_dev_addr, 0, ETH_ALEN);
1417 }
1418
1419 wpa_s->show_group_started = 0;
1420 if (client) {
1421 /*
1422 * Indicate event only after successfully completed 4-way
1423 * handshake, i.e., when the interface is ready for data
1424 * packets.
1425 */
1426 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001428 wpas_p2p_group_started(wpa_s, 1, ssid,
1429 ssid ? ssid->frequency : 0,
1430 ssid && ssid->passphrase == NULL &&
1431 ssid->psk_set ? ssid->psk : NULL,
1432 ssid ? ssid->passphrase : NULL,
1433 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 wpas_p2p_cross_connect_setup(wpa_s);
1435 wpas_p2p_set_group_idle_timeout(wpa_s);
1436 }
1437
1438 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001439 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1440 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001441 else {
1442 os_free(wpa_s->global->add_psk);
1443 wpa_s->global->add_psk = NULL;
1444 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001445
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001446 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001447 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001448 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450}
1451
1452
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001453struct send_action_work {
1454 unsigned int freq;
1455 u8 dst[ETH_ALEN];
1456 u8 src[ETH_ALEN];
1457 u8 bssid[ETH_ALEN];
1458 size_t len;
1459 unsigned int wait_time;
1460 u8 buf[0];
1461};
1462
1463
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001464static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1465{
1466 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1467
1468 wpa_printf(MSG_DEBUG,
1469 "P2P: Free Action frame radio work @%p (freq=%u dst="
1470 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1471 wpa_s->p2p_send_action_work, awork->freq,
1472 MAC2STR(awork->dst), MAC2STR(awork->src),
1473 MAC2STR(awork->bssid), awork->wait_time);
1474 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1475 awork->buf, awork->len);
1476 os_free(awork);
1477 wpa_s->p2p_send_action_work->ctx = NULL;
1478 radio_work_done(wpa_s->p2p_send_action_work);
1479 wpa_s->p2p_send_action_work = NULL;
1480}
1481
1482
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001483static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1484 void *timeout_ctx)
1485{
1486 struct wpa_supplicant *wpa_s = eloop_ctx;
1487
1488 if (!wpa_s->p2p_send_action_work)
1489 return;
1490
1491 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001492 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001493}
1494
1495
Dmitry Shmidt03658832014-08-13 11:03:49 -07001496static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001498 if (wpa_s->p2p_send_action_work) {
1499 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001500
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001501 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001502 wpa_printf(MSG_DEBUG,
1503 "P2P: Clear Action TX work @%p (wait_time=%u)",
1504 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001505 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001506 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001507 } else {
1508 /*
1509 * In theory, this should not be needed, but number of
1510 * places in the P2P code is still using non-zero wait
1511 * time for the last Action frame in the sequence and
1512 * some of these do not call send_action_done().
1513 */
1514 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1515 wpa_s, NULL);
1516 eloop_register_timeout(
1517 0, awork->wait_time * 1000,
1518 wpas_p2p_send_action_work_timeout,
1519 wpa_s, NULL);
1520 }
1521 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001522}
1523
1524
1525static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1526 unsigned int freq,
1527 const u8 *dst, const u8 *src,
1528 const u8 *bssid,
1529 const u8 *data, size_t data_len,
1530 enum offchannel_send_action_result
1531 result)
1532{
1533 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1534
1535 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1538 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001540 switch (result) {
1541 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1542 res = P2P_SEND_ACTION_SUCCESS;
1543 break;
1544 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1545 res = P2P_SEND_ACTION_NO_ACK;
1546 break;
1547 case OFFCHANNEL_SEND_ACTION_FAILED:
1548 res = P2P_SEND_ACTION_FAILED;
1549 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 }
1551
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001552 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001554 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1555 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001556 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001557 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1558 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001560 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1561 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001562 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001563 P2P_EVENT_FALLBACK_TO_GO_NEG
1564 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001565 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1566 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 }
1568}
1569
1570
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001571static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1572{
1573 struct wpa_supplicant *wpa_s = work->wpa_s;
1574 struct send_action_work *awork = work->ctx;
1575
1576 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001577 if (work->started) {
1578 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1579 wpa_s, NULL);
1580 wpa_s->p2p_send_action_work = NULL;
1581 offchannel_send_action_done(wpa_s);
1582 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001583 os_free(awork);
1584 return;
1585 }
1586
1587 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1588 awork->bssid, awork->buf, awork->len,
1589 awork->wait_time,
1590 wpas_p2p_send_action_tx_status, 1) < 0) {
1591 os_free(awork);
1592 radio_work_done(work);
1593 return;
1594 }
1595 wpa_s->p2p_send_action_work = work;
1596}
1597
1598
1599static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1600 unsigned int freq, const u8 *dst,
1601 const u8 *src, const u8 *bssid, const u8 *buf,
1602 size_t len, unsigned int wait_time)
1603{
1604 struct send_action_work *awork;
1605
Hai Shalom81f62d82019-07-22 12:10:00 -07001606 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001607 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1608 return -1;
1609 }
1610
1611 awork = os_zalloc(sizeof(*awork) + len);
1612 if (awork == NULL)
1613 return -1;
1614
1615 awork->freq = freq;
1616 os_memcpy(awork->dst, dst, ETH_ALEN);
1617 os_memcpy(awork->src, src, ETH_ALEN);
1618 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1619 awork->len = len;
1620 awork->wait_time = wait_time;
1621 os_memcpy(awork->buf, buf, len);
1622
Hai Shalom81f62d82019-07-22 12:10:00 -07001623 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001624 wpas_send_action_cb, awork) < 0) {
1625 os_free(awork);
1626 return -1;
1627 }
1628
1629 return 0;
1630}
1631
1632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1634 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001635 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636{
1637 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001638 int listen_freq = -1, send_freq = -1;
1639
Hai Shalom021b0b52019-04-10 11:17:58 -07001640 if (scheduled)
1641 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001642 if (wpa_s->p2p_listen_work)
1643 listen_freq = wpa_s->p2p_listen_work->freq;
1644 if (wpa_s->p2p_send_action_work)
1645 send_freq = wpa_s->p2p_send_action_work->freq;
1646 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001647 int res;
1648
1649 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1650 listen_freq, send_freq, freq);
1651 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1652 len, wait_time);
1653 if (res == 0 && scheduled)
1654 *scheduled = 1;
1655 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001656 }
1657
1658 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001659 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1660 wait_time,
1661 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662}
1663
1664
1665static void wpas_send_action_done(void *ctx)
1666{
1667 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001668
1669 if (wpa_s->p2p_send_action_work) {
1670 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1671 wpa_s, NULL);
1672 os_free(wpa_s->p2p_send_action_work->ctx);
1673 radio_work_done(wpa_s->p2p_send_action_work);
1674 wpa_s->p2p_send_action_work = NULL;
1675 }
1676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001677 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678}
1679
1680
1681static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1682 struct p2p_go_neg_results *params)
1683{
1684 if (wpa_s->go_params == NULL) {
1685 wpa_s->go_params = os_malloc(sizeof(*params));
1686 if (wpa_s->go_params == NULL)
1687 return -1;
1688 }
1689 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1690 return 0;
1691}
1692
1693
1694static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1695 struct p2p_go_neg_results *res)
1696{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001697 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001698 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1699 " dev_addr " MACSTR " wps_method %d",
1700 MAC2STR(res->peer_interface_addr),
1701 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1703 res->ssid, res->ssid_len);
1704 wpa_supplicant_ap_deinit(wpa_s);
1705 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001706 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001707 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001708#ifdef CONFIG_WPS_NFC
1709 } else if (res->wps_method == WPS_NFC) {
1710 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1711 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001712 wpa_s->p2pdev->p2p_oob_dev_pw,
1713 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1714 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001715 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001716 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001717 NULL,
1718 NULL, 0, 0);
1719#endif /* CONFIG_WPS_NFC */
1720 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001722 if (wpa_s->p2p_wps_method == WPS_P2PS)
1723 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1725 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1726 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1727 wpa_s->p2p_pin, 1, dev_pw_id);
1728 }
1729}
1730
1731
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001732static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1733 struct wpa_ssid *ssid)
1734{
1735 struct wpa_ssid *persistent;
1736 struct psk_list_entry *psk;
1737 struct hostapd_data *hapd;
1738
1739 if (!wpa_s->ap_iface)
1740 return;
1741
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001742 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001743 ssid->ssid_len);
1744 if (persistent == NULL)
1745 return;
1746
1747 hapd = wpa_s->ap_iface->bss[0];
1748
1749 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1750 list) {
1751 struct hostapd_wpa_psk *hpsk;
1752
1753 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1754 MACSTR " psk=%d",
1755 MAC2STR(psk->addr), psk->p2p);
1756 hpsk = os_zalloc(sizeof(*hpsk));
1757 if (hpsk == NULL)
1758 break;
1759 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1760 if (psk->p2p)
1761 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1762 else
1763 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1764 hpsk->next = hapd->conf->ssid.wpa_psk;
1765 hapd->conf->ssid.wpa_psk = hpsk;
1766 }
1767}
1768
1769
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001770static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1771{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001772 char buf[20 + P2P_MAX_CHANNELS * 6];
1773 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001774 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001775 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001776
Roshan Pius3a1667e2018-07-03 15:17:14 -07001777 pos = buf;
1778 end = pos + sizeof(buf);
1779 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1780 res = os_snprintf(pos, end - pos, " %d",
1781 wpa_s->p2p_group_common_freqs[i]);
1782 if (os_snprintf_error(end - pos, res))
1783 break;
1784 pos += res;
1785 }
1786 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001787
Roshan Pius3a1667e2018-07-03 15:17:14 -07001788 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001789}
1790
1791
1792static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1793 struct p2p_go_neg_results *params)
1794{
1795 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1796
1797 wpa_s->p2p_group_common_freqs_num = 0;
1798 os_free(wpa_s->p2p_group_common_freqs);
1799 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1800 if (!wpa_s->p2p_group_common_freqs)
1801 return;
1802
1803 for (i = 0; i < len; i++) {
1804 if (!wpa_s->go_params->freq_list[i])
1805 break;
1806 wpa_s->p2p_group_common_freqs[i] =
1807 wpa_s->go_params->freq_list[i];
1808 }
1809 wpa_s->p2p_group_common_freqs_num = i;
1810}
1811
1812
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001813static void p2p_config_write(struct wpa_supplicant *wpa_s)
1814{
1815#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001816 if (wpa_s->p2pdev->conf->update_config &&
1817 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001818 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1819#endif /* CONFIG_NO_CONFIG_WRITE */
1820}
1821
1822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823static void p2p_go_configured(void *ctx, void *data)
1824{
1825 struct wpa_supplicant *wpa_s = ctx;
1826 struct p2p_go_neg_results *params = data;
1827 struct wpa_ssid *ssid;
1828
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001829 wpa_s->ap_configured_cb = NULL;
1830 wpa_s->ap_configured_cb_ctx = NULL;
1831 wpa_s->ap_configured_cb_data = NULL;
1832 if (!wpa_s->go_params) {
1833 wpa_printf(MSG_ERROR,
1834 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1835 return;
1836 }
1837
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001838 p2p_go_save_group_common_freqs(wpa_s, params);
1839 p2p_go_dump_common_freqs(wpa_s);
1840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841 ssid = wpa_s->current_ssid;
1842 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1843 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1844 if (wpa_s->global->p2p_group_formation == wpa_s)
1845 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001846 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1847 params->passphrase[0] == '\0' ?
1848 params->psk : NULL,
1849 params->passphrase,
1850 wpa_s->global->p2p_dev_addr,
1851 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001852 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001853
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001854 if (wpa_s->p2pdev->p2ps_method_config_any) {
1855 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001856 wpa_dbg(wpa_s, MSG_DEBUG,
1857 "P2PS: Setting default PIN for ANY");
1858 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1859 "12345670", NULL, 0,
1860 0);
1861 } else {
1862 wpa_dbg(wpa_s, MSG_DEBUG,
1863 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001864 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001865 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001866 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001867 "12345670", NULL, 0, 0);
1868 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001869 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001870 }
1871
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001872 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001873 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001874 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001875 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001876 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001877 wpas_p2p_add_psk_list(wpa_s, ssid);
1878 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001879
1880 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001881 params->persistent_group, 0,
1882 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 wpas_p2p_cross_connect_setup(wpa_s);
1884 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001885
1886 if (wpa_s->p2p_first_connection_timeout) {
1887 wpa_dbg(wpa_s, MSG_DEBUG,
1888 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1889 wpa_s->p2p_first_connection_timeout);
1890 wpa_s->p2p_go_group_formation_completed = 0;
1891 wpa_s->global->p2p_group_formation = wpa_s;
1892 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001893 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001894 eloop_register_timeout(
1895 wpa_s->p2p_first_connection_timeout, 0,
1896 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001897 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001898 }
1899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 return;
1901 }
1902
1903 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1904 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1905 params->peer_interface_addr)) {
1906 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1907 "filtering");
1908 return;
1909 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001910 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001912 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001913#ifdef CONFIG_WPS_NFC
1914 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001915 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001916 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001917 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001918 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1919 return;
1920 }
1921 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001922 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1923 wpa_s->p2pdev->p2p_oob_dev_pw,
1924 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1925 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001926#endif /* CONFIG_WPS_NFC */
1927 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001929 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 os_free(wpa_s->go_params);
1931 wpa_s->go_params = NULL;
1932}
1933
1934
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001935/**
1936 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1937 * @freq: Frequency (MHz) to convert
1938 * @op_class: Buffer for returning operating class
1939 * @op_edmg_channel: Buffer for returning channel number
1940 * Returns: 0 on success, -1 on failure
1941 *
1942 * This can be used to find the highest channel bonding which includes the
1943 * specified frequency.
1944 */
1945static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1946 unsigned int freq,
1947 u8 *op_class, u8 *op_edmg_channel)
1948{
1949 struct hostapd_hw_modes *hwmode;
1950 struct ieee80211_edmg_config edmg;
1951 unsigned int i;
1952 enum chan_width chanwidth[] = {
1953 CHAN_WIDTH_8640,
1954 CHAN_WIDTH_6480,
1955 CHAN_WIDTH_4320,
1956 };
1957
1958 if (!wpa_s->hw.modes)
1959 return -1;
1960
1961 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08001962 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001963 if (!hwmode) {
1964 wpa_printf(MSG_ERROR,
1965 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1966 return -1;
1967 }
1968
1969 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1970 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1971 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1972 op_class,
1973 op_edmg_channel) < 0)
1974 continue;
1975
1976 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1977 if (edmg.channels &&
1978 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1979 wpa_printf(MSG_DEBUG,
1980 "Freq %u to EDMG channel %u at opclass %u",
1981 freq, *op_edmg_channel, *op_class);
1982 return 0;
1983 }
1984 }
1985
1986 return -1;
1987}
1988
1989
1990int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
1991 struct p2p_go_neg_results *params)
1992{
1993 u8 op_channel, op_class;
1994 int freq;
1995
1996 /* Try social channel as primary channel frequency */
1997 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
1998
1999 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2000 &op_channel) == 0) {
2001 wpa_printf(MSG_DEBUG,
2002 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2003 freq, op_channel, op_class);
2004 params->freq = freq;
2005 return 0;
2006 }
2007
2008 return -1;
2009}
2010
2011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2013 struct p2p_go_neg_results *params,
2014 int group_formation)
2015{
2016 struct wpa_ssid *ssid;
2017
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002018 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2019 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2020 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2021 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002023 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024
2025 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002026 if (ssid == NULL) {
2027 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002031 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002032 wpa_s->p2p_go_group_formation_completed = 0;
2033 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002034 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 wpa_config_set_network_defaults(ssid);
2037 ssid->temporary = 1;
2038 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002039 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2041 WPAS_MODE_P2P_GO;
2042 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002043 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002044 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002045 ssid->max_oper_chwidth = params->max_oper_chwidth;
2046 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002047 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002048 if (params->edmg) {
2049 u8 op_channel, op_class;
2050
2051 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2052 &op_class, &op_channel)) {
2053 ssid->edmg_channel = op_channel;
2054 ssid->enable_edmg = params->edmg;
2055 } else {
2056 wpa_dbg(wpa_s, MSG_DEBUG,
2057 "P2P: Could not match EDMG channel, freq %d, for GO",
2058 params->freq);
2059 }
2060 }
2061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 ssid->ssid = os_zalloc(params->ssid_len + 1);
2063 if (ssid->ssid) {
2064 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2065 ssid->ssid_len = params->ssid_len;
2066 }
2067 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2068 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2069 ssid->proto = WPA_PROTO_RSN;
2070 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002071 ssid->group_cipher = WPA_CIPHER_CCMP;
2072 if (params->freq > 56160) {
2073 /*
2074 * Enable GCMP instead of CCMP as pairwise_cipher and
2075 * group_cipher in 60 GHz.
2076 */
2077 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2078 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002079 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2080 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002081 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002082 if (os_strlen(params->passphrase) > 0) {
2083 ssid->passphrase = os_strdup(params->passphrase);
2084 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002085 wpa_msg_global(wpa_s, MSG_ERROR,
2086 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002087 wpa_config_remove_network(wpa_s->conf, ssid->id);
2088 return;
2089 }
2090 } else
2091 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002092 ssid->psk_set = params->psk_set;
2093 if (ssid->psk_set)
2094 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002095 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002096 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002097 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098
2099 wpa_s->ap_configured_cb = p2p_go_configured;
2100 wpa_s->ap_configured_cb_ctx = wpa_s;
2101 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002102 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002103 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 wpa_s->reassociate = 1;
2105 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002106 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2107 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 wpa_supplicant_req_scan(wpa_s, 0, 0);
2109}
2110
2111
2112static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2113 const struct wpa_supplicant *src)
2114{
2115 struct wpa_config *d;
2116 const struct wpa_config *s;
2117
2118 d = dst->conf;
2119 s = src->conf;
2120
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002121#define C(n) \
2122do { \
2123 if (s->n && !d->n) \
2124 d->n = os_strdup(s->n); \
2125} while (0)
2126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 C(device_name);
2128 C(manufacturer);
2129 C(model_name);
2130 C(model_number);
2131 C(serial_number);
2132 C(config_methods);
2133#undef C
2134
2135 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2136 os_memcpy(d->sec_device_type, s->sec_device_type,
2137 sizeof(d->sec_device_type));
2138 d->num_sec_device_types = s->num_sec_device_types;
2139
2140 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002141 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002143 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002144 d->max_num_sta = s->max_num_sta;
2145 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002146 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002147 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002148 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002149 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002150 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002151 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002152 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002153
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002154 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2155 !d->wps_nfc_pw_from_config) {
2156 wpabuf_free(d->wps_nfc_dh_privkey);
2157 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002158 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2159 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2160 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002161 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002162 d->go_interworking = s->go_interworking;
2163 d->go_access_network_type = s->go_access_network_type;
2164 d->go_internet = s->go_internet;
2165 d->go_venue_group = s->go_venue_group;
2166 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002167 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168}
2169
2170
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002171static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2172 char *ifname, size_t len)
2173{
2174 char *ifname_ptr = wpa_s->ifname;
2175
2176 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2177 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2178 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2179 }
2180
2181 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2182 if (os_strlen(ifname) >= IFNAMSIZ &&
2183 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002184 int res;
2185
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002186 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002187 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2188 if (os_snprintf_error(len, res) && len)
2189 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002190 }
2191}
2192
2193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2195 enum wpa_driver_if_type type)
2196{
2197 char ifname[120], force_ifname[120];
2198
2199 if (wpa_s->pending_interface_name[0]) {
2200 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2201 "- skip creation of a new one");
2202 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2203 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2204 "unknown?! ifname='%s'",
2205 wpa_s->pending_interface_name);
2206 return -1;
2207 }
2208 return 0;
2209 }
2210
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002211 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212 force_ifname[0] = '\0';
2213
2214 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2215 ifname);
2216 wpa_s->p2p_group_idx++;
2217
2218 wpa_s->pending_interface_type = type;
2219 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2220 wpa_s->pending_interface_addr, NULL) < 0) {
2221 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2222 "interface");
2223 return -1;
2224 }
2225
Jimmy Chen36c21992018-11-29 16:46:43 +08002226 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002227 random_mac_addr(wpa_s->pending_interface_addr);
2228 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2229 " for the group",
2230 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002231 }
2232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 if (force_ifname[0]) {
2234 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2235 force_ifname);
2236 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2237 sizeof(wpa_s->pending_interface_name));
2238 } else
2239 os_strlcpy(wpa_s->pending_interface_name, ifname,
2240 sizeof(wpa_s->pending_interface_name));
2241 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2242 MACSTR, wpa_s->pending_interface_name,
2243 MAC2STR(wpa_s->pending_interface_addr));
2244
2245 return 0;
2246}
2247
2248
2249static void wpas_p2p_remove_pending_group_interface(
2250 struct wpa_supplicant *wpa_s)
2251{
2252 if (!wpa_s->pending_interface_name[0] ||
2253 is_zero_ether_addr(wpa_s->pending_interface_addr))
2254 return; /* No pending virtual interface */
2255
2256 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2257 wpa_s->pending_interface_name);
2258 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2259 wpa_s->pending_interface_name);
2260 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2261 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002262 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263}
2264
2265
2266static struct wpa_supplicant *
2267wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2268{
2269 struct wpa_interface iface;
2270 struct wpa_supplicant *group_wpa_s;
2271
2272 if (!wpa_s->pending_interface_name[0]) {
2273 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2274 if (!wpas_p2p_create_iface(wpa_s))
2275 return NULL;
2276 /*
2277 * Something has forced us to remove the pending interface; try
2278 * to create a new one and hope for the best that we will get
2279 * the same local address.
2280 */
2281 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2282 WPA_IF_P2P_CLIENT) < 0)
2283 return NULL;
2284 }
2285
2286 os_memset(&iface, 0, sizeof(iface));
2287 iface.ifname = wpa_s->pending_interface_name;
2288 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002289 if (wpa_s->conf->ctrl_interface == NULL &&
2290 wpa_s->parent != wpa_s &&
2291 wpa_s->p2p_mgmt &&
2292 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2293 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2294 else
2295 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002297 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 if (group_wpa_s == NULL) {
2299 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2300 "wpa_supplicant interface");
2301 return NULL;
2302 }
2303 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2305 P2P_GROUP_INTERFACE_CLIENT;
2306 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002307 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308
2309 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2310
Jimmy Chen36c21992018-11-29 16:46:43 +08002311 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002312 if (wpa_drv_set_mac_addr(group_wpa_s,
2313 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002314 wpa_msg(group_wpa_s, MSG_INFO,
2315 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002316 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2317 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002318 return NULL;
2319 }
2320
2321 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2322 wpa_msg(group_wpa_s, MSG_INFO,
2323 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002324 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2325 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002326 return NULL;
2327 }
2328
Hai Shalom74f70d42019-02-11 14:42:39 -08002329 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2330 " for the group",
2331 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002332 }
2333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 return group_wpa_s;
2335}
2336
2337
2338static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2339 void *timeout_ctx)
2340{
2341 struct wpa_supplicant *wpa_s = eloop_ctx;
2342 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002343 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002344}
2345
2346
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002347static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2348 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002349{
2350 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002351 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 if (wpa_s->global->p2p)
2353 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002354 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355}
2356
2357
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002358static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2359{
2360 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2361 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002362 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002363 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002364 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002365 wpa_s->global->p2p_fail_on_wps_complete = 0;
2366}
2367
2368
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002369void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2370{
2371 if (wpa_s->global->p2p_group_formation != wpa_s)
2372 return;
2373 /* Speed up group formation timeout since this cannot succeed */
2374 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002375 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002376 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002377 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002378}
2379
2380
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002381static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382{
2383 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002384 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385
2386 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2387 wpa_drv_cancel_remain_on_channel(wpa_s);
2388 wpa_s->off_channel_freq = 0;
2389 wpa_s->roc_waiting_drv_freq = 0;
2390 }
2391
2392 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002393 wpa_msg_global(wpa_s, MSG_INFO,
2394 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2395 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002396 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397 wpas_p2p_remove_pending_group_interface(wpa_s);
2398 return;
2399 }
2400
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002401 if (!res->role_go) {
2402 /* Inform driver of the operating channel of GO. */
2403 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2404 }
2405
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002406 if (wpa_s->p2p_go_ht40)
2407 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002408 if (wpa_s->p2p_go_vht)
2409 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002410 if (wpa_s->p2p_go_he)
2411 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002412 if (wpa_s->p2p_go_edmg)
2413 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002414 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2415 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002416
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002417 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2418 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2419 " wps_method=%s",
2420 res->role_go ? "GO" : "client", res->freq, res->ht40,
2421 MAC2STR(res->peer_device_addr),
2422 MAC2STR(res->peer_interface_addr),
2423 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002424 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425
Dmitry Shmidt04949592012-07-19 12:16:46 -07002426 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2427 struct wpa_ssid *ssid;
2428 ssid = wpa_config_get_network(wpa_s->conf,
2429 wpa_s->p2p_persistent_id);
2430 if (ssid && ssid->disabled == 2 &&
2431 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2432 size_t len = os_strlen(ssid->passphrase);
2433 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2434 "on requested persistent group");
2435 os_memcpy(res->passphrase, ssid->passphrase, len);
2436 res->passphrase[len] = '\0';
2437 }
2438 }
2439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002441 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2443 if (group_wpa_s == NULL) {
2444 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002445 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2446 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002447 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448 return;
2449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2451 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002453 group_wpa_s = wpa_s->parent;
2454 wpa_s->global->p2p_group_formation = group_wpa_s;
2455 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002456 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002459 group_wpa_s->p2p_in_provisioning = 1;
2460 group_wpa_s->p2pdev = wpa_s;
2461 if (group_wpa_s != wpa_s) {
2462 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2463 sizeof(group_wpa_s->p2p_pin));
2464 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2465 }
2466 if (res->role_go) {
2467 wpas_start_wps_go(group_wpa_s, res, 1);
2468 } else {
2469 os_get_reltime(&group_wpa_s->scan_min_time);
2470 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 }
2472
Hai Shalomfdcde762020-04-02 11:19:20 -07002473 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2475
2476 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2477 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2478 (res->peer_config_timeout % 100) * 10000,
2479 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2480}
2481
2482
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002483static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2484 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485{
2486 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002487 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002488 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2489 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002491 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492}
2493
2494
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002495static void wpas_dev_found(void *ctx, const u8 *addr,
2496 const struct p2p_peer_info *info,
2497 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002499 u8 *wfd_dev_info = NULL;
2500 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002501 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002502 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002503#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 struct wpa_supplicant *wpa_s = ctx;
2505 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002506 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002507 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002508
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002509#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002510 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2511 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002512 if (wfd_dev_info_hex) {
2513 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2514 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2515 // Only used for notification, so not handling error.
2516 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2517 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002518
2519 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2520 WFD_SUBELEM_R2_DEVICE_INFO);
2521 if (wfd_r2_dev_info_hex) {
2522 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2523 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2524 // Only used for notification, so not handling error.
2525 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2526 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002527#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002528
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002529 if (info->p2ps_instance) {
2530 char str[256];
2531 const u8 *buf = wpabuf_head(info->p2ps_instance);
2532 size_t len = wpabuf_len(info->p2ps_instance);
2533
2534 while (len) {
2535 u32 id;
2536 u16 methods;
2537 u8 str_len;
2538
2539 if (len < 4 + 2 + 1)
2540 break;
2541 id = WPA_GET_LE32(buf);
2542 buf += sizeof(u32);
2543 methods = WPA_GET_BE16(buf);
2544 buf += sizeof(u16);
2545 str_len = *buf++;
2546 if (str_len > len - 4 - 2 - 1)
2547 break;
2548 os_memcpy(str, buf, str_len);
2549 str[str_len] = '\0';
2550 buf += str_len;
2551 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2552
2553 wpa_msg_global(wpa_s, MSG_INFO,
2554 P2P_EVENT_DEVICE_FOUND MACSTR
2555 " p2p_dev_addr=" MACSTR
2556 " pri_dev_type=%s name='%s'"
2557 " config_methods=0x%x"
2558 " dev_capab=0x%x"
2559 " group_capab=0x%x"
2560 " adv_id=%x asp_svc=%s%s",
2561 MAC2STR(addr),
2562 MAC2STR(info->p2p_device_addr),
2563 wps_dev_type_bin2str(
2564 info->pri_dev_type,
2565 devtype, sizeof(devtype)),
2566 info->device_name, methods,
2567 info->dev_capab, info->group_capab,
2568 id, str,
2569 info->vendor_elems ?
2570 " vendor_elems=1" : "");
2571 }
2572 goto done;
2573 }
2574
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002575 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2576 " p2p_dev_addr=" MACSTR
2577 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002578 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002579 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2580 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2581 sizeof(devtype)),
2582 info->device_name, info->config_methods,
2583 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002584 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002585 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002586 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2587 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002588 info->vendor_elems ? " vendor_elems=1" : "",
2589 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002591done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002592 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002593 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002594#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002595
Roshan Piusfd2fd662017-01-23 13:41:57 -08002596 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002597 wfd_dev_info_len, wfd_r2_dev_info,
2598 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002599 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600}
2601
2602
2603static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2604{
2605 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002606
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002607 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2608 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2611}
2612
2613
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002614static void wpas_find_stopped(void *ctx)
2615{
2616 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002617
2618 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2619 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2620
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002621 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002622 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002623}
2624
2625
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002626struct wpas_p2p_listen_work {
2627 unsigned int freq;
2628 unsigned int duration;
2629 struct wpabuf *probe_resp_ie;
2630};
2631
2632
2633static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2634{
2635 if (lwork == NULL)
2636 return;
2637 wpabuf_free(lwork->probe_resp_ie);
2638 os_free(lwork);
2639}
2640
2641
2642static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2643{
2644 struct wpas_p2p_listen_work *lwork;
2645
2646 if (!wpa_s->p2p_listen_work)
2647 return;
2648
2649 lwork = wpa_s->p2p_listen_work->ctx;
2650 wpas_p2p_listen_work_free(lwork);
2651 radio_work_done(wpa_s->p2p_listen_work);
2652 wpa_s->p2p_listen_work = NULL;
2653}
2654
2655
2656static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2657{
2658 struct wpa_supplicant *wpa_s = work->wpa_s;
2659 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002660 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002661
2662 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002663 if (work->started) {
2664 wpa_s->p2p_listen_work = NULL;
2665 wpas_stop_listen(wpa_s);
2666 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002667 wpas_p2p_listen_work_free(lwork);
2668 return;
2669 }
2670
2671 wpa_s->p2p_listen_work = work;
2672
2673 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2674
2675 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2676 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2677 "report received Probe Request frames");
2678 wpas_p2p_listen_work_done(wpa_s);
2679 return;
2680 }
2681
2682 wpa_s->pending_listen_freq = lwork->freq;
2683 wpa_s->pending_listen_duration = lwork->duration;
2684
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002685 duration = lwork->duration;
2686#ifdef CONFIG_TESTING_OPTIONS
2687 if (wpa_s->extra_roc_dur) {
2688 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2689 duration, duration + wpa_s->extra_roc_dur);
2690 duration += wpa_s->extra_roc_dur;
2691 }
2692#endif /* CONFIG_TESTING_OPTIONS */
2693
2694 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002695 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2696 "to remain on channel (%u MHz) for Listen "
2697 "state", lwork->freq);
2698 wpas_p2p_listen_work_done(wpa_s);
2699 wpa_s->pending_listen_freq = 0;
2700 return;
2701 }
2702 wpa_s->off_channel_freq = 0;
2703 wpa_s->roc_waiting_drv_freq = lwork->freq;
2704}
2705
2706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707static int wpas_start_listen(void *ctx, unsigned int freq,
2708 unsigned int duration,
2709 const struct wpabuf *probe_resp_ie)
2710{
2711 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002712 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002714 if (wpa_s->p2p_listen_work) {
2715 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716 return -1;
2717 }
2718
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002719 lwork = os_zalloc(sizeof(*lwork));
2720 if (lwork == NULL)
2721 return -1;
2722 lwork->freq = freq;
2723 lwork->duration = duration;
2724 if (probe_resp_ie) {
2725 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2726 if (lwork->probe_resp_ie == NULL) {
2727 wpas_p2p_listen_work_free(lwork);
2728 return -1;
2729 }
2730 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002732 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2733 lwork) < 0) {
2734 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 return -1;
2736 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737
2738 return 0;
2739}
2740
2741
2742static void wpas_stop_listen(void *ctx)
2743{
2744 struct wpa_supplicant *wpa_s = ctx;
2745 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2746 wpa_drv_cancel_remain_on_channel(wpa_s);
2747 wpa_s->off_channel_freq = 0;
2748 wpa_s->roc_waiting_drv_freq = 0;
2749 }
2750 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002751
2752 /*
2753 * Don't cancel Probe Request RX reporting for a connected P2P Client
2754 * handling Probe Request frames.
2755 */
2756 if (!wpa_s->p2p_cli_probe)
2757 wpa_drv_probe_req_report(wpa_s, 0);
2758
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002759 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760}
2761
2762
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002763static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2764 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765{
2766 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002767 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002768 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769}
2770
2771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2773 const u8 *peer, const char *params,
2774 unsigned int generated_pin)
2775{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002776 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2777 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778}
2779
2780
2781static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2782 const u8 *peer, const char *params)
2783{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002784 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2785 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786}
2787
2788
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002789static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2790 const u8 *dev_addr, const u8 *pri_dev_type,
2791 const char *dev_name, u16 supp_config_methods,
2792 u8 dev_capab, u8 group_capab, const u8 *group_id,
2793 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794{
2795 struct wpa_supplicant *wpa_s = ctx;
2796 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002797 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 u8 empty_dev_type[8];
2799 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002800 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002801 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002802
2803 if (group_id) {
2804 for (group = wpa_s->global->ifaces; group; group = group->next)
2805 {
2806 struct wpa_ssid *s = group->current_ssid;
2807 if (s != NULL &&
2808 s->mode == WPAS_MODE_P2P_GO &&
2809 group_id_len - ETH_ALEN == s->ssid_len &&
2810 os_memcmp(group_id + ETH_ALEN, s->ssid,
2811 s->ssid_len) == 0)
2812 break;
2813 }
2814 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815
2816 if (pri_dev_type == NULL) {
2817 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2818 pri_dev_type = empty_dev_type;
2819 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002820 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2821 " pri_dev_type=%s name='%s' config_methods=0x%x "
2822 "dev_capab=0x%x group_capab=0x%x%s%s",
2823 MAC2STR(dev_addr),
2824 wps_dev_type_bin2str(pri_dev_type, devtype,
2825 sizeof(devtype)),
2826 dev_name, supp_config_methods, dev_capab, group_capab,
2827 group ? " group=" : "",
2828 group ? group->ifname : "");
2829 if (os_snprintf_error(sizeof(params), res))
2830 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002834 if (wps_generate_pin(&generated_pin) < 0) {
2835 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2836 wpas_notify_p2p_provision_discovery(
2837 wpa_s, peer, 0 /* response */,
2838 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2839 return;
2840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 wpas_prov_disc_local_display(wpa_s, peer, params,
2842 generated_pin);
2843 } else if (config_methods & WPS_CONFIG_KEYPAD)
2844 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2845 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002846 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2847 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002848
2849 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2850 P2P_PROV_DISC_SUCCESS,
2851 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852}
2853
2854
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002855static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856{
2857 struct wpa_supplicant *wpa_s = ctx;
2858 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002859 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002861 if (wpa_s->pending_pd_before_join &&
2862 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2863 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2864 wpa_s->pending_pd_before_join = 0;
2865 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2866 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002867 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002868 return;
2869 }
2870
Dmitry Shmidt04949592012-07-19 12:16:46 -07002871 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002872 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2873 int res;
2874
2875 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2876 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2877 if (os_snprintf_error(sizeof(params), res))
2878 params[sizeof(params) - 1] = '\0';
2879 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002880 params[0] = '\0';
2881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002883 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002885 if (wps_generate_pin(&generated_pin) < 0) {
2886 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2887 wpas_notify_p2p_provision_discovery(
2888 wpa_s, peer, 0 /* response */,
2889 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2890 return;
2891 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002892 wpas_prov_disc_local_display(wpa_s, peer, params,
2893 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002895 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2896 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897
Jouni Malinen75ecf522011-06-27 15:19:46 -07002898 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2899 P2P_PROV_DISC_SUCCESS,
2900 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901}
2902
2903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002904static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002905 enum p2p_prov_disc_status status,
2906 u32 adv_id, const u8 *adv_mac,
2907 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002908{
2909 struct wpa_supplicant *wpa_s = ctx;
2910
Dmitry Shmidt04949592012-07-19 12:16:46 -07002911 if (wpa_s->p2p_fallback_to_go_neg) {
2912 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2913 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002914 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002915 P2P_EVENT_FALLBACK_TO_GO_NEG
2916 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002917 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2918 return;
2919 }
2920
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002921 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002922 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002923 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2924 "join-existing-group operation (no ACK for PD "
2925 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002926 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002927 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002928 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002929
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002930 if (adv_id && adv_mac && deferred_session_resp) {
2931 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2932 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2933 " deferred_session_resp='%s'",
2934 MAC2STR(peer), status, adv_id,
2935 deferred_session_resp);
2936 } else if (adv_id && adv_mac) {
2937 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2938 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2939 MAC2STR(peer), status, adv_id);
2940 } else {
2941 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2942 " p2p_dev_addr=" MACSTR " status=%d",
2943 MAC2STR(peer), status);
2944 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002945
Jouni Malinen75ecf522011-06-27 15:19:46 -07002946 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2947 status, 0, 0);
2948}
2949
2950
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002951static int freq_included(struct wpa_supplicant *wpa_s,
2952 const struct p2p_channels *channels,
2953 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002954{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002955 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2956 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2957 return 1;
2958 return 0;
2959}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002960
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002961
2962static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2963{
2964 unsigned int num = P2P_MAX_CHANNELS;
2965 int *common_freqs;
2966 int ret;
2967
2968 p2p_go_dump_common_freqs(wpa_s);
2969 common_freqs = os_calloc(num, sizeof(int));
2970 if (!common_freqs)
2971 return;
2972
2973 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2974 if (ret < 0) {
2975 wpa_dbg(wpa_s, MSG_DEBUG,
2976 "P2P: Failed to get group common freqs");
2977 os_free(common_freqs);
2978 return;
2979 }
2980
2981 os_free(wpa_s->p2p_group_common_freqs);
2982 wpa_s->p2p_group_common_freqs = common_freqs;
2983 wpa_s->p2p_group_common_freqs_num = num;
2984 p2p_go_dump_common_freqs(wpa_s);
2985}
2986
2987
2988/*
2989 * Check if the given frequency is one of the possible operating frequencies
2990 * set after the completion of the GO Negotiation.
2991 */
2992static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2993{
2994 unsigned int i;
2995
2996 p2p_go_dump_common_freqs(wpa_s);
2997
2998 /* assume no restrictions */
2999 if (!wpa_s->p2p_group_common_freqs_num)
3000 return 1;
3001
3002 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3003 if (wpa_s->p2p_group_common_freqs[i] == freq)
3004 return 1;
3005 }
3006 return 0;
3007}
3008
3009
3010static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3011 struct sta_info *sta, void *ctx)
3012{
3013 int *ecsa_support = ctx;
3014
3015 *ecsa_support &= sta->ecsa_supported;
3016
3017 return 0;
3018}
3019
3020
3021/* Check if all the peers support eCSA */
3022static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3023{
3024 int ecsa_support = 1;
3025
3026 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3027 &ecsa_support);
3028
3029 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003030}
3031
3032
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003033/**
3034 * Pick the best frequency to use from all the currently used frequencies.
3035 */
3036static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3037 struct wpa_used_freq_data *freqs,
3038 unsigned int num)
3039{
3040 unsigned int i, c;
3041
3042 /* find a candidate freq that is supported by P2P */
3043 for (c = 0; c < num; c++)
3044 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3045 break;
3046
3047 if (c == num)
3048 return 0;
3049
3050 /* once we have a candidate, try to find a 'better' one */
3051 for (i = c + 1; i < num; i++) {
3052 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3053 continue;
3054
3055 /*
3056 * 1. Infrastructure station interfaces have higher preference.
3057 * 2. P2P Clients have higher preference.
3058 * 3. All others.
3059 */
3060 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3061 c = i;
3062 break;
3063 }
3064
3065 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3066 c = i;
3067 }
3068 return freqs[c].freq;
3069}
3070
3071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3073 const u8 *go_dev_addr, const u8 *ssid,
3074 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003075 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003076 const struct p2p_channels *channels,
3077 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078{
3079 struct wpa_supplicant *wpa_s = ctx;
3080 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003081 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003083 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084
3085 if (!persistent_group) {
3086 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003087 " to join an active group (SSID: %s)",
3088 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3090 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3091 == 0 ||
3092 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3093 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3094 "authorized invitation");
3095 goto accept_inv;
3096 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003097
3098#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003099 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3100 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003101 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003102 wpa_s->p2p_wps_method = WPS_NFC;
3103 wpa_s->pending_join_wps_method = WPS_NFC;
3104 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003105 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003106 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003107 bssid, ETH_ALEN);
3108 goto accept_inv;
3109 }
3110#endif /* CONFIG_WPS_NFC */
3111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 /*
3113 * Do not accept the invitation automatically; notify user and
3114 * request approval.
3115 */
3116 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3117 }
3118
3119 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3120 if (grp) {
3121 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3122 "running persistent group");
3123 if (*go)
3124 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3125 goto accept_inv;
3126 }
3127
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003128 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3129 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3130 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3131 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003132 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003133 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3135
3136 for (s = wpa_s->conf->ssid; s; s = s->next) {
3137 if (s->disabled == 2 &&
3138 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3139 s->ssid_len == ssid_len &&
3140 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3141 break;
3142 }
3143
3144 if (!s) {
3145 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3146 " requested reinvocation of an unknown group",
3147 MAC2STR(sa));
3148 return P2P_SC_FAIL_UNKNOWN_GROUP;
3149 }
3150
3151 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3152 *go = 1;
3153 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3154 wpa_printf(MSG_DEBUG, "P2P: The only available "
3155 "interface is already in use - reject "
3156 "invitation");
3157 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3158 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003159 if (wpa_s->p2p_mgmt)
3160 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3161 ETH_ALEN);
3162 else
3163 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 } else if (s->mode == WPAS_MODE_P2P_GO) {
3165 *go = 1;
3166 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3167 {
3168 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3169 "interface address for the group");
3170 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3171 }
3172 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3173 ETH_ALEN);
3174 }
3175
3176accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003177 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3178
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003179 best_freq = 0;
3180 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3181 sizeof(struct wpa_used_freq_data));
3182 if (freqs) {
3183 int num_channels = wpa_s->num_multichan_concurrent;
3184 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3185 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3186 os_free(freqs);
3187 }
3188
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003189 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003190 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003191 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003192 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003193
3194 if (wpa_s->num_multichan_concurrent < 2 ||
3195 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3196 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 -07003197 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003198 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 }
3200
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003201 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3202 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003203 if (*go == 0) {
3204 /* We are the client */
3205 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3206 "running a GO but we are capable of MCC, "
3207 "figure out the best channel to use");
3208 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003209 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003210 /* We are the GO, and *force_freq is not in the
3211 * intersection */
3212 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3213 "in intersection but we are capable of MCC, "
3214 "figure out the best channel to use",
3215 *force_freq);
3216 *force_freq = 0;
3217 }
3218 }
3219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 return P2P_SC_SUCCESS;
3221}
3222
3223
3224static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3225 const u8 *ssid, size_t ssid_len,
3226 const u8 *go_dev_addr, u8 status,
3227 int op_freq)
3228{
3229 struct wpa_supplicant *wpa_s = ctx;
3230 struct wpa_ssid *s;
3231
3232 for (s = wpa_s->conf->ssid; s; s = s->next) {
3233 if (s->disabled == 2 &&
3234 s->ssid_len == ssid_len &&
3235 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3236 break;
3237 }
3238
3239 if (status == P2P_SC_SUCCESS) {
3240 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003241 " was accepted; op_freq=%d MHz, SSID=%s",
3242 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003244 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003245 if (go) {
3246 wpa_msg_global(wpa_s, MSG_INFO,
3247 P2P_EVENT_INVITATION_ACCEPTED
3248 "sa=" MACSTR
3249 " persistent=%d freq=%d",
3250 MAC2STR(sa), s->id, op_freq);
3251 } else {
3252 wpa_msg_global(wpa_s, MSG_INFO,
3253 P2P_EVENT_INVITATION_ACCEPTED
3254 "sa=" MACSTR
3255 " persistent=%d",
3256 MAC2STR(sa), s->id);
3257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003259 wpa_s, s, go, 0, op_freq, 0,
3260 wpa_s->conf->p2p_go_ht40,
3261 wpa_s->conf->p2p_go_vht,
3262 0,
3263 wpa_s->conf->p2p_go_he,
3264 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003265 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3266 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003268 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003269 wpa_msg_global(wpa_s, MSG_INFO,
3270 P2P_EVENT_INVITATION_ACCEPTED
3271 "sa=" MACSTR " go_dev_addr=" MACSTR
3272 " bssid=" MACSTR " unknown-network",
3273 MAC2STR(sa), MAC2STR(go_dev_addr),
3274 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003276 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003277 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 }
3279 return;
3280 }
3281
3282 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3283 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3284 " was rejected (status %u)", MAC2STR(sa), status);
3285 return;
3286 }
3287
3288 if (!s) {
3289 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003290 wpa_msg_global(wpa_s, MSG_INFO,
3291 P2P_EVENT_INVITATION_RECEIVED
3292 "sa=" MACSTR " go_dev_addr=" MACSTR
3293 " bssid=" MACSTR " unknown-network",
3294 MAC2STR(sa), MAC2STR(go_dev_addr),
3295 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003296 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003297 wpa_msg_global(wpa_s, MSG_INFO,
3298 P2P_EVENT_INVITATION_RECEIVED
3299 "sa=" MACSTR " go_dev_addr=" MACSTR
3300 " unknown-network",
3301 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003303 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3304 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305 return;
3306 }
3307
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003308 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003309 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3310 "sa=" MACSTR " persistent=%d freq=%d",
3311 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003312 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003313 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3314 "sa=" MACSTR " persistent=%d",
3315 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003316 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003317 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3318 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319}
3320
3321
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003322static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3323 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003324 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003325{
3326 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003327 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003328
3329 if (ssid == NULL)
3330 return;
3331
3332 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003333 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003334 ETH_ALEN) == 0)
3335 break;
3336 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003337 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003338 if (ssid->mode != WPAS_MODE_P2P_GO &&
3339 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3340 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3341 "due to invitation result", ssid->id);
3342 wpas_notify_network_removed(wpa_s, ssid);
3343 wpa_config_remove_network(wpa_s->conf, ssid->id);
3344 return;
3345 }
3346 return; /* Peer not found in client list */
3347 }
3348
3349 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003350 "group %d client list%s",
3351 MAC2STR(peer), ssid->id,
3352 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003353 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3354 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3355 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003356 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003357 if (p2p_wpa_s->conf->update_config &&
3358 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003359 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003360}
3361
3362
3363static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3364 const u8 *peer)
3365{
3366 struct wpa_ssid *ssid;
3367
3368 wpa_s = wpa_s->global->p2p_invite_group;
3369 if (wpa_s == NULL)
3370 return; /* No known invitation group */
3371 ssid = wpa_s->current_ssid;
3372 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3373 !ssid->p2p_persistent_group)
3374 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003375 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003376 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003377 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003378}
3379
3380
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003381static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003382 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003383 const u8 *peer, int neg_freq,
3384 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385{
3386 struct wpa_supplicant *wpa_s = ctx;
3387 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003388 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389
3390 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003391 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3392 "status=%d " MACSTR,
3393 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003395 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3396 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 }
3398 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3399
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003400 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3401 status, MAC2STR(peer));
3402 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003403 struct wpa_supplicant *group_if =
3404 wpa_s->global->p2p_invite_group;
3405
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003406 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3407 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003408
3409 /*
3410 * Invitation to an active group. If this is successful and we
3411 * are the GO, set the client wait to postpone some concurrent
3412 * operations and to allow provisioning and connection to happen
3413 * more quickly.
3414 */
3415 if (status == P2P_SC_SUCCESS &&
3416 group_if && group_if->current_ssid &&
3417 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3418 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3419#ifdef CONFIG_TESTING_OPTIONS
3420 if (group_if->p2p_go_csa_on_inv) {
3421 wpa_printf(MSG_DEBUG,
3422 "Testing: force P2P GO CSA after invitation");
3423 eloop_cancel_timeout(
3424 wpas_p2p_reconsider_moving_go,
3425 wpa_s, NULL);
3426 eloop_register_timeout(
3427 0, 50000,
3428 wpas_p2p_reconsider_moving_go,
3429 wpa_s, NULL);
3430 }
3431#endif /* CONFIG_TESTING_OPTIONS */
3432 }
3433 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003434 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003436 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3437 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3438 "invitation exchange to indicate readiness for "
3439 "re-invocation");
3440 }
3441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003443 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3444 ssid = wpa_config_get_network(
3445 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003446 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 wpas_p2p_remove_pending_group_interface(wpa_s);
3449 return;
3450 }
3451
3452 ssid = wpa_config_get_network(wpa_s->conf,
3453 wpa_s->pending_invite_ssid_id);
3454 if (ssid == NULL) {
3455 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3456 "data matching with invitation");
3457 return;
3458 }
3459
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003460 /*
3461 * The peer could have missed our ctrl::ack frame for Invitation
3462 * Response and continue retransmitting the frame. To reduce the
3463 * likelihood of the peer not getting successful TX status for the
3464 * Invitation Response frame, wait a short time here before starting
3465 * the persistent group so that we will remain on the current channel to
3466 * acknowledge any possible retransmission from the peer.
3467 */
3468 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3469 "starting persistent group");
3470 os_sleep(0, 50000);
3471
Dmitry Shmidt15907092014-03-25 10:42:57 -07003472 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003473 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003474 freq = neg_freq;
3475 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003476 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003477 freq = peer_oper_freq;
3478 else
3479 freq = 0;
3480
3481 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3482 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003484 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003485 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003486 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003487 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003488 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003489 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003490 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003491 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003492 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003493 ssid->mode == WPAS_MODE_P2P_GO ?
3494 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003495 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496}
3497
3498
Dmitry Shmidt04949592012-07-19 12:16:46 -07003499static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3500 unsigned int freq)
3501{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003502 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3503 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003504 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003505}
3506
3507
3508static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3509{
3510 reg->channel[reg->channels] = chan;
3511 reg->channels++;
3512}
3513
3514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003516 struct p2p_channels *chan,
3517 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003518{
3519 int i, cla = 0;
3520
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003521 wpa_s->global->p2p_24ghz_social_channels = 1;
3522
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003523 os_memset(cli_chan, 0, sizeof(*cli_chan));
3524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3526 "band");
3527
3528 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3529 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003530 chan->reg_class[cla].channels = 0;
3531 for (i = 0; i < 11; i++) {
3532 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3533 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3534 }
3535 if (chan->reg_class[cla].channels)
3536 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537
3538 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3539 "band");
3540
3541 /* Operating class 115 - 5 GHz, channels 36-48 */
3542 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003543 chan->reg_class[cla].channels = 0;
3544 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3545 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3546 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3547 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3548 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3549 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3550 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3551 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3552 if (chan->reg_class[cla].channels)
3553 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554
3555 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3556 "band");
3557
3558 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3559 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003560 chan->reg_class[cla].channels = 0;
3561 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3562 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3563 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3564 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3565 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3566 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3567 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3568 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3569 if (chan->reg_class[cla].channels)
3570 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571
3572 chan->reg_classes = cla;
3573 return 0;
3574}
3575
3576
Dmitry Shmidt04949592012-07-19 12:16:46 -07003577static int has_channel(struct wpa_global *global,
3578 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579{
3580 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003581 unsigned int freq;
3582
3583 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3584 chan * 5;
3585 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003586 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587
3588 for (i = 0; i < mode->num_channels; i++) {
3589 if (mode->channels[i].chan == chan) {
3590 if (flags)
3591 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003592 if (mode->channels[i].flag &
3593 (HOSTAPD_CHAN_DISABLED |
3594 HOSTAPD_CHAN_RADAR))
3595 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003596 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3597 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003598 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 }
3600 }
3601
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003602 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603}
3604
3605
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003606static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3607 struct hostapd_hw_modes *mode,
3608 u8 channel)
3609{
Hai Shalom60840252021-02-19 19:02:11 -08003610 u8 center_channels[] = { 42, 58, 106, 122, 138, 155, 171 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003611 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003612
3613 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3614 return 0;
3615
3616 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3617 /*
3618 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3619 * so the center channel is 6 channels away from the start/end.
3620 */
3621 if (channel >= center_channels[i] - 6 &&
3622 channel <= center_channels[i] + 6)
3623 return center_channels[i];
3624
3625 return 0;
3626}
3627
3628
3629static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3630 struct hostapd_hw_modes *mode,
3631 u8 channel, u8 bw)
3632{
3633 u8 center_chan;
3634 int i, flags;
3635 enum chan_allowed res, ret = ALLOWED;
3636
3637 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3638 if (!center_chan)
3639 return NOT_ALLOWED;
3640 if (center_chan >= 58 && center_chan <= 138)
3641 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3642
3643 /* check all the channels are available */
3644 for (i = 0; i < 4; i++) {
3645 int adj_chan = center_chan - 6 + i * 4;
3646
3647 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3648 if (res == NOT_ALLOWED)
3649 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003650 if (res == NO_IR)
3651 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003652
3653 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3654 return NOT_ALLOWED;
3655 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3656 return NOT_ALLOWED;
3657 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3658 return NOT_ALLOWED;
3659 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3660 return NOT_ALLOWED;
3661 }
3662
3663 return ret;
3664}
3665
3666
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003667static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3668 struct hostapd_hw_modes *mode,
3669 u8 channel)
3670{
Hai Shalom60840252021-02-19 19:02:11 -08003671 u8 center_channels[] = { 50, 114, 163 };
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003672 unsigned int i;
3673
3674 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3675 return 0;
3676
3677 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3678 /*
3679 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3680 * so the center channel is 14 channels away from the start/end.
3681 */
3682 if (channel >= center_channels[i] - 14 &&
3683 channel <= center_channels[i] + 14)
3684 return center_channels[i];
3685
3686 return 0;
3687}
3688
3689
3690static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3691 struct hostapd_hw_modes *mode,
3692 u8 channel, u8 bw)
3693{
3694 u8 center_chan;
3695 int i, flags;
3696 enum chan_allowed res, ret = ALLOWED;
3697
3698 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3699 if (!center_chan)
3700 return NOT_ALLOWED;
3701 /* VHT 160 MHz uses DFS channels in most countries. */
3702
3703 /* Check all the channels are available */
3704 for (i = 0; i < 8; i++) {
3705 int adj_chan = center_chan - 14 + i * 4;
3706
3707 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3708 if (res == NOT_ALLOWED)
3709 return NOT_ALLOWED;
3710
3711 if (res == NO_IR)
3712 ret = NO_IR;
3713
3714 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3715 return NOT_ALLOWED;
3716 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3717 return NOT_ALLOWED;
3718 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3719 return NOT_ALLOWED;
3720 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3721 return NOT_ALLOWED;
3722 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3723 return NOT_ALLOWED;
3724 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3725 return NOT_ALLOWED;
3726 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3727 return NOT_ALLOWED;
3728 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3729 return NOT_ALLOWED;
3730 }
3731
3732 return ret;
3733}
3734
3735
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003736static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3737 struct hostapd_hw_modes *mode,
3738 u8 channel)
3739{
3740 struct ieee80211_edmg_config edmg;
3741
3742 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3743 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3744 return ALLOWED;
3745
3746 return NOT_ALLOWED;
3747}
3748
3749
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003750static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3751 struct hostapd_hw_modes *mode,
3752 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003753{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003754 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003755 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003756
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003757 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3758 if (bw == BW40MINUS) {
3759 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3760 return NOT_ALLOWED;
3761 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3762 } else if (bw == BW40PLUS) {
3763 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3764 return NOT_ALLOWED;
3765 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3766 } else if (bw == BW80) {
3767 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003768 } else if (bw == BW160) {
3769 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003770 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3771 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003772 }
3773
3774 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3775 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003776 if (res == NO_IR || res2 == NO_IR)
3777 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003778 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003779}
3780
3781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003783 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003784 struct p2p_channels *cli_chan,
3785 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003787 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003788 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003790 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3792 "of all supported channels; assume dualband "
3793 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003794 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 }
3796
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003797 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003799 for (op = 0; global_op_class[op].op_class; op++) {
3800 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003802 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803
Hai Shalom899fcc72020-10-19 14:38:18 -07003804 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003805 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003806 continue;
3807
Hai Shalomfdcde762020-04-02 11:19:20 -07003808 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3809 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 if (mode == NULL)
3811 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003812 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3813 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003815 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003816
3817 /* Check for non-continuous jump in channel index
3818 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003819 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003820 ch < 149 && ch + o->inc > 149)
3821 ch = 149;
3822
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003823 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3824 if (res == ALLOWED) {
3825 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003826 if (cla == P2P_MAX_REG_CLASSES)
3827 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003828 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3829 o->op_class);
3830 reg = &chan->reg_class[cla];
3831 cla++;
3832 reg->reg_class = o->op_class;
3833 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003834 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3835 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003836 reg->channel[reg->channels] = ch;
3837 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003838 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003839 wpa_s->conf->p2p_add_cli_chan) {
3840 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003841 if (cli_cla == P2P_MAX_REG_CLASSES)
3842 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003843 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3844 o->op_class);
3845 cli_reg = &cli_chan->reg_class[cli_cla];
3846 cli_cla++;
3847 cli_reg->reg_class = o->op_class;
3848 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003849 if (cli_reg->channels ==
3850 P2P_MAX_REG_CLASS_CHANNELS)
3851 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003852 cli_reg->channel[cli_reg->channels] = ch;
3853 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 }
3856 if (reg) {
3857 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3858 reg->channel, reg->channels);
3859 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003860 if (cli_reg) {
3861 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3862 cli_reg->channel, cli_reg->channels);
3863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864 }
3865
3866 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003867 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003869 return 0;
3870}
3871
3872
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003873int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3874 struct hostapd_hw_modes *mode, u8 channel)
3875{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003876 int op;
3877 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003878
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003879 for (op = 0; global_op_class[op].op_class; op++) {
3880 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003881 u8 ch;
3882
Hai Shalom899fcc72020-10-19 14:38:18 -07003883 if (o->p2p == NO_P2P_SUPP ||
3884 (is_6ghz_op_class(o->op_class) &&
3885 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003886 continue;
3887
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003888 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3889 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003890 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3891 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003892 continue;
3893 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003894 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003895 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003896 }
3897 }
3898 return 0;
3899}
3900
3901
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003902int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3903 struct hostapd_hw_modes *mode, u8 channel)
3904{
3905 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3906 return 0;
3907
3908 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3909}
3910
3911
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003912int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3913 struct hostapd_hw_modes *mode, u8 channel)
3914{
3915 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3916 return 0;
3917 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3918}
3919
3920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3922 size_t buf_len)
3923{
3924 struct wpa_supplicant *wpa_s = ctx;
3925
3926 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3927 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3928 break;
3929 }
3930 if (wpa_s == NULL)
3931 return -1;
3932
3933 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3934}
3935
3936
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003937struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3938 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003939{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003940 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3941 struct wpa_ssid *s = wpa_s->current_ssid;
3942 if (s == NULL)
3943 continue;
3944 if (s->mode != WPAS_MODE_P2P_GO &&
3945 s->mode != WPAS_MODE_AP &&
3946 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3947 continue;
3948 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003949 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003950 continue;
3951 return wpa_s;
3952 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003953
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003954 return NULL;
3955
3956}
3957
3958
3959struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3960 const u8 *peer_dev_addr)
3961{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003962 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3963 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003964 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003965 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003966 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3967 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003968 }
3969
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003970 return NULL;
3971}
3972
3973
3974static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3975{
3976 struct wpa_supplicant *wpa_s = ctx;
3977
3978 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003979}
3980
3981
Dmitry Shmidt18463232014-01-24 12:29:41 -08003982static int wpas_is_concurrent_session_active(void *ctx)
3983{
3984 struct wpa_supplicant *wpa_s = ctx;
3985 struct wpa_supplicant *ifs;
3986
3987 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3988 if (ifs == wpa_s)
3989 continue;
3990 if (ifs->wpa_state > WPA_ASSOCIATED)
3991 return 1;
3992 }
3993 return 0;
3994}
3995
3996
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003997static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3998{
3999 struct wpa_supplicant *wpa_s = ctx;
4000 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4001}
4002
4003
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004004int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4005 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004006{
4007 struct wpa_interface iface;
4008 struct wpa_supplicant *p2pdev_wpa_s;
4009 char ifname[100];
4010 char force_name[100];
4011 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004012 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004013
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004014 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4015 wpa_s->ifname);
4016 if (os_snprintf_error(sizeof(ifname), ret))
4017 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004018 /* Cut length at the maximum size. Note that we don't need to ensure
4019 * collision free names here as the created interface is not a netdev.
4020 */
4021 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004022 force_name[0] = '\0';
4023 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004024
4025 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4026 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4027 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4028
4029 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004030 force_name, wpa_s->pending_interface_addr, NULL);
4031 if (ret < 0) {
4032 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4033 return ret;
4034 }
4035 os_strlcpy(wpa_s->pending_interface_name, ifname,
4036 sizeof(wpa_s->pending_interface_name));
4037
4038 os_memset(&iface, 0, sizeof(iface));
4039 iface.p2p_mgmt = 1;
4040 iface.ifname = wpa_s->pending_interface_name;
4041 iface.driver = wpa_s->driver->name;
4042 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004043
4044 /*
4045 * If a P2P Device configuration file was given, use it as the interface
4046 * configuration file (instead of using parent's configuration file.
4047 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004048 if (conf_p2p_dev) {
4049 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004050 iface.ctrl_interface = NULL;
4051 } else {
4052 iface.confname = wpa_s->confname;
4053 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4054 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004055
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004056 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004057 if (!p2pdev_wpa_s) {
4058 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4059 return -1;
4060 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004061
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004062 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004063 wpa_s->pending_interface_name[0] = '\0';
4064 return 0;
4065}
4066
4067
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004068static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4069 const u8 *noa, size_t noa_len)
4070{
4071 struct wpa_supplicant *wpa_s, *intf = ctx;
4072 char hex[100];
4073
4074 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4075 if (wpa_s->waiting_presence_resp)
4076 break;
4077 }
4078 if (!wpa_s) {
4079 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4080 return;
4081 }
4082 wpa_s->waiting_presence_resp = 0;
4083
4084 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4085 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4086 " status=%u noa=%s", MAC2STR(src), status, hex);
4087}
4088
4089
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004090static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4091 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004092 u8 *ret_ssid, size_t *ret_ssid_len,
4093 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004094{
4095 struct wpa_supplicant *wpa_s = ctx;
4096 struct wpa_ssid *s;
4097
4098 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4099 if (s) {
4100 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4101 *ret_ssid_len = s->ssid_len;
4102 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004103
4104 if (s->mode != WPAS_MODE_P2P_GO) {
4105 os_memset(intended_iface_addr, 0, ETH_ALEN);
4106 } else if (wpas_p2p_create_iface(wpa_s)) {
4107 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4108 return 0;
4109
4110 os_memcpy(intended_iface_addr,
4111 wpa_s->pending_interface_addr, ETH_ALEN);
4112 } else {
4113 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4114 ETH_ALEN);
4115 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004116 return 1;
4117 }
4118
4119 return 0;
4120}
4121
4122
4123static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004124 u8 *ssid, size_t *ssid_len, int *group_iface,
4125 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004126{
4127 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004128 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004129 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004130
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004131 /*
4132 * group_iface will be set to 1 only if a dedicated interface for P2P
4133 * role is required. First, we try to reuse an active GO. However,
4134 * if it is not present, we will try to reactivate an existing
4135 * persistent group and set group_iface to 1, so the caller will know
4136 * that the pending interface should be used.
4137 */
4138 *group_iface = 0;
4139
4140 if (freq)
4141 *freq = 0;
4142
4143 go = wpas_p2p_get_go_group(wpa_s);
4144 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004145 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004146 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004147 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004148 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4149 else
4150 return 0;
4151 } else {
4152 s = go->current_ssid;
4153 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4154 if (freq)
4155 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004156 }
4157
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004158 os_memcpy(ssid, s->ssid, s->ssid_len);
4159 *ssid_len = s->ssid_len;
4160
4161 return 1;
4162}
4163
4164
4165static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4166 const u8 *ssid, size_t ssid_len)
4167{
4168 struct wpa_supplicant *wpa_s = ctx;
4169 struct wpa_ssid *s;
4170 int save_config = 0;
4171 size_t i;
4172
4173 /* Start with our first choice of Persistent Groups */
4174 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4175 if (go && ssid && ssid_len &&
4176 s->ssid_len == ssid_len &&
4177 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4178 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4179 break;
4180
4181 /* Remove stale persistent group */
4182 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004183 wpa_dbg(wpa_s, MSG_DEBUG,
4184 "P2P: Remove stale persistent group id=%d",
4185 s->id);
4186 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004187 wpa_config_remove_network(wpa_s->conf, s->id);
4188 save_config = 1;
4189 continue;
4190 }
4191
4192 for (i = 0; i < s->num_p2p_clients; i++) {
4193 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4194 peer, ETH_ALEN) != 0)
4195 continue;
4196
4197 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4198 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4199 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4200 break;
4201 }
4202 s->num_p2p_clients--;
4203 save_config = 1;
4204 }
4205
4206 if (save_config)
4207 p2p_config_write(wpa_s);
4208
4209 /* Return TRUE if valid SSID remains */
4210 return s != NULL;
4211}
4212
4213
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004214static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4215 const u8 *feat_cap, size_t feat_cap_len)
4216{
4217 static const char pref[] = " feature_cap=";
4218 int ret;
4219
4220 buf[0] = '\0';
4221
4222 /*
4223 * We expect a feature capability to contain at least one byte to be
4224 * reported. The string buffer provided by the caller function is
4225 * expected to be big enough to contain all bytes of the attribute for
4226 * known specifications. This function truncates the reported bytes if
4227 * the feature capability data exceeds the string buffer size.
4228 */
4229 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4230 return;
4231
4232 os_memcpy(buf, pref, sizeof(pref));
4233 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4234 buf_len - sizeof(pref) + 1,
4235 feat_cap, feat_cap_len);
4236
4237 if (ret != (2 * (int) feat_cap_len))
4238 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4239}
4240
4241
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004242static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4243 const u8 *adv_mac, const u8 *ses_mac,
4244 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4245 u8 conncap, int passwd_id,
4246 const u8 *persist_ssid,
4247 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004248 int prov_start, const char *session_info,
4249 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004250 unsigned int freq,
4251 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004252{
4253 struct wpa_supplicant *wpa_s = ctx;
4254 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004255 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004256 int save_config = 0;
4257 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004258 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004259
4260 if (!dev)
4261 return;
4262
4263 os_memset(mac, 0, ETH_ALEN);
4264 if (!adv_mac)
4265 adv_mac = mac;
4266 if (!ses_mac)
4267 ses_mac = mac;
4268 if (!grp_mac)
4269 grp_mac = mac;
4270
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004271 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4272 feat_cap, feat_cap_len);
4273
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274 if (prov_start) {
4275 if (session_info == NULL) {
4276 wpa_msg_global(wpa_s, MSG_INFO,
4277 P2P_EVENT_P2PS_PROVISION_START MACSTR
4278 " adv_id=%x conncap=%x"
4279 " adv_mac=" MACSTR
4280 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004281 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004282 MAC2STR(dev), adv_id, conncap,
4283 MAC2STR(adv_mac),
4284 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004285 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004286 } else {
4287 wpa_msg_global(wpa_s, MSG_INFO,
4288 P2P_EVENT_P2PS_PROVISION_START MACSTR
4289 " adv_id=%x conncap=%x"
4290 " adv_mac=" MACSTR
4291 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004292 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004293 MAC2STR(dev), adv_id, conncap,
4294 MAC2STR(adv_mac),
4295 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004296 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004297 }
4298 return;
4299 }
4300
4301 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4302 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4303
4304 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4305 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4306 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4307
4308 if (persistent_go && !persistent_go->num_p2p_clients) {
4309 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004310 wpa_dbg(wpa_s, MSG_DEBUG,
4311 "P2P: Remove empty persistent group id=%d",
4312 persistent_go->id);
4313 wpas_notify_persistent_group_removed(wpa_s,
4314 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004315 wpa_config_remove_network(wpa_s->conf,
4316 persistent_go->id);
4317 }
4318
4319 wpa_msg_global(wpa_s, MSG_INFO,
4320 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4321 " status=%d"
4322 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004323 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004324 MAC2STR(dev), status,
4325 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004326 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004327 return;
4328 }
4329
4330 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004331 if (persist_ssid && persist_ssid_size)
4332 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4333 persist_ssid_size);
4334
4335 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4336 is_zero_ether_addr(grp_mac)) {
4337 wpa_dbg(wpa_s, MSG_ERROR,
4338 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4339 return;
4340 }
4341
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004342 for (;;) {
4343 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4344 if (!stale)
4345 break;
4346
4347 if (s && s->ssid_len == stale->ssid_len &&
4348 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4349 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4350 break;
4351
4352 /* Remove stale persistent group */
4353 if (stale->mode != WPAS_MODE_P2P_GO ||
4354 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004355 wpa_dbg(wpa_s, MSG_DEBUG,
4356 "P2P: Remove stale persistent group id=%d",
4357 stale->id);
4358 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004359 wpa_config_remove_network(wpa_s->conf, stale->id);
4360 } else {
4361 size_t i;
4362
4363 for (i = 0; i < stale->num_p2p_clients; i++) {
4364 if (os_memcmp(stale->p2p_client_list +
4365 i * ETH_ALEN,
4366 dev, ETH_ALEN) == 0) {
4367 os_memmove(stale->p2p_client_list +
4368 i * ETH_ALEN,
4369 stale->p2p_client_list +
4370 (i + 1) * ETH_ALEN,
4371 (stale->num_p2p_clients -
4372 i - 1) * ETH_ALEN);
4373 break;
4374 }
4375 }
4376 stale->num_p2p_clients--;
4377 }
4378 save_config = 1;
4379 }
4380
4381 if (save_config)
4382 p2p_config_write(wpa_s);
4383
4384 if (s) {
4385 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4386 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4387
4388 if (persistent_go && s != persistent_go &&
4389 !persistent_go->num_p2p_clients) {
4390 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004391 wpa_dbg(wpa_s, MSG_DEBUG,
4392 "P2P: Remove empty persistent group id=%d",
4393 persistent_go->id);
4394 wpas_notify_persistent_group_removed(wpa_s,
4395 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004396 wpa_config_remove_network(wpa_s->conf,
4397 persistent_go->id);
4398 /* Save config */
4399 }
4400
4401 wpa_msg_global(wpa_s, MSG_INFO,
4402 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4403 " status=%d"
4404 " adv_id=%x adv_mac=" MACSTR
4405 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004406 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004407 MAC2STR(dev), status,
4408 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004409 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004410 return;
4411 }
4412
Hai Shalom5f92bc92019-04-18 11:54:11 -07004413 wpa_s->global->pending_p2ps_group = 0;
4414 wpa_s->global->pending_p2ps_group_freq = 0;
4415
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004416 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004417 /*
4418 * We need to copy the interface name. Simply saving a
4419 * pointer isn't enough, since if we use pending_interface_name
4420 * it will be overwritten when the group is added.
4421 */
4422 char go_ifname[100];
4423
4424 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004425 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004426 if (!response_done) {
4427 wpa_s->global->pending_p2ps_group = 1;
4428 wpa_s->global->pending_p2ps_group_freq = freq;
4429 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004430
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004431 if (!wpas_p2p_create_iface(wpa_s))
4432 os_memcpy(go_ifname, wpa_s->ifname,
4433 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004434 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004435 os_memcpy(go_ifname,
4436 wpa_s->pending_interface_name,
4437 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004438
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004439 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004440 wpas_p2ps_prov_complete(
4441 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4442 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004443 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004444 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4445 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004446 return;
4447 }
4448
4449 /* If PD Resp complete, start up the GO */
4450 if (response_done && persistent_go) {
4451 wpas_p2p_group_add_persistent(
4452 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004453 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004454 persistent_go->mode ==
4455 WPAS_MODE_P2P_GO ?
4456 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004457 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004458 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004459 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004460 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004461 }
4462
4463 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004464 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4465 ETH_ALEN);
4466 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004467 }
4468 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004469 os_memcpy(go_ifname, go_wpa_s->ifname,
4470 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004471
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004472 if (is_zero_ether_addr(grp_mac)) {
4473 wpa_dbg(go_wpa_s, MSG_DEBUG,
4474 "P2P: Setting PIN-1 for ANY");
4475 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4476 "12345670", NULL, 0,
4477 0);
4478 } else {
4479 wpa_dbg(go_wpa_s, MSG_DEBUG,
4480 "P2P: Setting PIN-1 for " MACSTR,
4481 MAC2STR(grp_mac));
4482 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4483 "12345670", NULL, 0,
4484 0);
4485 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004486
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004487 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4488 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004489 }
4490
4491 wpa_msg_global(wpa_s, MSG_INFO,
4492 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4493 " status=%d conncap=%x"
4494 " adv_id=%x adv_mac=" MACSTR
4495 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004496 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004497 MAC2STR(dev), status, conncap,
4498 adv_id, MAC2STR(adv_mac),
4499 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004500 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004501 return;
4502 }
4503
4504 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4505 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4506
4507 if (persistent_go && !persistent_go->num_p2p_clients) {
4508 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004509 wpa_dbg(wpa_s, MSG_DEBUG,
4510 "P2P: Remove empty persistent group id=%d",
4511 persistent_go->id);
4512 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004513 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4514 }
4515
4516 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004517 char ssid_hex[32 * 2 + 1];
4518
4519 if (group_ssid)
4520 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4521 group_ssid, group_ssid_len);
4522 else
4523 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004524 wpa_msg_global(wpa_s, MSG_INFO,
4525 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4526 " status=%d conncap=%x"
4527 " adv_id=%x adv_mac=" MACSTR
4528 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004529 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004530 MAC2STR(dev), status, conncap,
4531 adv_id, MAC2STR(adv_mac),
4532 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004533 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4534 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004535 } else {
4536 wpa_msg_global(wpa_s, MSG_INFO,
4537 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4538 " status=%d conncap=%x"
4539 " adv_id=%x adv_mac=" MACSTR
4540 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004541 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004542 MAC2STR(dev), status, conncap,
4543 adv_id, MAC2STR(adv_mac),
4544 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004545 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004546 }
4547}
4548
4549
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004550static int _wpas_p2p_in_progress(void *ctx)
4551{
4552 struct wpa_supplicant *wpa_s = ctx;
4553 return wpas_p2p_in_progress(wpa_s);
4554}
4555
4556
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004557static int wpas_prov_disc_resp_cb(void *ctx)
4558{
4559 struct wpa_supplicant *wpa_s = ctx;
4560 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004561 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004562
4563 if (!wpa_s->global->pending_p2ps_group)
4564 return 0;
4565
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004566 freq = wpa_s->global->pending_p2ps_group_freq;
4567 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004568 wpa_s->global->pending_p2ps_group = 0;
4569
4570 if (wpas_p2p_get_go_group(wpa_s))
4571 return 0;
4572 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4573
4574 if (persistent_go) {
4575 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004576 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4577 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004578 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004579 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004580 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004581 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004582 }
4583
4584 return 1;
4585}
4586
4587
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004588static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4589 unsigned int *len,
4590 unsigned int *freq_list)
4591{
4592 struct wpa_supplicant *wpa_s = ctx;
4593
4594 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4595 WPA_IF_P2P_CLIENT, len, freq_list);
4596}
4597
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004598int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4599{
4600 u8 addr[ETH_ALEN] = {0};
4601
4602 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4603 return 0;
4604
Hai Shalom60840252021-02-19 19:02:11 -08004605 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4606 if (is_zero_ether_addr(
4607 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4608 !is_zero_ether_addr(wpa_s->own_addr)) {
4609 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4610 wpa_s->own_addr, ETH_ALEN);
4611 }
4612 return 0;
4613 }
4614
4615 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004616 if (random_mac_addr(addr) < 0) {
4617 wpa_msg(wpa_s, MSG_INFO,
4618 "Failed to generate random MAC address");
4619 return -EINVAL;
4620 }
4621
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004622 /* Store generated MAC address. */
4623 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4624 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004625 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004626 /* If there are existing saved groups, restore last MAC address.
4627 * if there is no last used MAC address, the last one is
4628 * factory MAC. */
4629 if (is_zero_ether_addr(
4630 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004631 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004632 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4633 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004634 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4635 }
4636
4637 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4638 wpa_msg(wpa_s, MSG_INFO,
4639 "Failed to set random MAC address");
4640 return -EINVAL;
4641 }
4642
4643 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4644 wpa_msg(wpa_s, MSG_INFO,
4645 "Could not update MAC address information");
4646 return -EINVAL;
4647 }
4648
4649 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4650 MAC2STR(addr));
4651
4652 return 0;
4653}
4654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655/**
4656 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4657 * @global: Pointer to global data from wpa_supplicant_init()
4658 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4659 * Returns: 0 on success, -1 on failure
4660 */
4661int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4662{
4663 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004664 int i;
4665
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004666 if (wpa_s->conf->p2p_disabled)
4667 return 0;
4668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4670 return 0;
4671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004672 if (global->p2p)
4673 return 0;
4674
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004675 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4676 wpa_msg(wpa_s, MSG_ERROR,
4677 "Failed to initialize P2P random MAC address.");
4678 return -1;
4679 }
4680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004683 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684 p2p.p2p_scan = wpas_p2p_scan;
4685 p2p.send_action = wpas_send_action;
4686 p2p.send_action_done = wpas_send_action_done;
4687 p2p.go_neg_completed = wpas_go_neg_completed;
4688 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4689 p2p.dev_found = wpas_dev_found;
4690 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004691 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692 p2p.start_listen = wpas_start_listen;
4693 p2p.stop_listen = wpas_stop_listen;
4694 p2p.send_probe_resp = wpas_send_probe_resp;
4695 p2p.sd_request = wpas_sd_request;
4696 p2p.sd_response = wpas_sd_response;
4697 p2p.prov_disc_req = wpas_prov_disc_req;
4698 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004699 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700 p2p.invitation_process = wpas_invitation_process;
4701 p2p.invitation_received = wpas_invitation_received;
4702 p2p.invitation_result = wpas_invitation_result;
4703 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004704 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004705 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004706 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004707 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004708 p2p.get_persistent_group = wpas_get_persistent_group;
4709 p2p.get_go_info = wpas_get_go_info;
4710 p2p.remove_stale_groups = wpas_remove_stale_groups;
4711 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4712 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4713 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004714 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004715 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716
4717 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004718 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 p2p.dev_name = wpa_s->conf->device_name;
4720 p2p.manufacturer = wpa_s->conf->manufacturer;
4721 p2p.model_name = wpa_s->conf->model_name;
4722 p2p.model_number = wpa_s->conf->model_number;
4723 p2p.serial_number = wpa_s->conf->serial_number;
4724 if (wpa_s->wps) {
4725 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4726 p2p.config_methods = wpa_s->wps->config_methods;
4727 }
4728
Hai Shalom60840252021-02-19 19:02:11 -08004729 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4730 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004731 wpa_printf(MSG_ERROR,
4732 "P2P: Failed to configure supported channel list");
4733 return -1;
4734 }
4735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736 if (wpa_s->conf->p2p_listen_reg_class &&
4737 wpa_s->conf->p2p_listen_channel) {
4738 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4739 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004740 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004742 /*
4743 * Pick one of the social channels randomly as the listen
4744 * channel.
4745 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004746 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004747 &p2p.channel,
4748 &global->p2p_go_avoid_freq,
4749 &global->p2p_disallow_freq) !=
4750 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004751 wpa_printf(MSG_INFO,
4752 "P2P: No social channels supported by the driver - do not enable P2P");
4753 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004754 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004755 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004757 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4758 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759
4760 if (wpa_s->conf->p2p_oper_reg_class &&
4761 wpa_s->conf->p2p_oper_channel) {
4762 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4763 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4764 p2p.cfg_op_channel = 1;
4765 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4766 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4767
4768 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004770 * Use random operation channel from 2.4 GHz band social
4771 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4772 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004774 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004775 &p2p.op_channel, NULL,
4776 NULL) != 0) {
4777 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004778 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004779 p2p.op_reg_class = 0;
4780 p2p.op_channel = 0;
4781 /* This will be overridden during group setup in
4782 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004783 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004784 p2p.cfg_op_channel = 0;
4785 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4786 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4787 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004788
4789 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4790 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4791 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4792 }
4793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004794 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4795 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4796 p2p.country[2] = 0x04;
4797 } else
4798 os_memcpy(p2p.country, "XX\x04", 3);
4799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4801 WPS_DEV_TYPE_LEN);
4802
4803 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4804 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4805 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4806
4807 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4808 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4809
4810 p2p.max_peers = 100;
4811
4812 if (wpa_s->conf->p2p_ssid_postfix) {
4813 p2p.ssid_postfix_len =
4814 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4815 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4816 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4817 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4818 p2p.ssid_postfix_len);
4819 }
4820
4821 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4822
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004823 p2p.max_listen = wpa_s->max_remain_on_chan;
4824
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004825 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4826 wpa_s->conf->p2p_passphrase_len <= 63)
4827 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4828 else
4829 p2p.passphrase_len = 8;
4830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004831 global->p2p = p2p_init(&p2p);
4832 if (global->p2p == NULL)
4833 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004834 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835
4836 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4837 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4838 continue;
4839 p2p_add_wps_vendor_extension(
4840 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4841 }
4842
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004843 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 return 0;
4846}
4847
4848
4849/**
4850 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4851 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4852 *
4853 * This function deinitialize per-interface P2P data.
4854 */
4855void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4856{
4857 if (wpa_s->driver && wpa_s->drv_priv)
4858 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004859
4860 if (wpa_s->go_params) {
4861 /* Clear any stored provisioning info */
4862 p2p_clear_provisioning_info(
4863 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004864 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004865 }
4866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 os_free(wpa_s->go_params);
4868 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004869 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4871 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07004872 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4874 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4875 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004876 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07004877 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004878 wpas_p2p_listen_work_done(wpa_s);
4879 if (wpa_s->p2p_send_action_work) {
4880 os_free(wpa_s->p2p_send_action_work->ctx);
4881 radio_work_done(wpa_s->p2p_send_action_work);
4882 wpa_s->p2p_send_action_work = NULL;
4883 }
4884 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004886 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4887 wpa_s->p2p_oob_dev_pw = NULL;
4888
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004889 os_free(wpa_s->p2p_group_common_freqs);
4890 wpa_s->p2p_group_common_freqs = NULL;
4891 wpa_s->p2p_group_common_freqs_num = 0;
4892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893 /* TODO: remove group interface from the driver if this wpa_s instance
4894 * is on top of a P2P group interface */
4895}
4896
4897
4898/**
4899 * wpas_p2p_deinit_global - Deinitialize global P2P module
4900 * @global: Pointer to global data from wpa_supplicant_init()
4901 *
4902 * This function deinitializes the global (per device) P2P module.
4903 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004904static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905{
4906 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004908 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004909
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004910 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911
4912 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4914 wpa_s = wpa_s->next;
4915 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 tmp = global->ifaces;
4917 while (tmp &&
4918 (tmp == wpa_s ||
4919 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4920 tmp = tmp->next;
4921 }
4922 if (tmp == NULL)
4923 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004924 /* Disconnect from the P2P group and deinit the interface */
4925 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 }
4927
4928 /*
4929 * Deinit GO data on any possibly remaining interface (if main
4930 * interface is used as GO).
4931 */
4932 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4933 if (wpa_s->ap_iface)
4934 wpas_p2p_group_deinit(wpa_s);
4935 }
4936
4937 p2p_deinit(global->p2p);
4938 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004939 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940}
4941
4942
4943static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4944{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004945 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004946 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 if (wpa_s->drv_flags &
4948 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4949 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4950 return 1; /* P2P group requires a new interface in every case
4951 */
4952 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4953 return 0; /* driver does not support concurrent operations */
4954 if (wpa_s->global->ifaces->next)
4955 return 1; /* more that one interface already in use */
4956 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4957 return 1; /* this interface is already in use */
4958 return 0;
4959}
4960
4961
4962static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4963 const u8 *peer_addr,
4964 enum p2p_wps_method wps_method,
4965 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004966 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004967 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004969 if (persistent_group && wpa_s->conf->persistent_reconnect)
4970 persistent_group = 2;
4971
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004972 /*
4973 * Increase GO config timeout if HT40 is used since it takes some time
4974 * to scan channels for coex purposes before the BSS can be started.
4975 */
4976 p2p_set_config_timeout(wpa_s->global->p2p,
4977 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4980 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004981 persistent_group, ssid ? ssid->ssid : NULL,
4982 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004983 wpa_s->p2p_pd_before_go_neg, pref_freq,
4984 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4985 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986}
4987
4988
4989static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4990 const u8 *peer_addr,
4991 enum p2p_wps_method wps_method,
4992 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004993 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004994 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004996 if (persistent_group && wpa_s->conf->persistent_reconnect)
4997 persistent_group = 2;
4998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5000 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005001 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005002 ssid ? ssid->ssid_len : 0, pref_freq,
5003 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5004 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005}
5006
5007
5008static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5009{
5010 wpa_s->p2p_join_scan_count++;
5011 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5012 wpa_s->p2p_join_scan_count);
5013 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5014 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5015 " for join operationg - stop join attempt",
5016 MAC2STR(wpa_s->pending_join_iface_addr));
5017 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005018 if (wpa_s->p2p_auto_pd) {
5019 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005020 wpa_msg_global(wpa_s, MSG_INFO,
5021 P2P_EVENT_PROV_DISC_FAILURE
5022 " p2p_dev_addr=" MACSTR " status=N/A",
5023 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005024 return;
5025 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005026 if (wpa_s->p2p_fallback_to_go_neg) {
5027 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5028 "failed - fall back to GO Negotiation");
5029 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5030 P2P_EVENT_FALLBACK_TO_GO_NEG
5031 "reason=join-failed");
5032 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5033 return;
5034 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005035 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005036 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005037 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 }
5039}
5040
5041
Dmitry Shmidt04949592012-07-19 12:16:46 -07005042static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5043{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005044 int res;
5045 unsigned int num, i;
5046 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005047
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005048 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5049 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005050 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005051 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005052
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005053 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5054 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005055 if (!freqs)
5056 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005057
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005058 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5059 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005060
5061 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005062 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005063 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5064 freq);
5065 res = 0;
5066 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005067 }
5068 }
5069
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005070 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005071 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005072
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005073exit_free:
5074 os_free(freqs);
5075 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005076}
5077
5078
5079static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5080 const u8 *peer_dev_addr)
5081{
5082 struct wpa_bss *bss;
5083 int updated;
5084
5085 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5086 if (bss == NULL)
5087 return -1;
5088 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5089 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5090 "last scan");
5091 return 0;
5092 }
5093
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005094 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5095 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005096 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5097 "%ld.%06ld (%supdated in last scan)",
5098 bss->last_update.sec, bss->last_update.usec,
5099 updated ? "": "not ");
5100
5101 return updated;
5102}
5103
5104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5106 struct wpa_scan_results *scan_res)
5107{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005108 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109 int freq;
5110 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5113
5114 if (wpa_s->global->p2p_disabled)
5115 return;
5116
Dmitry Shmidt04949592012-07-19 12:16:46 -07005117 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5118 scan_res ? (int) scan_res->num : -1,
5119 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120
5121 if (scan_res)
5122 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5123
Dmitry Shmidt04949592012-07-19 12:16:46 -07005124 if (wpa_s->p2p_auto_pd) {
5125 int join = wpas_p2p_peer_go(wpa_s,
5126 wpa_s->pending_join_dev_addr);
5127 if (join == 0 &&
5128 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5129 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005130 bss = wpa_bss_get_bssid_latest(
5131 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 if (bss) {
5133 freq = bss->freq;
5134 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5135 "the peer " MACSTR " at %d MHz",
5136 wpa_s->auto_pd_scan_retry,
5137 MAC2STR(wpa_s->
5138 pending_join_dev_addr),
5139 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005140 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005141 return;
5142 }
5143 }
5144
5145 if (join < 0)
5146 join = 0;
5147
5148 wpa_s->p2p_auto_pd = 0;
5149 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5150 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5151 MAC2STR(wpa_s->pending_join_dev_addr), join);
5152 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005153 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005154 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005155 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005156 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005157 wpa_msg_global(wpa_s, MSG_INFO,
5158 P2P_EVENT_PROV_DISC_FAILURE
5159 " p2p_dev_addr=" MACSTR " status=N/A",
5160 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005161 }
5162 return;
5163 }
5164
5165 if (wpa_s->p2p_auto_join) {
5166 int join = wpas_p2p_peer_go(wpa_s,
5167 wpa_s->pending_join_dev_addr);
5168 if (join < 0) {
5169 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5170 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005171 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005172 P2P_EVENT_FALLBACK_TO_GO_NEG
5173 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005174 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5175 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5176 wpa_s->p2p_persistent_group, 0, 0, 0,
5177 wpa_s->p2p_go_intent,
5178 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005179 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005180 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005181 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005182 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005183 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005184 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005185 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005186 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005187 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005188 return;
5189 }
5190
5191 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5192 "try to join the group", join ? "" :
5193 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005194 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005195 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005196 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005197 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005198 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005199 }
5200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005201 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5202 wpa_s->pending_join_iface_addr);
5203 if (freq < 0 &&
5204 p2p_get_interface_addr(wpa_s->global->p2p,
5205 wpa_s->pending_join_dev_addr,
5206 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005207 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5208 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5210 "address for join from " MACSTR " to " MACSTR
5211 " based on newly discovered P2P peer entry",
5212 MAC2STR(wpa_s->pending_join_iface_addr),
5213 MAC2STR(iface_addr));
5214 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5215 ETH_ALEN);
5216
5217 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5218 wpa_s->pending_join_iface_addr);
5219 }
5220 if (freq >= 0) {
5221 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5222 "from P2P peer table: %d MHz", freq);
5223 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005224 if (wpa_s->p2p_join_ssid_len) {
5225 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5226 MACSTR " and SSID %s",
5227 MAC2STR(wpa_s->pending_join_iface_addr),
5228 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5229 wpa_s->p2p_join_ssid_len));
5230 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5231 wpa_s->p2p_join_ssid,
5232 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005233 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005234 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5235 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5236 bss = wpa_bss_get_bssid_latest(wpa_s,
5237 wpa_s->pending_join_iface_addr);
5238 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005240 u8 dev_addr[ETH_ALEN];
5241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242 freq = bss->freq;
5243 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005244 "from BSS table: %d MHz (SSID %s)", freq,
5245 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005246 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005247 dev_addr) == 0 &&
5248 os_memcmp(wpa_s->pending_join_dev_addr,
5249 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5250 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5251 ETH_ALEN) != 0) {
5252 wpa_printf(MSG_DEBUG,
5253 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5254 MAC2STR(dev_addr),
5255 MAC2STR(wpa_s->pending_join_dev_addr));
5256 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5257 ETH_ALEN);
5258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 }
5260 if (freq > 0) {
5261 u16 method;
5262
Dmitry Shmidt04949592012-07-19 12:16:46 -07005263 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005264 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005265 P2P_EVENT_GROUP_FORMATION_FAILURE
5266 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005267 wpas_notify_p2p_group_formation_failure(
5268 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005269 return;
5270 }
5271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5273 "prior to joining an existing group (GO " MACSTR
5274 " freq=%u MHz)",
5275 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5276 wpa_s->pending_pd_before_join = 1;
5277
5278 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005279 case WPS_PIN_DISPLAY:
5280 method = WPS_CONFIG_KEYPAD;
5281 break;
5282 case WPS_PIN_KEYPAD:
5283 method = WPS_CONFIG_DISPLAY;
5284 break;
5285 case WPS_PBC:
5286 method = WPS_CONFIG_PUSHBUTTON;
5287 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005288 case WPS_P2PS:
5289 method = WPS_CONFIG_P2PS;
5290 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005291 default:
5292 method = 0;
5293 break;
5294 }
5295
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005296 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5297 wpa_s->pending_join_dev_addr) ==
5298 method)) {
5299 /*
5300 * We have already performed provision discovery for
5301 * joining the group. Proceed directly to join
5302 * operation without duplicated provision discovery. */
5303 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5304 "with " MACSTR " already done - proceed to "
5305 "join",
5306 MAC2STR(wpa_s->pending_join_dev_addr));
5307 wpa_s->pending_pd_before_join = 0;
5308 goto start;
5309 }
5310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005311 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005312 wpa_s->pending_join_dev_addr,
5313 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005314 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5316 "Discovery Request before joining an "
5317 "existing group");
5318 wpa_s->pending_pd_before_join = 0;
5319 goto start;
5320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005321 return;
5322 }
5323
5324 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5325 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5326 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5327 wpas_p2p_check_join_scan_limit(wpa_s);
5328 return;
5329
5330start:
5331 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005332 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5333 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005334}
5335
5336
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005337static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5338 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340 int ret;
5341 struct wpa_driver_scan_params params;
5342 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005343 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005345 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346
5347 os_memset(&params, 0, sizeof(params));
5348
5349 /* P2P Wildcard SSID */
5350 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005351 if (ssid && ssid_len) {
5352 params.ssids[0].ssid = ssid;
5353 params.ssids[0].ssid_len = ssid_len;
5354 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5355 wpa_s->p2p_join_ssid_len = ssid_len;
5356 } else {
5357 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5358 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5359 wpa_s->p2p_join_ssid_len = 0;
5360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361
5362 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5364 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5365 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005366 if (wps_ie == NULL) {
5367 wpas_p2p_scan_res_join(wpa_s, NULL);
5368 return;
5369 }
5370
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005371 if (!freq) {
5372 int oper_freq;
5373 /*
5374 * If freq is not provided, check the operating freq of the GO
5375 * and use a single channel scan on if possible.
5376 */
5377 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5378 wpa_s->pending_join_iface_addr);
5379 if (oper_freq > 0)
5380 freq = oper_freq;
5381 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005382 if (freq > 0) {
5383 freqs[0] = freq;
5384 params.freqs = freqs;
Hai Shalom899fcc72020-10-19 14:38:18 -07005385 } else if (wpa_s->conf->p2p_6ghz_disable) {
5386 wpa_printf(MSG_DEBUG,
5387 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005388 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5389 0);
5390 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5391 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005394 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5395 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5396 if (ies == NULL) {
5397 wpabuf_free(wps_ie);
5398 wpas_p2p_scan_res_join(wpa_s, NULL);
5399 return;
5400 }
5401 wpabuf_put_buf(ies, wps_ie);
5402 wpabuf_free(wps_ie);
5403
5404 bands = wpas_get_bands(wpa_s, freqs);
5405 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5406
5407 params.p2p_probe = 1;
5408 params.extra_ies = wpabuf_head(ies);
5409 params.extra_ies_len = wpabuf_len(ies);
5410
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005411 if (wpa_s->clear_driver_scan_cache) {
5412 wpa_printf(MSG_DEBUG,
5413 "Request driver to clear scan cache due to local BSS flush");
5414 params.only_new_results = 1;
5415 }
5416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 /*
5418 * Run a scan to update BSS table and start Provision Discovery once
5419 * the new scan results become available.
5420 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005421 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalom899fcc72020-10-19 14:38:18 -07005422 if (wpa_s->conf->p2p_6ghz_disable && params.freqs != freqs)
5423 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005424 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005425 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005426 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005427 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005428 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005430
5431 wpabuf_free(ies);
5432
5433 if (ret) {
5434 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5435 "try again later");
5436 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5437 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5438 wpas_p2p_check_join_scan_limit(wpa_s);
5439 }
5440}
5441
5442
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5444{
5445 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005446 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005447}
5448
5449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005451 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005452 int auto_join, int op_freq,
5453 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005454{
5455 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005456 MACSTR " dev " MACSTR " op_freq=%d)%s",
5457 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005458 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005459 if (ssid && ssid_len) {
5460 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5461 wpa_ssid_txt(ssid, ssid_len));
5462 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463
Dmitry Shmidt04949592012-07-19 12:16:46 -07005464 wpa_s->p2p_auto_pd = 0;
5465 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5467 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5468 wpa_s->pending_join_wps_method = wps_method;
5469
5470 /* Make sure we are not running find during connection establishment */
5471 wpas_p2p_stop_find(wpa_s);
5472
5473 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005474 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475 return 0;
5476}
5477
5478
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005479static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5480 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481{
5482 struct wpa_supplicant *group;
5483 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005484 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485
5486 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5487 if (group == NULL)
5488 return -1;
5489 if (group != wpa_s) {
5490 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5491 sizeof(group->p2p_pin));
5492 group->p2p_wps_method = wpa_s->p2p_wps_method;
5493 }
5494
Hai Shalom74f70d42019-02-11 14:42:39 -08005495 /*
5496 * Need to mark the current interface for p2p_group_formation
5497 * when a separate group interface is not used. This is needed
5498 * to allow p2p_cancel stop a pending p2p_connect-join.
5499 * wpas_p2p_init_group_interface() addresses this for the case
5500 * where a separate group interface is used.
5501 */
5502 if (group == wpa_s->parent)
5503 wpa_s->global->p2p_group_formation = group;
5504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005506 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507
5508 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005509 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5511 ETH_ALEN);
5512 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005513 if (freq && ssid && ssid_len) {
5514 res.freq = freq;
5515 res.ssid_len = ssid_len;
5516 os_memcpy(res.ssid, ssid, ssid_len);
5517 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005518 if (ssid && ssid_len) {
5519 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5520 ssid, ssid_len);
5521 } else {
5522 bss = wpa_bss_get_bssid_latest(
5523 wpa_s, wpa_s->pending_join_iface_addr);
5524 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005525 if (bss) {
5526 res.freq = bss->freq;
5527 res.ssid_len = bss->ssid_len;
5528 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5529 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5530 bss->freq,
5531 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005532 } else if (ssid && ssid_len) {
5533 res.ssid_len = ssid_len;
5534 os_memcpy(res.ssid, ssid, ssid_len);
5535 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5536 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005537 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 }
5539
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005540 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5541 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5542 "starting client");
5543 wpa_drv_cancel_remain_on_channel(wpa_s);
5544 wpa_s->off_channel_freq = 0;
5545 wpa_s->roc_waiting_drv_freq = 0;
5546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 wpas_start_wps_enrollee(group, &res);
5548
5549 /*
5550 * Allow a longer timeout for join-a-running-group than normal 15
5551 * second group formation timeout since the GO may not have authorized
5552 * our connection yet.
5553 */
5554 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5555 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5556 wpa_s, NULL);
5557
5558 return 0;
5559}
5560
5561
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005562static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005563 int *force_freq, int *pref_freq, int go,
5564 unsigned int *pref_freq_list,
5565 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005566{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005567 struct wpa_used_freq_data *freqs;
5568 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005569 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305570 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005571
5572 max_pref_freq = *num_pref_freq;
5573 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005574
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005575 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5576 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005577 if (!freqs)
5578 return -1;
5579
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005580 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5581 wpa_s->num_multichan_concurrent);
5582
5583 /*
5584 * It is possible that the total number of used frequencies is bigger
5585 * than the number of frequencies used for P2P, so get the system wide
5586 * number of unused frequencies.
5587 */
5588 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5589
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005590 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005591 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5592 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005593
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005594 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005595 int ret;
5596 if (go)
5597 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5598 else
5599 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5600 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005601 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005602 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5603 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005604 /*
5605 * If freq is a DFS channel and DFS is offloaded
5606 * to the driver, allow P2P GO to use it.
5607 */
5608 wpa_printf(MSG_DEBUG,
5609 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5610 freq);
5611 } else {
5612 wpa_printf(MSG_DEBUG,
5613 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5614 freq);
5615 res = -3;
5616 goto exit_free;
5617 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005618 }
5619
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005620 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005621 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005622 freq_in_use = 1;
5623 }
5624
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005625 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005626 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5627 freq);
5628 res = -2;
5629 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005630 }
5631 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5632 "requested channel (%u MHz)", freq);
5633 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005634 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005635 }
5636
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005637 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005638
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305639 if (*pref_freq == 0) {
5640 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005641 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305642 while (i < wpa_s->conf->num_p2p_pref_chan) {
5643 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5644 wpa_s->conf->p2p_pref_chan[i].op_class,
5645 wpa_s->conf->p2p_pref_chan[i].chan);
5646
5647 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5648 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5649 best_freq = p2p_pref_freq;
5650 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5651 "from P2P preferred channel list", best_freq);
5652 break;
5653 } else {
5654 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5655 "frequency (%u MHz) ", p2p_pref_freq);
5656 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005657 i++;
5658 }
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305659 } else {
5660 enum wpa_driver_if_type iface_type;
5661
5662 if (go)
5663 iface_type = WPA_IF_P2P_GO;
5664 else
5665 iface_type = WPA_IF_P2P_CLIENT;
5666
5667 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5668 best_freq, go);
5669
5670 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5671 &max_pref_freq,
5672 pref_freq_list);
5673 if (!res && max_pref_freq > 0) {
5674 *num_pref_freq = max_pref_freq;
5675 i = 0;
5676 while (i < *num_pref_freq &&
5677 (!p2p_supported_freq(wpa_s->global->p2p,
5678 pref_freq_list[i]) ||
5679 wpas_p2p_disallowed_freq(wpa_s->global,
5680 pref_freq_list[i]))) {
5681 wpa_printf(MSG_DEBUG,
5682 "P2P: preferred_freq_list[%d]=%d is disallowed",
5683 i, pref_freq_list[i]);
5684 i++;
5685 }
5686 if (i != *num_pref_freq) {
5687 best_freq = pref_freq_list[i];
5688 wpa_printf(MSG_DEBUG,
5689 "P2P: Using preferred_freq_list[%d]=%d",
5690 i, best_freq);
5691 } else {
5692 wpa_printf(MSG_DEBUG,
5693 "P2P: All driver preferred frequencies are "
5694 "disallowed for P2P use");
5695 *num_pref_freq = 0;
5696 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005697 } else {
5698 wpa_printf(MSG_DEBUG,
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305699 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005700 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005701 }
5702 }
5703
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005704 /* We have a candidate frequency to use */
5705 if (best_freq > 0) {
5706 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005707 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005708 best_freq);
5709 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005710 } else {
5711 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 -07005712 best_freq);
5713 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005714 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005715 } else if (num_unused > 0) {
5716 wpa_printf(MSG_DEBUG,
5717 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5718 *force_freq = 0;
5719 } else {
5720 wpa_printf(MSG_DEBUG,
5721 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5722 res = -2;
5723 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005724 }
5725
5726exit_ok:
5727 res = 0;
5728exit_free:
5729 os_free(freqs);
5730 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005731}
5732
5733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005734/**
5735 * wpas_p2p_connect - Request P2P Group Formation to be started
5736 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5737 * @peer_addr: Address of the peer P2P Device
5738 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5739 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005740 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005741 * @join: Whether to join an existing group (as a client) instead of starting
5742 * Group Owner negotiation; @peer_addr is BSSID in that case
5743 * @auth: Whether to only authorize the connection instead of doing that and
5744 * initiating Group Owner negotiation
5745 * @go_intent: GO Intent or -1 to use default
5746 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005747 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005748 * @persistent_id: Persistent group credentials to use for forcing GO
5749 * parameters or -1 to generate new values (SSID/passphrase)
5750 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5751 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005752 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005753 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005754 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005755 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005756 * @group_ssid: Specific Group SSID for join or %NULL if not set
5757 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005758 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5759 * failure, -2 on failure due to channel not currently available,
5760 * -3 if forced channel is not supported
5761 */
5762int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5763 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005764 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005765 int go_intent, int freq, unsigned int vht_center_freq2,
5766 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005767 unsigned int vht_chwidth, int he, int edmg,
5768 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005769{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005770 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005771 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005773 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005774 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005775 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005776
5777 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5778 return -1;
5779
Dmitry Shmidt04949592012-07-19 12:16:46 -07005780 if (persistent_id >= 0) {
5781 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5782 if (ssid == NULL || ssid->disabled != 2 ||
5783 ssid->mode != WPAS_MODE_P2P_GO)
5784 return -1;
5785 }
5786
Hai Shalom899fcc72020-10-19 14:38:18 -07005787 if (is_6ghz_freq(freq) && wpa_s->conf->p2p_6ghz_disable)
5788 return -2;
5789
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005790 os_free(wpa_s->global->add_psk);
5791 wpa_s->global->add_psk = NULL;
5792
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005793 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005794 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005795 wpa_s->global->pending_p2ps_group_freq = 0;
5796 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798 if (go_intent < 0)
5799 go_intent = wpa_s->conf->p2p_go_intent;
5800
5801 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005802 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005803
5804 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005805 wpa_s->p2p_persistent_group = !!persistent_group;
5806 wpa_s->p2p_persistent_id = persistent_id;
5807 wpa_s->p2p_go_intent = go_intent;
5808 wpa_s->p2p_connect_freq = freq;
5809 wpa_s->p2p_fallback_to_go_neg = 0;
5810 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005811 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005812 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005813 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5814 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005815 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005816 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817
5818 if (pin)
5819 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5820 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005821 if (wps_generate_pin((unsigned int *) &ret) < 0)
5822 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005823 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5824 "%08d", ret);
5825 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5826 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5828 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005829 } else if (wps_method == WPS_P2PS) {
5830 /* Force the P2Ps default PIN to be used */
5831 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005832 } else
5833 wpa_s->p2p_pin[0] = '\0';
5834
Dmitry Shmidt04949592012-07-19 12:16:46 -07005835 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5837 if (auth) {
5838 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5839 "connect a running group from " MACSTR,
5840 MAC2STR(peer_addr));
5841 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5842 return ret;
5843 }
5844 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5845 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5846 iface_addr) < 0) {
5847 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5848 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5849 dev_addr);
5850 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005851 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005852 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005853 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5854 "%ld.%06ld",
5855 wpa_s->p2p_auto_started.sec,
5856 wpa_s->p2p_auto_started.usec);
5857 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005858 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005859 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005860 auto_join, freq,
5861 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862 return -1;
5863 return ret;
5864 }
5865
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005866 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005867 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005868 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005869 if (res)
5870 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005871 wpas_p2p_set_own_freq_preference(wpa_s,
5872 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005873
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005874 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5877
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005878 if (wpa_s->create_p2p_iface) {
5879 /* Prepare to add a new interface for the group */
5880 iftype = WPA_IF_P2P_GROUP;
5881 if (go_intent == 15)
5882 iftype = WPA_IF_P2P_GO;
5883 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5884 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5885 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005889 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005890 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005891 if (wpa_s->p2p_mgmt)
5892 if_addr = wpa_s->parent->own_addr;
5893 else
5894 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005895 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897
5898 if (auth) {
5899 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005900 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005901 force_freq, persistent_group, ssid,
5902 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903 return -1;
5904 return ret;
5905 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005906
5907 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5908 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005909 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005910 if (wpa_s->create_p2p_iface)
5911 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912 return -1;
5913 }
5914 return ret;
5915}
5916
5917
5918/**
5919 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5920 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5921 * @freq: Frequency of the channel in MHz
5922 * @duration: Duration of the stay on the channel in milliseconds
5923 *
5924 * This callback is called when the driver indicates that it has started the
5925 * requested remain-on-channel duration.
5926 */
5927void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5928 unsigned int freq, unsigned int duration)
5929{
5930 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5931 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005932 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)",
5933 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5934 wpa_s->roc_waiting_drv_freq, freq, duration);
5935 if (wpa_s->off_channel_freq &&
5936 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5938 wpa_s->pending_listen_duration);
5939 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005940 } else {
5941 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5942 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5943 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005944 }
5945}
5946
5947
Jithu Jance57cea1a2014-08-20 10:22:04 -07005948int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005949{
5950 /* Limit maximum Listen state time based on driver limitation. */
5951 if (timeout > wpa_s->max_remain_on_chan)
5952 timeout = wpa_s->max_remain_on_chan;
5953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005954 return p2p_listen(wpa_s->global->p2p, timeout);
5955}
5956
5957
5958/**
5959 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5960 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5961 * @freq: Frequency of the channel in MHz
5962 *
5963 * This callback is called when the driver indicates that a remain-on-channel
5964 * operation has been completed, i.e., the duration on the requested channel
5965 * has timed out.
5966 */
5967void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5968 unsigned int freq)
5969{
5970 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5971 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005972 wpa_s->global->p2p_long_listen,
5973 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005974 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005975 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5976 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005977 if (wpa_s->global->p2p_long_listen > 0)
5978 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5980 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005981 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005983 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005985 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005986 } else {
5987 /*
5988 * When listen duration is over, stop listen & update p2p_state
5989 * to IDLE.
5990 */
5991 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992 }
5993}
5994
5995
5996/**
5997 * wpas_p2p_group_remove - Remove a P2P group
5998 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5999 * @ifname: Network interface name of the group interface or "*" to remove all
6000 * groups
6001 * Returns: 0 on success, -1 on failure
6002 *
6003 * This function is used to remove a P2P group. This can be used to disconnect
6004 * from a group in which the local end is a P2P Client or to end a P2P Group in
6005 * case the local end is the Group Owner. If a virtual network interface was
6006 * created for this group, that interface will be removed. Otherwise, only the
6007 * configured P2P group network will be removed from the interface.
6008 */
6009int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6010{
6011 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006012 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013
6014 if (os_strcmp(ifname, "*") == 0) {
6015 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006016 bool calling_wpa_s_group_removed = false;
6017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 wpa_s = global->ifaces;
6019 while (wpa_s) {
6020 prev = wpa_s;
6021 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006022 if (prev->p2p_group_interface !=
6023 NOT_P2P_GROUP_INTERFACE ||
6024 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006025 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006026 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006027 if (prev == calling_wpa_s)
6028 calling_wpa_s_group_removed = true;
6029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006031
6032 if (!calling_wpa_s_group_removed &&
6033 (calling_wpa_s->p2p_group_interface !=
6034 NOT_P2P_GROUP_INTERFACE ||
6035 (calling_wpa_s->current_ssid &&
6036 calling_wpa_s->current_ssid->p2p_group))) {
6037 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6038 wpas_p2p_disconnect_safely(calling_wpa_s,
6039 calling_wpa_s);
6040 }
6041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 return 0;
6043 }
6044
6045 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6046 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6047 break;
6048 }
6049
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006050 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006051}
6052
6053
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006054static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6055{
6056 unsigned int r;
6057
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006058 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6059 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
6060 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
6061 int res;
6062
6063 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6064 &size, pref_freq_list);
6065 if (!res && size > 0) {
6066 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006067 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006068 (!p2p_supported_freq(wpa_s->global->p2p,
6069 pref_freq_list[i]) ||
6070 wpas_p2p_disallowed_freq(wpa_s->global,
6071 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006072 wpa_printf(MSG_DEBUG,
6073 "P2P: preferred_freq_list[%d]=%d is disallowed",
6074 i, pref_freq_list[i]);
6075 i++;
6076 }
6077 if (i != size) {
6078 freq = pref_freq_list[i];
6079 wpa_printf(MSG_DEBUG,
6080 "P2P: Using preferred_freq_list[%d]=%d",
6081 i, freq);
6082 } else {
6083 wpa_printf(MSG_DEBUG,
6084 "P2P: All driver preferred frequencies are disallowed for P2P use");
6085 }
6086 } else {
6087 wpa_printf(MSG_DEBUG,
6088 "P2P: No preferred frequency list available");
6089 }
6090 }
6091
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006092 if (freq == 2) {
6093 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6094 "band");
6095 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006096 p2p_supported_freq_go(wpa_s->global->p2p,
6097 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006098 freq = wpa_s->best_24_freq;
6099 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6100 "channel: %d MHz", freq);
6101 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006102 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6103 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006104 int possible_2g_freqs[] = {
6105 /* operating class 81 */
6106 2412, 2437, 2462,
6107 };
6108 int possible_2g_freqs_num =
6109 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6110 int i;
6111 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6112 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6113 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6114 break;
6115 }
6116 }
6117
6118 if (i >= possible_2g_freqs_num) {
6119 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6120 "2.4 GHz channel for P2P group");
6121 return -1;
6122 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006123 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6124 "channel: %d MHz", freq);
6125 }
6126 }
6127
6128 if (freq == 5) {
6129 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6130 "band");
6131 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006132 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006133 wpa_s->best_5_freq)) {
6134 freq = wpa_s->best_5_freq;
6135 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6136 "channel: %d MHz", freq);
6137 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006138 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6139 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006140
6141 /*
6142 * most of 5G channels are DFS, only operating class 115 and 124
6143 * are available possibly, randomly pick a start to check them.
6144 */
6145 int possible_5g_freqs[] = {
6146 /* operating class 115 */
6147 5180, 5200, 5220, 5240,
6148 /* operating class 124 */
6149 5745, 5765, 5785, 5805,
6150 };
6151 int possible_5g_freqs_num =
6152 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6153
6154 int i;
6155 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6156 if (p2p_supported_freq_go(
6157 wpa_s->global->p2p,
6158 possible_5g_freqs[r % possible_5g_freqs_num])) {
6159 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6160 break;
6161 }
6162 }
6163
6164 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006165 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6166 "5 GHz channel for P2P group");
6167 return -1;
6168 }
6169 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6170 "channel: %d MHz", freq);
6171 }
6172 }
6173
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006174 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006175 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006176 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6177 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006178 /*
6179 * If freq is a DFS channel and DFS is offloaded to the
6180 * driver, allow P2P GO to use it.
6181 */
6182 wpa_printf(MSG_DEBUG, "P2P: "
6183 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6184 __func__, freq);
6185 return freq;
6186 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006187 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6188 "(%u MHz) is not supported for P2P uses",
6189 freq);
6190 return -1;
6191 }
6192
6193 return freq;
6194}
6195
6196
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006197static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6198 const struct p2p_channels *channels,
6199 int freq)
6200{
6201 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6202 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6203 freq_included(wpa_s, channels, freq))
6204 return 1;
6205 return 0;
6206}
6207
6208
6209static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6210 struct p2p_go_neg_results *params,
6211 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006212{
6213 unsigned int i, r;
6214
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006215 /* try all channels in operating class 115 */
6216 for (i = 0; i < 4; i++) {
6217 params->freq = 5180 + i * 20;
6218 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006219 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006220 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6221 goto out;
6222 }
6223
6224 /* try all channels in operating class 124 */
6225 for (i = 0; i < 4; i++) {
6226 params->freq = 5745 + i * 20;
6227 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006228 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006229 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6230 goto out;
6231 }
6232
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006233 /* try social channel class 180 channel 2 */
6234 params->freq = 58320 + 1 * 2160;
6235 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006236 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006237 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6238 goto out;
6239
6240 /* try all channels in reg. class 180 */
6241 for (i = 0; i < 4; i++) {
6242 params->freq = 58320 + i * 2160;
6243 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006244 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006245 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6246 goto out;
6247 }
6248
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006249 /* try some random selection of the social channels */
6250 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6251 return;
6252
6253 for (i = 0; i < 3; i++) {
6254 params->freq = 2412 + ((r + i) % 3) * 25;
6255 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6256 goto out;
6257 }
6258
6259 /* try all other channels in operating class 81 */
6260 for (i = 0; i < 11; i++) {
6261 params->freq = 2412 + i * 5;
6262
6263 /* skip social channels; covered in the previous loop */
6264 if (params->freq == 2412 ||
6265 params->freq == 2437 ||
6266 params->freq == 2462)
6267 continue;
6268
6269 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6270 goto out;
6271 }
6272
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006273 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006274 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006275 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006276out:
6277 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6278 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006279}
6280
6281
Roshan Pius3a1667e2018-07-03 15:17:14 -07006282static int wpas_same_band(int freq1, int freq2)
6283{
6284 enum hostapd_hw_mode mode1, mode2;
6285 u8 chan1, chan2;
6286
6287 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6288 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6289 if (mode1 == NUM_HOSTAPD_MODES)
6290 return 0;
6291 return mode1 == mode2;
6292}
6293
6294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006295static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6296 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006297 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006298 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006299 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006300 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006301{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006302 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006303 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006304 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006305 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006306 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006307
6308 os_memset(params, 0, sizeof(*params));
6309 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006310 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006311 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006312 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006313 params->max_oper_chwidth = max_oper_chwidth;
6314 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006315 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006316
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006317 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6318 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006319 if (!freqs)
6320 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006322 num = get_shared_radio_freqs_data(wpa_s, freqs,
6323 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006324
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006325 if (wpa_s->current_ssid &&
6326 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6327 wpa_s->wpa_state == WPA_COMPLETED) {
6328 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6329 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006330
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006331 /*
6332 * If the frequency selection is done for an active P2P GO that
6333 * is not sharing a frequency, allow to select a new frequency
6334 * even if there are no unused frequencies as we are about to
6335 * move the P2P GO so its frequency can be re-used.
6336 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006337 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006338 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6339 freqs[i].flags == 0) {
6340 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006341 break;
6342 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006343 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006344 }
6345
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006346 /* Try to use EDMG channel */
6347 if (params->edmg) {
6348 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6349 goto success;
6350 params->edmg = 0;
6351 }
6352
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006353 /* try using the forced freq */
6354 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006355 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6356 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006357 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006358 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006359 freq);
6360 goto fail;
6361 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006362 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6363 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006364 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6365 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006366 /*
6367 * If freq is a DFS channel and DFS is offloaded
6368 * to the driver, allow P2P GO to use it.
6369 */
6370 wpa_printf(MSG_DEBUG,
6371 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6372 __func__, freq);
6373 } else {
6374 wpa_printf(MSG_DEBUG,
6375 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6376 freq);
6377 goto fail;
6378 }
6379 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006380
6381 for (i = 0; i < num; i++) {
6382 if (freqs[i].freq == freq) {
6383 wpa_printf(MSG_DEBUG,
6384 "P2P: forced freq (%d MHz) is also shared",
6385 freq);
6386 params->freq = freq;
6387 goto success;
6388 }
6389 }
6390
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006391 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006392 wpa_printf(MSG_DEBUG,
6393 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6394 freq);
6395 goto fail;
6396 }
6397
6398 wpa_printf(MSG_DEBUG,
6399 "P2P: force GO freq (%d MHz) on a free channel",
6400 freq);
6401 params->freq = freq;
6402 goto success;
6403 }
6404
6405 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006406 if (num &&
6407 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006408 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6409 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6410 wpa_printf(MSG_DEBUG,
6411 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006412 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006413 params->freq = cand;
6414 goto success;
6415 }
6416
6417 /* try using one of the shared freqs */
6418 for (i = 0; i < num; i++) {
6419 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6420 freqs[i].freq)) {
6421 wpa_printf(MSG_DEBUG,
6422 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006423 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006424 params->freq = freqs[i].freq;
6425 goto success;
6426 }
6427 }
6428 }
6429
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006430 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006431 wpa_printf(MSG_DEBUG,
6432 "P2P: Cannot force GO on any of the channels we are already using");
6433 goto fail;
6434 }
6435
6436 /* try using the setting from the configuration file */
6437 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6438 wpa_s->conf->p2p_oper_channel >= 1 &&
6439 wpa_s->conf->p2p_oper_channel <= 11 &&
6440 wpas_p2p_supported_freq_go(
6441 wpa_s, channels,
6442 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6443 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6444 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6445 "frequency %d MHz", params->freq);
6446 goto success;
6447 }
6448
6449 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6450 wpa_s->conf->p2p_oper_reg_class == 116 ||
6451 wpa_s->conf->p2p_oper_reg_class == 117 ||
6452 wpa_s->conf->p2p_oper_reg_class == 124 ||
6453 wpa_s->conf->p2p_oper_reg_class == 125 ||
6454 wpa_s->conf->p2p_oper_reg_class == 126 ||
6455 wpa_s->conf->p2p_oper_reg_class == 127) &&
6456 wpas_p2p_supported_freq_go(wpa_s, channels,
6457 5000 +
6458 5 * wpa_s->conf->p2p_oper_channel)) {
6459 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6460 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6461 "frequency %d MHz", params->freq);
6462 goto success;
6463 }
6464
6465 /* Try using best channels */
6466 if (wpa_s->conf->p2p_oper_channel == 0 &&
6467 wpa_s->best_overall_freq > 0 &&
6468 wpas_p2p_supported_freq_go(wpa_s, channels,
6469 wpa_s->best_overall_freq)) {
6470 params->freq = wpa_s->best_overall_freq;
6471 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6472 "channel %d MHz", params->freq);
6473 goto success;
6474 }
6475
6476 if (wpa_s->conf->p2p_oper_channel == 0 &&
6477 wpa_s->best_24_freq > 0 &&
6478 wpas_p2p_supported_freq_go(wpa_s, channels,
6479 wpa_s->best_24_freq)) {
6480 params->freq = wpa_s->best_24_freq;
6481 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6482 "channel %d MHz", params->freq);
6483 goto success;
6484 }
6485
6486 if (wpa_s->conf->p2p_oper_channel == 0 &&
6487 wpa_s->best_5_freq > 0 &&
6488 wpas_p2p_supported_freq_go(wpa_s, channels,
6489 wpa_s->best_5_freq)) {
6490 params->freq = wpa_s->best_5_freq;
6491 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6492 "channel %d MHz", params->freq);
6493 goto success;
6494 }
6495
6496 /* try using preferred channels */
6497 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6498 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6499 params->freq = cand;
6500 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6501 "channels", params->freq);
6502 goto success;
6503 }
6504
Roshan Pius3a1667e2018-07-03 15:17:14 -07006505 /* Try using a channel that allows VHT to be used with 80 MHz */
6506 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6507 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6508 enum hostapd_hw_mode mode;
6509 struct hostapd_hw_modes *hwmode;
6510 u8 chan;
6511
6512 cand = wpa_s->p2p_group_common_freqs[i];
6513 mode = ieee80211_freq_to_chan(cand, &chan);
6514 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006515 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006516 if (!hwmode ||
6517 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6518 BW80) != ALLOWED)
6519 continue;
6520 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6521 params->freq = cand;
6522 wpa_printf(MSG_DEBUG,
6523 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6524 params->freq);
6525 goto success;
6526 }
6527 }
6528 }
6529
6530 /* Try using a channel that allows HT to be used with 40 MHz on the same
6531 * band so that CSA can be used */
6532 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6533 wpa_s->p2p_group_common_freqs) {
6534 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6535 enum hostapd_hw_mode mode;
6536 struct hostapd_hw_modes *hwmode;
6537 u8 chan;
6538
6539 cand = wpa_s->p2p_group_common_freqs[i];
6540 mode = ieee80211_freq_to_chan(cand, &chan);
6541 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006542 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006543 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6544 cand) ||
6545 !hwmode ||
6546 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6547 BW40MINUS) != ALLOWED &&
6548 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6549 BW40PLUS) != ALLOWED))
6550 continue;
6551 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6552 params->freq = cand;
6553 wpa_printf(MSG_DEBUG,
6554 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6555 params->freq);
6556 goto success;
6557 }
6558 }
6559 }
6560
6561 /* Try using one of the group common freqs on the same band so that CSA
6562 * can be used */
6563 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6564 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6565 cand = wpa_s->p2p_group_common_freqs[i];
6566 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6567 cand))
6568 continue;
6569 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6570 params->freq = cand;
6571 wpa_printf(MSG_DEBUG,
6572 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6573 params->freq);
6574 goto success;
6575 }
6576 }
6577 }
6578
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006579 /* Try using one of the group common freqs */
6580 if (wpa_s->p2p_group_common_freqs) {
6581 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6582 cand = wpa_s->p2p_group_common_freqs[i];
6583 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6584 params->freq = cand;
6585 wpa_printf(MSG_DEBUG,
6586 "P2P: Use freq %d MHz common with the peer",
6587 params->freq);
6588 goto success;
6589 }
6590 }
6591 }
6592
6593 /* no preference, select some channel */
6594 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6595
6596 if (params->freq == 0) {
6597 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6598 goto fail;
6599 }
6600
6601success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006602 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006603 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006604fail:
6605 os_free(freqs);
6606 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607}
6608
6609
6610static struct wpa_supplicant *
6611wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6612 int go)
6613{
6614 struct wpa_supplicant *group_wpa_s;
6615
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006616 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006617 if (wpa_s->p2p_mgmt) {
6618 /*
6619 * We may be called on the p2p_dev interface which
6620 * cannot be used for group operations, so always use
6621 * the primary interface.
6622 */
6623 wpa_s->parent->p2pdev = wpa_s;
6624 wpa_s = wpa_s->parent;
6625 }
6626 wpa_dbg(wpa_s, MSG_DEBUG,
6627 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006628 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006629 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006630 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006631 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633
6634 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006635 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006636 wpa_msg_global(wpa_s, MSG_ERROR,
6637 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006638 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006639 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006640 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6641 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006642 wpa_msg_global(wpa_s, MSG_ERROR,
6643 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006644 wpas_p2p_remove_pending_group_interface(wpa_s);
6645 return NULL;
6646 }
6647
Roshan Pius3a1667e2018-07-03 15:17:14 -07006648 if (go && wpa_s->p2p_go_do_acs) {
6649 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6650 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6651 wpa_s->p2p_go_do_acs = 0;
6652 }
6653
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006654 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6655 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006656 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006657 return group_wpa_s;
6658}
6659
6660
6661/**
6662 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6663 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6664 * @persistent_group: Whether to create a persistent group
6665 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006666 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006667 * @ht40: Start GO with 40 MHz channel width
6668 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006669 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006670 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006671 * Returns: 0 on success, -1 on failure
6672 *
6673 * This function creates a new P2P group with the local end as the Group Owner,
6674 * i.e., without using Group Owner Negotiation.
6675 */
6676int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006677 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006678 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006679{
6680 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681
6682 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6683 return -1;
6684
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006685 os_free(wpa_s->global->add_psk);
6686 wpa_s->global->add_psk = NULL;
6687
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006688 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006689 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006690 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006691
Roshan Pius3a1667e2018-07-03 15:17:14 -07006692 if (!wpa_s->p2p_go_do_acs) {
6693 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6694 if (freq < 0)
6695 return -1;
6696 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006698 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006699 ht40, vht, max_oper_chwidth, he, edmg,
6700 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006701 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006703 p2p_go_params(wpa_s->global->p2p, &params);
6704 params.persistent_group = persistent_group;
6705
6706 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6707 if (wpa_s == NULL)
6708 return -1;
6709 wpas_start_wps_go(wpa_s, &params, 0);
6710
6711 return 0;
6712}
6713
6714
6715static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006716 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006717 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006718{
6719 struct wpa_ssid *ssid;
6720
6721 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6722 if (wpa_s == NULL)
6723 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006724 if (force_scan)
6725 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006726 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727
6728 wpa_supplicant_ap_deinit(wpa_s);
6729
6730 ssid = wpa_config_add_network(wpa_s->conf);
6731 if (ssid == NULL)
6732 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006733 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006734 wpa_config_set_network_defaults(ssid);
6735 ssid->temporary = 1;
6736 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006737 ssid->pbss = params->pbss;
6738 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6739 WPA_CIPHER_CCMP;
6740 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006741 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6742 ssid->ssid = os_malloc(params->ssid_len);
6743 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006744 wpa_config_remove_network(wpa_s->conf, ssid->id);
6745 return -1;
6746 }
6747 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6748 ssid->ssid_len = params->ssid_len;
6749 ssid->p2p_group = 1;
6750 ssid->export_keys = 1;
6751 if (params->psk_set) {
6752 os_memcpy(ssid->psk, params->psk, 32);
6753 ssid->psk_set = 1;
6754 }
6755 if (params->passphrase)
6756 ssid->passphrase = os_strdup(params->passphrase);
6757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006758 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006759 wpa_s->p2p_in_invitation = 1;
6760 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006761 wpa_s->p2p_go_group_formation_completed = 0;
6762 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006763
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006764 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006765 NULL);
6766 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6767 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006768 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006769 wpa_supplicant_select_network(wpa_s, ssid);
6770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006771 return 0;
6772}
6773
6774
6775int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6776 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006777 int force_freq, int neg_freq,
6778 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006779 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006780 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006781 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006782 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006783{
6784 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006785 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006786
6787 if (ssid->disabled != 2 || ssid->ssid == NULL)
6788 return -1;
6789
6790 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6791 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6792 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6793 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006794 if (go == 0 &&
6795 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006796 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006797 /*
6798 * This can happen if Invitation Response frame was lost
6799 * and the peer (GO of a persistent group) tries to
6800 * invite us again. Reschedule the timeout to avoid
6801 * terminating the wait for the connection too early
6802 * since we now know that the peer is still trying to
6803 * invite us instead of having already started the GO.
6804 */
6805 wpa_printf(MSG_DEBUG,
6806 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6807 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6808 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006809 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006810 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006811 return 0;
6812 }
6813
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006814 os_free(wpa_s->global->add_psk);
6815 wpa_s->global->add_psk = NULL;
6816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006817 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006818 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819
Dmitry Shmidt04949592012-07-19 12:16:46 -07006820 wpa_s->p2p_fallback_to_go_neg = 0;
6821
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006822 if (ssid->mode == WPAS_MODE_P2P_GO) {
6823 if (force_freq > 0) {
6824 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6825 if (freq < 0)
6826 return -1;
6827 } else {
6828 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6829 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006830 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006831 freq = 0;
6832 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006833 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006834 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006835 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006836 struct os_reltime now;
6837 struct wpa_bss *bss =
6838 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006839
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006840 os_get_reltime(&now);
6841 if (bss &&
6842 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006843 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006844 freq = bss->freq;
6845 else
6846 freq = 0;
6847 }
6848
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006849 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6850 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006851 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006852 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006853 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006854
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006855 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006856 ht40, vht, max_oper_chwidth, he, edmg,
6857 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006858 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006859
6860 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006861 params.psk_set = ssid->psk_set;
6862 if (params.psk_set)
6863 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006864 if (ssid->passphrase) {
6865 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6866 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6867 "persistent group");
6868 return -1;
6869 }
6870 os_strlcpy(params.passphrase, ssid->passphrase,
6871 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006873 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6874 params.ssid_len = ssid->ssid_len;
6875 params.persistent_group = 1;
6876
6877 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6878 if (wpa_s == NULL)
6879 return -1;
6880
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006881 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6882
Dmitry Shmidt56052862013-10-04 10:23:25 -07006883 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006884 wpas_start_wps_go(wpa_s, &params, 0);
6885
6886 return 0;
6887}
6888
6889
6890static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6891 struct wpabuf *proberesp_ies)
6892{
6893 struct wpa_supplicant *wpa_s = ctx;
6894 if (wpa_s->ap_iface) {
6895 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006896 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6897 wpabuf_free(beacon_ies);
6898 wpabuf_free(proberesp_ies);
6899 return;
6900 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006901 if (beacon_ies) {
6902 wpabuf_free(hapd->p2p_beacon_ie);
6903 hapd->p2p_beacon_ie = beacon_ies;
6904 }
6905 wpabuf_free(hapd->p2p_probe_resp_ie);
6906 hapd->p2p_probe_resp_ie = proberesp_ies;
6907 } else {
6908 wpabuf_free(beacon_ies);
6909 wpabuf_free(proberesp_ies);
6910 }
6911 wpa_supplicant_ap_update_beacon(wpa_s);
6912}
6913
6914
6915static void wpas_p2p_idle_update(void *ctx, int idle)
6916{
6917 struct wpa_supplicant *wpa_s = ctx;
6918 if (!wpa_s->ap_iface)
6919 return;
6920 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006921 if (idle) {
6922 if (wpa_s->global->p2p_fail_on_wps_complete &&
6923 wpa_s->p2p_in_provisioning) {
6924 wpas_p2p_grpform_fail_after_wps(wpa_s);
6925 return;
6926 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006927 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006928 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006929 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6930}
6931
6932
6933struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006934 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935{
6936 struct p2p_group *group;
6937 struct p2p_group_config *cfg;
6938
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006939 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6940 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941 return NULL;
6942
6943 cfg = os_zalloc(sizeof(*cfg));
6944 if (cfg == NULL)
6945 return NULL;
6946
Dmitry Shmidt04949592012-07-19 12:16:46 -07006947 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006948 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006949 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006950 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006951 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6952 if (wpa_s->max_stations &&
6953 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6954 cfg->max_clients = wpa_s->max_stations;
6955 else
6956 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006957 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6958 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006959 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006960 cfg->cb_ctx = wpa_s;
6961 cfg->ie_update = wpas_p2p_ie_update;
6962 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006963 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6964 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006965
6966 group = p2p_group_init(wpa_s->global->p2p, cfg);
6967 if (group == NULL)
6968 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006969 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970 p2p_group_notif_formation_done(group);
6971 wpa_s->p2p_group = group;
6972 return group;
6973}
6974
6975
6976void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6977 int registrar)
6978{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006979 struct wpa_ssid *ssid = wpa_s->current_ssid;
6980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981 if (!wpa_s->p2p_in_provisioning) {
6982 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6983 "provisioning not in progress");
6984 return;
6985 }
6986
Dmitry Shmidt04949592012-07-19 12:16:46 -07006987 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6988 u8 go_dev_addr[ETH_ALEN];
6989 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6990 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6991 ssid->ssid_len);
6992 /* Clear any stored provisioning info */
6993 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6994 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006995
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006996 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006997 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006998 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006999 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7000 /*
7001 * Use a separate timeout for initial data connection to
7002 * complete to allow the group to be removed automatically if
7003 * something goes wrong in this step before the P2P group idle
7004 * timeout mechanism is taken into use.
7005 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007006 wpa_dbg(wpa_s, MSG_DEBUG,
7007 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7008 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007009 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7010 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007011 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007012 /* Complete group formation on successful data connection. */
7013 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007014 } else if (ssid) {
7015 /*
7016 * Use a separate timeout for initial data connection to
7017 * complete to allow the group to be removed automatically if
7018 * the client does not complete data connection successfully.
7019 */
7020 wpa_dbg(wpa_s, MSG_DEBUG,
7021 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7022 P2P_MAX_INITIAL_CONN_WAIT_GO);
7023 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7024 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007025 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007026 /*
7027 * Complete group formation on first successful data connection
7028 */
7029 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007031 if (wpa_s->global->p2p)
7032 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007033 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007034}
7035
7036
Jouni Malinen75ecf522011-06-27 15:19:46 -07007037void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7038 struct wps_event_fail *fail)
7039{
7040 if (!wpa_s->p2p_in_provisioning) {
7041 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7042 "provisioning not in progress");
7043 return;
7044 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007045
7046 if (wpa_s->go_params) {
7047 p2p_clear_provisioning_info(
7048 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007049 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007050 }
7051
Jouni Malinen75ecf522011-06-27 15:19:46 -07007052 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007053
7054 if (wpa_s == wpa_s->global->p2p_group_formation) {
7055 /*
7056 * Allow some time for the failed WPS negotiation exchange to
7057 * complete, but remove the group since group formation cannot
7058 * succeed after provisioning failure.
7059 */
7060 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7061 wpa_s->global->p2p_fail_on_wps_complete = 1;
7062 eloop_deplete_timeout(0, 50000,
7063 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007064 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007065 }
7066}
7067
7068
7069int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7070{
7071 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7072 !wpa_s->p2p_in_provisioning)
7073 return 0;
7074
7075 wpas_p2p_grpform_fail_after_wps(wpa_s);
7076
7077 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007078}
7079
7080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007081int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007082 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007083 enum wpas_p2p_prov_disc_use use,
7084 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007085{
7086 u16 config_methods;
7087
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007088 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007089 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007090 wpa_s->p2p_fallback_to_go_neg = 0;
7091 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007092 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7093 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007094 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7095 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7096
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007097 wpa_printf(MSG_DEBUG,
7098 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7099 __func__, p2ps_prov->conncap,
7100 p2ps_prov->adv_id, p2ps_prov->conncap,
7101 p2ps_prov->status, p2ps_prov->info);
7102
7103 config_methods = 0;
7104 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007105 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007108 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7109 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007110 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007111 else {
7112 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007113 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007114 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007116
Dmitry Shmidt04949592012-07-19 12:16:46 -07007117 if (use == WPAS_P2P_PD_AUTO) {
7118 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7119 wpa_s->pending_pd_config_methods = config_methods;
7120 wpa_s->p2p_auto_pd = 1;
7121 wpa_s->p2p_auto_join = 0;
7122 wpa_s->pending_pd_before_join = 0;
7123 wpa_s->auto_pd_scan_retry = 0;
7124 wpas_p2p_stop_find(wpa_s);
7125 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007126 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007127 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7128 wpa_s->p2p_auto_started.sec,
7129 wpa_s->p2p_auto_started.usec);
7130 wpas_p2p_join_scan(wpa_s, NULL);
7131 return 0;
7132 }
7133
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007134 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7135 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007136 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007137 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007138
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007139 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007140 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007141 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007142}
7143
7144
7145int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7146 char *end)
7147{
7148 return p2p_scan_result_text(ies, ies_len, buf, end);
7149}
7150
7151
7152static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7153{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007154 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007155 return;
7156
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007157 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007158 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007159 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7160 wpa_s, NULL);
7161 offchannel_send_action_done(wpa_s);
7162 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7165 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007166 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007167}
7168
7169
7170int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7171 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007172 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007173 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007174 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007175{
7176 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007177 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007178
Dmitry Shmidt04949592012-07-19 12:16:46 -07007179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007180 wpa_s->p2p_in_provisioning) {
7181 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7182 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7183 " (P2P disabled)" : "",
7184 wpa_s->p2p_in_provisioning ?
7185 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007186 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007187 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007188
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007189 wpa_supplicant_cancel_sched_scan(wpa_s);
7190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007191 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007192 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007193 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194}
7195
7196
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007197static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7198 struct wpa_scan_results *scan_res)
7199{
7200 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7201
7202 if (wpa_s->p2p_scan_work) {
7203 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7204 wpa_s->p2p_scan_work = NULL;
7205 radio_work_done(work);
7206 }
7207
7208 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7209 return;
7210
7211 /*
7212 * Indicate that results have been processed so that the P2P module can
7213 * continue pending tasks.
7214 */
Hai Shalom60840252021-02-19 19:02:11 -08007215 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007216}
7217
7218
7219static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007220{
7221 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007222 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007223 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7224 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007225
7226 if (wpa_s->global->p2p)
7227 p2p_stop_find(wpa_s->global->p2p);
7228
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007229 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7230 wpa_printf(MSG_DEBUG,
7231 "P2P: Do not consider the scan results after stop_find");
7232 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7233 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007234}
7235
7236
7237void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7238{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007239 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007240 if (!wpa_s->global->pending_group_iface_for_p2ps)
7241 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007242}
7243
7244
7245static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7246{
7247 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007248 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007249}
7250
7251
7252int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7253{
7254 int res;
7255
7256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7257 return -1;
7258
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007259 if (wpa_s->p2p_lo_started) {
7260 wpa_printf(MSG_DEBUG,
7261 "P2P: Cannot start P2P listen, it is offloaded");
7262 return -1;
7263 }
7264
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007265 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007266 wpas_p2p_clear_pending_action_tx(wpa_s);
7267
7268 if (timeout == 0) {
7269 /*
7270 * This is a request for unlimited Listen state. However, at
7271 * least for now, this is mapped to a Listen state for one
7272 * hour.
7273 */
7274 timeout = 3600;
7275 }
7276 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007277 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007279 /*
7280 * Stop previous find/listen operation to avoid trying to request a new
7281 * remain-on-channel operation while the driver is still running the
7282 * previous one.
7283 */
7284 if (wpa_s->global->p2p)
7285 p2p_stop_find(wpa_s->global->p2p);
7286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007287 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7288 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007289 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007290 eloop_register_timeout(timeout, 0,
7291 wpas_p2p_long_listen_timeout,
7292 wpa_s, NULL);
7293 }
7294
7295 return res;
7296}
7297
7298
7299int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7300 u8 *buf, size_t len, int p2p_group)
7301{
7302 struct wpabuf *p2p_ie;
7303 int ret;
7304
7305 if (wpa_s->global->p2p_disabled)
7306 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007307 /*
7308 * Advertize mandatory cross connection capability even on
7309 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7310 */
7311 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007312 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007313 if (wpa_s->global->p2p == NULL)
7314 return -1;
7315 if (bss == NULL)
7316 return -1;
7317
7318 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7319 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7320 p2p_group, p2p_ie);
7321 wpabuf_free(p2p_ie);
7322
7323 return ret;
7324}
7325
7326
7327int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007328 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007329 const u8 *ie, size_t ie_len,
7330 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007331{
7332 if (wpa_s->global->p2p_disabled)
7333 return 0;
7334 if (wpa_s->global->p2p == NULL)
7335 return 0;
7336
Dmitry Shmidt04949592012-07-19 12:16:46 -07007337 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007338 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007339 case P2P_PREQ_NOT_P2P:
7340 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7341 ssi_signal);
7342 /* fall through */
7343 case P2P_PREQ_MALFORMED:
7344 case P2P_PREQ_NOT_LISTEN:
7345 case P2P_PREQ_NOT_PROCESSED:
7346 default: /* make gcc happy */
7347 return 0;
7348 case P2P_PREQ_PROCESSED:
7349 return 1;
7350 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007351}
7352
7353
7354void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7355 const u8 *sa, const u8 *bssid,
7356 u8 category, const u8 *data, size_t len, int freq)
7357{
7358 if (wpa_s->global->p2p_disabled)
7359 return;
7360 if (wpa_s->global->p2p == NULL)
7361 return;
7362
7363 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7364 freq);
7365}
7366
7367
7368void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7369{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007370 unsigned int bands;
7371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007372 if (wpa_s->global->p2p_disabled)
7373 return;
7374 if (wpa_s->global->p2p == NULL)
7375 return;
7376
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007377 bands = wpas_get_bands(wpa_s, NULL);
7378 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007379}
7380
7381
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007382static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007383{
7384 p2p_group_deinit(wpa_s->p2p_group);
7385 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007386
7387 wpa_s->ap_configured_cb = NULL;
7388 wpa_s->ap_configured_cb_ctx = NULL;
7389 wpa_s->ap_configured_cb_data = NULL;
7390 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007391}
7392
7393
7394int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7395{
Hai Shalomfdcde762020-04-02 11:19:20 -07007396 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007398 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7399 return -1;
7400
7401 return p2p_reject(wpa_s->global->p2p, addr);
7402}
7403
7404
7405/* Invite to reinvoke a persistent group */
7406int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007407 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007408 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007409 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007410{
7411 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007412 u8 *bssid = NULL;
7413 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007414 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007415 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007416 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007417
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007418 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007419 if (peer_addr)
7420 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7421 else
7422 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007423
Jouni Malinen31be0a42012-08-31 21:20:51 +03007424 wpa_s->p2p_persistent_go_freq = freq;
7425 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007426 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007427 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007428 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7429 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007430 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007431 if (ssid->mode == WPAS_MODE_P2P_GO) {
7432 role = P2P_INVITE_ROLE_GO;
7433 if (peer_addr == NULL) {
7434 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7435 "address in invitation command");
7436 return -1;
7437 }
7438 if (wpas_p2p_create_iface(wpa_s)) {
7439 if (wpas_p2p_add_group_interface(wpa_s,
7440 WPA_IF_P2P_GO) < 0) {
7441 wpa_printf(MSG_ERROR, "P2P: Failed to "
7442 "allocate a new interface for the "
7443 "group");
7444 return -1;
7445 }
7446 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007447 } else if (wpa_s->p2p_mgmt)
7448 bssid = wpa_s->parent->own_addr;
7449 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007450 bssid = wpa_s->own_addr;
7451 } else {
7452 role = P2P_INVITE_ROLE_CLIENT;
7453 peer_addr = ssid->bssid;
7454 }
7455 wpa_s->pending_invite_ssid_id = ssid->id;
7456
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007457 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007458 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007459 role == P2P_INVITE_ROLE_GO,
7460 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007461 if (res)
7462 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007464 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7465 return -1;
7466
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007467 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7468
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007469 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7470 no_pref_freq_given && pref_freq > 0 &&
7471 wpa_s->num_multichan_concurrent > 1 &&
7472 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7473 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7474 pref_freq);
7475 pref_freq = 0;
7476 }
7477
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007478 /*
7479 * Stop any find/listen operations before invitation and possibly
7480 * connection establishment.
7481 */
7482 wpas_p2p_stop_find_oper(wpa_s);
7483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007484 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007485 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007486 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007487}
7488
7489
7490/* Invite to join an active group */
7491int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7492 const u8 *peer_addr, const u8 *go_dev_addr)
7493{
7494 struct wpa_global *global = wpa_s->global;
7495 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007496 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007497 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007498 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007499 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007500 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007501 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007502
Jouni Malinen31be0a42012-08-31 21:20:51 +03007503 wpa_s->p2p_persistent_go_freq = 0;
7504 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007505 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007506 wpa_s->p2p_go_vht_center_freq2 = 0;
7507 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007508 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007510 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7511 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7512 break;
7513 }
7514 if (wpa_s == NULL) {
7515 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7516 return -1;
7517 }
7518
7519 ssid = wpa_s->current_ssid;
7520 if (ssid == NULL) {
7521 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7522 "invitation");
7523 return -1;
7524 }
7525
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007526 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007527 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007528 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007529 ssid->ssid, ssid->ssid_len);
7530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531 if (ssid->mode == WPAS_MODE_P2P_GO) {
7532 role = P2P_INVITE_ROLE_ACTIVE_GO;
7533 bssid = wpa_s->own_addr;
7534 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007535 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007536 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537 } else {
7538 role = P2P_INVITE_ROLE_CLIENT;
7539 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7540 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7541 "invite to current group");
7542 return -1;
7543 }
7544 bssid = wpa_s->bssid;
7545 if (go_dev_addr == NULL &&
7546 !is_zero_ether_addr(wpa_s->go_dev_addr))
7547 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007548 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7549 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007551 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7554 return -1;
7555
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007556 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007557 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007558 role == P2P_INVITE_ROLE_ACTIVE_GO,
7559 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007560 if (res)
7561 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007562 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007564 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007565 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007566 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007567}
7568
7569
7570void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7571{
7572 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007573 u8 go_dev_addr[ETH_ALEN];
7574 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007575 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007576 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007577 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007578
Dmitry Shmidt04949592012-07-19 12:16:46 -07007579 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7580 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007581 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007582 }
7583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007584 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007585 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007586
7587 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007588 if (!wpa_s->p2p_go_group_formation_completed &&
7589 wpa_s->global->p2p_group_formation == wpa_s) {
7590 wpa_dbg(wpa_s, MSG_DEBUG,
7591 "P2P: Marking group formation completed on client on data connection");
7592 wpa_s->p2p_go_group_formation_completed = 1;
7593 wpa_s->global->p2p_group_formation = NULL;
7594 wpa_s->p2p_in_provisioning = 0;
7595 wpa_s->p2p_in_invitation = 0;
7596 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007598 os_memset(go_dev_addr, 0, ETH_ALEN);
7599 if (ssid->bssid_set)
7600 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7601 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7602 ssid->ssid_len);
7603 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7604
7605 if (wpa_s->global->p2p_group_formation == wpa_s)
7606 wpa_s->global->p2p_group_formation = NULL;
7607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007608 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7609 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007610
7611 ip_addr[0] = '\0';
7612 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007613 int res;
7614
7615 res = os_snprintf(ip_addr, sizeof(ip_addr),
7616 " ip_addr=%u.%u.%u.%u "
7617 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7618 ip[0], ip[1], ip[2], ip[3],
7619 ip[4], ip[5], ip[6], ip[7],
7620 ip[8], ip[9], ip[10], ip[11]);
7621 if (os_snprintf_error(sizeof(ip_addr), res))
7622 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007623 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007624 }
7625
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007626 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7627 ssid->passphrase == NULL && ssid->psk_set ?
7628 ssid->psk : NULL,
7629 ssid->passphrase, go_dev_addr, persistent,
7630 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007631
7632 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007633 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7634 ssid, go_dev_addr);
7635
Hai Shalom5f92bc92019-04-18 11:54:11 -07007636 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007637}
7638
7639
7640int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7641 u32 interval1, u32 duration2, u32 interval2)
7642{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007643 int ret;
7644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007645 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7646 return -1;
7647
7648 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7649 wpa_s->current_ssid == NULL ||
7650 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7651 return -1;
7652
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007653 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7654 wpa_s->own_addr, wpa_s->assoc_freq,
7655 duration1, interval1, duration2, interval2);
7656 if (ret == 0)
7657 wpa_s->waiting_presence_resp = 1;
7658
7659 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660}
7661
7662
7663int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7664 unsigned int interval)
7665{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007666 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7667 return -1;
7668
7669 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7670}
7671
7672
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007673static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7674{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007675 if (wpa_s->current_ssid == NULL) {
7676 /*
7677 * current_ssid can be cleared when P2P client interface gets
7678 * disconnected, so assume this interface was used as P2P
7679 * client.
7680 */
7681 return 1;
7682 }
7683 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007684 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7685}
7686
7687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7689{
7690 struct wpa_supplicant *wpa_s = eloop_ctx;
7691
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007692 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007693 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7694 "disabled");
7695 return;
7696 }
7697
Dmitry Shmidt04949592012-07-19 12:16:46 -07007698 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7699 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007700 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007701}
7702
7703
7704static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7705{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007706 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007707
Dmitry Shmidt04949592012-07-19 12:16:46 -07007708 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7709 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007711 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7712 return;
7713
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007714 timeout = wpa_s->conf->p2p_group_idle;
7715 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7716 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7717 timeout = P2P_MAX_CLIENT_IDLE;
7718
7719 if (timeout == 0)
7720 return;
7721
Dmitry Shmidt04949592012-07-19 12:16:46 -07007722 if (timeout < 0) {
7723 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7724 timeout = 0; /* special client mode no-timeout */
7725 else
7726 return;
7727 }
7728
7729 if (wpa_s->p2p_in_provisioning) {
7730 /*
7731 * Use the normal group formation timeout during the
7732 * provisioning phase to avoid terminating this process too
7733 * early due to group idle timeout.
7734 */
7735 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7736 "during provisioning");
7737 return;
7738 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307739
Dmitry Shmidt04949592012-07-19 12:16:46 -07007740 if (wpa_s->show_group_started) {
7741 /*
7742 * Use the normal group formation timeout between the end of
7743 * the provisioning phase and completion of 4-way handshake to
7744 * avoid terminating this process too early due to group idle
7745 * timeout.
7746 */
7747 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7748 "while waiting for initial 4-way handshake to "
7749 "complete");
7750 return;
7751 }
7752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007753 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007754 timeout);
7755 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7756 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757}
7758
7759
Jouni Malinen2b89da82012-08-31 22:04:41 +03007760/* Returns 1 if the interface was removed */
7761int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7762 u16 reason_code, const u8 *ie, size_t ie_len,
7763 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007764{
7765 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007766 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007767
Dmitry Shmidt04949592012-07-19 12:16:46 -07007768 if (!locally_generated)
7769 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7770 ie_len);
7771
7772 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7773 wpa_s->current_ssid &&
7774 wpa_s->current_ssid->p2p_group &&
7775 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7776 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7777 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007778 if (wpas_p2p_group_delete(wpa_s,
7779 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7780 > 0)
7781 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007782 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007783
7784 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785}
7786
7787
7788void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007789 u16 reason_code, const u8 *ie, size_t ie_len,
7790 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007791{
7792 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7793 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007794
Dmitry Shmidt04949592012-07-19 12:16:46 -07007795 if (!locally_generated)
7796 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7797 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007798}
7799
7800
7801void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7802{
7803 struct p2p_data *p2p = wpa_s->global->p2p;
7804
7805 if (p2p == NULL)
7806 return;
7807
7808 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7809 return;
7810
7811 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7812 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7813
7814 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7815 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7816
7817 if (wpa_s->wps &&
7818 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7819 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7820
7821 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7822 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7823
7824 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7825 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7826 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7827 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7828 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7829 }
7830
7831 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7832 p2p_set_sec_dev_types(p2p,
7833 (void *) wpa_s->conf->sec_device_type,
7834 wpa_s->conf->num_sec_device_types);
7835
7836 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7837 int i;
7838 p2p_remove_wps_vendor_extensions(p2p);
7839 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7840 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7841 continue;
7842 p2p_add_wps_vendor_extension(
7843 p2p, wpa_s->conf->wps_vendor_ext[i]);
7844 }
7845 }
7846
7847 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7848 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7849 char country[3];
7850 country[0] = wpa_s->conf->country[0];
7851 country[1] = wpa_s->conf->country[1];
7852 country[2] = 0x04;
7853 p2p_set_country(p2p, country);
7854 }
7855
7856 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7857 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7858 wpa_s->conf->p2p_ssid_postfix ?
7859 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7860 0);
7861 }
7862
7863 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7864 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007865
7866 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7867 u8 reg_class, channel;
7868 int ret;
7869 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007870 u8 channel_forced;
7871
Jouni Malinen75ecf522011-06-27 15:19:46 -07007872 if (wpa_s->conf->p2p_listen_reg_class &&
7873 wpa_s->conf->p2p_listen_channel) {
7874 reg_class = wpa_s->conf->p2p_listen_reg_class;
7875 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007876 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007877 } else {
7878 reg_class = 81;
7879 /*
7880 * Pick one of the social channels randomly as the
7881 * listen channel.
7882 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007883 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7884 channel = 1;
7885 else
7886 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007887 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007888 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007889 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7890 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007891 if (ret)
7892 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7893 "failed: %d", ret);
7894 }
7895 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7896 u8 op_reg_class, op_channel, cfg_op_channel;
7897 int ret = 0;
7898 unsigned int r;
7899 if (wpa_s->conf->p2p_oper_reg_class &&
7900 wpa_s->conf->p2p_oper_channel) {
7901 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7902 op_channel = wpa_s->conf->p2p_oper_channel;
7903 cfg_op_channel = 1;
7904 } else {
7905 op_reg_class = 81;
7906 /*
7907 * Use random operation channel from (1, 6, 11)
7908 *if no other preference is indicated.
7909 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007910 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7911 op_channel = 1;
7912 else
7913 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007914 cfg_op_channel = 0;
7915 }
7916 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7917 cfg_op_channel);
7918 if (ret)
7919 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7920 "failed: %d", ret);
7921 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007922
7923 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7924 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7925 wpa_s->conf->p2p_pref_chan) < 0) {
7926 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7927 "update failed");
7928 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007929
7930 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7931 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7932 "update failed");
7933 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007934 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007935
7936 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7937 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007938}
7939
7940
7941int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7942 int duration)
7943{
7944 if (!wpa_s->ap_iface)
7945 return -1;
7946 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7947 duration);
7948}
7949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007950
7951int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7952{
7953 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7954 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007955
7956 wpa_s->global->cross_connection = enabled;
7957 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7958
7959 if (!enabled) {
7960 struct wpa_supplicant *iface;
7961
7962 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7963 {
7964 if (iface->cross_connect_enabled == 0)
7965 continue;
7966
7967 iface->cross_connect_enabled = 0;
7968 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007969 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007970 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7971 iface->ifname,
7972 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007973 }
7974 }
7975
7976 return 0;
7977}
7978
7979
7980static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7981{
7982 struct wpa_supplicant *iface;
7983
7984 if (!uplink->global->cross_connection)
7985 return;
7986
7987 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7988 if (!iface->cross_connect_enabled)
7989 continue;
7990 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7991 0)
7992 continue;
7993 if (iface->ap_iface == NULL)
7994 continue;
7995 if (iface->cross_connect_in_use)
7996 continue;
7997
7998 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007999 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008000 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8001 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008002 }
8003}
8004
8005
8006static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8007{
8008 struct wpa_supplicant *iface;
8009
8010 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8011 if (!iface->cross_connect_enabled)
8012 continue;
8013 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8014 0)
8015 continue;
8016 if (!iface->cross_connect_in_use)
8017 continue;
8018
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008019 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008020 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8021 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022 iface->cross_connect_in_use = 0;
8023 }
8024}
8025
8026
8027void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8028{
8029 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8030 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8031 wpa_s->cross_connect_disallowed)
8032 wpas_p2p_disable_cross_connect(wpa_s);
8033 else
8034 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008035 if (!wpa_s->ap_iface &&
8036 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8037 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008038}
8039
8040
8041void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8042{
8043 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008044 if (!wpa_s->ap_iface &&
8045 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8046 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008047 wpas_p2p_set_group_idle_timeout(wpa_s);
8048}
8049
8050
8051static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8052{
8053 struct wpa_supplicant *iface;
8054
8055 if (!wpa_s->global->cross_connection)
8056 return;
8057
8058 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8059 if (iface == wpa_s)
8060 continue;
8061 if (iface->drv_flags &
8062 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8063 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008064 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8065 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008066 continue;
8067
8068 wpa_s->cross_connect_enabled = 1;
8069 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8070 sizeof(wpa_s->cross_connect_uplink));
8071 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8072 "%s to %s whenever uplink is available",
8073 wpa_s->ifname, wpa_s->cross_connect_uplink);
8074
8075 if (iface->ap_iface || iface->current_ssid == NULL ||
8076 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8077 iface->cross_connect_disallowed ||
8078 iface->wpa_state != WPA_COMPLETED)
8079 break;
8080
8081 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008082 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008083 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8084 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008085 break;
8086 }
8087}
8088
8089
8090int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8091{
8092 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8093 !wpa_s->p2p_in_provisioning)
8094 return 0; /* not P2P client operation */
8095
8096 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8097 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008098 if (wpa_s != wpa_s->p2pdev)
8099 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008100 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008101 return 1;
8102}
8103
8104
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008105void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8106{
8107 struct wpa_supplicant *wpa_s = eloop_ctx;
8108 wpas_p2p_notif_pbc_overlap(wpa_s);
8109}
8110
8111
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008112void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8113 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008114{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008115 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008116 struct wpa_used_freq_data *freqs = NULL;
8117 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008118
8119 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8120 return;
8121
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008122 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8123 if (!freqs)
8124 return;
8125
8126 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008128 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008129 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008130 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8131 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008132 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8133 "channel list");
8134 return;
8135 }
8136
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008137 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008138
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008139 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008140
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008141 /*
8142 * The used frequencies map changed, so it is possible that a GO is
8143 * using a channel that is no longer valid for P2P use. It is also
8144 * possible that due to policy consideration, it would be preferable to
8145 * move it to a frequency already used by other station interfaces.
8146 */
8147 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8148
8149 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008150}
8151
8152
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008153static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8154 struct wpa_scan_results *scan_res)
8155{
8156 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8157}
8158
8159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008160int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8161{
8162 struct wpa_global *global = wpa_s->global;
8163 int found = 0;
8164 const u8 *peer;
8165
8166 if (global->p2p == NULL)
8167 return -1;
8168
8169 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8170
8171 if (wpa_s->pending_interface_name[0] &&
8172 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8173 found = 1;
8174
8175 peer = p2p_get_go_neg_peer(global->p2p);
8176 if (peer) {
8177 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8178 MACSTR, MAC2STR(peer));
8179 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008180 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008181 }
8182
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008183 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8184 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8185 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8186 found = 1;
8187 }
8188
8189 if (wpa_s->pending_pd_before_join) {
8190 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8191 wpa_s->pending_pd_before_join = 0;
8192 found = 1;
8193 }
8194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008195 wpas_p2p_stop_find(wpa_s);
8196
8197 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8198 if (wpa_s == global->p2p_group_formation &&
8199 (wpa_s->p2p_in_provisioning ||
8200 wpa_s->parent->pending_interface_type ==
8201 WPA_IF_P2P_CLIENT)) {
8202 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8203 "formation found - cancelling",
8204 wpa_s->ifname);
8205 found = 1;
8206 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008207 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008208 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008209 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008210 break;
8211 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008212 wpas_p2p_group_delete(wpa_s,
8213 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008214 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008215 } else if (wpa_s->p2p_in_invitation) {
8216 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8217 wpa_s->ifname);
8218 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008219 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008220 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008221 }
8222 }
8223
8224 if (!found) {
8225 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8226 return -1;
8227 }
8228
8229 return 0;
8230}
8231
8232
8233void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8234{
8235 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8236 return;
8237
8238 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8239 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008240 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008241}
8242
8243
8244void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8245 int freq_24, int freq_5, int freq_overall)
8246{
8247 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008248 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008249 return;
8250 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8251}
8252
8253
8254int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8255{
8256 u8 peer[ETH_ALEN];
8257 struct p2p_data *p2p = wpa_s->global->p2p;
8258
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008259 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008260 return -1;
8261
8262 if (hwaddr_aton(addr, peer))
8263 return -1;
8264
8265 return p2p_unauthorize(p2p, peer);
8266}
8267
8268
8269/**
8270 * wpas_p2p_disconnect - Disconnect from a P2P Group
8271 * @wpa_s: Pointer to wpa_supplicant data
8272 * Returns: 0 on success, -1 on failure
8273 *
8274 * This can be used to disconnect from a group in which the local end is a P2P
8275 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8276 * virtual network interface was created for this group, that interface will be
8277 * removed. Otherwise, only the configured P2P group network will be removed
8278 * from the interface.
8279 */
8280int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8281{
8282
8283 if (wpa_s == NULL)
8284 return -1;
8285
Jouni Malinen2b89da82012-08-31 22:04:41 +03008286 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8287 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008288}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008289
8290
8291int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8292{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008293 int ret;
8294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008295 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8296 return 0;
8297
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008298 ret = p2p_in_progress(wpa_s->global->p2p);
8299 if (ret == 0) {
8300 /*
8301 * Check whether there is an ongoing WPS provisioning step (or
8302 * other parts of group formation) on another interface since
8303 * p2p_in_progress() does not report this to avoid issues for
8304 * scans during such provisioning step.
8305 */
8306 if (wpa_s->global->p2p_group_formation &&
8307 wpa_s->global->p2p_group_formation != wpa_s) {
8308 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8309 "in group formation",
8310 wpa_s->global->p2p_group_formation->ifname);
8311 ret = 1;
8312 }
8313 }
8314
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008315 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008316 struct os_reltime now;
8317 os_get_reltime(&now);
8318 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8319 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008320 /* Wait for the first client has expired */
8321 wpa_s->global->p2p_go_wait_client.sec = 0;
8322 } else {
8323 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8324 ret = 1;
8325 }
8326 }
8327
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008328 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008329}
8330
8331
8332void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8333 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008334{
8335 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8336 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008337 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008338 /**
8339 * Remove the network by scheduling the group formation
8340 * timeout to happen immediately. The teardown code
8341 * needs to be scheduled to run asynch later so that we
8342 * don't delete data from under ourselves unexpectedly.
8343 * Calling wpas_p2p_group_formation_timeout directly
8344 * causes a series of crashes in WPS failure scenarios.
8345 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008346 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8347 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008348 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008349 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008350 }
8351}
8352
8353
8354struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008355 const u8 *addr, const u8 *ssid,
8356 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008357{
8358 struct wpa_ssid *s;
8359 size_t i;
8360
8361 for (s = wpa_s->conf->ssid; s; s = s->next) {
8362 if (s->disabled != 2)
8363 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008364 if (ssid &&
8365 (ssid_len != s->ssid_len ||
8366 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8367 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008368 if (addr == NULL) {
8369 if (s->mode == WPAS_MODE_P2P_GO)
8370 return s;
8371 continue;
8372 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008373 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8374 return s; /* peer is GO in the persistent group */
8375 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8376 continue;
8377 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008378 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008379 addr, ETH_ALEN) == 0)
8380 return s; /* peer is P2P client in persistent
8381 * group */
8382 }
8383 }
8384
8385 return NULL;
8386}
8387
8388
8389void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8390 const u8 *addr)
8391{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008392 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008393 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008394 /*
8395 * This can happen if WPS provisioning step is not terminated
8396 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8397 * peer was able to connect, there is no need to time out group
8398 * formation after this, though. In addition, this is used with
8399 * the initial connection wait on the GO as a separate formation
8400 * timeout and as such, expected to be hit after the initial WPS
8401 * provisioning step.
8402 */
8403 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008404
8405 if (!wpa_s->p2p_go_group_formation_completed &&
8406 !wpa_s->group_formation_reported) {
8407 /*
8408 * GO has not yet notified group formation success since
8409 * the WPS step was not completed cleanly. Do that
8410 * notification now since the P2P Client was able to
8411 * connect and as such, must have received the
8412 * credential from the WPS step.
8413 */
8414 if (wpa_s->global->p2p)
8415 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008416 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008417 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008418 }
8419 if (!wpa_s->p2p_go_group_formation_completed) {
8420 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8421 wpa_s->p2p_go_group_formation_completed = 1;
8422 wpa_s->global->p2p_group_formation = NULL;
8423 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008424 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008425 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008426 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008427 if (addr == NULL)
8428 return;
8429 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8430}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008431
Dmitry Shmidt04949592012-07-19 12:16:46 -07008432
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008433static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8434 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008435{
8436 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008437 int ret = 0;
8438
Dmitry Shmidt04949592012-07-19 12:16:46 -07008439 if (wpa_s->global->p2p_group_formation)
8440 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008441 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008442 offchannel_send_action_done(wpa_s);
8443 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008444 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008445 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8446 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8447 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8448 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008449 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008450 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008451 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008452 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008453 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008454 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008455 wpa_s->p2p_go_he,
8456 wpa_s->p2p_go_edmg,
8457 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008458 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008459}
8460
8461
8462int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8463{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008464 int res;
8465
Dmitry Shmidt04949592012-07-19 12:16:46 -07008466 if (!wpa_s->p2p_fallback_to_go_neg ||
8467 wpa_s->p2p_in_provisioning <= 5)
8468 return 0;
8469
8470 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8471 return 0; /* peer operating as a GO */
8472
8473 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8474 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008475 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008476 "reason=GO-not-found");
8477 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008478
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008479 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008480}
8481
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008482
8483unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8484{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008485 struct wpa_supplicant *ifs;
8486
8487 if (wpa_s->wpa_state > WPA_SCANNING) {
8488 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8489 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008490 wpa_s->conf->p2p_search_delay);
8491 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008492 }
8493
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008494 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8495 radio_list) {
8496 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008497 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8498 "delay due to concurrent operation on "
8499 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008500 wpa_s->conf->p2p_search_delay,
8501 ifs->ifname);
8502 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008503 }
8504 }
8505
8506 return 0;
8507}
8508
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008509
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008510static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8511 struct wpa_ssid *s, const u8 *addr,
8512 int iface_addr)
8513{
8514 struct psk_list_entry *psk, *tmp;
8515 int changed = 0;
8516
8517 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8518 list) {
8519 if ((iface_addr && !psk->p2p &&
8520 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8521 (!iface_addr && psk->p2p &&
8522 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8523 wpa_dbg(wpa_s, MSG_DEBUG,
8524 "P2P: Remove persistent group PSK list entry for "
8525 MACSTR " p2p=%u",
8526 MAC2STR(psk->addr), psk->p2p);
8527 dl_list_del(&psk->list);
8528 os_free(psk);
8529 changed++;
8530 }
8531 }
8532
8533 return changed;
8534}
8535
8536
8537void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8538 const u8 *p2p_dev_addr,
8539 const u8 *psk, size_t psk_len)
8540{
8541 struct wpa_ssid *ssid = wpa_s->current_ssid;
8542 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008543 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008544
8545 if (psk_len != sizeof(p->psk))
8546 return;
8547
8548 if (p2p_dev_addr) {
8549 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8550 " p2p_dev_addr=" MACSTR,
8551 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8552 if (is_zero_ether_addr(p2p_dev_addr))
8553 p2p_dev_addr = NULL;
8554 } else {
8555 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8556 MAC2STR(mac_addr));
8557 }
8558
8559 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8560 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8561 /* To be added to persistent group once created */
8562 if (wpa_s->global->add_psk == NULL) {
8563 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8564 if (wpa_s->global->add_psk == NULL)
8565 return;
8566 }
8567 p = wpa_s->global->add_psk;
8568 if (p2p_dev_addr) {
8569 p->p2p = 1;
8570 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8571 } else {
8572 p->p2p = 0;
8573 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8574 }
8575 os_memcpy(p->psk, psk, psk_len);
8576 return;
8577 }
8578
8579 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8580 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8581 return;
8582 }
8583
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008584 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008585 ssid->ssid_len);
8586 if (!persistent) {
8587 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8588 return;
8589 }
8590
8591 p = os_zalloc(sizeof(*p));
8592 if (p == NULL)
8593 return;
8594 if (p2p_dev_addr) {
8595 p->p2p = 1;
8596 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8597 } else {
8598 p->p2p = 0;
8599 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8600 }
8601 os_memcpy(p->psk, psk, psk_len);
8602
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008603 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8604 (last = dl_list_last(&persistent->psk_list,
8605 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008606 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8607 MACSTR " (p2p=%u) to make room for a new one",
8608 MAC2STR(last->addr), last->p2p);
8609 dl_list_del(&last->list);
8610 os_free(last);
8611 }
8612
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008613 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008614 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8615 p2p_dev_addr == NULL);
8616 if (p2p_dev_addr) {
8617 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8618 MACSTR, MAC2STR(p2p_dev_addr));
8619 } else {
8620 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8621 MAC2STR(mac_addr));
8622 }
8623 dl_list_add(&persistent->psk_list, &p->list);
8624
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008625 if (wpa_s->p2pdev->conf->update_config &&
8626 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008627 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008628}
8629
8630
8631static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8632 struct wpa_ssid *s, const u8 *addr,
8633 int iface_addr)
8634{
8635 int res;
8636
8637 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008638 if (res > 0 && wpa_s->conf->update_config &&
8639 wpa_config_write(wpa_s->confname, wpa_s->conf))
8640 wpa_dbg(wpa_s, MSG_DEBUG,
8641 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008642}
8643
8644
8645static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8646 const u8 *peer, int iface_addr)
8647{
8648 struct hostapd_data *hapd;
8649 struct hostapd_wpa_psk *psk, *prev, *rem;
8650 struct sta_info *sta;
8651
8652 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8653 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8654 return;
8655
8656 /* Remove per-station PSK entry */
8657 hapd = wpa_s->ap_iface->bss[0];
8658 prev = NULL;
8659 psk = hapd->conf->ssid.wpa_psk;
8660 while (psk) {
8661 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8662 (!iface_addr &&
8663 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8664 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8665 MACSTR " iface_addr=%d",
8666 MAC2STR(peer), iface_addr);
8667 if (prev)
8668 prev->next = psk->next;
8669 else
8670 hapd->conf->ssid.wpa_psk = psk->next;
8671 rem = psk;
8672 psk = psk->next;
8673 os_free(rem);
8674 } else {
8675 prev = psk;
8676 psk = psk->next;
8677 }
8678 }
8679
8680 /* Disconnect from group */
8681 if (iface_addr)
8682 sta = ap_get_sta(hapd, peer);
8683 else
8684 sta = ap_get_sta_p2p(hapd, peer);
8685 if (sta) {
8686 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8687 " (iface_addr=%d) from group",
8688 MAC2STR(peer), iface_addr);
8689 hostapd_drv_sta_deauth(hapd, sta->addr,
8690 WLAN_REASON_DEAUTH_LEAVING);
8691 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8692 }
8693}
8694
8695
8696void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8697 int iface_addr)
8698{
8699 struct wpa_ssid *s;
8700 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008701 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008702
8703 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8704
8705 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008706 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008707 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8708 continue;
8709 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008710 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8711 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008712 }
8713
8714 /* Remove from any operating group */
8715 for (w = wpa_s->global->ifaces; w; w = w->next)
8716 wpas_p2p_remove_client_go(w, peer, iface_addr);
8717}
8718
8719
8720static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8721{
8722 struct wpa_supplicant *wpa_s = eloop_ctx;
8723 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8724}
8725
8726
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008727static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8728{
8729 struct wpa_supplicant *wpa_s = eloop_ctx;
8730
8731 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8732 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8733}
8734
8735
8736int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8737 struct wpa_ssid *ssid)
8738{
8739 struct wpa_supplicant *iface;
8740
8741 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8742 if (!iface->current_ssid ||
8743 iface->current_ssid->frequency == freq ||
8744 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8745 !iface->current_ssid->p2p_group))
8746 continue;
8747
8748 /* Remove the connection with least priority */
8749 if (!wpas_is_p2p_prioritized(iface)) {
8750 /* STA connection has priority over existing
8751 * P2P connection, so remove the interface. */
8752 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8753 eloop_register_timeout(0, 0,
8754 wpas_p2p_group_freq_conflict,
8755 iface, NULL);
8756 /* If connection in progress is P2P connection, do not
8757 * proceed for the connection. */
8758 if (wpa_s == iface)
8759 return -1;
8760 else
8761 return 0;
8762 } else {
8763 /* P2P connection has priority, disable the STA network
8764 */
8765 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8766 ssid);
8767 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8768 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8769 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8770 ETH_ALEN);
8771 /* If P2P connection is in progress, continue
8772 * connecting...*/
8773 if (wpa_s == iface)
8774 return 0;
8775 else
8776 return -1;
8777 }
8778 }
8779
8780 return 0;
8781}
8782
8783
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008784int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8785{
8786 struct wpa_ssid *ssid = wpa_s->current_ssid;
8787
8788 if (ssid == NULL || !ssid->p2p_group)
8789 return 0;
8790
8791 if (wpa_s->p2p_last_4way_hs_fail &&
8792 wpa_s->p2p_last_4way_hs_fail == ssid) {
8793 u8 go_dev_addr[ETH_ALEN];
8794 struct wpa_ssid *persistent;
8795
8796 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8797 ssid->ssid,
8798 ssid->ssid_len) <= 0) {
8799 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8800 goto disconnect;
8801 }
8802
8803 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8804 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008805 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008806 ssid->ssid,
8807 ssid->ssid_len);
8808 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8809 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8810 goto disconnect;
8811 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008812 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008813 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8814 persistent->id);
8815 disconnect:
8816 wpa_s->p2p_last_4way_hs_fail = NULL;
8817 /*
8818 * Remove the group from a timeout to avoid issues with caller
8819 * continuing to use the interface if this is on a P2P group
8820 * interface.
8821 */
8822 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8823 wpa_s, NULL);
8824 return 1;
8825 }
8826
8827 wpa_s->p2p_last_4way_hs_fail = ssid;
8828 return 0;
8829}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008830
8831
8832#ifdef CONFIG_WPS_NFC
8833
8834static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8835 struct wpabuf *p2p)
8836{
8837 struct wpabuf *ret;
8838 size_t wsc_len;
8839
8840 if (p2p == NULL) {
8841 wpabuf_free(wsc);
8842 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8843 return NULL;
8844 }
8845
8846 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8847 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8848 if (ret == NULL) {
8849 wpabuf_free(wsc);
8850 wpabuf_free(p2p);
8851 return NULL;
8852 }
8853
8854 wpabuf_put_be16(ret, wsc_len);
8855 if (wsc)
8856 wpabuf_put_buf(ret, wsc);
8857 wpabuf_put_be16(ret, wpabuf_len(p2p));
8858 wpabuf_put_buf(ret, p2p);
8859
8860 wpabuf_free(wsc);
8861 wpabuf_free(p2p);
8862 wpa_hexdump_buf(MSG_DEBUG,
8863 "P2P: Generated NFC connection handover message", ret);
8864
8865 if (ndef && ret) {
8866 struct wpabuf *tmp;
8867 tmp = ndef_build_p2p(ret);
8868 wpabuf_free(ret);
8869 if (tmp == NULL) {
8870 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8871 return NULL;
8872 }
8873 ret = tmp;
8874 }
8875
8876 return ret;
8877}
8878
8879
8880static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8881 struct wpa_ssid **ssid, u8 *go_dev_addr)
8882{
8883 struct wpa_supplicant *iface;
8884
8885 if (go_dev_addr)
8886 os_memset(go_dev_addr, 0, ETH_ALEN);
8887 if (ssid)
8888 *ssid = NULL;
8889 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8890 if (iface->wpa_state < WPA_ASSOCIATING ||
8891 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8892 !iface->current_ssid->p2p_group ||
8893 iface->current_ssid->mode != WPAS_MODE_INFRA)
8894 continue;
8895 if (ssid)
8896 *ssid = iface->current_ssid;
8897 if (go_dev_addr)
8898 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8899 return iface->assoc_freq;
8900 }
8901 return 0;
8902}
8903
8904
8905struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8906 int ndef)
8907{
8908 struct wpabuf *wsc, *p2p;
8909 struct wpa_ssid *ssid;
8910 u8 go_dev_addr[ETH_ALEN];
8911 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8912
8913 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8914 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8915 return NULL;
8916 }
8917
8918 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8919 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8920 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8921 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8922 return NULL;
8923 }
8924
8925 if (cli_freq == 0) {
8926 wsc = wps_build_nfc_handover_req_p2p(
8927 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8928 } else
8929 wsc = NULL;
8930 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8931 go_dev_addr, ssid ? ssid->ssid : NULL,
8932 ssid ? ssid->ssid_len : 0);
8933
8934 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8935}
8936
8937
8938struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8939 int ndef, int tag)
8940{
8941 struct wpabuf *wsc, *p2p;
8942 struct wpa_ssid *ssid;
8943 u8 go_dev_addr[ETH_ALEN];
8944 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8945
8946 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8947 return NULL;
8948
8949 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8950 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8951 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8952 return NULL;
8953
8954 if (cli_freq == 0) {
8955 wsc = wps_build_nfc_handover_sel_p2p(
8956 wpa_s->parent->wps,
8957 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8958 DEV_PW_NFC_CONNECTION_HANDOVER,
8959 wpa_s->conf->wps_nfc_dh_pubkey,
8960 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8961 } else
8962 wsc = NULL;
8963 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8964 go_dev_addr, ssid ? ssid->ssid : NULL,
8965 ssid ? ssid->ssid_len : 0);
8966
8967 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8968}
8969
8970
8971static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8972 struct p2p_nfc_params *params)
8973{
8974 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8975 "connection handover (freq=%d)",
8976 params->go_freq);
8977
8978 if (params->go_freq && params->go_ssid_len) {
8979 wpa_s->p2p_wps_method = WPS_NFC;
8980 wpa_s->pending_join_wps_method = WPS_NFC;
8981 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8982 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8983 ETH_ALEN);
8984 return wpas_p2p_join_start(wpa_s, params->go_freq,
8985 params->go_ssid,
8986 params->go_ssid_len);
8987 }
8988
8989 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8990 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008991 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008992 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008993 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008994 params->go_ssid_len ? params->go_ssid : NULL,
8995 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008996}
8997
8998
8999static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9000 struct p2p_nfc_params *params, int tag)
9001{
9002 int res, persistent;
9003 struct wpa_ssid *ssid;
9004
9005 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9006 "connection handover");
9007 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9008 ssid = wpa_s->current_ssid;
9009 if (ssid == NULL)
9010 continue;
9011 if (ssid->mode != WPAS_MODE_P2P_GO)
9012 continue;
9013 if (wpa_s->ap_iface == NULL)
9014 continue;
9015 break;
9016 }
9017 if (wpa_s == NULL) {
9018 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9019 return -1;
9020 }
9021
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009022 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009023 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009024 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009025 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9026 return -1;
9027 }
9028 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009029 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9030 wpa_s->p2pdev->p2p_oob_dev_pw,
9031 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9032 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009033 if (res)
9034 return res;
9035
9036 if (!tag) {
9037 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9038 return 0;
9039 }
9040
9041 if (!params->peer ||
9042 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9043 return 0;
9044
9045 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9046 " to join", MAC2STR(params->peer->p2p_device_addr));
9047
9048 wpa_s->global->p2p_invite_group = wpa_s;
9049 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009050 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009051 params->peer->p2p_device_addr,
9052 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009053 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009054
9055 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9056 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9057 ssid->ssid, ssid->ssid_len, ssid->frequency,
9058 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009059 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009060}
9061
9062
9063static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9064 struct p2p_nfc_params *params,
9065 int forced_freq)
9066{
9067 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9068 "connection handover");
9069 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9070 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009071 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009072 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009073 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9074 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009075}
9076
9077
9078static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9079 struct p2p_nfc_params *params,
9080 int forced_freq)
9081{
9082 int res;
9083
9084 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9085 "connection handover");
9086 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9087 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009088 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009089 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009090 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9091 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009092 if (res)
9093 return res;
9094
9095 res = wpas_p2p_listen(wpa_s, 60);
9096 if (res) {
9097 p2p_unauthorize(wpa_s->global->p2p,
9098 params->peer->p2p_device_addr);
9099 }
9100
9101 return res;
9102}
9103
9104
9105static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9106 const struct wpabuf *data,
9107 int sel, int tag, int forced_freq)
9108{
9109 const u8 *pos, *end;
9110 u16 len, id;
9111 struct p2p_nfc_params params;
9112 int res;
9113
9114 os_memset(&params, 0, sizeof(params));
9115 params.sel = sel;
9116
9117 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9118
9119 pos = wpabuf_head(data);
9120 end = pos + wpabuf_len(data);
9121
9122 if (end - pos < 2) {
9123 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9124 "attributes");
9125 return -1;
9126 }
9127 len = WPA_GET_BE16(pos);
9128 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009129 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009130 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9131 "attributes");
9132 return -1;
9133 }
9134 params.wsc_attr = pos;
9135 params.wsc_len = len;
9136 pos += len;
9137
9138 if (end - pos < 2) {
9139 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9140 "attributes");
9141 return -1;
9142 }
9143 len = WPA_GET_BE16(pos);
9144 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009145 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009146 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9147 "attributes");
9148 return -1;
9149 }
9150 params.p2p_attr = pos;
9151 params.p2p_len = len;
9152 pos += len;
9153
9154 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9155 params.wsc_attr, params.wsc_len);
9156 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9157 params.p2p_attr, params.p2p_len);
9158 if (pos < end) {
9159 wpa_hexdump(MSG_DEBUG,
9160 "P2P: Ignored extra data after P2P attributes",
9161 pos, end - pos);
9162 }
9163
9164 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9165 if (res)
9166 return res;
9167
9168 if (params.next_step == NO_ACTION)
9169 return 0;
9170
9171 if (params.next_step == BOTH_GO) {
9172 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9173 MAC2STR(params.peer->p2p_device_addr));
9174 return 0;
9175 }
9176
9177 if (params.next_step == PEER_CLIENT) {
9178 if (!is_zero_ether_addr(params.go_dev_addr)) {
9179 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9180 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9181 " ssid=\"%s\"",
9182 MAC2STR(params.peer->p2p_device_addr),
9183 params.go_freq,
9184 MAC2STR(params.go_dev_addr),
9185 wpa_ssid_txt(params.go_ssid,
9186 params.go_ssid_len));
9187 } else {
9188 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9189 "peer=" MACSTR " freq=%d",
9190 MAC2STR(params.peer->p2p_device_addr),
9191 params.go_freq);
9192 }
9193 return 0;
9194 }
9195
9196 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9197 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9198 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9199 return 0;
9200 }
9201
9202 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9203 wpa_s->p2p_oob_dev_pw = NULL;
9204
9205 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9206 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9207 "received");
9208 return -1;
9209 }
9210
9211 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9212 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9213 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9214 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9215 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9216 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9217 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9218
9219 if (tag) {
9220 if (id < 0x10) {
9221 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9222 "peer OOB Device Password Id %u", id);
9223 return -1;
9224 }
9225 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9226 "Device Password Id %u", id);
9227 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9228 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9229 params.oob_dev_pw_len -
9230 WPS_OOB_PUBKEY_HASH_LEN - 2);
9231 wpa_s->p2p_oob_dev_pw_id = id;
9232 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9233 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9234 params.oob_dev_pw_len -
9235 WPS_OOB_PUBKEY_HASH_LEN - 2);
9236 if (wpa_s->p2p_oob_dev_pw == NULL)
9237 return -1;
9238
9239 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9240 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9241 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9242 return -1;
9243 } else {
9244 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9245 "without Device Password");
9246 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9247 }
9248
9249 switch (params.next_step) {
9250 case NO_ACTION:
9251 case BOTH_GO:
9252 case PEER_CLIENT:
9253 /* already covered above */
9254 return 0;
9255 case JOIN_GROUP:
9256 return wpas_p2p_nfc_join_group(wpa_s, &params);
9257 case AUTH_JOIN:
9258 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9259 case INIT_GO_NEG:
9260 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9261 case RESP_GO_NEG:
9262 /* TODO: use own OOB Dev Pw */
9263 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9264 }
9265
9266 return -1;
9267}
9268
9269
9270int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9271 const struct wpabuf *data, int forced_freq)
9272{
9273 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9274 return -1;
9275
9276 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9277}
9278
9279
9280int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9281 const struct wpabuf *req,
9282 const struct wpabuf *sel, int forced_freq)
9283{
9284 struct wpabuf *tmp;
9285 int ret;
9286
9287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9288 return -1;
9289
9290 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9291
9292 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9293 wpabuf_head(req), wpabuf_len(req));
9294 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9295 wpabuf_head(sel), wpabuf_len(sel));
9296 if (forced_freq)
9297 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9298 tmp = ndef_parse_p2p(init ? sel : req);
9299 if (tmp == NULL) {
9300 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9301 return -1;
9302 }
9303
9304 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9305 forced_freq);
9306 wpabuf_free(tmp);
9307
9308 return ret;
9309}
9310
9311
9312int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9313{
9314 const u8 *if_addr;
9315 int go_intent = wpa_s->conf->p2p_go_intent;
9316 struct wpa_supplicant *iface;
9317
9318 if (wpa_s->global->p2p == NULL)
9319 return -1;
9320
9321 if (!enabled) {
9322 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9323 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9324 {
9325 if (!iface->ap_iface)
9326 continue;
9327 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9328 }
9329 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9330 0, NULL);
9331 if (wpa_s->p2p_nfc_tag_enabled)
9332 wpas_p2p_remove_pending_group_interface(wpa_s);
9333 wpa_s->p2p_nfc_tag_enabled = 0;
9334 return 0;
9335 }
9336
9337 if (wpa_s->global->p2p_disabled)
9338 return -1;
9339
9340 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9341 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9342 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9343 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9344 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9345 "to allow static handover cases");
9346 return -1;
9347 }
9348
9349 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9350
9351 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9352 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9353 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9354 if (wpa_s->p2p_oob_dev_pw == NULL)
9355 return -1;
9356 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9357
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009358 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9359 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9360 /*
9361 * P2P Group Interface present and the command came on group
9362 * interface, so enable the token for the current interface.
9363 */
9364 wpa_s->create_p2p_iface = 0;
9365 } else {
9366 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9367 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009368
9369 if (wpa_s->create_p2p_iface) {
9370 enum wpa_driver_if_type iftype;
9371 /* Prepare to add a new interface for the group */
9372 iftype = WPA_IF_P2P_GROUP;
9373 if (go_intent == 15)
9374 iftype = WPA_IF_P2P_GO;
9375 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9376 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9377 "interface for the group");
9378 return -1;
9379 }
9380
9381 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009382 } else if (wpa_s->p2p_mgmt)
9383 if_addr = wpa_s->parent->own_addr;
9384 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009385 if_addr = wpa_s->own_addr;
9386
9387 wpa_s->p2p_nfc_tag_enabled = enabled;
9388
9389 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9390 struct hostapd_data *hapd;
9391 if (iface->ap_iface == NULL)
9392 continue;
9393 hapd = iface->ap_iface->bss[0];
9394 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9395 hapd->conf->wps_nfc_dh_pubkey =
9396 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9397 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9398 hapd->conf->wps_nfc_dh_privkey =
9399 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9400 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9401 hapd->conf->wps_nfc_dev_pw =
9402 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9403 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9404
9405 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9406 wpa_dbg(iface, MSG_DEBUG,
9407 "P2P: Failed to enable NFC Tag for GO");
9408 }
9409 }
9410 p2p_set_authorized_oob_dev_pw_id(
9411 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9412 if_addr);
9413
9414 return 0;
9415}
9416
9417#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009418
9419
9420static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9421 struct wpa_used_freq_data *freqs,
9422 unsigned int num)
9423{
9424 u8 curr_chan, cand, chan;
9425 unsigned int i;
9426
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009427 /*
9428 * If possible, optimize the Listen channel to be a channel that is
9429 * already used by one of the other interfaces.
9430 */
9431 if (!wpa_s->conf->p2p_optimize_listen_chan)
9432 return;
9433
9434 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9435 return;
9436
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009437 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9438 for (i = 0, cand = 0; i < num; i++) {
9439 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9440 if (curr_chan == chan) {
9441 cand = 0;
9442 break;
9443 }
9444
9445 if (chan == 1 || chan == 6 || chan == 11)
9446 cand = chan;
9447 }
9448
9449 if (cand) {
9450 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009451 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009452 cand);
9453 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9454 }
9455}
9456
9457
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009458static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009459{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009460 struct hostapd_config *conf;
9461 struct p2p_go_neg_results params;
9462 struct csa_settings csa_settings;
9463 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9464 int old_freq = current_ssid->frequency;
9465 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009466
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009467 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9468 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9469 return -1;
9470 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009471
9472 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009473 * TODO: This function may not always work correctly. For example,
9474 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009475 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009476 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9477 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009478 wpa_dbg(wpa_s, MSG_DEBUG,
9479 "P2P CSA: Failed to select new frequency for GO");
9480 return -1;
9481 }
9482
9483 if (current_ssid->frequency == params.freq) {
9484 wpa_dbg(wpa_s, MSG_DEBUG,
9485 "P2P CSA: Selected same frequency - not moving GO");
9486 return 0;
9487 }
9488
9489 conf = hostapd_config_defaults();
9490 if (!conf) {
9491 wpa_dbg(wpa_s, MSG_DEBUG,
9492 "P2P CSA: Failed to allocate default config");
9493 return -1;
9494 }
9495
9496 current_ssid->frequency = params.freq;
9497 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9498 wpa_dbg(wpa_s, MSG_DEBUG,
9499 "P2P CSA: Failed to create new GO config");
9500 ret = -1;
9501 goto out;
9502 }
9503
9504 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9505 wpa_dbg(wpa_s, MSG_DEBUG,
9506 "P2P CSA: CSA to a different band is not supported");
9507 ret = -1;
9508 goto out;
9509 }
9510
9511 os_memset(&csa_settings, 0, sizeof(csa_settings));
9512 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9513 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9514 csa_settings.freq_params.freq = params.freq;
9515 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9516 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9517 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9518
9519 if (conf->ieee80211ac) {
9520 int freq1 = 0, freq2 = 0;
9521 u8 chan, opclass;
9522
9523 if (ieee80211_freq_to_channel_ext(params.freq,
9524 conf->secondary_channel,
9525 conf->vht_oper_chwidth,
9526 &opclass, &chan) ==
9527 NUM_HOSTAPD_MODES) {
9528 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9529 ret = -1;
9530 goto out;
9531 }
9532
9533 if (conf->vht_oper_centr_freq_seg0_idx)
9534 freq1 = ieee80211_chan_to_freq(
9535 NULL, opclass,
9536 conf->vht_oper_centr_freq_seg0_idx);
9537
9538 if (conf->vht_oper_centr_freq_seg1_idx)
9539 freq2 = ieee80211_chan_to_freq(
9540 NULL, opclass,
9541 conf->vht_oper_centr_freq_seg1_idx);
9542
9543 if (freq1 < 0 || freq2 < 0) {
9544 wpa_dbg(wpa_s, MSG_DEBUG,
9545 "P2P CSA: Selected invalid VHT center freqs");
9546 ret = -1;
9547 goto out;
9548 }
9549
9550 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9551 csa_settings.freq_params.center_freq1 = freq1;
9552 csa_settings.freq_params.center_freq2 = freq2;
9553
9554 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009555 case CHANWIDTH_80MHZ:
9556 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009557 csa_settings.freq_params.bandwidth = 80;
9558 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009559 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009560 csa_settings.freq_params.bandwidth = 160;
9561 break;
9562 }
9563 }
9564
9565 ret = ap_switch_channel(wpa_s, &csa_settings);
9566out:
9567 current_ssid->frequency = old_freq;
9568 hostapd_config_free(conf);
9569 return ret;
9570}
9571
9572
9573static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9574{
9575 struct p2p_go_neg_results params;
9576 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009577 void (*ap_configured_cb)(void *ctx, void *data);
9578 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009579
9580 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9581
9582 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9583 current_ssid->frequency);
9584
9585 /* Stop the AP functionality */
9586 /* TODO: Should do this in a way that does not indicated to possible
9587 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009588 /* If this action occurs before a group is started, the callback should
9589 * be preserved, or GROUP-STARTED event would be lost. If this action
9590 * occurs after a group is started, these pointers are all NULL and
9591 * harmless. */
9592 ap_configured_cb = wpa_s->ap_configured_cb;
9593 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9594 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009595 wpa_supplicant_ap_deinit(wpa_s);
9596
9597 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009598 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9599 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009600 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9601 wpas_p2p_group_delete(wpa_s,
9602 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9603 return;
9604 }
9605 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9606 params.freq);
9607
9608 if (params.freq &&
9609 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9610 wpa_printf(MSG_DEBUG,
9611 "P2P: Selected freq (%u MHz) is not valid for P2P",
9612 params.freq);
9613 wpas_p2p_group_delete(wpa_s,
9614 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9615 return;
9616 }
9617
Hai Shalom899fcc72020-10-19 14:38:18 -07009618 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009619 wpa_s->ap_configured_cb = ap_configured_cb;
9620 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9621 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009622
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009623 /* Update the frequency */
9624 current_ssid->frequency = params.freq;
9625 wpa_s->connect_without_scan = current_ssid;
9626 wpa_s->reassociate = 1;
9627 wpa_s->disconnected = 0;
9628 wpa_supplicant_req_scan(wpa_s, 0, 0);
9629}
9630
9631
9632static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9633{
9634 struct wpa_supplicant *wpa_s = eloop_ctx;
9635
9636 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009637 return;
9638
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009639 wpas_p2p_go_update_common_freqs(wpa_s);
9640
9641 /* Do not move GO in the middle of a CSA */
9642 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9643 wpa_printf(MSG_DEBUG,
9644 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009645 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009646 }
9647
9648 /*
9649 * First, try a channel switch flow. If it is not supported or fails,
9650 * take down the GO and bring it up again.
9651 */
9652 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9653 wpas_p2p_move_go_no_csa(wpa_s);
9654}
9655
9656
9657static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9658{
9659 struct wpa_supplicant *wpa_s = eloop_ctx;
9660 struct wpa_used_freq_data *freqs = NULL;
9661 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009662
9663 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9664 if (!freqs)
9665 return;
9666
9667 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9668
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009669 /* Previous attempt to move a GO was not possible -- try again. */
9670 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9671 WPAS_P2P_CHANNEL_UPDATE_ANY);
9672
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009673 os_free(freqs);
9674}
9675
9676
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009677/*
9678 * Consider moving a GO from its currently used frequency:
9679 * 1. It is possible that due to regulatory consideration the frequency
9680 * can no longer be used and there is a need to evacuate the GO.
9681 * 2. It is possible that due to MCC considerations, it would be preferable
9682 * to move the GO to a channel that is currently used by some other
9683 * station interface.
9684 *
9685 * In case a frequency that became invalid is once again valid, cancel a
9686 * previously initiated GO frequency change.
9687 */
9688static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9689 struct wpa_used_freq_data *freqs,
9690 unsigned int num)
9691{
9692 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9693 unsigned int timeout;
9694 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009695 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009696
9697 wpas_p2p_go_update_common_freqs(wpa_s);
9698
9699 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009700 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009701 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009702 for (i = 0, invalid_freq = 0; i < num; i++) {
9703 if (freqs[i].freq == freq) {
9704 flags = freqs[i].flags;
9705
9706 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009707 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9708 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009709 wpa_dbg(wpa_s, MSG_DEBUG,
9710 "P2P: Freq=%d MHz no longer valid for GO",
9711 freq);
9712 invalid_freq = 1;
9713 }
9714 } else if (freqs[i].flags == 0) {
9715 /* Freq is not used by any other station interface */
9716 continue;
9717 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009718 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009719 /* Freq is not valid for P2P use cases */
9720 continue;
9721 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9722 P2P_GO_FREQ_MOVE_SCM) {
9723 policy_move = 1;
9724 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9725 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9726 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9727 policy_move = 1;
9728 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9729 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9730 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9731 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9732 policy_move = 1;
9733 } else if ((wpa_s->drv_flags &
9734 WPA_DRIVER_FLAGS_AP_CSA) &&
9735 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9736 u8 chan;
9737
9738 /*
9739 * We do not support CSA between bands, so move
9740 * GO only within the same band.
9741 */
9742 if (wpa_s->ap_iface->current_mode->mode ==
9743 ieee80211_freq_to_chan(freqs[i].freq,
9744 &chan))
9745 policy_move = 1;
9746 }
9747 }
9748 }
9749
9750 wpa_dbg(wpa_s, MSG_DEBUG,
9751 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9752 invalid_freq, policy_move, flags);
9753
9754 /*
9755 * The channel is valid, or we are going to have a policy move, so
9756 * cancel timeout.
9757 */
9758 if (!invalid_freq || policy_move) {
9759 wpa_dbg(wpa_s, MSG_DEBUG,
9760 "P2P: Cancel a GO move from freq=%d MHz", freq);
9761 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9762
9763 if (wpas_p2p_in_progress(wpa_s)) {
9764 wpa_dbg(wpa_s, MSG_DEBUG,
9765 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9766 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9767 wpa_s, NULL);
9768 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9769 wpas_p2p_reconsider_moving_go,
9770 wpa_s, NULL);
9771 return;
9772 }
9773 }
9774
9775 if (!invalid_freq && (!policy_move || flags != 0)) {
9776 wpa_dbg(wpa_s, MSG_DEBUG,
9777 "P2P: Not initiating a GO frequency change");
9778 return;
9779 }
9780
9781 /*
9782 * Do not consider moving GO if it is in the middle of a CSA. When the
9783 * CSA is finished this flow should be retriggered.
9784 */
9785 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9786 wpa_dbg(wpa_s, MSG_DEBUG,
9787 "P2P: Not initiating a GO frequency change - CSA is in progress");
9788 return;
9789 }
9790
9791 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9792 timeout = P2P_GO_FREQ_CHANGE_TIME;
9793 else
9794 timeout = 0;
9795
9796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9797 freq, timeout);
9798 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9799 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9800}
9801
9802
9803static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9804 struct wpa_used_freq_data *freqs,
9805 unsigned int num,
9806 enum wpas_p2p_channel_update_trig trig)
9807{
9808 struct wpa_supplicant *ifs;
9809
9810 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9811 NULL);
9812
9813 /*
9814 * Travers all the radio interfaces, and for each GO interface, check
9815 * if there is a need to move the GO from the frequency it is using,
9816 * or in case the frequency is valid again, cancel the evacuation flow.
9817 */
9818 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9819 radio_list) {
9820 if (ifs->current_ssid == NULL ||
9821 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9822 continue;
9823
9824 /*
9825 * The GO was just started or completed channel switch, no need
9826 * to move it.
9827 */
9828 if (wpa_s == ifs &&
9829 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9830 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9831 wpa_dbg(wpa_s, MSG_DEBUG,
9832 "P2P: GO move - schedule re-consideration");
9833 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9834 wpas_p2p_reconsider_moving_go,
9835 wpa_s, NULL);
9836 continue;
9837 }
9838
9839 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9840 }
9841}
9842
9843
9844void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9845{
9846 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9847 return;
9848
9849 wpas_p2p_update_channel_list(wpa_s,
9850 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9851}
9852
9853
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009854void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9855{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009856 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9857 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9858 "the management interface is being removed");
9859 wpas_p2p_deinit_global(wpa_s->global);
9860 }
9861}
9862
9863
9864void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9865{
9866 if (wpa_s->ap_iface->bss)
9867 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9868 wpas_p2p_group_deinit(wpa_s);
9869}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009870
9871
9872int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9873 unsigned int period, unsigned int interval,
9874 unsigned int count)
9875{
9876 struct p2p_data *p2p = wpa_s->global->p2p;
9877 u8 *device_types;
9878 size_t dev_types_len;
9879 struct wpabuf *buf;
9880 int ret;
9881
9882 if (wpa_s->p2p_lo_started) {
9883 wpa_dbg(wpa_s, MSG_DEBUG,
9884 "P2P Listen offload is already started");
9885 return 0;
9886 }
9887
9888 if (wpa_s->global->p2p == NULL ||
9889 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9890 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9891 return -1;
9892 }
9893
9894 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9895 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9896 freq);
9897 return -1;
9898 }
9899
9900 /* Get device type */
9901 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9902 WPS_DEV_TYPE_LEN;
9903 device_types = os_malloc(dev_types_len);
9904 if (!device_types)
9905 return -1;
9906 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9907 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9908 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9909
9910 /* Get Probe Response IE(s) */
9911 buf = p2p_build_probe_resp_template(p2p, freq);
9912 if (!buf) {
9913 os_free(device_types);
9914 return -1;
9915 }
9916
9917 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9918 device_types, dev_types_len,
9919 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9920 if (ret < 0)
9921 wpa_dbg(wpa_s, MSG_DEBUG,
9922 "P2P: Failed to start P2P listen offload");
9923
9924 os_free(device_types);
9925 wpabuf_free(buf);
9926
9927 if (ret == 0) {
9928 wpa_s->p2p_lo_started = 1;
9929
9930 /* Stop current P2P listen if any */
9931 wpas_stop_listen(wpa_s);
9932 }
9933
9934 return ret;
9935}
9936
9937
9938int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9939{
9940 int ret;
9941
9942 if (!wpa_s->p2p_lo_started)
9943 return 0;
9944
9945 ret = wpa_drv_p2p_lo_stop(wpa_s);
9946 if (ret < 0)
9947 wpa_dbg(wpa_s, MSG_DEBUG,
9948 "P2P: Failed to stop P2P listen offload");
9949
9950 wpa_s->p2p_lo_started = 0;
9951 return ret;
9952}