blob: 643dd0de49ceb4cbf87d379b87a9b569f0d0b13d [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 Yadawad70e5f9d2021-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);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001408 } else {
1409 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 persistent = wpas_p2p_persistent_group(wpa_s,
1411 go_dev_addr,
1412 ssid->ssid,
1413 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 client = wpa_s->p2p_group_interface ==
1417 P2P_GROUP_INTERFACE_CLIENT;
1418 os_memset(go_dev_addr, 0, ETH_ALEN);
1419 }
1420
1421 wpa_s->show_group_started = 0;
1422 if (client) {
1423 /*
1424 * Indicate event only after successfully completed 4-way
1425 * handshake, i.e., when the interface is ready for data
1426 * packets.
1427 */
1428 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001430 wpas_p2p_group_started(wpa_s, 1, ssid,
1431 ssid ? ssid->frequency : 0,
1432 ssid && ssid->passphrase == NULL &&
1433 ssid->psk_set ? ssid->psk : NULL,
1434 ssid ? ssid->passphrase : NULL,
1435 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436 wpas_p2p_cross_connect_setup(wpa_s);
1437 wpas_p2p_set_group_idle_timeout(wpa_s);
1438 }
1439
1440 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001441 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1442 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001443 else {
1444 os_free(wpa_s->global->add_psk);
1445 wpa_s->global->add_psk = NULL;
1446 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001447
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001448 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001449 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001451 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452}
1453
1454
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001455struct send_action_work {
1456 unsigned int freq;
1457 u8 dst[ETH_ALEN];
1458 u8 src[ETH_ALEN];
1459 u8 bssid[ETH_ALEN];
1460 size_t len;
1461 unsigned int wait_time;
1462 u8 buf[0];
1463};
1464
1465
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001466static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1467{
1468 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1469
1470 wpa_printf(MSG_DEBUG,
1471 "P2P: Free Action frame radio work @%p (freq=%u dst="
1472 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1473 wpa_s->p2p_send_action_work, awork->freq,
1474 MAC2STR(awork->dst), MAC2STR(awork->src),
1475 MAC2STR(awork->bssid), awork->wait_time);
1476 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1477 awork->buf, awork->len);
1478 os_free(awork);
1479 wpa_s->p2p_send_action_work->ctx = NULL;
1480 radio_work_done(wpa_s->p2p_send_action_work);
1481 wpa_s->p2p_send_action_work = NULL;
1482}
1483
1484
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001485static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1486 void *timeout_ctx)
1487{
1488 struct wpa_supplicant *wpa_s = eloop_ctx;
1489
1490 if (!wpa_s->p2p_send_action_work)
1491 return;
1492
1493 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001494 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495}
1496
1497
Dmitry Shmidt03658832014-08-13 11:03:49 -07001498static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001500 if (wpa_s->p2p_send_action_work) {
1501 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001502
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001503 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001504 wpa_printf(MSG_DEBUG,
1505 "P2P: Clear Action TX work @%p (wait_time=%u)",
1506 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001507 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001508 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001509 } else {
1510 /*
1511 * In theory, this should not be needed, but number of
1512 * places in the P2P code is still using non-zero wait
1513 * time for the last Action frame in the sequence and
1514 * some of these do not call send_action_done().
1515 */
1516 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1517 wpa_s, NULL);
1518 eloop_register_timeout(
1519 0, awork->wait_time * 1000,
1520 wpas_p2p_send_action_work_timeout,
1521 wpa_s, NULL);
1522 }
1523 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001524}
1525
1526
1527static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1528 unsigned int freq,
1529 const u8 *dst, const u8 *src,
1530 const u8 *bssid,
1531 const u8 *data, size_t data_len,
1532 enum offchannel_send_action_result
1533 result)
1534{
1535 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1536
1537 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1540 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001542 switch (result) {
1543 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1544 res = P2P_SEND_ACTION_SUCCESS;
1545 break;
1546 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1547 res = P2P_SEND_ACTION_NO_ACK;
1548 break;
1549 case OFFCHANNEL_SEND_ACTION_FAILED:
1550 res = P2P_SEND_ACTION_FAILED;
1551 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001552 }
1553
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001554 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001556 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1557 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001558 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001559 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1560 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1563 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001564 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001565 P2P_EVENT_FALLBACK_TO_GO_NEG
1566 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001567 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1568 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 }
1570}
1571
1572
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001573static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1574{
1575 struct wpa_supplicant *wpa_s = work->wpa_s;
1576 struct send_action_work *awork = work->ctx;
1577
1578 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001579 if (work->started) {
1580 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1581 wpa_s, NULL);
1582 wpa_s->p2p_send_action_work = NULL;
1583 offchannel_send_action_done(wpa_s);
1584 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001585 os_free(awork);
1586 return;
1587 }
1588
1589 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1590 awork->bssid, awork->buf, awork->len,
1591 awork->wait_time,
1592 wpas_p2p_send_action_tx_status, 1) < 0) {
1593 os_free(awork);
1594 radio_work_done(work);
1595 return;
1596 }
1597 wpa_s->p2p_send_action_work = work;
1598}
1599
1600
1601static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1602 unsigned int freq, const u8 *dst,
1603 const u8 *src, const u8 *bssid, const u8 *buf,
1604 size_t len, unsigned int wait_time)
1605{
1606 struct send_action_work *awork;
1607
Hai Shalom81f62d82019-07-22 12:10:00 -07001608 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001609 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1610 return -1;
1611 }
1612
1613 awork = os_zalloc(sizeof(*awork) + len);
1614 if (awork == NULL)
1615 return -1;
1616
1617 awork->freq = freq;
1618 os_memcpy(awork->dst, dst, ETH_ALEN);
1619 os_memcpy(awork->src, src, ETH_ALEN);
1620 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1621 awork->len = len;
1622 awork->wait_time = wait_time;
1623 os_memcpy(awork->buf, buf, len);
1624
Hai Shalom81f62d82019-07-22 12:10:00 -07001625 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001626 wpas_send_action_cb, awork) < 0) {
1627 os_free(awork);
1628 return -1;
1629 }
1630
1631 return 0;
1632}
1633
1634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1636 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001637 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638{
1639 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001640 int listen_freq = -1, send_freq = -1;
1641
Hai Shalom021b0b52019-04-10 11:17:58 -07001642 if (scheduled)
1643 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001644 if (wpa_s->p2p_listen_work)
1645 listen_freq = wpa_s->p2p_listen_work->freq;
1646 if (wpa_s->p2p_send_action_work)
1647 send_freq = wpa_s->p2p_send_action_work->freq;
1648 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001649 int res;
1650
1651 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1652 listen_freq, send_freq, freq);
1653 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1654 len, wait_time);
1655 if (res == 0 && scheduled)
1656 *scheduled = 1;
1657 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001658 }
1659
1660 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001661 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1662 wait_time,
1663 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664}
1665
1666
1667static void wpas_send_action_done(void *ctx)
1668{
1669 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001670
1671 if (wpa_s->p2p_send_action_work) {
1672 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1673 wpa_s, NULL);
1674 os_free(wpa_s->p2p_send_action_work->ctx);
1675 radio_work_done(wpa_s->p2p_send_action_work);
1676 wpa_s->p2p_send_action_work = NULL;
1677 }
1678
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001679 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680}
1681
1682
1683static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1684 struct p2p_go_neg_results *params)
1685{
1686 if (wpa_s->go_params == NULL) {
1687 wpa_s->go_params = os_malloc(sizeof(*params));
1688 if (wpa_s->go_params == NULL)
1689 return -1;
1690 }
1691 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1692 return 0;
1693}
1694
1695
1696static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1697 struct p2p_go_neg_results *res)
1698{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001699 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001700 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1701 " dev_addr " MACSTR " wps_method %d",
1702 MAC2STR(res->peer_interface_addr),
1703 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1705 res->ssid, res->ssid_len);
1706 wpa_supplicant_ap_deinit(wpa_s);
1707 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001708 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001709 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001710#ifdef CONFIG_WPS_NFC
1711 } else if (res->wps_method == WPS_NFC) {
1712 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1713 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001714 wpa_s->p2pdev->p2p_oob_dev_pw,
1715 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1716 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001717 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001718 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001719 NULL,
1720 NULL, 0, 0);
1721#endif /* CONFIG_WPS_NFC */
1722 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001724 if (wpa_s->p2p_wps_method == WPS_P2PS)
1725 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1727 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1728 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1729 wpa_s->p2p_pin, 1, dev_pw_id);
1730 }
1731}
1732
1733
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001734static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1735 struct wpa_ssid *ssid)
1736{
1737 struct wpa_ssid *persistent;
1738 struct psk_list_entry *psk;
1739 struct hostapd_data *hapd;
1740
1741 if (!wpa_s->ap_iface)
1742 return;
1743
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001744 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001745 ssid->ssid_len);
1746 if (persistent == NULL)
1747 return;
1748
1749 hapd = wpa_s->ap_iface->bss[0];
1750
1751 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1752 list) {
1753 struct hostapd_wpa_psk *hpsk;
1754
1755 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1756 MACSTR " psk=%d",
1757 MAC2STR(psk->addr), psk->p2p);
1758 hpsk = os_zalloc(sizeof(*hpsk));
1759 if (hpsk == NULL)
1760 break;
1761 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1762 if (psk->p2p)
1763 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1764 else
1765 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1766 hpsk->next = hapd->conf->ssid.wpa_psk;
1767 hapd->conf->ssid.wpa_psk = hpsk;
1768 }
1769}
1770
1771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001772static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1773{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001774 char buf[20 + P2P_MAX_CHANNELS * 6];
1775 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001776 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001777 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001778
Roshan Pius3a1667e2018-07-03 15:17:14 -07001779 pos = buf;
1780 end = pos + sizeof(buf);
1781 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1782 res = os_snprintf(pos, end - pos, " %d",
1783 wpa_s->p2p_group_common_freqs[i]);
1784 if (os_snprintf_error(end - pos, res))
1785 break;
1786 pos += res;
1787 }
1788 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001789
Roshan Pius3a1667e2018-07-03 15:17:14 -07001790 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001791}
1792
1793
1794static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1795 struct p2p_go_neg_results *params)
1796{
1797 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1798
1799 wpa_s->p2p_group_common_freqs_num = 0;
1800 os_free(wpa_s->p2p_group_common_freqs);
1801 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1802 if (!wpa_s->p2p_group_common_freqs)
1803 return;
1804
1805 for (i = 0; i < len; i++) {
1806 if (!wpa_s->go_params->freq_list[i])
1807 break;
1808 wpa_s->p2p_group_common_freqs[i] =
1809 wpa_s->go_params->freq_list[i];
1810 }
1811 wpa_s->p2p_group_common_freqs_num = i;
1812}
1813
1814
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001815static void p2p_config_write(struct wpa_supplicant *wpa_s)
1816{
1817#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001818 if (wpa_s->p2pdev->conf->update_config &&
1819 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001820 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1821#endif /* CONFIG_NO_CONFIG_WRITE */
1822}
1823
1824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825static void p2p_go_configured(void *ctx, void *data)
1826{
1827 struct wpa_supplicant *wpa_s = ctx;
1828 struct p2p_go_neg_results *params = data;
1829 struct wpa_ssid *ssid;
1830
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001831 wpa_s->ap_configured_cb = NULL;
1832 wpa_s->ap_configured_cb_ctx = NULL;
1833 wpa_s->ap_configured_cb_data = NULL;
1834 if (!wpa_s->go_params) {
1835 wpa_printf(MSG_ERROR,
1836 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1837 return;
1838 }
1839
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001840 p2p_go_save_group_common_freqs(wpa_s, params);
1841 p2p_go_dump_common_freqs(wpa_s);
1842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 ssid = wpa_s->current_ssid;
1844 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1845 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1846 if (wpa_s->global->p2p_group_formation == wpa_s)
1847 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001848 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1849 params->passphrase[0] == '\0' ?
1850 params->psk : NULL,
1851 params->passphrase,
1852 wpa_s->global->p2p_dev_addr,
1853 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001854 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001855
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001856 if (wpa_s->p2pdev->p2ps_method_config_any) {
1857 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001858 wpa_dbg(wpa_s, MSG_DEBUG,
1859 "P2PS: Setting default PIN for ANY");
1860 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1861 "12345670", NULL, 0,
1862 0);
1863 } else {
1864 wpa_dbg(wpa_s, MSG_DEBUG,
1865 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001866 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001867 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001868 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001869 "12345670", NULL, 0, 0);
1870 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001871 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001872 }
1873
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001874 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001875 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001876 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001877 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001878 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001879 wpas_p2p_add_psk_list(wpa_s, ssid);
1880 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001881
1882 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001883 params->persistent_group, 0,
1884 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 wpas_p2p_cross_connect_setup(wpa_s);
1886 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001887
1888 if (wpa_s->p2p_first_connection_timeout) {
1889 wpa_dbg(wpa_s, MSG_DEBUG,
1890 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1891 wpa_s->p2p_first_connection_timeout);
1892 wpa_s->p2p_go_group_formation_completed = 0;
1893 wpa_s->global->p2p_group_formation = wpa_s;
1894 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001895 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001896 eloop_register_timeout(
1897 wpa_s->p2p_first_connection_timeout, 0,
1898 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001899 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001900 }
1901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902 return;
1903 }
1904
1905 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1906 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1907 params->peer_interface_addr)) {
1908 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1909 "filtering");
1910 return;
1911 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001912 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001914 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001915#ifdef CONFIG_WPS_NFC
1916 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001917 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001918 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001919 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001920 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1921 return;
1922 }
1923 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001924 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1925 wpa_s->p2pdev->p2p_oob_dev_pw,
1926 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1927 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001928#endif /* CONFIG_WPS_NFC */
1929 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001931 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 os_free(wpa_s->go_params);
1933 wpa_s->go_params = NULL;
1934}
1935
1936
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001937/**
1938 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1939 * @freq: Frequency (MHz) to convert
1940 * @op_class: Buffer for returning operating class
1941 * @op_edmg_channel: Buffer for returning channel number
1942 * Returns: 0 on success, -1 on failure
1943 *
1944 * This can be used to find the highest channel bonding which includes the
1945 * specified frequency.
1946 */
1947static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1948 unsigned int freq,
1949 u8 *op_class, u8 *op_edmg_channel)
1950{
1951 struct hostapd_hw_modes *hwmode;
1952 struct ieee80211_edmg_config edmg;
1953 unsigned int i;
1954 enum chan_width chanwidth[] = {
1955 CHAN_WIDTH_8640,
1956 CHAN_WIDTH_6480,
1957 CHAN_WIDTH_4320,
1958 };
1959
1960 if (!wpa_s->hw.modes)
1961 return -1;
1962
1963 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08001964 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001965 if (!hwmode) {
1966 wpa_printf(MSG_ERROR,
1967 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
1968 return -1;
1969 }
1970
1971 /* Find the highest EDMG channel bandwidth to start the P2P GO */
1972 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
1973 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
1974 op_class,
1975 op_edmg_channel) < 0)
1976 continue;
1977
1978 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
1979 if (edmg.channels &&
1980 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
1981 wpa_printf(MSG_DEBUG,
1982 "Freq %u to EDMG channel %u at opclass %u",
1983 freq, *op_edmg_channel, *op_class);
1984 return 0;
1985 }
1986 }
1987
1988 return -1;
1989}
1990
1991
1992int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
1993 struct p2p_go_neg_results *params)
1994{
1995 u8 op_channel, op_class;
1996 int freq;
1997
1998 /* Try social channel as primary channel frequency */
1999 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2000
2001 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2002 &op_channel) == 0) {
2003 wpa_printf(MSG_DEBUG,
2004 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2005 freq, op_channel, op_class);
2006 params->freq = freq;
2007 return 0;
2008 }
2009
2010 return -1;
2011}
2012
2013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2015 struct p2p_go_neg_results *params,
2016 int group_formation)
2017{
2018 struct wpa_ssid *ssid;
2019
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002020 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2021 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2022 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2023 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026
2027 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002028 if (ssid == NULL) {
2029 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002033 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002034 wpa_s->p2p_go_group_formation_completed = 0;
2035 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002036 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 wpa_config_set_network_defaults(ssid);
2039 ssid->temporary = 1;
2040 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002041 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2043 WPAS_MODE_P2P_GO;
2044 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002045 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002046 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002047 ssid->max_oper_chwidth = params->max_oper_chwidth;
2048 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002049 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002050 if (params->edmg) {
2051 u8 op_channel, op_class;
2052
2053 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2054 &op_class, &op_channel)) {
2055 ssid->edmg_channel = op_channel;
2056 ssid->enable_edmg = params->edmg;
2057 } else {
2058 wpa_dbg(wpa_s, MSG_DEBUG,
2059 "P2P: Could not match EDMG channel, freq %d, for GO",
2060 params->freq);
2061 }
2062 }
2063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 ssid->ssid = os_zalloc(params->ssid_len + 1);
2065 if (ssid->ssid) {
2066 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2067 ssid->ssid_len = params->ssid_len;
2068 }
2069 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2070 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
2071 ssid->proto = WPA_PROTO_RSN;
2072 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002073 ssid->group_cipher = WPA_CIPHER_CCMP;
2074 if (params->freq > 56160) {
2075 /*
2076 * Enable GCMP instead of CCMP as pairwise_cipher and
2077 * group_cipher in 60 GHz.
2078 */
2079 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2080 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002081 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2082 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002083 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002084 if (os_strlen(params->passphrase) > 0) {
2085 ssid->passphrase = os_strdup(params->passphrase);
2086 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002087 wpa_msg_global(wpa_s, MSG_ERROR,
2088 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002089 wpa_config_remove_network(wpa_s->conf, ssid->id);
2090 return;
2091 }
2092 } else
2093 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002094 ssid->psk_set = params->psk_set;
2095 if (ssid->psk_set)
2096 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002097 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002098 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002099 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100
2101 wpa_s->ap_configured_cb = p2p_go_configured;
2102 wpa_s->ap_configured_cb_ctx = wpa_s;
2103 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002104 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002105 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 wpa_s->reassociate = 1;
2107 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002108 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2109 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 wpa_supplicant_req_scan(wpa_s, 0, 0);
2111}
2112
2113
2114static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2115 const struct wpa_supplicant *src)
2116{
2117 struct wpa_config *d;
2118 const struct wpa_config *s;
2119
2120 d = dst->conf;
2121 s = src->conf;
2122
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002123#define C(n) \
2124do { \
2125 if (s->n && !d->n) \
2126 d->n = os_strdup(s->n); \
2127} while (0)
2128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 C(device_name);
2130 C(manufacturer);
2131 C(model_name);
2132 C(model_number);
2133 C(serial_number);
2134 C(config_methods);
2135#undef C
2136
2137 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2138 os_memcpy(d->sec_device_type, s->sec_device_type,
2139 sizeof(d->sec_device_type));
2140 d->num_sec_device_types = s->num_sec_device_types;
2141
2142 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002143 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002145 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146 d->max_num_sta = s->max_num_sta;
2147 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002148 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002149 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002150 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002151 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002152 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002153 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002154 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002155
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002156 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2157 !d->wps_nfc_pw_from_config) {
2158 wpabuf_free(d->wps_nfc_dh_privkey);
2159 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002160 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2161 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2162 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002163 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002164 d->go_interworking = s->go_interworking;
2165 d->go_access_network_type = s->go_access_network_type;
2166 d->go_internet = s->go_internet;
2167 d->go_venue_group = s->go_venue_group;
2168 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002169 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170}
2171
2172
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002173static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2174 char *ifname, size_t len)
2175{
2176 char *ifname_ptr = wpa_s->ifname;
2177
2178 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2179 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2180 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2181 }
2182
2183 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2184 if (os_strlen(ifname) >= IFNAMSIZ &&
2185 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002186 int res;
2187
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002188 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002189 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2190 if (os_snprintf_error(len, res) && len)
2191 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002192 }
2193}
2194
2195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2197 enum wpa_driver_if_type type)
2198{
2199 char ifname[120], force_ifname[120];
2200
2201 if (wpa_s->pending_interface_name[0]) {
2202 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2203 "- skip creation of a new one");
2204 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2205 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2206 "unknown?! ifname='%s'",
2207 wpa_s->pending_interface_name);
2208 return -1;
2209 }
2210 return 0;
2211 }
2212
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002213 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214 force_ifname[0] = '\0';
2215
2216 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2217 ifname);
2218 wpa_s->p2p_group_idx++;
2219
2220 wpa_s->pending_interface_type = type;
2221 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2222 wpa_s->pending_interface_addr, NULL) < 0) {
2223 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2224 "interface");
2225 return -1;
2226 }
2227
Jimmy Chen36c21992018-11-29 16:46:43 +08002228 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002229 random_mac_addr(wpa_s->pending_interface_addr);
2230 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2231 " for the group",
2232 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002233 }
2234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 if (force_ifname[0]) {
2236 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2237 force_ifname);
2238 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2239 sizeof(wpa_s->pending_interface_name));
2240 } else
2241 os_strlcpy(wpa_s->pending_interface_name, ifname,
2242 sizeof(wpa_s->pending_interface_name));
2243 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2244 MACSTR, wpa_s->pending_interface_name,
2245 MAC2STR(wpa_s->pending_interface_addr));
2246
2247 return 0;
2248}
2249
2250
2251static void wpas_p2p_remove_pending_group_interface(
2252 struct wpa_supplicant *wpa_s)
2253{
2254 if (!wpa_s->pending_interface_name[0] ||
2255 is_zero_ether_addr(wpa_s->pending_interface_addr))
2256 return; /* No pending virtual interface */
2257
2258 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2259 wpa_s->pending_interface_name);
2260 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2261 wpa_s->pending_interface_name);
2262 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2263 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002264 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265}
2266
2267
2268static struct wpa_supplicant *
2269wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2270{
2271 struct wpa_interface iface;
2272 struct wpa_supplicant *group_wpa_s;
2273
2274 if (!wpa_s->pending_interface_name[0]) {
2275 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2276 if (!wpas_p2p_create_iface(wpa_s))
2277 return NULL;
2278 /*
2279 * Something has forced us to remove the pending interface; try
2280 * to create a new one and hope for the best that we will get
2281 * the same local address.
2282 */
2283 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2284 WPA_IF_P2P_CLIENT) < 0)
2285 return NULL;
2286 }
2287
2288 os_memset(&iface, 0, sizeof(iface));
2289 iface.ifname = wpa_s->pending_interface_name;
2290 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002291 if (wpa_s->conf->ctrl_interface == NULL &&
2292 wpa_s->parent != wpa_s &&
2293 wpa_s->p2p_mgmt &&
2294 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2295 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2296 else
2297 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002299 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300 if (group_wpa_s == NULL) {
2301 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2302 "wpa_supplicant interface");
2303 return NULL;
2304 }
2305 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2307 P2P_GROUP_INTERFACE_CLIENT;
2308 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002309 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310
2311 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2312
Jimmy Chen36c21992018-11-29 16:46:43 +08002313 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002314 if (wpa_drv_set_mac_addr(group_wpa_s,
2315 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002316 wpa_msg(group_wpa_s, MSG_INFO,
2317 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002318 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2319 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002320 return NULL;
2321 }
2322
2323 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2324 wpa_msg(group_wpa_s, MSG_INFO,
2325 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002326 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2327 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002328 return NULL;
2329 }
2330
Hai Shalom74f70d42019-02-11 14:42:39 -08002331 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2332 " for the group",
2333 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002334 }
2335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 return group_wpa_s;
2337}
2338
2339
2340static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2341 void *timeout_ctx)
2342{
2343 struct wpa_supplicant *wpa_s = eloop_ctx;
2344 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002345 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002346}
2347
2348
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002349static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2350 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002351{
2352 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002353 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354 if (wpa_s->global->p2p)
2355 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002356 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357}
2358
2359
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002360static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2361{
2362 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2363 eloop_cancel_timeout(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 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002366 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002367 wpa_s->global->p2p_fail_on_wps_complete = 0;
2368}
2369
2370
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002371void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2372{
2373 if (wpa_s->global->p2p_group_formation != wpa_s)
2374 return;
2375 /* Speed up group formation timeout since this cannot succeed */
2376 eloop_cancel_timeout(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 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002379 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002380}
2381
2382
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002383static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384{
2385 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002386 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387
2388 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2389 wpa_drv_cancel_remain_on_channel(wpa_s);
2390 wpa_s->off_channel_freq = 0;
2391 wpa_s->roc_waiting_drv_freq = 0;
2392 }
2393
2394 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002395 wpa_msg_global(wpa_s, MSG_INFO,
2396 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2397 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002398 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399 wpas_p2p_remove_pending_group_interface(wpa_s);
2400 return;
2401 }
2402
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002403 if (!res->role_go) {
2404 /* Inform driver of the operating channel of GO. */
2405 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2406 }
2407
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002408 if (wpa_s->p2p_go_ht40)
2409 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002410 if (wpa_s->p2p_go_vht)
2411 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002412 if (wpa_s->p2p_go_he)
2413 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002414 if (wpa_s->p2p_go_edmg)
2415 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002416 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2417 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002418
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002419 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2420 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2421 " wps_method=%s",
2422 res->role_go ? "GO" : "client", res->freq, res->ht40,
2423 MAC2STR(res->peer_device_addr),
2424 MAC2STR(res->peer_interface_addr),
2425 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002426 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427
Dmitry Shmidt04949592012-07-19 12:16:46 -07002428 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2429 struct wpa_ssid *ssid;
2430 ssid = wpa_config_get_network(wpa_s->conf,
2431 wpa_s->p2p_persistent_id);
2432 if (ssid && ssid->disabled == 2 &&
2433 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2434 size_t len = os_strlen(ssid->passphrase);
2435 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2436 "on requested persistent group");
2437 os_memcpy(res->passphrase, ssid->passphrase, len);
2438 res->passphrase[len] = '\0';
2439 }
2440 }
2441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002443 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2445 if (group_wpa_s == NULL) {
2446 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002447 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2448 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002449 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 return;
2451 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2453 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002455 group_wpa_s = wpa_s->parent;
2456 wpa_s->global->p2p_group_formation = group_wpa_s;
2457 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002458 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002461 group_wpa_s->p2p_in_provisioning = 1;
2462 group_wpa_s->p2pdev = wpa_s;
2463 if (group_wpa_s != wpa_s) {
2464 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2465 sizeof(group_wpa_s->p2p_pin));
2466 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2467 }
2468 if (res->role_go) {
2469 wpas_start_wps_go(group_wpa_s, res, 1);
2470 } else {
2471 os_get_reltime(&group_wpa_s->scan_min_time);
2472 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 }
2474
Hai Shalomfdcde762020-04-02 11:19:20 -07002475 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2477
2478 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2479 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2480 (res->peer_config_timeout % 100) * 10000,
2481 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2482}
2483
2484
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002485static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2486 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487{
2488 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002489 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002490 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2491 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002493 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494}
2495
2496
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002497static void wpas_dev_found(void *ctx, const u8 *addr,
2498 const struct p2p_peer_info *info,
2499 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002501 u8 *wfd_dev_info = NULL;
2502 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002503 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002504 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002505#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506 struct wpa_supplicant *wpa_s = ctx;
2507 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002508 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002509 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002510
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002511#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002512 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2513 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002514 if (wfd_dev_info_hex) {
2515 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2516 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2517 // Only used for notification, so not handling error.
2518 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2519 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002520
2521 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2522 WFD_SUBELEM_R2_DEVICE_INFO);
2523 if (wfd_r2_dev_info_hex) {
2524 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2525 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2526 // Only used for notification, so not handling error.
2527 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2528 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002529#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002530
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002531 if (info->p2ps_instance) {
2532 char str[256];
2533 const u8 *buf = wpabuf_head(info->p2ps_instance);
2534 size_t len = wpabuf_len(info->p2ps_instance);
2535
2536 while (len) {
2537 u32 id;
2538 u16 methods;
2539 u8 str_len;
2540
2541 if (len < 4 + 2 + 1)
2542 break;
2543 id = WPA_GET_LE32(buf);
2544 buf += sizeof(u32);
2545 methods = WPA_GET_BE16(buf);
2546 buf += sizeof(u16);
2547 str_len = *buf++;
2548 if (str_len > len - 4 - 2 - 1)
2549 break;
2550 os_memcpy(str, buf, str_len);
2551 str[str_len] = '\0';
2552 buf += str_len;
2553 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2554
2555 wpa_msg_global(wpa_s, MSG_INFO,
2556 P2P_EVENT_DEVICE_FOUND MACSTR
2557 " p2p_dev_addr=" MACSTR
2558 " pri_dev_type=%s name='%s'"
2559 " config_methods=0x%x"
2560 " dev_capab=0x%x"
2561 " group_capab=0x%x"
2562 " adv_id=%x asp_svc=%s%s",
2563 MAC2STR(addr),
2564 MAC2STR(info->p2p_device_addr),
2565 wps_dev_type_bin2str(
2566 info->pri_dev_type,
2567 devtype, sizeof(devtype)),
2568 info->device_name, methods,
2569 info->dev_capab, info->group_capab,
2570 id, str,
2571 info->vendor_elems ?
2572 " vendor_elems=1" : "");
2573 }
2574 goto done;
2575 }
2576
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002577 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2578 " p2p_dev_addr=" MACSTR
2579 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002580 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002581 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2582 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2583 sizeof(devtype)),
2584 info->device_name, info->config_methods,
2585 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002586 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002587 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002588 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2589 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002590 info->vendor_elems ? " vendor_elems=1" : "",
2591 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002593done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002594 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002595 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002596#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002597
Roshan Piusfd2fd662017-01-23 13:41:57 -08002598 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002599 wfd_dev_info_len, wfd_r2_dev_info,
2600 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002601 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602}
2603
2604
2605static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2606{
2607 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002609 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2610 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2613}
2614
2615
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002616static void wpas_find_stopped(void *ctx)
2617{
2618 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002619
2620 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2621 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2622
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002623 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002624 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002625}
2626
2627
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002628struct wpas_p2p_listen_work {
2629 unsigned int freq;
2630 unsigned int duration;
2631 struct wpabuf *probe_resp_ie;
2632};
2633
2634
2635static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2636{
2637 if (lwork == NULL)
2638 return;
2639 wpabuf_free(lwork->probe_resp_ie);
2640 os_free(lwork);
2641}
2642
2643
2644static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2645{
2646 struct wpas_p2p_listen_work *lwork;
2647
2648 if (!wpa_s->p2p_listen_work)
2649 return;
2650
2651 lwork = wpa_s->p2p_listen_work->ctx;
2652 wpas_p2p_listen_work_free(lwork);
2653 radio_work_done(wpa_s->p2p_listen_work);
2654 wpa_s->p2p_listen_work = NULL;
2655}
2656
2657
2658static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2659{
2660 struct wpa_supplicant *wpa_s = work->wpa_s;
2661 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002662 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002663
2664 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002665 if (work->started) {
2666 wpa_s->p2p_listen_work = NULL;
2667 wpas_stop_listen(wpa_s);
2668 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002669 wpas_p2p_listen_work_free(lwork);
2670 return;
2671 }
2672
2673 wpa_s->p2p_listen_work = work;
2674
2675 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2676
2677 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2678 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2679 "report received Probe Request frames");
2680 wpas_p2p_listen_work_done(wpa_s);
2681 return;
2682 }
2683
2684 wpa_s->pending_listen_freq = lwork->freq;
2685 wpa_s->pending_listen_duration = lwork->duration;
2686
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002687 duration = lwork->duration;
2688#ifdef CONFIG_TESTING_OPTIONS
2689 if (wpa_s->extra_roc_dur) {
2690 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2691 duration, duration + wpa_s->extra_roc_dur);
2692 duration += wpa_s->extra_roc_dur;
2693 }
2694#endif /* CONFIG_TESTING_OPTIONS */
2695
2696 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002697 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2698 "to remain on channel (%u MHz) for Listen "
2699 "state", lwork->freq);
2700 wpas_p2p_listen_work_done(wpa_s);
2701 wpa_s->pending_listen_freq = 0;
2702 return;
2703 }
2704 wpa_s->off_channel_freq = 0;
2705 wpa_s->roc_waiting_drv_freq = lwork->freq;
2706}
2707
2708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002709static int wpas_start_listen(void *ctx, unsigned int freq,
2710 unsigned int duration,
2711 const struct wpabuf *probe_resp_ie)
2712{
2713 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002714 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002716 if (wpa_s->p2p_listen_work) {
2717 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002718 return -1;
2719 }
2720
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002721 lwork = os_zalloc(sizeof(*lwork));
2722 if (lwork == NULL)
2723 return -1;
2724 lwork->freq = freq;
2725 lwork->duration = duration;
2726 if (probe_resp_ie) {
2727 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2728 if (lwork->probe_resp_ie == NULL) {
2729 wpas_p2p_listen_work_free(lwork);
2730 return -1;
2731 }
2732 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002734 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2735 lwork) < 0) {
2736 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 return -1;
2738 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002739
2740 return 0;
2741}
2742
2743
2744static void wpas_stop_listen(void *ctx)
2745{
2746 struct wpa_supplicant *wpa_s = ctx;
2747 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2748 wpa_drv_cancel_remain_on_channel(wpa_s);
2749 wpa_s->off_channel_freq = 0;
2750 wpa_s->roc_waiting_drv_freq = 0;
2751 }
2752 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002753
2754 /*
2755 * Don't cancel Probe Request RX reporting for a connected P2P Client
2756 * handling Probe Request frames.
2757 */
2758 if (!wpa_s->p2p_cli_probe)
2759 wpa_drv_probe_req_report(wpa_s, 0);
2760
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002761 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762}
2763
2764
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002765static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2766 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767{
2768 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002769 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002770 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771}
2772
2773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2775 const u8 *peer, const char *params,
2776 unsigned int generated_pin)
2777{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002778 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2779 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780}
2781
2782
2783static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2784 const u8 *peer, const char *params)
2785{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002786 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2787 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788}
2789
2790
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002791static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2792 const u8 *dev_addr, const u8 *pri_dev_type,
2793 const char *dev_name, u16 supp_config_methods,
2794 u8 dev_capab, u8 group_capab, const u8 *group_id,
2795 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796{
2797 struct wpa_supplicant *wpa_s = ctx;
2798 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002799 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800 u8 empty_dev_type[8];
2801 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002802 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002803 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002804
2805 if (group_id) {
2806 for (group = wpa_s->global->ifaces; group; group = group->next)
2807 {
2808 struct wpa_ssid *s = group->current_ssid;
2809 if (s != NULL &&
2810 s->mode == WPAS_MODE_P2P_GO &&
2811 group_id_len - ETH_ALEN == s->ssid_len &&
2812 os_memcmp(group_id + ETH_ALEN, s->ssid,
2813 s->ssid_len) == 0)
2814 break;
2815 }
2816 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817
2818 if (pri_dev_type == NULL) {
2819 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2820 pri_dev_type = empty_dev_type;
2821 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002822 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2823 " pri_dev_type=%s name='%s' config_methods=0x%x "
2824 "dev_capab=0x%x group_capab=0x%x%s%s",
2825 MAC2STR(dev_addr),
2826 wps_dev_type_bin2str(pri_dev_type, devtype,
2827 sizeof(devtype)),
2828 dev_name, supp_config_methods, dev_capab, group_capab,
2829 group ? " group=" : "",
2830 group ? group->ifname : "");
2831 if (os_snprintf_error(sizeof(params), res))
2832 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002836 if (wps_generate_pin(&generated_pin) < 0) {
2837 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2838 wpas_notify_p2p_provision_discovery(
2839 wpa_s, peer, 0 /* response */,
2840 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2841 return;
2842 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 wpas_prov_disc_local_display(wpa_s, peer, params,
2844 generated_pin);
2845 } else if (config_methods & WPS_CONFIG_KEYPAD)
2846 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2847 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002848 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2849 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002850
2851 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2852 P2P_PROV_DISC_SUCCESS,
2853 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854}
2855
2856
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002857static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858{
2859 struct wpa_supplicant *wpa_s = ctx;
2860 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002861 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002863 if (wpa_s->pending_pd_before_join &&
2864 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2865 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2866 wpa_s->pending_pd_before_join = 0;
2867 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2868 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002869 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002870 return;
2871 }
2872
Dmitry Shmidt04949592012-07-19 12:16:46 -07002873 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002874 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2875 int res;
2876
2877 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2878 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2879 if (os_snprintf_error(sizeof(params), res))
2880 params[sizeof(params) - 1] = '\0';
2881 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002882 params[0] = '\0';
2883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002885 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002887 if (wps_generate_pin(&generated_pin) < 0) {
2888 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2889 wpas_notify_p2p_provision_discovery(
2890 wpa_s, peer, 0 /* response */,
2891 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2892 return;
2893 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002894 wpas_prov_disc_local_display(wpa_s, peer, params,
2895 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002897 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2898 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899
Jouni Malinen75ecf522011-06-27 15:19:46 -07002900 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2901 P2P_PROV_DISC_SUCCESS,
2902 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903}
2904
2905
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002906static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002907 enum p2p_prov_disc_status status,
2908 u32 adv_id, const u8 *adv_mac,
2909 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002910{
2911 struct wpa_supplicant *wpa_s = ctx;
2912
Dmitry Shmidt04949592012-07-19 12:16:46 -07002913 if (wpa_s->p2p_fallback_to_go_neg) {
2914 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2915 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002916 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002917 P2P_EVENT_FALLBACK_TO_GO_NEG
2918 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002919 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2920 return;
2921 }
2922
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002923 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002924 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002925 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2926 "join-existing-group operation (no ACK for PD "
2927 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002928 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002929 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002930 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002931
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002932 if (adv_id && adv_mac && deferred_session_resp) {
2933 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2934 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2935 " deferred_session_resp='%s'",
2936 MAC2STR(peer), status, adv_id,
2937 deferred_session_resp);
2938 } else if (adv_id && adv_mac) {
2939 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2940 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2941 MAC2STR(peer), status, adv_id);
2942 } else {
2943 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2944 " p2p_dev_addr=" MACSTR " status=%d",
2945 MAC2STR(peer), status);
2946 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002947
Jouni Malinen75ecf522011-06-27 15:19:46 -07002948 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2949 status, 0, 0);
2950}
2951
2952
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002953static int freq_included(struct wpa_supplicant *wpa_s,
2954 const struct p2p_channels *channels,
2955 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002956{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002957 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2958 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2959 return 1;
2960 return 0;
2961}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002962
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002963
2964static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2965{
2966 unsigned int num = P2P_MAX_CHANNELS;
2967 int *common_freqs;
2968 int ret;
2969
2970 p2p_go_dump_common_freqs(wpa_s);
2971 common_freqs = os_calloc(num, sizeof(int));
2972 if (!common_freqs)
2973 return;
2974
2975 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2976 if (ret < 0) {
2977 wpa_dbg(wpa_s, MSG_DEBUG,
2978 "P2P: Failed to get group common freqs");
2979 os_free(common_freqs);
2980 return;
2981 }
2982
2983 os_free(wpa_s->p2p_group_common_freqs);
2984 wpa_s->p2p_group_common_freqs = common_freqs;
2985 wpa_s->p2p_group_common_freqs_num = num;
2986 p2p_go_dump_common_freqs(wpa_s);
2987}
2988
2989
2990/*
2991 * Check if the given frequency is one of the possible operating frequencies
2992 * set after the completion of the GO Negotiation.
2993 */
2994static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2995{
2996 unsigned int i;
2997
2998 p2p_go_dump_common_freqs(wpa_s);
2999
3000 /* assume no restrictions */
3001 if (!wpa_s->p2p_group_common_freqs_num)
3002 return 1;
3003
3004 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3005 if (wpa_s->p2p_group_common_freqs[i] == freq)
3006 return 1;
3007 }
3008 return 0;
3009}
3010
3011
3012static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3013 struct sta_info *sta, void *ctx)
3014{
3015 int *ecsa_support = ctx;
3016
3017 *ecsa_support &= sta->ecsa_supported;
3018
3019 return 0;
3020}
3021
3022
3023/* Check if all the peers support eCSA */
3024static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3025{
3026 int ecsa_support = 1;
3027
3028 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3029 &ecsa_support);
3030
3031 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003032}
3033
3034
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003035/**
3036 * Pick the best frequency to use from all the currently used frequencies.
3037 */
3038static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3039 struct wpa_used_freq_data *freqs,
3040 unsigned int num)
3041{
3042 unsigned int i, c;
3043
3044 /* find a candidate freq that is supported by P2P */
3045 for (c = 0; c < num; c++)
3046 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3047 break;
3048
3049 if (c == num)
3050 return 0;
3051
3052 /* once we have a candidate, try to find a 'better' one */
3053 for (i = c + 1; i < num; i++) {
3054 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3055 continue;
3056
3057 /*
3058 * 1. Infrastructure station interfaces have higher preference.
3059 * 2. P2P Clients have higher preference.
3060 * 3. All others.
3061 */
3062 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3063 c = i;
3064 break;
3065 }
3066
3067 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3068 c = i;
3069 }
3070 return freqs[c].freq;
3071}
3072
3073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3075 const u8 *go_dev_addr, const u8 *ssid,
3076 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003077 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003078 const struct p2p_channels *channels,
3079 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003080{
3081 struct wpa_supplicant *wpa_s = ctx;
3082 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003083 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003085 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086
3087 if (!persistent_group) {
3088 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003089 " to join an active group (SSID: %s)",
3090 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3092 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3093 == 0 ||
3094 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3095 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3096 "authorized invitation");
3097 goto accept_inv;
3098 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003099
3100#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003101 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3102 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003103 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003104 wpa_s->p2p_wps_method = WPS_NFC;
3105 wpa_s->pending_join_wps_method = WPS_NFC;
3106 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003107 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003108 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003109 bssid, ETH_ALEN);
3110 goto accept_inv;
3111 }
3112#endif /* CONFIG_WPS_NFC */
3113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 /*
3115 * Do not accept the invitation automatically; notify user and
3116 * request approval.
3117 */
3118 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3119 }
3120
3121 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3122 if (grp) {
3123 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3124 "running persistent group");
3125 if (*go)
3126 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3127 goto accept_inv;
3128 }
3129
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003130 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3131 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3132 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3133 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003134 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003135 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003136 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3137
3138 for (s = wpa_s->conf->ssid; s; s = s->next) {
3139 if (s->disabled == 2 &&
3140 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3141 s->ssid_len == ssid_len &&
3142 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3143 break;
3144 }
3145
3146 if (!s) {
3147 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3148 " requested reinvocation of an unknown group",
3149 MAC2STR(sa));
3150 return P2P_SC_FAIL_UNKNOWN_GROUP;
3151 }
3152
3153 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3154 *go = 1;
3155 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3156 wpa_printf(MSG_DEBUG, "P2P: The only available "
3157 "interface is already in use - reject "
3158 "invitation");
3159 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3160 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003161 if (wpa_s->p2p_mgmt)
3162 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3163 ETH_ALEN);
3164 else
3165 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 } else if (s->mode == WPAS_MODE_P2P_GO) {
3167 *go = 1;
3168 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3169 {
3170 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3171 "interface address for the group");
3172 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3173 }
3174 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3175 ETH_ALEN);
3176 }
3177
3178accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003179 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3180
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003181 best_freq = 0;
3182 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3183 sizeof(struct wpa_used_freq_data));
3184 if (freqs) {
3185 int num_channels = wpa_s->num_multichan_concurrent;
3186 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3187 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3188 os_free(freqs);
3189 }
3190
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003191 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003192 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003193 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003194 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003195
3196 if (wpa_s->num_multichan_concurrent < 2 ||
3197 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3198 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 -07003199 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201 }
3202
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003203 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3204 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003205 if (*go == 0) {
3206 /* We are the client */
3207 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3208 "running a GO but we are capable of MCC, "
3209 "figure out the best channel to use");
3210 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003211 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003212 /* We are the GO, and *force_freq is not in the
3213 * intersection */
3214 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3215 "in intersection but we are capable of MCC, "
3216 "figure out the best channel to use",
3217 *force_freq);
3218 *force_freq = 0;
3219 }
3220 }
3221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 return P2P_SC_SUCCESS;
3223}
3224
3225
3226static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3227 const u8 *ssid, size_t ssid_len,
3228 const u8 *go_dev_addr, u8 status,
3229 int op_freq)
3230{
3231 struct wpa_supplicant *wpa_s = ctx;
3232 struct wpa_ssid *s;
3233
3234 for (s = wpa_s->conf->ssid; s; s = s->next) {
3235 if (s->disabled == 2 &&
3236 s->ssid_len == ssid_len &&
3237 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3238 break;
3239 }
3240
3241 if (status == P2P_SC_SUCCESS) {
3242 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003243 " was accepted; op_freq=%d MHz, SSID=%s",
3244 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003246 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003247 if (go) {
3248 wpa_msg_global(wpa_s, MSG_INFO,
3249 P2P_EVENT_INVITATION_ACCEPTED
3250 "sa=" MACSTR
3251 " persistent=%d freq=%d",
3252 MAC2STR(sa), s->id, op_freq);
3253 } else {
3254 wpa_msg_global(wpa_s, MSG_INFO,
3255 P2P_EVENT_INVITATION_ACCEPTED
3256 "sa=" MACSTR
3257 " persistent=%d",
3258 MAC2STR(sa), s->id);
3259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003261 wpa_s, s, go, 0, op_freq, 0,
3262 wpa_s->conf->p2p_go_ht40,
3263 wpa_s->conf->p2p_go_vht,
3264 0,
3265 wpa_s->conf->p2p_go_he,
3266 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003267 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3268 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003270 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003271 wpa_msg_global(wpa_s, MSG_INFO,
3272 P2P_EVENT_INVITATION_ACCEPTED
3273 "sa=" MACSTR " go_dev_addr=" MACSTR
3274 " bssid=" MACSTR " unknown-network",
3275 MAC2STR(sa), MAC2STR(go_dev_addr),
3276 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003278 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003279 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 }
3281 return;
3282 }
3283
3284 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3285 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3286 " was rejected (status %u)", MAC2STR(sa), status);
3287 return;
3288 }
3289
3290 if (!s) {
3291 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003292 wpa_msg_global(wpa_s, MSG_INFO,
3293 P2P_EVENT_INVITATION_RECEIVED
3294 "sa=" MACSTR " go_dev_addr=" MACSTR
3295 " bssid=" MACSTR " unknown-network",
3296 MAC2STR(sa), MAC2STR(go_dev_addr),
3297 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003299 wpa_msg_global(wpa_s, MSG_INFO,
3300 P2P_EVENT_INVITATION_RECEIVED
3301 "sa=" MACSTR " go_dev_addr=" MACSTR
3302 " unknown-network",
3303 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003305 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3306 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 return;
3308 }
3309
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003310 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003311 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3312 "sa=" MACSTR " persistent=%d freq=%d",
3313 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003314 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003315 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3316 "sa=" MACSTR " persistent=%d",
3317 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003318 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003319 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3320 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321}
3322
3323
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003324static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3325 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003326 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003327{
3328 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003329 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003330
3331 if (ssid == NULL)
3332 return;
3333
3334 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003335 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003336 ETH_ALEN) == 0)
3337 break;
3338 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003339 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003340 if (ssid->mode != WPAS_MODE_P2P_GO &&
3341 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3342 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3343 "due to invitation result", ssid->id);
3344 wpas_notify_network_removed(wpa_s, ssid);
3345 wpa_config_remove_network(wpa_s->conf, ssid->id);
3346 return;
3347 }
3348 return; /* Peer not found in client list */
3349 }
3350
3351 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003352 "group %d client list%s",
3353 MAC2STR(peer), ssid->id,
3354 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003355 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3356 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3357 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003358 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003359 if (p2p_wpa_s->conf->update_config &&
3360 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003361 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003362}
3363
3364
3365static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3366 const u8 *peer)
3367{
3368 struct wpa_ssid *ssid;
3369
3370 wpa_s = wpa_s->global->p2p_invite_group;
3371 if (wpa_s == NULL)
3372 return; /* No known invitation group */
3373 ssid = wpa_s->current_ssid;
3374 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3375 !ssid->p2p_persistent_group)
3376 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003377 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003378 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003379 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003380}
3381
3382
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003383static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003384 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003385 const u8 *peer, int neg_freq,
3386 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387{
3388 struct wpa_supplicant *wpa_s = ctx;
3389 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003390 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391
3392 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003393 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3394 "status=%d " MACSTR,
3395 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003397 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3398 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 }
3400 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3401
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003402 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3403 status, MAC2STR(peer));
3404 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003405 struct wpa_supplicant *group_if =
3406 wpa_s->global->p2p_invite_group;
3407
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003408 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3409 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003410
3411 /*
3412 * Invitation to an active group. If this is successful and we
3413 * are the GO, set the client wait to postpone some concurrent
3414 * operations and to allow provisioning and connection to happen
3415 * more quickly.
3416 */
3417 if (status == P2P_SC_SUCCESS &&
3418 group_if && group_if->current_ssid &&
3419 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3420 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3421#ifdef CONFIG_TESTING_OPTIONS
3422 if (group_if->p2p_go_csa_on_inv) {
3423 wpa_printf(MSG_DEBUG,
3424 "Testing: force P2P GO CSA after invitation");
3425 eloop_cancel_timeout(
3426 wpas_p2p_reconsider_moving_go,
3427 wpa_s, NULL);
3428 eloop_register_timeout(
3429 0, 50000,
3430 wpas_p2p_reconsider_moving_go,
3431 wpa_s, NULL);
3432 }
3433#endif /* CONFIG_TESTING_OPTIONS */
3434 }
3435 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003438 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3439 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3440 "invitation exchange to indicate readiness for "
3441 "re-invocation");
3442 }
3443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003445 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3446 ssid = wpa_config_get_network(
3447 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003448 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 wpas_p2p_remove_pending_group_interface(wpa_s);
3451 return;
3452 }
3453
3454 ssid = wpa_config_get_network(wpa_s->conf,
3455 wpa_s->pending_invite_ssid_id);
3456 if (ssid == NULL) {
3457 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3458 "data matching with invitation");
3459 return;
3460 }
3461
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003462 /*
3463 * The peer could have missed our ctrl::ack frame for Invitation
3464 * Response and continue retransmitting the frame. To reduce the
3465 * likelihood of the peer not getting successful TX status for the
3466 * Invitation Response frame, wait a short time here before starting
3467 * the persistent group so that we will remain on the current channel to
3468 * acknowledge any possible retransmission from the peer.
3469 */
3470 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3471 "starting persistent group");
3472 os_sleep(0, 50000);
3473
Dmitry Shmidt15907092014-03-25 10:42:57 -07003474 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003475 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003476 freq = neg_freq;
3477 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003478 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003479 freq = peer_oper_freq;
3480 else
3481 freq = 0;
3482
3483 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3484 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003486 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003487 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003488 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003489 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003490 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003491 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003492 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003493 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003494 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003495 ssid->mode == WPAS_MODE_P2P_GO ?
3496 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003497 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498}
3499
3500
Dmitry Shmidt04949592012-07-19 12:16:46 -07003501static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3502 unsigned int freq)
3503{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003504 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3505 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003506 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003507}
3508
3509
3510static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3511{
3512 reg->channel[reg->channels] = chan;
3513 reg->channels++;
3514}
3515
3516
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003517static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003518 struct p2p_channels *chan,
3519 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520{
3521 int i, cla = 0;
3522
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003523 wpa_s->global->p2p_24ghz_social_channels = 1;
3524
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003525 os_memset(cli_chan, 0, sizeof(*cli_chan));
3526
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003527 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3528 "band");
3529
3530 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3531 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003532 chan->reg_class[cla].channels = 0;
3533 for (i = 0; i < 11; i++) {
3534 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3535 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3536 }
3537 if (chan->reg_class[cla].channels)
3538 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539
3540 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3541 "band");
3542
3543 /* Operating class 115 - 5 GHz, channels 36-48 */
3544 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003545 chan->reg_class[cla].channels = 0;
3546 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3547 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3548 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3549 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3550 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3551 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3552 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3553 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3554 if (chan->reg_class[cla].channels)
3555 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556
3557 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3558 "band");
3559
3560 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3561 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003562 chan->reg_class[cla].channels = 0;
3563 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3564 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3565 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3566 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3567 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3568 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3569 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3570 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3571 if (chan->reg_class[cla].channels)
3572 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003573
3574 chan->reg_classes = cla;
3575 return 0;
3576}
3577
3578
Dmitry Shmidt04949592012-07-19 12:16:46 -07003579static int has_channel(struct wpa_global *global,
3580 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581{
3582 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003583 unsigned int freq;
3584
3585 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3586 chan * 5;
3587 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003588 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003589
3590 for (i = 0; i < mode->num_channels; i++) {
3591 if (mode->channels[i].chan == chan) {
3592 if (flags)
3593 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003594 if (mode->channels[i].flag &
3595 (HOSTAPD_CHAN_DISABLED |
3596 HOSTAPD_CHAN_RADAR))
3597 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003598 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3599 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003600 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 }
3602 }
3603
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003604 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605}
3606
3607
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003608static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3609 struct hostapd_hw_modes *mode,
3610 u8 channel)
3611{
Hai Shalom60840252021-02-19 19:02:11 -08003612 u8 center_channels[] = { 42, 58, 106, 122, 138, 155, 171 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003613 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003614
3615 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3616 return 0;
3617
3618 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3619 /*
3620 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3621 * so the center channel is 6 channels away from the start/end.
3622 */
3623 if (channel >= center_channels[i] - 6 &&
3624 channel <= center_channels[i] + 6)
3625 return center_channels[i];
3626
3627 return 0;
3628}
3629
3630
3631static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3632 struct hostapd_hw_modes *mode,
3633 u8 channel, u8 bw)
3634{
3635 u8 center_chan;
3636 int i, flags;
3637 enum chan_allowed res, ret = ALLOWED;
3638
3639 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3640 if (!center_chan)
3641 return NOT_ALLOWED;
3642 if (center_chan >= 58 && center_chan <= 138)
3643 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3644
3645 /* check all the channels are available */
3646 for (i = 0; i < 4; i++) {
3647 int adj_chan = center_chan - 6 + i * 4;
3648
3649 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3650 if (res == NOT_ALLOWED)
3651 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003652 if (res == NO_IR)
3653 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003654
3655 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3656 return NOT_ALLOWED;
3657 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3658 return NOT_ALLOWED;
3659 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3660 return NOT_ALLOWED;
3661 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3662 return NOT_ALLOWED;
3663 }
3664
3665 return ret;
3666}
3667
3668
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003669static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3670 struct hostapd_hw_modes *mode,
3671 u8 channel)
3672{
Hai Shalom60840252021-02-19 19:02:11 -08003673 u8 center_channels[] = { 50, 114, 163 };
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003674 unsigned int i;
3675
3676 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3677 return 0;
3678
3679 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3680 /*
3681 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3682 * so the center channel is 14 channels away from the start/end.
3683 */
3684 if (channel >= center_channels[i] - 14 &&
3685 channel <= center_channels[i] + 14)
3686 return center_channels[i];
3687
3688 return 0;
3689}
3690
3691
3692static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3693 struct hostapd_hw_modes *mode,
3694 u8 channel, u8 bw)
3695{
3696 u8 center_chan;
3697 int i, flags;
3698 enum chan_allowed res, ret = ALLOWED;
3699
3700 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3701 if (!center_chan)
3702 return NOT_ALLOWED;
3703 /* VHT 160 MHz uses DFS channels in most countries. */
3704
3705 /* Check all the channels are available */
3706 for (i = 0; i < 8; i++) {
3707 int adj_chan = center_chan - 14 + i * 4;
3708
3709 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3710 if (res == NOT_ALLOWED)
3711 return NOT_ALLOWED;
3712
3713 if (res == NO_IR)
3714 ret = NO_IR;
3715
3716 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3717 return NOT_ALLOWED;
3718 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3719 return NOT_ALLOWED;
3720 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3721 return NOT_ALLOWED;
3722 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3723 return NOT_ALLOWED;
3724 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3725 return NOT_ALLOWED;
3726 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3727 return NOT_ALLOWED;
3728 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3729 return NOT_ALLOWED;
3730 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3731 return NOT_ALLOWED;
3732 }
3733
3734 return ret;
3735}
3736
3737
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003738static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3739 struct hostapd_hw_modes *mode,
3740 u8 channel)
3741{
3742 struct ieee80211_edmg_config edmg;
3743
3744 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3745 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3746 return ALLOWED;
3747
3748 return NOT_ALLOWED;
3749}
3750
3751
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003752static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3753 struct hostapd_hw_modes *mode,
3754 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003755{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003756 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003757 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003758
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003759 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3760 if (bw == BW40MINUS) {
3761 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3762 return NOT_ALLOWED;
3763 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3764 } else if (bw == BW40PLUS) {
3765 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3766 return NOT_ALLOWED;
3767 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3768 } else if (bw == BW80) {
3769 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003770 } else if (bw == BW160) {
3771 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003772 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3773 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003774 }
3775
3776 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3777 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003778 if (res == NO_IR || res2 == NO_IR)
3779 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003780 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003781}
3782
3783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003785 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003786 struct p2p_channels *cli_chan,
3787 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003789 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003790 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003792 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3794 "of all supported channels; assume dualband "
3795 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003796 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 }
3798
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003799 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003801 for (op = 0; global_op_class[op].op_class; op++) {
3802 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003804 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003805
Hai Shalom899fcc72020-10-19 14:38:18 -07003806 if (o->p2p == NO_P2P_SUPP ||
Hai Shalom60840252021-02-19 19:02:11 -08003807 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003808 continue;
3809
Hai Shalomfdcde762020-04-02 11:19:20 -07003810 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
3811 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812 if (mode == NULL)
3813 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003814 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3815 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003817 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07003818
3819 /* Check for non-continuous jump in channel index
3820 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08003821 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07003822 ch < 149 && ch + o->inc > 149)
3823 ch = 149;
3824
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003825 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3826 if (res == ALLOWED) {
3827 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003828 if (cla == P2P_MAX_REG_CLASSES)
3829 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003830 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3831 o->op_class);
3832 reg = &chan->reg_class[cla];
3833 cla++;
3834 reg->reg_class = o->op_class;
3835 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003836 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
3837 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003838 reg->channel[reg->channels] = ch;
3839 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003840 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003841 wpa_s->conf->p2p_add_cli_chan) {
3842 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003843 if (cli_cla == P2P_MAX_REG_CLASSES)
3844 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003845 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3846 o->op_class);
3847 cli_reg = &cli_chan->reg_class[cli_cla];
3848 cli_cla++;
3849 cli_reg->reg_class = o->op_class;
3850 }
Hai Shalomfdcde762020-04-02 11:19:20 -07003851 if (cli_reg->channels ==
3852 P2P_MAX_REG_CLASS_CHANNELS)
3853 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003854 cli_reg->channel[cli_reg->channels] = ch;
3855 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 }
3858 if (reg) {
3859 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3860 reg->channel, reg->channels);
3861 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003862 if (cli_reg) {
3863 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3864 cli_reg->channel, cli_reg->channels);
3865 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 }
3867
3868 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003869 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 return 0;
3872}
3873
3874
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003875int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3876 struct hostapd_hw_modes *mode, u8 channel)
3877{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003878 int op;
3879 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003881 for (op = 0; global_op_class[op].op_class; op++) {
3882 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003883 u8 ch;
3884
Hai Shalom899fcc72020-10-19 14:38:18 -07003885 if (o->p2p == NO_P2P_SUPP ||
3886 (is_6ghz_op_class(o->op_class) &&
3887 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003888 continue;
3889
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003890 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3891 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003892 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3893 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003894 continue;
3895 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003896 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003897 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003898 }
3899 }
3900 return 0;
3901}
3902
3903
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003904int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3905 struct hostapd_hw_modes *mode, u8 channel)
3906{
3907 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3908 return 0;
3909
3910 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3911}
3912
3913
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003914int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3915 struct hostapd_hw_modes *mode, u8 channel)
3916{
3917 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3918 return 0;
3919 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3920}
3921
3922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3924 size_t buf_len)
3925{
3926 struct wpa_supplicant *wpa_s = ctx;
3927
3928 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3929 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3930 break;
3931 }
3932 if (wpa_s == NULL)
3933 return -1;
3934
3935 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3936}
3937
3938
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003939struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3940 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003941{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003942 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3943 struct wpa_ssid *s = wpa_s->current_ssid;
3944 if (s == NULL)
3945 continue;
3946 if (s->mode != WPAS_MODE_P2P_GO &&
3947 s->mode != WPAS_MODE_AP &&
3948 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3949 continue;
3950 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003951 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003952 continue;
3953 return wpa_s;
3954 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003955
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003956 return NULL;
3957
3958}
3959
3960
3961struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3962 const u8 *peer_dev_addr)
3963{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003964 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3965 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003966 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003967 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003968 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3969 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003970 }
3971
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003972 return NULL;
3973}
3974
3975
3976static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3977{
3978 struct wpa_supplicant *wpa_s = ctx;
3979
3980 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003981}
3982
3983
Dmitry Shmidt18463232014-01-24 12:29:41 -08003984static int wpas_is_concurrent_session_active(void *ctx)
3985{
3986 struct wpa_supplicant *wpa_s = ctx;
3987 struct wpa_supplicant *ifs;
3988
3989 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3990 if (ifs == wpa_s)
3991 continue;
3992 if (ifs->wpa_state > WPA_ASSOCIATED)
3993 return 1;
3994 }
3995 return 0;
3996}
3997
3998
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003999static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4000{
4001 struct wpa_supplicant *wpa_s = ctx;
4002 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4003}
4004
4005
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004006int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4007 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004008{
4009 struct wpa_interface iface;
4010 struct wpa_supplicant *p2pdev_wpa_s;
4011 char ifname[100];
4012 char force_name[100];
4013 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004014 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004015
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004016 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4017 wpa_s->ifname);
4018 if (os_snprintf_error(sizeof(ifname), ret))
4019 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004020 /* Cut length at the maximum size. Note that we don't need to ensure
4021 * collision free names here as the created interface is not a netdev.
4022 */
4023 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004024 force_name[0] = '\0';
4025 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004026
4027 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4028 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4029 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4030
4031 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004032 force_name, wpa_s->pending_interface_addr, NULL);
4033 if (ret < 0) {
4034 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4035 return ret;
4036 }
4037 os_strlcpy(wpa_s->pending_interface_name, ifname,
4038 sizeof(wpa_s->pending_interface_name));
4039
4040 os_memset(&iface, 0, sizeof(iface));
4041 iface.p2p_mgmt = 1;
4042 iface.ifname = wpa_s->pending_interface_name;
4043 iface.driver = wpa_s->driver->name;
4044 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004045
4046 /*
4047 * If a P2P Device configuration file was given, use it as the interface
4048 * configuration file (instead of using parent's configuration file.
4049 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004050 if (conf_p2p_dev) {
4051 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004052 iface.ctrl_interface = NULL;
4053 } else {
4054 iface.confname = wpa_s->confname;
4055 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4056 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004057
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004058 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004059 if (!p2pdev_wpa_s) {
4060 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4061 return -1;
4062 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004063
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004064 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004065 wpa_s->pending_interface_name[0] = '\0';
4066 return 0;
4067}
4068
4069
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004070static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4071 const u8 *noa, size_t noa_len)
4072{
4073 struct wpa_supplicant *wpa_s, *intf = ctx;
4074 char hex[100];
4075
4076 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4077 if (wpa_s->waiting_presence_resp)
4078 break;
4079 }
4080 if (!wpa_s) {
4081 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4082 return;
4083 }
4084 wpa_s->waiting_presence_resp = 0;
4085
4086 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4087 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4088 " status=%u noa=%s", MAC2STR(src), status, hex);
4089}
4090
4091
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004092static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4093 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004094 u8 *ret_ssid, size_t *ret_ssid_len,
4095 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004096{
4097 struct wpa_supplicant *wpa_s = ctx;
4098 struct wpa_ssid *s;
4099
4100 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4101 if (s) {
4102 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4103 *ret_ssid_len = s->ssid_len;
4104 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004105
4106 if (s->mode != WPAS_MODE_P2P_GO) {
4107 os_memset(intended_iface_addr, 0, ETH_ALEN);
4108 } else if (wpas_p2p_create_iface(wpa_s)) {
4109 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4110 return 0;
4111
4112 os_memcpy(intended_iface_addr,
4113 wpa_s->pending_interface_addr, ETH_ALEN);
4114 } else {
4115 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4116 ETH_ALEN);
4117 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004118 return 1;
4119 }
4120
4121 return 0;
4122}
4123
4124
4125static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004126 u8 *ssid, size_t *ssid_len, int *group_iface,
4127 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004128{
4129 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004130 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004131 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004132
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004133 /*
4134 * group_iface will be set to 1 only if a dedicated interface for P2P
4135 * role is required. First, we try to reuse an active GO. However,
4136 * if it is not present, we will try to reactivate an existing
4137 * persistent group and set group_iface to 1, so the caller will know
4138 * that the pending interface should be used.
4139 */
4140 *group_iface = 0;
4141
4142 if (freq)
4143 *freq = 0;
4144
4145 go = wpas_p2p_get_go_group(wpa_s);
4146 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004147 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004148 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004149 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004150 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4151 else
4152 return 0;
4153 } else {
4154 s = go->current_ssid;
4155 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4156 if (freq)
4157 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004158 }
4159
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004160 os_memcpy(ssid, s->ssid, s->ssid_len);
4161 *ssid_len = s->ssid_len;
4162
4163 return 1;
4164}
4165
4166
4167static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4168 const u8 *ssid, size_t ssid_len)
4169{
4170 struct wpa_supplicant *wpa_s = ctx;
4171 struct wpa_ssid *s;
4172 int save_config = 0;
4173 size_t i;
4174
4175 /* Start with our first choice of Persistent Groups */
4176 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4177 if (go && ssid && ssid_len &&
4178 s->ssid_len == ssid_len &&
4179 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4180 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4181 break;
4182
4183 /* Remove stale persistent group */
4184 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004185 wpa_dbg(wpa_s, MSG_DEBUG,
4186 "P2P: Remove stale persistent group id=%d",
4187 s->id);
4188 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004189 wpa_config_remove_network(wpa_s->conf, s->id);
4190 save_config = 1;
4191 continue;
4192 }
4193
4194 for (i = 0; i < s->num_p2p_clients; i++) {
4195 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4196 peer, ETH_ALEN) != 0)
4197 continue;
4198
4199 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4200 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4201 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4202 break;
4203 }
4204 s->num_p2p_clients--;
4205 save_config = 1;
4206 }
4207
4208 if (save_config)
4209 p2p_config_write(wpa_s);
4210
4211 /* Return TRUE if valid SSID remains */
4212 return s != NULL;
4213}
4214
4215
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004216static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4217 const u8 *feat_cap, size_t feat_cap_len)
4218{
4219 static const char pref[] = " feature_cap=";
4220 int ret;
4221
4222 buf[0] = '\0';
4223
4224 /*
4225 * We expect a feature capability to contain at least one byte to be
4226 * reported. The string buffer provided by the caller function is
4227 * expected to be big enough to contain all bytes of the attribute for
4228 * known specifications. This function truncates the reported bytes if
4229 * the feature capability data exceeds the string buffer size.
4230 */
4231 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4232 return;
4233
4234 os_memcpy(buf, pref, sizeof(pref));
4235 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4236 buf_len - sizeof(pref) + 1,
4237 feat_cap, feat_cap_len);
4238
4239 if (ret != (2 * (int) feat_cap_len))
4240 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4241}
4242
4243
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004244static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4245 const u8 *adv_mac, const u8 *ses_mac,
4246 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4247 u8 conncap, int passwd_id,
4248 const u8 *persist_ssid,
4249 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004250 int prov_start, const char *session_info,
4251 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004252 unsigned int freq,
4253 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004254{
4255 struct wpa_supplicant *wpa_s = ctx;
4256 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004257 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004258 int save_config = 0;
4259 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004260 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004261
4262 if (!dev)
4263 return;
4264
4265 os_memset(mac, 0, ETH_ALEN);
4266 if (!adv_mac)
4267 adv_mac = mac;
4268 if (!ses_mac)
4269 ses_mac = mac;
4270 if (!grp_mac)
4271 grp_mac = mac;
4272
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004273 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4274 feat_cap, feat_cap_len);
4275
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004276 if (prov_start) {
4277 if (session_info == NULL) {
4278 wpa_msg_global(wpa_s, MSG_INFO,
4279 P2P_EVENT_P2PS_PROVISION_START MACSTR
4280 " adv_id=%x conncap=%x"
4281 " adv_mac=" MACSTR
4282 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004283 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004284 MAC2STR(dev), adv_id, conncap,
4285 MAC2STR(adv_mac),
4286 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004287 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004288 } else {
4289 wpa_msg_global(wpa_s, MSG_INFO,
4290 P2P_EVENT_P2PS_PROVISION_START MACSTR
4291 " adv_id=%x conncap=%x"
4292 " adv_mac=" MACSTR
4293 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004294 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004295 MAC2STR(dev), adv_id, conncap,
4296 MAC2STR(adv_mac),
4297 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004298 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004299 }
4300 return;
4301 }
4302
4303 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4304 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4305
4306 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4307 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4308 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4309
4310 if (persistent_go && !persistent_go->num_p2p_clients) {
4311 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004312 wpa_dbg(wpa_s, MSG_DEBUG,
4313 "P2P: Remove empty persistent group id=%d",
4314 persistent_go->id);
4315 wpas_notify_persistent_group_removed(wpa_s,
4316 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004317 wpa_config_remove_network(wpa_s->conf,
4318 persistent_go->id);
4319 }
4320
4321 wpa_msg_global(wpa_s, MSG_INFO,
4322 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4323 " status=%d"
4324 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004325 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004326 MAC2STR(dev), status,
4327 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004328 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004329 return;
4330 }
4331
4332 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004333 if (persist_ssid && persist_ssid_size)
4334 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4335 persist_ssid_size);
4336
4337 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4338 is_zero_ether_addr(grp_mac)) {
4339 wpa_dbg(wpa_s, MSG_ERROR,
4340 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4341 return;
4342 }
4343
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004344 for (;;) {
4345 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4346 if (!stale)
4347 break;
4348
4349 if (s && s->ssid_len == stale->ssid_len &&
4350 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4351 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4352 break;
4353
4354 /* Remove stale persistent group */
4355 if (stale->mode != WPAS_MODE_P2P_GO ||
4356 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004357 wpa_dbg(wpa_s, MSG_DEBUG,
4358 "P2P: Remove stale persistent group id=%d",
4359 stale->id);
4360 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004361 wpa_config_remove_network(wpa_s->conf, stale->id);
4362 } else {
4363 size_t i;
4364
4365 for (i = 0; i < stale->num_p2p_clients; i++) {
4366 if (os_memcmp(stale->p2p_client_list +
4367 i * ETH_ALEN,
4368 dev, ETH_ALEN) == 0) {
4369 os_memmove(stale->p2p_client_list +
4370 i * ETH_ALEN,
4371 stale->p2p_client_list +
4372 (i + 1) * ETH_ALEN,
4373 (stale->num_p2p_clients -
4374 i - 1) * ETH_ALEN);
4375 break;
4376 }
4377 }
4378 stale->num_p2p_clients--;
4379 }
4380 save_config = 1;
4381 }
4382
4383 if (save_config)
4384 p2p_config_write(wpa_s);
4385
4386 if (s) {
4387 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4388 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4389
4390 if (persistent_go && s != persistent_go &&
4391 !persistent_go->num_p2p_clients) {
4392 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004393 wpa_dbg(wpa_s, MSG_DEBUG,
4394 "P2P: Remove empty persistent group id=%d",
4395 persistent_go->id);
4396 wpas_notify_persistent_group_removed(wpa_s,
4397 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004398 wpa_config_remove_network(wpa_s->conf,
4399 persistent_go->id);
4400 /* Save config */
4401 }
4402
4403 wpa_msg_global(wpa_s, MSG_INFO,
4404 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4405 " status=%d"
4406 " adv_id=%x adv_mac=" MACSTR
4407 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004408 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004409 MAC2STR(dev), status,
4410 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004411 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004412 return;
4413 }
4414
Hai Shalom5f92bc92019-04-18 11:54:11 -07004415 wpa_s->global->pending_p2ps_group = 0;
4416 wpa_s->global->pending_p2ps_group_freq = 0;
4417
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004418 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004419 /*
4420 * We need to copy the interface name. Simply saving a
4421 * pointer isn't enough, since if we use pending_interface_name
4422 * it will be overwritten when the group is added.
4423 */
4424 char go_ifname[100];
4425
4426 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004427 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004428 if (!response_done) {
4429 wpa_s->global->pending_p2ps_group = 1;
4430 wpa_s->global->pending_p2ps_group_freq = freq;
4431 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004432
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004433 if (!wpas_p2p_create_iface(wpa_s))
4434 os_memcpy(go_ifname, wpa_s->ifname,
4435 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004436 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004437 os_memcpy(go_ifname,
4438 wpa_s->pending_interface_name,
4439 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004440
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004441 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004442 wpas_p2ps_prov_complete(
4443 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4444 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004445 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004446 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4447 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004448 return;
4449 }
4450
4451 /* If PD Resp complete, start up the GO */
4452 if (response_done && persistent_go) {
4453 wpas_p2p_group_add_persistent(
4454 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004455 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004456 persistent_go->mode ==
4457 WPAS_MODE_P2P_GO ?
4458 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004459 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004460 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004461 wpas_p2p_group_add(wpa_s, 1, freq,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004462 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004463 }
4464
4465 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004466 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4467 ETH_ALEN);
4468 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004469 }
4470 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004471 os_memcpy(go_ifname, go_wpa_s->ifname,
4472 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004473
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004474 if (is_zero_ether_addr(grp_mac)) {
4475 wpa_dbg(go_wpa_s, MSG_DEBUG,
4476 "P2P: Setting PIN-1 for ANY");
4477 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4478 "12345670", NULL, 0,
4479 0);
4480 } else {
4481 wpa_dbg(go_wpa_s, MSG_DEBUG,
4482 "P2P: Setting PIN-1 for " MACSTR,
4483 MAC2STR(grp_mac));
4484 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4485 "12345670", NULL, 0,
4486 0);
4487 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004488
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004489 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4490 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004491 }
4492
4493 wpa_msg_global(wpa_s, MSG_INFO,
4494 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4495 " status=%d conncap=%x"
4496 " adv_id=%x adv_mac=" MACSTR
4497 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004498 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004499 MAC2STR(dev), status, conncap,
4500 adv_id, MAC2STR(adv_mac),
4501 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004502 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004503 return;
4504 }
4505
4506 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4507 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4508
4509 if (persistent_go && !persistent_go->num_p2p_clients) {
4510 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004511 wpa_dbg(wpa_s, MSG_DEBUG,
4512 "P2P: Remove empty persistent group id=%d",
4513 persistent_go->id);
4514 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004515 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4516 }
4517
4518 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004519 char ssid_hex[32 * 2 + 1];
4520
4521 if (group_ssid)
4522 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4523 group_ssid, group_ssid_len);
4524 else
4525 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004526 wpa_msg_global(wpa_s, MSG_INFO,
4527 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4528 " status=%d conncap=%x"
4529 " adv_id=%x adv_mac=" MACSTR
4530 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004531 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004532 MAC2STR(dev), status, conncap,
4533 adv_id, MAC2STR(adv_mac),
4534 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004535 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4536 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004537 } else {
4538 wpa_msg_global(wpa_s, MSG_INFO,
4539 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4540 " status=%d conncap=%x"
4541 " adv_id=%x adv_mac=" MACSTR
4542 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004543 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004544 MAC2STR(dev), status, conncap,
4545 adv_id, MAC2STR(adv_mac),
4546 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004547 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004548 }
4549}
4550
4551
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004552static int _wpas_p2p_in_progress(void *ctx)
4553{
4554 struct wpa_supplicant *wpa_s = ctx;
4555 return wpas_p2p_in_progress(wpa_s);
4556}
4557
4558
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004559static int wpas_prov_disc_resp_cb(void *ctx)
4560{
4561 struct wpa_supplicant *wpa_s = ctx;
4562 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004563 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004564
4565 if (!wpa_s->global->pending_p2ps_group)
4566 return 0;
4567
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004568 freq = wpa_s->global->pending_p2ps_group_freq;
4569 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004570 wpa_s->global->pending_p2ps_group = 0;
4571
4572 if (wpas_p2p_get_go_group(wpa_s))
4573 return 0;
4574 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4575
4576 if (persistent_go) {
4577 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004578 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4579 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004580 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004581 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004582 } else {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004583 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004584 }
4585
4586 return 1;
4587}
4588
4589
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004590static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4591 unsigned int *len,
4592 unsigned int *freq_list)
4593{
4594 struct wpa_supplicant *wpa_s = ctx;
4595
4596 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4597 WPA_IF_P2P_CLIENT, len, freq_list);
4598}
4599
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004600int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4601{
4602 u8 addr[ETH_ALEN] = {0};
4603
4604 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4605 return 0;
4606
Hai Shalom60840252021-02-19 19:02:11 -08004607 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4608 if (is_zero_ether_addr(
4609 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4610 !is_zero_ether_addr(wpa_s->own_addr)) {
4611 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4612 wpa_s->own_addr, ETH_ALEN);
4613 }
4614 return 0;
4615 }
4616
4617 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004618 if (random_mac_addr(addr) < 0) {
4619 wpa_msg(wpa_s, MSG_INFO,
4620 "Failed to generate random MAC address");
4621 return -EINVAL;
4622 }
4623
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004624 /* Store generated MAC address. */
4625 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4626 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004627 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004628 /* If there are existing saved groups, restore last MAC address.
4629 * if there is no last used MAC address, the last one is
4630 * factory MAC. */
4631 if (is_zero_ether_addr(
4632 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004633 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004634 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4635 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004636 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4637 }
4638
4639 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4640 wpa_msg(wpa_s, MSG_INFO,
4641 "Failed to set random MAC address");
4642 return -EINVAL;
4643 }
4644
4645 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4646 wpa_msg(wpa_s, MSG_INFO,
4647 "Could not update MAC address information");
4648 return -EINVAL;
4649 }
4650
4651 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4652 MAC2STR(addr));
4653
4654 return 0;
4655}
4656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004657/**
4658 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4659 * @global: Pointer to global data from wpa_supplicant_init()
4660 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4661 * Returns: 0 on success, -1 on failure
4662 */
4663int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4664{
4665 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 int i;
4667
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004668 if (wpa_s->conf->p2p_disabled)
4669 return 0;
4670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4672 return 0;
4673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 if (global->p2p)
4675 return 0;
4676
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004677 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4678 wpa_msg(wpa_s, MSG_ERROR,
4679 "Failed to initialize P2P random MAC address.");
4680 return -1;
4681 }
4682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004684 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004685 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004686 p2p.p2p_scan = wpas_p2p_scan;
4687 p2p.send_action = wpas_send_action;
4688 p2p.send_action_done = wpas_send_action_done;
4689 p2p.go_neg_completed = wpas_go_neg_completed;
4690 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4691 p2p.dev_found = wpas_dev_found;
4692 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004693 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004694 p2p.start_listen = wpas_start_listen;
4695 p2p.stop_listen = wpas_stop_listen;
4696 p2p.send_probe_resp = wpas_send_probe_resp;
4697 p2p.sd_request = wpas_sd_request;
4698 p2p.sd_response = wpas_sd_response;
4699 p2p.prov_disc_req = wpas_prov_disc_req;
4700 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004701 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 p2p.invitation_process = wpas_invitation_process;
4703 p2p.invitation_received = wpas_invitation_received;
4704 p2p.invitation_result = wpas_invitation_result;
4705 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004706 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004707 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004708 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004709 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004710 p2p.get_persistent_group = wpas_get_persistent_group;
4711 p2p.get_go_info = wpas_get_go_info;
4712 p2p.remove_stale_groups = wpas_remove_stale_groups;
4713 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4714 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4715 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004716 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004717 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718
4719 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004720 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721 p2p.dev_name = wpa_s->conf->device_name;
4722 p2p.manufacturer = wpa_s->conf->manufacturer;
4723 p2p.model_name = wpa_s->conf->model_name;
4724 p2p.model_number = wpa_s->conf->model_number;
4725 p2p.serial_number = wpa_s->conf->serial_number;
4726 if (wpa_s->wps) {
4727 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4728 p2p.config_methods = wpa_s->wps->config_methods;
4729 }
4730
Hai Shalom60840252021-02-19 19:02:11 -08004731 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4732 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004733 wpa_printf(MSG_ERROR,
4734 "P2P: Failed to configure supported channel list");
4735 return -1;
4736 }
4737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738 if (wpa_s->conf->p2p_listen_reg_class &&
4739 wpa_s->conf->p2p_listen_channel) {
4740 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4741 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004742 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004744 /*
4745 * Pick one of the social channels randomly as the listen
4746 * channel.
4747 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004748 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004749 &p2p.channel,
4750 &global->p2p_go_avoid_freq,
4751 &global->p2p_disallow_freq) !=
4752 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004753 wpa_printf(MSG_INFO,
4754 "P2P: No social channels supported by the driver - do not enable P2P");
4755 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004756 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004757 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004759 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4760 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004761
4762 if (wpa_s->conf->p2p_oper_reg_class &&
4763 wpa_s->conf->p2p_oper_channel) {
4764 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4765 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4766 p2p.cfg_op_channel = 1;
4767 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4768 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4769
4770 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004771 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004772 * Use random operation channel from 2.4 GHz band social
4773 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4774 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004775 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004776 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08004777 &p2p.op_channel, NULL,
4778 NULL) != 0) {
4779 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004780 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08004781 p2p.op_reg_class = 0;
4782 p2p.op_channel = 0;
4783 /* This will be overridden during group setup in
4784 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004785 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004786 p2p.cfg_op_channel = 0;
4787 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4788 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4789 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004790
4791 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4792 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4793 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4794 }
4795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4797 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4798 p2p.country[2] = 0x04;
4799 } else
4800 os_memcpy(p2p.country, "XX\x04", 3);
4801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4803 WPS_DEV_TYPE_LEN);
4804
4805 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4806 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4807 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4808
4809 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4810 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4811
4812 p2p.max_peers = 100;
4813
4814 if (wpa_s->conf->p2p_ssid_postfix) {
4815 p2p.ssid_postfix_len =
4816 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4817 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4818 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4819 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4820 p2p.ssid_postfix_len);
4821 }
4822
4823 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4824
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004825 p2p.max_listen = wpa_s->max_remain_on_chan;
4826
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004827 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4828 wpa_s->conf->p2p_passphrase_len <= 63)
4829 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4830 else
4831 p2p.passphrase_len = 8;
4832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 global->p2p = p2p_init(&p2p);
4834 if (global->p2p == NULL)
4835 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837
4838 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4839 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4840 continue;
4841 p2p_add_wps_vendor_extension(
4842 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4843 }
4844
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004845 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847 return 0;
4848}
4849
4850
4851/**
4852 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4853 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4854 *
4855 * This function deinitialize per-interface P2P data.
4856 */
4857void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4858{
4859 if (wpa_s->driver && wpa_s->drv_priv)
4860 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004861
4862 if (wpa_s->go_params) {
4863 /* Clear any stored provisioning info */
4864 p2p_clear_provisioning_info(
4865 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004866 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004867 }
4868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 os_free(wpa_s->go_params);
4870 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004871 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004872 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4873 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07004874 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4876 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4877 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004878 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07004879 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004880 wpas_p2p_listen_work_done(wpa_s);
4881 if (wpa_s->p2p_send_action_work) {
4882 os_free(wpa_s->p2p_send_action_work->ctx);
4883 radio_work_done(wpa_s->p2p_send_action_work);
4884 wpa_s->p2p_send_action_work = NULL;
4885 }
4886 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004888 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4889 wpa_s->p2p_oob_dev_pw = NULL;
4890
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004891 os_free(wpa_s->p2p_group_common_freqs);
4892 wpa_s->p2p_group_common_freqs = NULL;
4893 wpa_s->p2p_group_common_freqs_num = 0;
4894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895 /* TODO: remove group interface from the driver if this wpa_s instance
4896 * is on top of a P2P group interface */
4897}
4898
4899
4900/**
4901 * wpas_p2p_deinit_global - Deinitialize global P2P module
4902 * @global: Pointer to global data from wpa_supplicant_init()
4903 *
4904 * This function deinitializes the global (per device) P2P module.
4905 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004906static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907{
4908 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004910 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004911
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004912 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913
4914 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4916 wpa_s = wpa_s->next;
4917 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918 tmp = global->ifaces;
4919 while (tmp &&
4920 (tmp == wpa_s ||
4921 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4922 tmp = tmp->next;
4923 }
4924 if (tmp == NULL)
4925 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004926 /* Disconnect from the P2P group and deinit the interface */
4927 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 }
4929
4930 /*
4931 * Deinit GO data on any possibly remaining interface (if main
4932 * interface is used as GO).
4933 */
4934 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4935 if (wpa_s->ap_iface)
4936 wpas_p2p_group_deinit(wpa_s);
4937 }
4938
4939 p2p_deinit(global->p2p);
4940 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004941 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942}
4943
4944
4945static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4946{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004947 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004948 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949 if (wpa_s->drv_flags &
4950 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4951 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4952 return 1; /* P2P group requires a new interface in every case
4953 */
4954 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4955 return 0; /* driver does not support concurrent operations */
4956 if (wpa_s->global->ifaces->next)
4957 return 1; /* more that one interface already in use */
4958 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4959 return 1; /* this interface is already in use */
4960 return 0;
4961}
4962
4963
4964static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4965 const u8 *peer_addr,
4966 enum p2p_wps_method wps_method,
4967 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004968 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004969 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004971 if (persistent_group && wpa_s->conf->persistent_reconnect)
4972 persistent_group = 2;
4973
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004974 /*
4975 * Increase GO config timeout if HT40 is used since it takes some time
4976 * to scan channels for coex purposes before the BSS can be started.
4977 */
4978 p2p_set_config_timeout(wpa_s->global->p2p,
4979 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4982 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004983 persistent_group, ssid ? ssid->ssid : NULL,
4984 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004985 wpa_s->p2p_pd_before_go_neg, pref_freq,
4986 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4987 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988}
4989
4990
4991static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4992 const u8 *peer_addr,
4993 enum p2p_wps_method wps_method,
4994 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004995 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004996 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004998 if (persistent_group && wpa_s->conf->persistent_reconnect)
4999 persistent_group = 2;
5000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5002 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005003 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005004 ssid ? ssid->ssid_len : 0, pref_freq,
5005 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5006 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007}
5008
5009
5010static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5011{
5012 wpa_s->p2p_join_scan_count++;
5013 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5014 wpa_s->p2p_join_scan_count);
5015 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5016 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5017 " for join operationg - stop join attempt",
5018 MAC2STR(wpa_s->pending_join_iface_addr));
5019 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005020 if (wpa_s->p2p_auto_pd) {
5021 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005022 wpa_msg_global(wpa_s, MSG_INFO,
5023 P2P_EVENT_PROV_DISC_FAILURE
5024 " p2p_dev_addr=" MACSTR " status=N/A",
5025 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005026 return;
5027 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005028 if (wpa_s->p2p_fallback_to_go_neg) {
5029 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5030 "failed - fall back to GO Negotiation");
5031 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5032 P2P_EVENT_FALLBACK_TO_GO_NEG
5033 "reason=join-failed");
5034 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5035 return;
5036 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005037 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005038 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005039 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040 }
5041}
5042
5043
Dmitry Shmidt04949592012-07-19 12:16:46 -07005044static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5045{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005046 int res;
5047 unsigned int num, i;
5048 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005049
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005050 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5051 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005052 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005053 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005054
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005055 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5056 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005057 if (!freqs)
5058 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005059
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005060 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5061 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005062
5063 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005064 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005065 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5066 freq);
5067 res = 0;
5068 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005069 }
5070 }
5071
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005072 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005073 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005074
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005075exit_free:
5076 os_free(freqs);
5077 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005078}
5079
5080
5081static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5082 const u8 *peer_dev_addr)
5083{
5084 struct wpa_bss *bss;
5085 int updated;
5086
5087 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5088 if (bss == NULL)
5089 return -1;
5090 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5091 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5092 "last scan");
5093 return 0;
5094 }
5095
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005096 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5097 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005098 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5099 "%ld.%06ld (%supdated in last scan)",
5100 bss->last_update.sec, bss->last_update.usec,
5101 updated ? "": "not ");
5102
5103 return updated;
5104}
5105
5106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5108 struct wpa_scan_results *scan_res)
5109{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005110 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111 int freq;
5112 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5115
5116 if (wpa_s->global->p2p_disabled)
5117 return;
5118
Dmitry Shmidt04949592012-07-19 12:16:46 -07005119 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5120 scan_res ? (int) scan_res->num : -1,
5121 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122
5123 if (scan_res)
5124 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5125
Dmitry Shmidt04949592012-07-19 12:16:46 -07005126 if (wpa_s->p2p_auto_pd) {
5127 int join = wpas_p2p_peer_go(wpa_s,
5128 wpa_s->pending_join_dev_addr);
5129 if (join == 0 &&
5130 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5131 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005132 bss = wpa_bss_get_bssid_latest(
5133 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005134 if (bss) {
5135 freq = bss->freq;
5136 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5137 "the peer " MACSTR " at %d MHz",
5138 wpa_s->auto_pd_scan_retry,
5139 MAC2STR(wpa_s->
5140 pending_join_dev_addr),
5141 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005142 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005143 return;
5144 }
5145 }
5146
5147 if (join < 0)
5148 join = 0;
5149
5150 wpa_s->p2p_auto_pd = 0;
5151 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5152 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5153 MAC2STR(wpa_s->pending_join_dev_addr), join);
5154 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005155 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005156 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005157 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005158 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005159 wpa_msg_global(wpa_s, MSG_INFO,
5160 P2P_EVENT_PROV_DISC_FAILURE
5161 " p2p_dev_addr=" MACSTR " status=N/A",
5162 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005163 }
5164 return;
5165 }
5166
5167 if (wpa_s->p2p_auto_join) {
5168 int join = wpas_p2p_peer_go(wpa_s,
5169 wpa_s->pending_join_dev_addr);
5170 if (join < 0) {
5171 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5172 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005173 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005174 P2P_EVENT_FALLBACK_TO_GO_NEG
5175 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005176 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5177 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5178 wpa_s->p2p_persistent_group, 0, 0, 0,
5179 wpa_s->p2p_go_intent,
5180 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005181 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005182 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005183 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005184 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005185 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005186 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005187 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005188 wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005189 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005190 return;
5191 }
5192
5193 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5194 "try to join the group", join ? "" :
5195 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005196 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005197 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005198 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005199 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005200 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005201 }
5202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5204 wpa_s->pending_join_iface_addr);
5205 if (freq < 0 &&
5206 p2p_get_interface_addr(wpa_s->global->p2p,
5207 wpa_s->pending_join_dev_addr,
5208 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005209 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5210 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5212 "address for join from " MACSTR " to " MACSTR
5213 " based on newly discovered P2P peer entry",
5214 MAC2STR(wpa_s->pending_join_iface_addr),
5215 MAC2STR(iface_addr));
5216 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5217 ETH_ALEN);
5218
5219 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5220 wpa_s->pending_join_iface_addr);
5221 }
5222 if (freq >= 0) {
5223 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5224 "from P2P peer table: %d MHz", freq);
5225 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005226 if (wpa_s->p2p_join_ssid_len) {
5227 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5228 MACSTR " and SSID %s",
5229 MAC2STR(wpa_s->pending_join_iface_addr),
5230 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5231 wpa_s->p2p_join_ssid_len));
5232 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5233 wpa_s->p2p_join_ssid,
5234 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005235 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005236 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5237 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5238 bss = wpa_bss_get_bssid_latest(wpa_s,
5239 wpa_s->pending_join_iface_addr);
5240 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005242 u8 dev_addr[ETH_ALEN];
5243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005244 freq = bss->freq;
5245 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005246 "from BSS table: %d MHz (SSID %s)", freq,
5247 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005248 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005249 dev_addr) == 0 &&
5250 os_memcmp(wpa_s->pending_join_dev_addr,
5251 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
5252 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
5253 ETH_ALEN) != 0) {
5254 wpa_printf(MSG_DEBUG,
5255 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5256 MAC2STR(dev_addr),
5257 MAC2STR(wpa_s->pending_join_dev_addr));
5258 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5259 ETH_ALEN);
5260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261 }
5262 if (freq > 0) {
5263 u16 method;
5264
Dmitry Shmidt04949592012-07-19 12:16:46 -07005265 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005266 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005267 P2P_EVENT_GROUP_FORMATION_FAILURE
5268 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005269 wpas_notify_p2p_group_formation_failure(
5270 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005271 return;
5272 }
5273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5275 "prior to joining an existing group (GO " MACSTR
5276 " freq=%u MHz)",
5277 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5278 wpa_s->pending_pd_before_join = 1;
5279
5280 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281 case WPS_PIN_DISPLAY:
5282 method = WPS_CONFIG_KEYPAD;
5283 break;
5284 case WPS_PIN_KEYPAD:
5285 method = WPS_CONFIG_DISPLAY;
5286 break;
5287 case WPS_PBC:
5288 method = WPS_CONFIG_PUSHBUTTON;
5289 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005290 case WPS_P2PS:
5291 method = WPS_CONFIG_P2PS;
5292 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005293 default:
5294 method = 0;
5295 break;
5296 }
5297
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005298 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5299 wpa_s->pending_join_dev_addr) ==
5300 method)) {
5301 /*
5302 * We have already performed provision discovery for
5303 * joining the group. Proceed directly to join
5304 * operation without duplicated provision discovery. */
5305 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5306 "with " MACSTR " already done - proceed to "
5307 "join",
5308 MAC2STR(wpa_s->pending_join_dev_addr));
5309 wpa_s->pending_pd_before_join = 0;
5310 goto start;
5311 }
5312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005313 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005314 wpa_s->pending_join_dev_addr,
5315 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005316 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005317 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5318 "Discovery Request before joining an "
5319 "existing group");
5320 wpa_s->pending_pd_before_join = 0;
5321 goto start;
5322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323 return;
5324 }
5325
5326 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5327 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5328 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5329 wpas_p2p_check_join_scan_limit(wpa_s);
5330 return;
5331
5332start:
5333 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005334 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5335 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336}
5337
5338
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005339static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5340 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342 int ret;
5343 struct wpa_driver_scan_params params;
5344 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005345 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005346 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005347 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005348
5349 os_memset(&params, 0, sizeof(params));
5350
5351 /* P2P Wildcard SSID */
5352 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005353 if (ssid && ssid_len) {
5354 params.ssids[0].ssid = ssid;
5355 params.ssids[0].ssid_len = ssid_len;
5356 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5357 wpa_s->p2p_join_ssid_len = ssid_len;
5358 } else {
5359 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5360 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5361 wpa_s->p2p_join_ssid_len = 0;
5362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005363
5364 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005365 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5366 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5367 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 if (wps_ie == NULL) {
5369 wpas_p2p_scan_res_join(wpa_s, NULL);
5370 return;
5371 }
5372
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005373 if (!freq) {
5374 int oper_freq;
5375 /*
5376 * If freq is not provided, check the operating freq of the GO
5377 * and use a single channel scan on if possible.
5378 */
5379 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5380 wpa_s->pending_join_iface_addr);
5381 if (oper_freq > 0)
5382 freq = oper_freq;
5383 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005384 if (freq > 0) {
5385 freqs[0] = freq;
5386 params.freqs = freqs;
Hai Shalom899fcc72020-10-19 14:38:18 -07005387 } else if (wpa_s->conf->p2p_6ghz_disable) {
5388 wpa_printf(MSG_DEBUG,
5389 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -08005390 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, &params,
5391 0);
5392 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
5393 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005395
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005396 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5397 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5398 if (ies == NULL) {
5399 wpabuf_free(wps_ie);
5400 wpas_p2p_scan_res_join(wpa_s, NULL);
5401 return;
5402 }
5403 wpabuf_put_buf(ies, wps_ie);
5404 wpabuf_free(wps_ie);
5405
5406 bands = wpas_get_bands(wpa_s, freqs);
5407 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5408
5409 params.p2p_probe = 1;
5410 params.extra_ies = wpabuf_head(ies);
5411 params.extra_ies_len = wpabuf_len(ies);
5412
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005413 if (wpa_s->clear_driver_scan_cache) {
5414 wpa_printf(MSG_DEBUG,
5415 "Request driver to clear scan cache due to local BSS flush");
5416 params.only_new_results = 1;
5417 }
5418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005419 /*
5420 * Run a scan to update BSS table and start Provision Discovery once
5421 * the new scan results become available.
5422 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005423 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalom899fcc72020-10-19 14:38:18 -07005424 if (wpa_s->conf->p2p_6ghz_disable && params.freqs != freqs)
5425 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005426 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005427 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005428 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005429 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005430 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432
5433 wpabuf_free(ies);
5434
5435 if (ret) {
5436 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5437 "try again later");
5438 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5439 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5440 wpas_p2p_check_join_scan_limit(wpa_s);
5441 }
5442}
5443
5444
Dmitry Shmidt04949592012-07-19 12:16:46 -07005445static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5446{
5447 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005448 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005449}
5450
5451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005453 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005454 int auto_join, int op_freq,
5455 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456{
5457 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005458 MACSTR " dev " MACSTR " op_freq=%d)%s",
5459 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005460 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005461 if (ssid && ssid_len) {
5462 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5463 wpa_ssid_txt(ssid, ssid_len));
5464 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465
Dmitry Shmidt04949592012-07-19 12:16:46 -07005466 wpa_s->p2p_auto_pd = 0;
5467 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5469 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5470 wpa_s->pending_join_wps_method = wps_method;
5471
5472 /* Make sure we are not running find during connection establishment */
5473 wpas_p2p_stop_find(wpa_s);
5474
5475 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005476 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 return 0;
5478}
5479
5480
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005481static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5482 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005483{
5484 struct wpa_supplicant *group;
5485 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005486 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005487
5488 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5489 if (group == NULL)
5490 return -1;
5491 if (group != wpa_s) {
5492 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5493 sizeof(group->p2p_pin));
5494 group->p2p_wps_method = wpa_s->p2p_wps_method;
5495 }
5496
Hai Shalom74f70d42019-02-11 14:42:39 -08005497 /*
5498 * Need to mark the current interface for p2p_group_formation
5499 * when a separate group interface is not used. This is needed
5500 * to allow p2p_cancel stop a pending p2p_connect-join.
5501 * wpas_p2p_init_group_interface() addresses this for the case
5502 * where a separate group interface is used.
5503 */
5504 if (group == wpa_s->parent)
5505 wpa_s->global->p2p_group_formation = group;
5506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005508 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509
5510 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005511 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5513 ETH_ALEN);
5514 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005515 if (freq && ssid && ssid_len) {
5516 res.freq = freq;
5517 res.ssid_len = ssid_len;
5518 os_memcpy(res.ssid, ssid, ssid_len);
5519 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005520 if (ssid && ssid_len) {
5521 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5522 ssid, ssid_len);
5523 } else {
5524 bss = wpa_bss_get_bssid_latest(
5525 wpa_s, wpa_s->pending_join_iface_addr);
5526 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005527 if (bss) {
5528 res.freq = bss->freq;
5529 res.ssid_len = bss->ssid_len;
5530 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5531 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5532 bss->freq,
5533 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005534 } else if (ssid && ssid_len) {
5535 res.ssid_len = ssid_len;
5536 os_memcpy(res.ssid, ssid, ssid_len);
5537 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5538 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005539 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005540 }
5541
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005542 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5543 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5544 "starting client");
5545 wpa_drv_cancel_remain_on_channel(wpa_s);
5546 wpa_s->off_channel_freq = 0;
5547 wpa_s->roc_waiting_drv_freq = 0;
5548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005549 wpas_start_wps_enrollee(group, &res);
5550
5551 /*
5552 * Allow a longer timeout for join-a-running-group than normal 15
5553 * second group formation timeout since the GO may not have authorized
5554 * our connection yet.
5555 */
5556 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5557 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5558 wpa_s, NULL);
5559
5560 return 0;
5561}
5562
5563
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005564static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005565 int *force_freq, int *pref_freq, int go,
5566 unsigned int *pref_freq_list,
5567 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005568{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005569 struct wpa_used_freq_data *freqs;
5570 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005571 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad70e5f9d2021-08-30 21:28:05 +05305572 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005573
5574 max_pref_freq = *num_pref_freq;
5575 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005576
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005577 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5578 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005579 if (!freqs)
5580 return -1;
5581
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005582 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5583 wpa_s->num_multichan_concurrent);
5584
5585 /*
5586 * It is possible that the total number of used frequencies is bigger
5587 * than the number of frequencies used for P2P, so get the system wide
5588 * number of unused frequencies.
5589 */
5590 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5591
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005592 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005593 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5594 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005595
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005596 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005597 int ret;
5598 if (go)
5599 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5600 else
5601 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5602 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005603 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005604 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5605 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005606 /*
5607 * If freq is a DFS channel and DFS is offloaded
5608 * to the driver, allow P2P GO to use it.
5609 */
5610 wpa_printf(MSG_DEBUG,
5611 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5612 freq);
5613 } else {
5614 wpa_printf(MSG_DEBUG,
5615 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5616 freq);
5617 res = -3;
5618 goto exit_free;
5619 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005620 }
5621
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005622 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005623 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005624 freq_in_use = 1;
5625 }
5626
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005627 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005628 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5629 freq);
5630 res = -2;
5631 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005632 }
5633 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5634 "requested channel (%u MHz)", freq);
5635 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005636 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005637 }
5638
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005639 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005640
Vinayak Yadawad70e5f9d2021-08-30 21:28:05 +05305641 if (*pref_freq == 0) {
5642 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005643 i = 0;
Vinayak Yadawad70e5f9d2021-08-30 21:28:05 +05305644 while (i < wpa_s->conf->num_p2p_pref_chan) {
5645 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5646 wpa_s->conf->p2p_pref_chan[i].op_class,
5647 wpa_s->conf->p2p_pref_chan[i].chan);
5648
5649 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5650 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5651 best_freq = p2p_pref_freq;
5652 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5653 "from P2P preferred channel list", best_freq);
5654 break;
5655 } else {
5656 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5657 "frequency (%u MHz) ", p2p_pref_freq);
5658 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005659 i++;
5660 }
Vinayak Yadawad70e5f9d2021-08-30 21:28:05 +05305661 } else {
5662 enum wpa_driver_if_type iface_type;
5663
5664 if (go)
5665 iface_type = WPA_IF_P2P_GO;
5666 else
5667 iface_type = WPA_IF_P2P_CLIENT;
5668
5669 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5670 best_freq, go);
5671
5672 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5673 &max_pref_freq,
5674 pref_freq_list);
5675 if (!res && max_pref_freq > 0) {
5676 *num_pref_freq = max_pref_freq;
5677 i = 0;
5678 while (i < *num_pref_freq &&
5679 (!p2p_supported_freq(wpa_s->global->p2p,
5680 pref_freq_list[i]) ||
5681 wpas_p2p_disallowed_freq(wpa_s->global,
5682 pref_freq_list[i]))) {
5683 wpa_printf(MSG_DEBUG,
5684 "P2P: preferred_freq_list[%d]=%d is disallowed",
5685 i, pref_freq_list[i]);
5686 i++;
5687 }
5688 if (i != *num_pref_freq) {
5689 best_freq = pref_freq_list[i];
5690 wpa_printf(MSG_DEBUG,
5691 "P2P: Using preferred_freq_list[%d]=%d",
5692 i, best_freq);
5693 } else {
5694 wpa_printf(MSG_DEBUG,
5695 "P2P: All driver preferred frequencies are "
5696 "disallowed for P2P use");
5697 *num_pref_freq = 0;
5698 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005699 } else {
5700 wpa_printf(MSG_DEBUG,
Vinayak Yadawad70e5f9d2021-08-30 21:28:05 +05305701 "P2P: No preferred frequency list available");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005702 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005703 }
5704 }
5705
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005706 /* We have a candidate frequency to use */
5707 if (best_freq > 0) {
5708 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005709 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005710 best_freq);
5711 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005712 } else {
5713 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 -07005714 best_freq);
5715 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005716 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005717 } else if (num_unused > 0) {
5718 wpa_printf(MSG_DEBUG,
5719 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5720 *force_freq = 0;
5721 } else {
5722 wpa_printf(MSG_DEBUG,
5723 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5724 res = -2;
5725 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005726 }
5727
5728exit_ok:
5729 res = 0;
5730exit_free:
5731 os_free(freqs);
5732 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005733}
5734
5735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005736/**
5737 * wpas_p2p_connect - Request P2P Group Formation to be started
5738 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5739 * @peer_addr: Address of the peer P2P Device
5740 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5741 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005742 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743 * @join: Whether to join an existing group (as a client) instead of starting
5744 * Group Owner negotiation; @peer_addr is BSSID in that case
5745 * @auth: Whether to only authorize the connection instead of doing that and
5746 * initiating Group Owner negotiation
5747 * @go_intent: GO Intent or -1 to use default
5748 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005749 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005750 * @persistent_id: Persistent group credentials to use for forcing GO
5751 * parameters or -1 to generate new values (SSID/passphrase)
5752 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5753 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005754 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005755 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005756 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07005757 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005758 * @group_ssid: Specific Group SSID for join or %NULL if not set
5759 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005760 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5761 * failure, -2 on failure due to channel not currently available,
5762 * -3 if forced channel is not supported
5763 */
5764int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5765 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005766 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005767 int go_intent, int freq, unsigned int vht_center_freq2,
5768 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005769 unsigned int vht_chwidth, int he, int edmg,
5770 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005771{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005772 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005773 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005774 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005775 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005776 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005777 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778
5779 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5780 return -1;
5781
Dmitry Shmidt04949592012-07-19 12:16:46 -07005782 if (persistent_id >= 0) {
5783 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5784 if (ssid == NULL || ssid->disabled != 2 ||
5785 ssid->mode != WPAS_MODE_P2P_GO)
5786 return -1;
5787 }
5788
Hai Shalom899fcc72020-10-19 14:38:18 -07005789 if (is_6ghz_freq(freq) && wpa_s->conf->p2p_6ghz_disable)
5790 return -2;
5791
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005792 os_free(wpa_s->global->add_psk);
5793 wpa_s->global->add_psk = NULL;
5794
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005795 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005796 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005797 wpa_s->global->pending_p2ps_group_freq = 0;
5798 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 if (go_intent < 0)
5801 go_intent = wpa_s->conf->p2p_go_intent;
5802
5803 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07005804 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805
5806 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005807 wpa_s->p2p_persistent_group = !!persistent_group;
5808 wpa_s->p2p_persistent_id = persistent_id;
5809 wpa_s->p2p_go_intent = go_intent;
5810 wpa_s->p2p_connect_freq = freq;
5811 wpa_s->p2p_fallback_to_go_neg = 0;
5812 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005813 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005814 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005815 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5816 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08005817 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005818 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005819
5820 if (pin)
5821 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5822 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005823 if (wps_generate_pin((unsigned int *) &ret) < 0)
5824 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005825 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5826 "%08d", ret);
5827 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5828 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5830 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005831 } else if (wps_method == WPS_P2PS) {
5832 /* Force the P2Ps default PIN to be used */
5833 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005834 } else
5835 wpa_s->p2p_pin[0] = '\0';
5836
Dmitry Shmidt04949592012-07-19 12:16:46 -07005837 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005838 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5839 if (auth) {
5840 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5841 "connect a running group from " MACSTR,
5842 MAC2STR(peer_addr));
5843 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5844 return ret;
5845 }
5846 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5847 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5848 iface_addr) < 0) {
5849 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5850 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5851 dev_addr);
5852 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005853 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005854 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005855 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5856 "%ld.%06ld",
5857 wpa_s->p2p_auto_started.sec,
5858 wpa_s->p2p_auto_started.usec);
5859 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005860 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005861 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005862 auto_join, freq,
5863 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 return -1;
5865 return ret;
5866 }
5867
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005868 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005869 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005870 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005871 if (res)
5872 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005873 wpas_p2p_set_own_freq_preference(wpa_s,
5874 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005875
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005876 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005880 if (wpa_s->create_p2p_iface) {
5881 /* Prepare to add a new interface for the group */
5882 iftype = WPA_IF_P2P_GROUP;
5883 if (go_intent == 15)
5884 iftype = WPA_IF_P2P_GO;
5885 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5886 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5887 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005889 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005890
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005891 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005892 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005893 if (wpa_s->p2p_mgmt)
5894 if_addr = wpa_s->parent->own_addr;
5895 else
5896 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005897 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5898 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899
5900 if (auth) {
5901 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005902 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005903 force_freq, persistent_group, ssid,
5904 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905 return -1;
5906 return ret;
5907 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005908
5909 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5910 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005911 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005912 if (wpa_s->create_p2p_iface)
5913 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914 return -1;
5915 }
5916 return ret;
5917}
5918
5919
5920/**
5921 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5922 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5923 * @freq: Frequency of the channel in MHz
5924 * @duration: Duration of the stay on the channel in milliseconds
5925 *
5926 * This callback is called when the driver indicates that it has started the
5927 * requested remain-on-channel duration.
5928 */
5929void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5930 unsigned int freq, unsigned int duration)
5931{
5932 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5933 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005934 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)",
5935 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5936 wpa_s->roc_waiting_drv_freq, freq, duration);
5937 if (wpa_s->off_channel_freq &&
5938 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005939 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5940 wpa_s->pending_listen_duration);
5941 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005942 } else {
5943 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5944 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5945 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005946 }
5947}
5948
5949
Jithu Jance57cea1a2014-08-20 10:22:04 -07005950int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951{
5952 /* Limit maximum Listen state time based on driver limitation. */
5953 if (timeout > wpa_s->max_remain_on_chan)
5954 timeout = wpa_s->max_remain_on_chan;
5955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956 return p2p_listen(wpa_s->global->p2p, timeout);
5957}
5958
5959
5960/**
5961 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5962 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5963 * @freq: Frequency of the channel in MHz
5964 *
5965 * This callback is called when the driver indicates that a remain-on-channel
5966 * operation has been completed, i.e., the duration on the requested channel
5967 * has timed out.
5968 */
5969void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5970 unsigned int freq)
5971{
5972 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5973 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07005974 wpa_s->global->p2p_long_listen,
5975 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005976 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005977 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5978 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005979 if (wpa_s->global->p2p_long_listen > 0)
5980 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5982 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005983 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07005985 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07005987 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005988 } else {
5989 /*
5990 * When listen duration is over, stop listen & update p2p_state
5991 * to IDLE.
5992 */
5993 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994 }
5995}
5996
5997
5998/**
5999 * wpas_p2p_group_remove - Remove a P2P group
6000 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6001 * @ifname: Network interface name of the group interface or "*" to remove all
6002 * groups
6003 * Returns: 0 on success, -1 on failure
6004 *
6005 * This function is used to remove a P2P group. This can be used to disconnect
6006 * from a group in which the local end is a P2P Client or to end a P2P Group in
6007 * case the local end is the Group Owner. If a virtual network interface was
6008 * created for this group, that interface will be removed. Otherwise, only the
6009 * configured P2P group network will be removed from the interface.
6010 */
6011int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6012{
6013 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006014 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015
6016 if (os_strcmp(ifname, "*") == 0) {
6017 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006018 bool calling_wpa_s_group_removed = false;
6019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006020 wpa_s = global->ifaces;
6021 while (wpa_s) {
6022 prev = wpa_s;
6023 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006024 if (prev->p2p_group_interface !=
6025 NOT_P2P_GROUP_INTERFACE ||
6026 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006027 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006028 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006029 if (prev == calling_wpa_s)
6030 calling_wpa_s_group_removed = true;
6031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006033
6034 if (!calling_wpa_s_group_removed &&
6035 (calling_wpa_s->p2p_group_interface !=
6036 NOT_P2P_GROUP_INTERFACE ||
6037 (calling_wpa_s->current_ssid &&
6038 calling_wpa_s->current_ssid->p2p_group))) {
6039 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6040 wpas_p2p_disconnect_safely(calling_wpa_s,
6041 calling_wpa_s);
6042 }
6043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044 return 0;
6045 }
6046
6047 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6048 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6049 break;
6050 }
6051
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006052 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053}
6054
6055
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006056static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6057{
6058 unsigned int r;
6059
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006060 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6061 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
6062 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
6063 int res;
6064
6065 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6066 &size, pref_freq_list);
6067 if (!res && size > 0) {
6068 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006069 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006070 (!p2p_supported_freq(wpa_s->global->p2p,
6071 pref_freq_list[i]) ||
6072 wpas_p2p_disallowed_freq(wpa_s->global,
6073 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006074 wpa_printf(MSG_DEBUG,
6075 "P2P: preferred_freq_list[%d]=%d is disallowed",
6076 i, pref_freq_list[i]);
6077 i++;
6078 }
6079 if (i != size) {
6080 freq = pref_freq_list[i];
6081 wpa_printf(MSG_DEBUG,
6082 "P2P: Using preferred_freq_list[%d]=%d",
6083 i, freq);
6084 } else {
6085 wpa_printf(MSG_DEBUG,
6086 "P2P: All driver preferred frequencies are disallowed for P2P use");
6087 }
6088 } else {
6089 wpa_printf(MSG_DEBUG,
6090 "P2P: No preferred frequency list available");
6091 }
6092 }
6093
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006094 if (freq == 2) {
6095 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6096 "band");
6097 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006098 p2p_supported_freq_go(wpa_s->global->p2p,
6099 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006100 freq = wpa_s->best_24_freq;
6101 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6102 "channel: %d MHz", freq);
6103 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006104 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6105 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006106 freq = 2412 + (r % 3) * 25;
6107 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6108 "channel: %d MHz", freq);
6109 }
6110 }
6111
6112 if (freq == 5) {
6113 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6114 "band");
6115 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006116 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006117 wpa_s->best_5_freq)) {
6118 freq = wpa_s->best_5_freq;
6119 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6120 "channel: %d MHz", freq);
6121 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006122 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6123 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006124
6125 /*
6126 * most of 5G channels are DFS, only operating class 115 and 124
6127 * are available possibly, randomly pick a start to check them.
6128 */
6129 int possible_5g_freqs[] = {
6130 /* operating class 115 */
6131 5180, 5200, 5220, 5240,
6132 /* operating class 124 */
6133 5745, 5765, 5785, 5805,
6134 };
6135 int possible_5g_freqs_num =
6136 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6137
6138 int i;
6139 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6140 if (p2p_supported_freq_go(
6141 wpa_s->global->p2p,
6142 possible_5g_freqs[r % possible_5g_freqs_num])) {
6143 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6144 break;
6145 }
6146 }
6147
6148 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006149 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6150 "5 GHz channel for P2P group");
6151 return -1;
6152 }
6153 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6154 "channel: %d MHz", freq);
6155 }
6156 }
6157
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006158 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006159 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006160 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6161 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006162 /*
6163 * If freq is a DFS channel and DFS is offloaded to the
6164 * driver, allow P2P GO to use it.
6165 */
6166 wpa_printf(MSG_DEBUG, "P2P: "
6167 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6168 __func__, freq);
6169 return freq;
6170 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006171 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6172 "(%u MHz) is not supported for P2P uses",
6173 freq);
6174 return -1;
6175 }
6176
6177 return freq;
6178}
6179
6180
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006181static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6182 const struct p2p_channels *channels,
6183 int freq)
6184{
6185 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6186 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6187 freq_included(wpa_s, channels, freq))
6188 return 1;
6189 return 0;
6190}
6191
6192
6193static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6194 struct p2p_go_neg_results *params,
6195 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006196{
6197 unsigned int i, r;
6198
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006199 /* try all channels in operating class 115 */
6200 for (i = 0; i < 4; i++) {
6201 params->freq = 5180 + i * 20;
6202 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006203 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006204 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6205 goto out;
6206 }
6207
6208 /* try all channels in operating class 124 */
6209 for (i = 0; i < 4; i++) {
6210 params->freq = 5745 + i * 20;
6211 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006212 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006213 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6214 goto out;
6215 }
6216
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006217 /* try social channel class 180 channel 2 */
6218 params->freq = 58320 + 1 * 2160;
6219 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006220 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006221 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6222 goto out;
6223
6224 /* try all channels in reg. class 180 */
6225 for (i = 0; i < 4; i++) {
6226 params->freq = 58320 + i * 2160;
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 Shmidt661b4f72014-09-29 14:58:27 -07006229 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6230 goto out;
6231 }
6232
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006233 /* try some random selection of the social channels */
6234 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6235 return;
6236
6237 for (i = 0; i < 3; i++) {
6238 params->freq = 2412 + ((r + i) % 3) * 25;
6239 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6240 goto out;
6241 }
6242
6243 /* try all other channels in operating class 81 */
6244 for (i = 0; i < 11; i++) {
6245 params->freq = 2412 + i * 5;
6246
6247 /* skip social channels; covered in the previous loop */
6248 if (params->freq == 2412 ||
6249 params->freq == 2437 ||
6250 params->freq == 2462)
6251 continue;
6252
6253 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6254 goto out;
6255 }
6256
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006257 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006258 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006259 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006260out:
6261 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6262 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006263}
6264
6265
Roshan Pius3a1667e2018-07-03 15:17:14 -07006266static int wpas_same_band(int freq1, int freq2)
6267{
6268 enum hostapd_hw_mode mode1, mode2;
6269 u8 chan1, chan2;
6270
6271 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6272 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6273 if (mode1 == NUM_HOSTAPD_MODES)
6274 return 0;
6275 return mode1 == mode2;
6276}
6277
6278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006279static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6280 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006281 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006282 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006283 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006284 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006285{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006286 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006287 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006288 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006289 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006290 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291
6292 os_memset(params, 0, sizeof(*params));
6293 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006294 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006295 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006296 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006297 params->max_oper_chwidth = max_oper_chwidth;
6298 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006299 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006301 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6302 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006303 if (!freqs)
6304 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006305
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006306 num = get_shared_radio_freqs_data(wpa_s, freqs,
6307 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006308
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006309 if (wpa_s->current_ssid &&
6310 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6311 wpa_s->wpa_state == WPA_COMPLETED) {
6312 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6313 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006314
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006315 /*
6316 * If the frequency selection is done for an active P2P GO that
6317 * is not sharing a frequency, allow to select a new frequency
6318 * even if there are no unused frequencies as we are about to
6319 * move the P2P GO so its frequency can be re-used.
6320 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006321 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006322 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6323 freqs[i].flags == 0) {
6324 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006325 break;
6326 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006327 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006328 }
6329
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006330 /* Try to use EDMG channel */
6331 if (params->edmg) {
6332 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6333 goto success;
6334 params->edmg = 0;
6335 }
6336
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006337 /* try using the forced freq */
6338 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006339 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6340 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006341 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006342 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006343 freq);
6344 goto fail;
6345 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006346 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6347 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006348 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6349 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006350 /*
6351 * If freq is a DFS channel and DFS is offloaded
6352 * to the driver, allow P2P GO to use it.
6353 */
6354 wpa_printf(MSG_DEBUG,
6355 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6356 __func__, freq);
6357 } else {
6358 wpa_printf(MSG_DEBUG,
6359 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6360 freq);
6361 goto fail;
6362 }
6363 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006364
6365 for (i = 0; i < num; i++) {
6366 if (freqs[i].freq == freq) {
6367 wpa_printf(MSG_DEBUG,
6368 "P2P: forced freq (%d MHz) is also shared",
6369 freq);
6370 params->freq = freq;
6371 goto success;
6372 }
6373 }
6374
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006375 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006376 wpa_printf(MSG_DEBUG,
6377 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6378 freq);
6379 goto fail;
6380 }
6381
6382 wpa_printf(MSG_DEBUG,
6383 "P2P: force GO freq (%d MHz) on a free channel",
6384 freq);
6385 params->freq = freq;
6386 goto success;
6387 }
6388
6389 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006390 if (num &&
6391 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006392 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6393 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6394 wpa_printf(MSG_DEBUG,
6395 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006396 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006397 params->freq = cand;
6398 goto success;
6399 }
6400
6401 /* try using one of the shared freqs */
6402 for (i = 0; i < num; i++) {
6403 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6404 freqs[i].freq)) {
6405 wpa_printf(MSG_DEBUG,
6406 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006407 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006408 params->freq = freqs[i].freq;
6409 goto success;
6410 }
6411 }
6412 }
6413
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006414 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006415 wpa_printf(MSG_DEBUG,
6416 "P2P: Cannot force GO on any of the channels we are already using");
6417 goto fail;
6418 }
6419
6420 /* try using the setting from the configuration file */
6421 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6422 wpa_s->conf->p2p_oper_channel >= 1 &&
6423 wpa_s->conf->p2p_oper_channel <= 11 &&
6424 wpas_p2p_supported_freq_go(
6425 wpa_s, channels,
6426 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6427 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6428 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6429 "frequency %d MHz", params->freq);
6430 goto success;
6431 }
6432
6433 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6434 wpa_s->conf->p2p_oper_reg_class == 116 ||
6435 wpa_s->conf->p2p_oper_reg_class == 117 ||
6436 wpa_s->conf->p2p_oper_reg_class == 124 ||
6437 wpa_s->conf->p2p_oper_reg_class == 125 ||
6438 wpa_s->conf->p2p_oper_reg_class == 126 ||
6439 wpa_s->conf->p2p_oper_reg_class == 127) &&
6440 wpas_p2p_supported_freq_go(wpa_s, channels,
6441 5000 +
6442 5 * wpa_s->conf->p2p_oper_channel)) {
6443 params->freq = 5000 + 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 /* Try using best channels */
6450 if (wpa_s->conf->p2p_oper_channel == 0 &&
6451 wpa_s->best_overall_freq > 0 &&
6452 wpas_p2p_supported_freq_go(wpa_s, channels,
6453 wpa_s->best_overall_freq)) {
6454 params->freq = wpa_s->best_overall_freq;
6455 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6456 "channel %d MHz", params->freq);
6457 goto success;
6458 }
6459
6460 if (wpa_s->conf->p2p_oper_channel == 0 &&
6461 wpa_s->best_24_freq > 0 &&
6462 wpas_p2p_supported_freq_go(wpa_s, channels,
6463 wpa_s->best_24_freq)) {
6464 params->freq = wpa_s->best_24_freq;
6465 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6466 "channel %d MHz", params->freq);
6467 goto success;
6468 }
6469
6470 if (wpa_s->conf->p2p_oper_channel == 0 &&
6471 wpa_s->best_5_freq > 0 &&
6472 wpas_p2p_supported_freq_go(wpa_s, channels,
6473 wpa_s->best_5_freq)) {
6474 params->freq = wpa_s->best_5_freq;
6475 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6476 "channel %d MHz", params->freq);
6477 goto success;
6478 }
6479
6480 /* try using preferred channels */
6481 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6482 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6483 params->freq = cand;
6484 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6485 "channels", params->freq);
6486 goto success;
6487 }
6488
Roshan Pius3a1667e2018-07-03 15:17:14 -07006489 /* Try using a channel that allows VHT to be used with 80 MHz */
6490 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6491 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6492 enum hostapd_hw_mode mode;
6493 struct hostapd_hw_modes *hwmode;
6494 u8 chan;
6495
6496 cand = wpa_s->p2p_group_common_freqs[i];
6497 mode = ieee80211_freq_to_chan(cand, &chan);
6498 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006499 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006500 if (!hwmode ||
6501 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6502 BW80) != ALLOWED)
6503 continue;
6504 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6505 params->freq = cand;
6506 wpa_printf(MSG_DEBUG,
6507 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6508 params->freq);
6509 goto success;
6510 }
6511 }
6512 }
6513
6514 /* Try using a channel that allows HT to be used with 40 MHz on the same
6515 * band so that CSA can be used */
6516 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6517 wpa_s->p2p_group_common_freqs) {
6518 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6519 enum hostapd_hw_mode mode;
6520 struct hostapd_hw_modes *hwmode;
6521 u8 chan;
6522
6523 cand = wpa_s->p2p_group_common_freqs[i];
6524 mode = ieee80211_freq_to_chan(cand, &chan);
6525 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006526 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006527 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6528 cand) ||
6529 !hwmode ||
6530 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6531 BW40MINUS) != ALLOWED &&
6532 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6533 BW40PLUS) != ALLOWED))
6534 continue;
6535 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6536 params->freq = cand;
6537 wpa_printf(MSG_DEBUG,
6538 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6539 params->freq);
6540 goto success;
6541 }
6542 }
6543 }
6544
6545 /* Try using one of the group common freqs on the same band so that CSA
6546 * can be used */
6547 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6548 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6549 cand = wpa_s->p2p_group_common_freqs[i];
6550 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6551 cand))
6552 continue;
6553 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6554 params->freq = cand;
6555 wpa_printf(MSG_DEBUG,
6556 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6557 params->freq);
6558 goto success;
6559 }
6560 }
6561 }
6562
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006563 /* Try using one of the group common freqs */
6564 if (wpa_s->p2p_group_common_freqs) {
6565 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6566 cand = wpa_s->p2p_group_common_freqs[i];
6567 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6568 params->freq = cand;
6569 wpa_printf(MSG_DEBUG,
6570 "P2P: Use freq %d MHz common with the peer",
6571 params->freq);
6572 goto success;
6573 }
6574 }
6575 }
6576
6577 /* no preference, select some channel */
6578 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6579
6580 if (params->freq == 0) {
6581 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6582 goto fail;
6583 }
6584
6585success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006586 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006587 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006588fail:
6589 os_free(freqs);
6590 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006591}
6592
6593
6594static struct wpa_supplicant *
6595wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6596 int go)
6597{
6598 struct wpa_supplicant *group_wpa_s;
6599
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006600 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006601 if (wpa_s->p2p_mgmt) {
6602 /*
6603 * We may be called on the p2p_dev interface which
6604 * cannot be used for group operations, so always use
6605 * the primary interface.
6606 */
6607 wpa_s->parent->p2pdev = wpa_s;
6608 wpa_s = wpa_s->parent;
6609 }
6610 wpa_dbg(wpa_s, MSG_DEBUG,
6611 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006612 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006613 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006614 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006615 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617
6618 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006619 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006620 wpa_msg_global(wpa_s, MSG_ERROR,
6621 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006622 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006624 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6625 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006626 wpa_msg_global(wpa_s, MSG_ERROR,
6627 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006628 wpas_p2p_remove_pending_group_interface(wpa_s);
6629 return NULL;
6630 }
6631
Roshan Pius3a1667e2018-07-03 15:17:14 -07006632 if (go && wpa_s->p2p_go_do_acs) {
6633 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6634 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6635 wpa_s->p2p_go_do_acs = 0;
6636 }
6637
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006638 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6639 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006640 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006641 return group_wpa_s;
6642}
6643
6644
6645/**
6646 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6647 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6648 * @persistent_group: Whether to create a persistent group
6649 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006650 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006651 * @ht40: Start GO with 40 MHz channel width
6652 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006653 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006654 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006655 * Returns: 0 on success, -1 on failure
6656 *
6657 * This function creates a new P2P group with the local end as the Group Owner,
6658 * i.e., without using Group Owner Negotiation.
6659 */
6660int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006661 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006662 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006663{
6664 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006665
6666 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6667 return -1;
6668
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006669 os_free(wpa_s->global->add_psk);
6670 wpa_s->global->add_psk = NULL;
6671
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006672 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006673 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006674 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006675
Roshan Pius3a1667e2018-07-03 15:17:14 -07006676 if (!wpa_s->p2p_go_do_acs) {
6677 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6678 if (freq < 0)
6679 return -1;
6680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006682 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006683 ht40, vht, max_oper_chwidth, he, edmg,
6684 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006685 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006687 p2p_go_params(wpa_s->global->p2p, &params);
6688 params.persistent_group = persistent_group;
6689
6690 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6691 if (wpa_s == NULL)
6692 return -1;
6693 wpas_start_wps_go(wpa_s, &params, 0);
6694
6695 return 0;
6696}
6697
6698
6699static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006700 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006701 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006702{
6703 struct wpa_ssid *ssid;
6704
6705 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6706 if (wpa_s == NULL)
6707 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006708 if (force_scan)
6709 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006710 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006711
6712 wpa_supplicant_ap_deinit(wpa_s);
6713
6714 ssid = wpa_config_add_network(wpa_s->conf);
6715 if (ssid == NULL)
6716 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006717 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006718 wpa_config_set_network_defaults(ssid);
6719 ssid->temporary = 1;
6720 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006721 ssid->pbss = params->pbss;
6722 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6723 WPA_CIPHER_CCMP;
6724 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6726 ssid->ssid = os_malloc(params->ssid_len);
6727 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006728 wpa_config_remove_network(wpa_s->conf, ssid->id);
6729 return -1;
6730 }
6731 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6732 ssid->ssid_len = params->ssid_len;
6733 ssid->p2p_group = 1;
6734 ssid->export_keys = 1;
6735 if (params->psk_set) {
6736 os_memcpy(ssid->psk, params->psk, 32);
6737 ssid->psk_set = 1;
6738 }
6739 if (params->passphrase)
6740 ssid->passphrase = os_strdup(params->passphrase);
6741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006742 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006743 wpa_s->p2p_in_invitation = 1;
6744 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006745 wpa_s->p2p_go_group_formation_completed = 0;
6746 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006747
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006748 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006749 NULL);
6750 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6751 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006752 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006753 wpa_supplicant_select_network(wpa_s, ssid);
6754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006755 return 0;
6756}
6757
6758
6759int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6760 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006761 int force_freq, int neg_freq,
6762 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006763 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006764 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006765 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006766 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006767{
6768 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006769 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006770
6771 if (ssid->disabled != 2 || ssid->ssid == NULL)
6772 return -1;
6773
6774 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6775 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6776 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6777 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006778 if (go == 0 &&
6779 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006780 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006781 /*
6782 * This can happen if Invitation Response frame was lost
6783 * and the peer (GO of a persistent group) tries to
6784 * invite us again. Reschedule the timeout to avoid
6785 * terminating the wait for the connection too early
6786 * since we now know that the peer is still trying to
6787 * invite us instead of having already started the GO.
6788 */
6789 wpa_printf(MSG_DEBUG,
6790 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6791 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6792 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006793 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006795 return 0;
6796 }
6797
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006798 os_free(wpa_s->global->add_psk);
6799 wpa_s->global->add_psk = NULL;
6800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006802 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006803
Dmitry Shmidt04949592012-07-19 12:16:46 -07006804 wpa_s->p2p_fallback_to_go_neg = 0;
6805
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006806 if (ssid->mode == WPAS_MODE_P2P_GO) {
6807 if (force_freq > 0) {
6808 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6809 if (freq < 0)
6810 return -1;
6811 } else {
6812 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6813 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006814 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006815 freq = 0;
6816 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006817 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006818 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006819 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006820 struct os_reltime now;
6821 struct wpa_bss *bss =
6822 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006823
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006824 os_get_reltime(&now);
6825 if (bss &&
6826 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006827 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006828 freq = bss->freq;
6829 else
6830 freq = 0;
6831 }
6832
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006833 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6834 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006835 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006836 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006837 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006838
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006839 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006840 ht40, vht, max_oper_chwidth, he, edmg,
6841 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006842 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006843
6844 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006845 params.psk_set = ssid->psk_set;
6846 if (params.psk_set)
6847 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006848 if (ssid->passphrase) {
6849 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6850 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6851 "persistent group");
6852 return -1;
6853 }
6854 os_strlcpy(params.passphrase, ssid->passphrase,
6855 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006856 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6858 params.ssid_len = ssid->ssid_len;
6859 params.persistent_group = 1;
6860
6861 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6862 if (wpa_s == NULL)
6863 return -1;
6864
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006865 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6866
Dmitry Shmidt56052862013-10-04 10:23:25 -07006867 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006868 wpas_start_wps_go(wpa_s, &params, 0);
6869
6870 return 0;
6871}
6872
6873
6874static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6875 struct wpabuf *proberesp_ies)
6876{
6877 struct wpa_supplicant *wpa_s = ctx;
6878 if (wpa_s->ap_iface) {
6879 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006880 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6881 wpabuf_free(beacon_ies);
6882 wpabuf_free(proberesp_ies);
6883 return;
6884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006885 if (beacon_ies) {
6886 wpabuf_free(hapd->p2p_beacon_ie);
6887 hapd->p2p_beacon_ie = beacon_ies;
6888 }
6889 wpabuf_free(hapd->p2p_probe_resp_ie);
6890 hapd->p2p_probe_resp_ie = proberesp_ies;
6891 } else {
6892 wpabuf_free(beacon_ies);
6893 wpabuf_free(proberesp_ies);
6894 }
6895 wpa_supplicant_ap_update_beacon(wpa_s);
6896}
6897
6898
6899static void wpas_p2p_idle_update(void *ctx, int idle)
6900{
6901 struct wpa_supplicant *wpa_s = ctx;
6902 if (!wpa_s->ap_iface)
6903 return;
6904 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006905 if (idle) {
6906 if (wpa_s->global->p2p_fail_on_wps_complete &&
6907 wpa_s->p2p_in_provisioning) {
6908 wpas_p2p_grpform_fail_after_wps(wpa_s);
6909 return;
6910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006912 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006913 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6914}
6915
6916
6917struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006918 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006919{
6920 struct p2p_group *group;
6921 struct p2p_group_config *cfg;
6922
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006923 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6924 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925 return NULL;
6926
6927 cfg = os_zalloc(sizeof(*cfg));
6928 if (cfg == NULL)
6929 return NULL;
6930
Dmitry Shmidt04949592012-07-19 12:16:46 -07006931 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006932 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006933 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006934 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6936 if (wpa_s->max_stations &&
6937 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6938 cfg->max_clients = wpa_s->max_stations;
6939 else
6940 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006941 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6942 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006943 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006944 cfg->cb_ctx = wpa_s;
6945 cfg->ie_update = wpas_p2p_ie_update;
6946 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006947 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6948 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006949
6950 group = p2p_group_init(wpa_s->global->p2p, cfg);
6951 if (group == NULL)
6952 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006953 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006954 p2p_group_notif_formation_done(group);
6955 wpa_s->p2p_group = group;
6956 return group;
6957}
6958
6959
6960void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6961 int registrar)
6962{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006963 struct wpa_ssid *ssid = wpa_s->current_ssid;
6964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006965 if (!wpa_s->p2p_in_provisioning) {
6966 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6967 "provisioning not in progress");
6968 return;
6969 }
6970
Dmitry Shmidt04949592012-07-19 12:16:46 -07006971 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6972 u8 go_dev_addr[ETH_ALEN];
6973 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6974 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6975 ssid->ssid_len);
6976 /* Clear any stored provisioning info */
6977 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6978 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006979
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006980 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006982 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006983 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6984 /*
6985 * Use a separate timeout for initial data connection to
6986 * complete to allow the group to be removed automatically if
6987 * something goes wrong in this step before the P2P group idle
6988 * timeout mechanism is taken into use.
6989 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006990 wpa_dbg(wpa_s, MSG_DEBUG,
6991 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6992 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006993 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6994 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006995 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006996 /* Complete group formation on successful data connection. */
6997 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006998 } else if (ssid) {
6999 /*
7000 * Use a separate timeout for initial data connection to
7001 * complete to allow the group to be removed automatically if
7002 * the client does not complete data connection successfully.
7003 */
7004 wpa_dbg(wpa_s, MSG_DEBUG,
7005 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7006 P2P_MAX_INITIAL_CONN_WAIT_GO);
7007 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7008 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007009 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007010 /*
7011 * Complete group formation on first successful data connection
7012 */
7013 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007014 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007015 if (wpa_s->global->p2p)
7016 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007017 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007018}
7019
7020
Jouni Malinen75ecf522011-06-27 15:19:46 -07007021void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7022 struct wps_event_fail *fail)
7023{
7024 if (!wpa_s->p2p_in_provisioning) {
7025 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7026 "provisioning not in progress");
7027 return;
7028 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007029
7030 if (wpa_s->go_params) {
7031 p2p_clear_provisioning_info(
7032 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007033 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007034 }
7035
Jouni Malinen75ecf522011-06-27 15:19:46 -07007036 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007037
7038 if (wpa_s == wpa_s->global->p2p_group_formation) {
7039 /*
7040 * Allow some time for the failed WPS negotiation exchange to
7041 * complete, but remove the group since group formation cannot
7042 * succeed after provisioning failure.
7043 */
7044 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7045 wpa_s->global->p2p_fail_on_wps_complete = 1;
7046 eloop_deplete_timeout(0, 50000,
7047 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007048 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007049 }
7050}
7051
7052
7053int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7054{
7055 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7056 !wpa_s->p2p_in_provisioning)
7057 return 0;
7058
7059 wpas_p2p_grpform_fail_after_wps(wpa_s);
7060
7061 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007062}
7063
7064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007065int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007066 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007067 enum wpas_p2p_prov_disc_use use,
7068 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007069{
7070 u16 config_methods;
7071
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007072 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007073 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007074 wpa_s->p2p_fallback_to_go_neg = 0;
7075 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007076 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7077 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007078 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7079 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7080
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007081 wpa_printf(MSG_DEBUG,
7082 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7083 __func__, p2ps_prov->conncap,
7084 p2ps_prov->adv_id, p2ps_prov->conncap,
7085 p2ps_prov->status, p2ps_prov->info);
7086
7087 config_methods = 0;
7088 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007089 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007090 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007091 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007092 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7093 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007095 else {
7096 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007097 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007098 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007099 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007100
Dmitry Shmidt04949592012-07-19 12:16:46 -07007101 if (use == WPAS_P2P_PD_AUTO) {
7102 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7103 wpa_s->pending_pd_config_methods = config_methods;
7104 wpa_s->p2p_auto_pd = 1;
7105 wpa_s->p2p_auto_join = 0;
7106 wpa_s->pending_pd_before_join = 0;
7107 wpa_s->auto_pd_scan_retry = 0;
7108 wpas_p2p_stop_find(wpa_s);
7109 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007110 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007111 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7112 wpa_s->p2p_auto_started.sec,
7113 wpa_s->p2p_auto_started.usec);
7114 wpas_p2p_join_scan(wpa_s, NULL);
7115 return 0;
7116 }
7117
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007118 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7119 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007121 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007122
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007123 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007124 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007125 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007126}
7127
7128
7129int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7130 char *end)
7131{
7132 return p2p_scan_result_text(ies, ies_len, buf, end);
7133}
7134
7135
7136static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7137{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007138 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007139 return;
7140
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007141 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007142 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007143 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7144 wpa_s, NULL);
7145 offchannel_send_action_done(wpa_s);
7146 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7149 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007150 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007151}
7152
7153
7154int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7155 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007156 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007157 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007158 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007159{
7160 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007161 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007162
Dmitry Shmidt04949592012-07-19 12:16:46 -07007163 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007164 wpa_s->p2p_in_provisioning) {
7165 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7166 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7167 " (P2P disabled)" : "",
7168 wpa_s->p2p_in_provisioning ?
7169 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007170 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007172
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007173 wpa_supplicant_cancel_sched_scan(wpa_s);
7174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007175 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007176 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007177 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007178}
7179
7180
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007181static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7182 struct wpa_scan_results *scan_res)
7183{
7184 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7185
7186 if (wpa_s->p2p_scan_work) {
7187 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7188 wpa_s->p2p_scan_work = NULL;
7189 radio_work_done(work);
7190 }
7191
7192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7193 return;
7194
7195 /*
7196 * Indicate that results have been processed so that the P2P module can
7197 * continue pending tasks.
7198 */
Hai Shalom60840252021-02-19 19:02:11 -08007199 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007200}
7201
7202
7203static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007204{
7205 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007206 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7208 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007209
7210 if (wpa_s->global->p2p)
7211 p2p_stop_find(wpa_s->global->p2p);
7212
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007213 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7214 wpa_printf(MSG_DEBUG,
7215 "P2P: Do not consider the scan results after stop_find");
7216 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7217 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007218}
7219
7220
7221void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7222{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007223 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007224 if (!wpa_s->global->pending_group_iface_for_p2ps)
7225 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226}
7227
7228
7229static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7230{
7231 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007232 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233}
7234
7235
7236int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7237{
7238 int res;
7239
7240 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7241 return -1;
7242
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007243 if (wpa_s->p2p_lo_started) {
7244 wpa_printf(MSG_DEBUG,
7245 "P2P: Cannot start P2P listen, it is offloaded");
7246 return -1;
7247 }
7248
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007249 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007250 wpas_p2p_clear_pending_action_tx(wpa_s);
7251
7252 if (timeout == 0) {
7253 /*
7254 * This is a request for unlimited Listen state. However, at
7255 * least for now, this is mapped to a Listen state for one
7256 * hour.
7257 */
7258 timeout = 3600;
7259 }
7260 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007261 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007262
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007263 /*
7264 * Stop previous find/listen operation to avoid trying to request a new
7265 * remain-on-channel operation while the driver is still running the
7266 * previous one.
7267 */
7268 if (wpa_s->global->p2p)
7269 p2p_stop_find(wpa_s->global->p2p);
7270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007271 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7272 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007273 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007274 eloop_register_timeout(timeout, 0,
7275 wpas_p2p_long_listen_timeout,
7276 wpa_s, NULL);
7277 }
7278
7279 return res;
7280}
7281
7282
7283int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7284 u8 *buf, size_t len, int p2p_group)
7285{
7286 struct wpabuf *p2p_ie;
7287 int ret;
7288
7289 if (wpa_s->global->p2p_disabled)
7290 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007291 /*
7292 * Advertize mandatory cross connection capability even on
7293 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7294 */
7295 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007296 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007297 if (wpa_s->global->p2p == NULL)
7298 return -1;
7299 if (bss == NULL)
7300 return -1;
7301
7302 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7303 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7304 p2p_group, p2p_ie);
7305 wpabuf_free(p2p_ie);
7306
7307 return ret;
7308}
7309
7310
7311int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007312 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007313 const u8 *ie, size_t ie_len,
7314 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007315{
7316 if (wpa_s->global->p2p_disabled)
7317 return 0;
7318 if (wpa_s->global->p2p == NULL)
7319 return 0;
7320
Dmitry Shmidt04949592012-07-19 12:16:46 -07007321 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007322 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007323 case P2P_PREQ_NOT_P2P:
7324 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7325 ssi_signal);
7326 /* fall through */
7327 case P2P_PREQ_MALFORMED:
7328 case P2P_PREQ_NOT_LISTEN:
7329 case P2P_PREQ_NOT_PROCESSED:
7330 default: /* make gcc happy */
7331 return 0;
7332 case P2P_PREQ_PROCESSED:
7333 return 1;
7334 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007335}
7336
7337
7338void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7339 const u8 *sa, const u8 *bssid,
7340 u8 category, const u8 *data, size_t len, int freq)
7341{
7342 if (wpa_s->global->p2p_disabled)
7343 return;
7344 if (wpa_s->global->p2p == NULL)
7345 return;
7346
7347 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7348 freq);
7349}
7350
7351
7352void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7353{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007354 unsigned int bands;
7355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007356 if (wpa_s->global->p2p_disabled)
7357 return;
7358 if (wpa_s->global->p2p == NULL)
7359 return;
7360
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007361 bands = wpas_get_bands(wpa_s, NULL);
7362 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007363}
7364
7365
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007366static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007367{
7368 p2p_group_deinit(wpa_s->p2p_group);
7369 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007370
7371 wpa_s->ap_configured_cb = NULL;
7372 wpa_s->ap_configured_cb_ctx = NULL;
7373 wpa_s->ap_configured_cb_data = NULL;
7374 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007375}
7376
7377
7378int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7379{
Hai Shalomfdcde762020-04-02 11:19:20 -07007380 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007382 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7383 return -1;
7384
7385 return p2p_reject(wpa_s->global->p2p, addr);
7386}
7387
7388
7389/* Invite to reinvoke a persistent group */
7390int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007391 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007392 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007393 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394{
7395 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007396 u8 *bssid = NULL;
7397 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007398 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007399 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007400 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007401
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007402 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007403 if (peer_addr)
7404 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7405 else
7406 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007407
Jouni Malinen31be0a42012-08-31 21:20:51 +03007408 wpa_s->p2p_persistent_go_freq = freq;
7409 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007410 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007411 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007412 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7413 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007414 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007415 if (ssid->mode == WPAS_MODE_P2P_GO) {
7416 role = P2P_INVITE_ROLE_GO;
7417 if (peer_addr == NULL) {
7418 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7419 "address in invitation command");
7420 return -1;
7421 }
7422 if (wpas_p2p_create_iface(wpa_s)) {
7423 if (wpas_p2p_add_group_interface(wpa_s,
7424 WPA_IF_P2P_GO) < 0) {
7425 wpa_printf(MSG_ERROR, "P2P: Failed to "
7426 "allocate a new interface for the "
7427 "group");
7428 return -1;
7429 }
7430 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007431 } else if (wpa_s->p2p_mgmt)
7432 bssid = wpa_s->parent->own_addr;
7433 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007434 bssid = wpa_s->own_addr;
7435 } else {
7436 role = P2P_INVITE_ROLE_CLIENT;
7437 peer_addr = ssid->bssid;
7438 }
7439 wpa_s->pending_invite_ssid_id = ssid->id;
7440
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007441 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007442 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007443 role == P2P_INVITE_ROLE_GO,
7444 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007445 if (res)
7446 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007448 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7449 return -1;
7450
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007451 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7452
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007453 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7454 no_pref_freq_given && pref_freq > 0 &&
7455 wpa_s->num_multichan_concurrent > 1 &&
7456 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7457 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7458 pref_freq);
7459 pref_freq = 0;
7460 }
7461
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007462 /*
7463 * Stop any find/listen operations before invitation and possibly
7464 * connection establishment.
7465 */
7466 wpas_p2p_stop_find_oper(wpa_s);
7467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007468 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007469 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007470 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471}
7472
7473
7474/* Invite to join an active group */
7475int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7476 const u8 *peer_addr, const u8 *go_dev_addr)
7477{
7478 struct wpa_global *global = wpa_s->global;
7479 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007480 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007481 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007482 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007483 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007484 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007485 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007486
Jouni Malinen31be0a42012-08-31 21:20:51 +03007487 wpa_s->p2p_persistent_go_freq = 0;
7488 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007489 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007490 wpa_s->p2p_go_vht_center_freq2 = 0;
7491 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007492 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007494 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7495 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7496 break;
7497 }
7498 if (wpa_s == NULL) {
7499 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7500 return -1;
7501 }
7502
7503 ssid = wpa_s->current_ssid;
7504 if (ssid == NULL) {
7505 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7506 "invitation");
7507 return -1;
7508 }
7509
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007510 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007511 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007512 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007513 ssid->ssid, ssid->ssid_len);
7514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007515 if (ssid->mode == WPAS_MODE_P2P_GO) {
7516 role = P2P_INVITE_ROLE_ACTIVE_GO;
7517 bssid = wpa_s->own_addr;
7518 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007519 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007520 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007521 } else {
7522 role = P2P_INVITE_ROLE_CLIENT;
7523 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7524 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7525 "invite to current group");
7526 return -1;
7527 }
7528 bssid = wpa_s->bssid;
7529 if (go_dev_addr == NULL &&
7530 !is_zero_ether_addr(wpa_s->go_dev_addr))
7531 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007532 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7533 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007535 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7538 return -1;
7539
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007540 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007541 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007542 role == P2P_INVITE_ROLE_ACTIVE_GO,
7543 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007544 if (res)
7545 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007546 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007548 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007549 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007550 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551}
7552
7553
7554void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7555{
7556 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557 u8 go_dev_addr[ETH_ALEN];
7558 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007559 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007560 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007561 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007562
Dmitry Shmidt04949592012-07-19 12:16:46 -07007563 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7564 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007565 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007566 }
7567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007568 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007569 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007570
7571 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007572 if (!wpa_s->p2p_go_group_formation_completed &&
7573 wpa_s->global->p2p_group_formation == wpa_s) {
7574 wpa_dbg(wpa_s, MSG_DEBUG,
7575 "P2P: Marking group formation completed on client on data connection");
7576 wpa_s->p2p_go_group_formation_completed = 1;
7577 wpa_s->global->p2p_group_formation = NULL;
7578 wpa_s->p2p_in_provisioning = 0;
7579 wpa_s->p2p_in_invitation = 0;
7580 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582 os_memset(go_dev_addr, 0, ETH_ALEN);
7583 if (ssid->bssid_set)
7584 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7585 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7586 ssid->ssid_len);
7587 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7588
7589 if (wpa_s->global->p2p_group_formation == wpa_s)
7590 wpa_s->global->p2p_group_formation = NULL;
7591
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007592 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7593 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007594
7595 ip_addr[0] = '\0';
7596 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007597 int res;
7598
7599 res = os_snprintf(ip_addr, sizeof(ip_addr),
7600 " ip_addr=%u.%u.%u.%u "
7601 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7602 ip[0], ip[1], ip[2], ip[3],
7603 ip[4], ip[5], ip[6], ip[7],
7604 ip[8], ip[9], ip[10], ip[11]);
7605 if (os_snprintf_error(sizeof(ip_addr), res))
7606 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007607 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007608 }
7609
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007610 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7611 ssid->passphrase == NULL && ssid->psk_set ?
7612 ssid->psk : NULL,
7613 ssid->passphrase, go_dev_addr, persistent,
7614 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007615
7616 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007617 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7618 ssid, go_dev_addr);
7619
Hai Shalom5f92bc92019-04-18 11:54:11 -07007620 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007621}
7622
7623
7624int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7625 u32 interval1, u32 duration2, u32 interval2)
7626{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007627 int ret;
7628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007629 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7630 return -1;
7631
7632 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7633 wpa_s->current_ssid == NULL ||
7634 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7635 return -1;
7636
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007637 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7638 wpa_s->own_addr, wpa_s->assoc_freq,
7639 duration1, interval1, duration2, interval2);
7640 if (ret == 0)
7641 wpa_s->waiting_presence_resp = 1;
7642
7643 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644}
7645
7646
7647int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7648 unsigned int interval)
7649{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007650 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7651 return -1;
7652
7653 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7654}
7655
7656
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007657static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7658{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007659 if (wpa_s->current_ssid == NULL) {
7660 /*
7661 * current_ssid can be cleared when P2P client interface gets
7662 * disconnected, so assume this interface was used as P2P
7663 * client.
7664 */
7665 return 1;
7666 }
7667 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007668 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7669}
7670
7671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007672static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7673{
7674 struct wpa_supplicant *wpa_s = eloop_ctx;
7675
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007676 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007677 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7678 "disabled");
7679 return;
7680 }
7681
Dmitry Shmidt04949592012-07-19 12:16:46 -07007682 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7683 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007684 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007685}
7686
7687
7688static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7689{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007690 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007691
Dmitry Shmidt04949592012-07-19 12:16:46 -07007692 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7693 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007695 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7696 return;
7697
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007698 timeout = wpa_s->conf->p2p_group_idle;
7699 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7700 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7701 timeout = P2P_MAX_CLIENT_IDLE;
7702
7703 if (timeout == 0)
7704 return;
7705
Dmitry Shmidt04949592012-07-19 12:16:46 -07007706 if (timeout < 0) {
7707 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7708 timeout = 0; /* special client mode no-timeout */
7709 else
7710 return;
7711 }
7712
7713 if (wpa_s->p2p_in_provisioning) {
7714 /*
7715 * Use the normal group formation timeout during the
7716 * provisioning phase to avoid terminating this process too
7717 * early due to group idle timeout.
7718 */
7719 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7720 "during provisioning");
7721 return;
7722 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307723
Dmitry Shmidt04949592012-07-19 12:16:46 -07007724 if (wpa_s->show_group_started) {
7725 /*
7726 * Use the normal group formation timeout between the end of
7727 * the provisioning phase and completion of 4-way handshake to
7728 * avoid terminating this process too early due to group idle
7729 * timeout.
7730 */
7731 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7732 "while waiting for initial 4-way handshake to "
7733 "complete");
7734 return;
7735 }
7736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007737 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007738 timeout);
7739 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7740 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007741}
7742
7743
Jouni Malinen2b89da82012-08-31 22:04:41 +03007744/* Returns 1 if the interface was removed */
7745int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7746 u16 reason_code, const u8 *ie, size_t ie_len,
7747 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007748{
7749 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007750 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007751
Dmitry Shmidt04949592012-07-19 12:16:46 -07007752 if (!locally_generated)
7753 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7754 ie_len);
7755
7756 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7757 wpa_s->current_ssid &&
7758 wpa_s->current_ssid->p2p_group &&
7759 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7760 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7761 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007762 if (wpas_p2p_group_delete(wpa_s,
7763 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7764 > 0)
7765 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007766 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007767
7768 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007769}
7770
7771
7772void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007773 u16 reason_code, const u8 *ie, size_t ie_len,
7774 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007775{
7776 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7777 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007778
Dmitry Shmidt04949592012-07-19 12:16:46 -07007779 if (!locally_generated)
7780 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7781 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007782}
7783
7784
7785void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7786{
7787 struct p2p_data *p2p = wpa_s->global->p2p;
7788
7789 if (p2p == NULL)
7790 return;
7791
7792 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7793 return;
7794
7795 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7796 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7797
7798 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7799 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7800
7801 if (wpa_s->wps &&
7802 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7803 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7804
7805 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7806 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7807
7808 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7809 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7810 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7811 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7812 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7813 }
7814
7815 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7816 p2p_set_sec_dev_types(p2p,
7817 (void *) wpa_s->conf->sec_device_type,
7818 wpa_s->conf->num_sec_device_types);
7819
7820 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7821 int i;
7822 p2p_remove_wps_vendor_extensions(p2p);
7823 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7824 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7825 continue;
7826 p2p_add_wps_vendor_extension(
7827 p2p, wpa_s->conf->wps_vendor_ext[i]);
7828 }
7829 }
7830
7831 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7832 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7833 char country[3];
7834 country[0] = wpa_s->conf->country[0];
7835 country[1] = wpa_s->conf->country[1];
7836 country[2] = 0x04;
7837 p2p_set_country(p2p, country);
7838 }
7839
7840 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7841 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7842 wpa_s->conf->p2p_ssid_postfix ?
7843 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7844 0);
7845 }
7846
7847 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7848 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007849
7850 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7851 u8 reg_class, channel;
7852 int ret;
7853 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007854 u8 channel_forced;
7855
Jouni Malinen75ecf522011-06-27 15:19:46 -07007856 if (wpa_s->conf->p2p_listen_reg_class &&
7857 wpa_s->conf->p2p_listen_channel) {
7858 reg_class = wpa_s->conf->p2p_listen_reg_class;
7859 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007860 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007861 } else {
7862 reg_class = 81;
7863 /*
7864 * Pick one of the social channels randomly as the
7865 * listen channel.
7866 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007867 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7868 channel = 1;
7869 else
7870 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007871 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007872 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007873 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7874 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007875 if (ret)
7876 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7877 "failed: %d", ret);
7878 }
7879 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7880 u8 op_reg_class, op_channel, cfg_op_channel;
7881 int ret = 0;
7882 unsigned int r;
7883 if (wpa_s->conf->p2p_oper_reg_class &&
7884 wpa_s->conf->p2p_oper_channel) {
7885 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7886 op_channel = wpa_s->conf->p2p_oper_channel;
7887 cfg_op_channel = 1;
7888 } else {
7889 op_reg_class = 81;
7890 /*
7891 * Use random operation channel from (1, 6, 11)
7892 *if no other preference is indicated.
7893 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007894 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7895 op_channel = 1;
7896 else
7897 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007898 cfg_op_channel = 0;
7899 }
7900 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7901 cfg_op_channel);
7902 if (ret)
7903 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7904 "failed: %d", ret);
7905 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007906
7907 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7908 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7909 wpa_s->conf->p2p_pref_chan) < 0) {
7910 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7911 "update failed");
7912 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007913
7914 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7915 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7916 "update failed");
7917 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007918 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007919
7920 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7921 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007922}
7923
7924
7925int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7926 int duration)
7927{
7928 if (!wpa_s->ap_iface)
7929 return -1;
7930 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7931 duration);
7932}
7933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007934
7935int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7936{
7937 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7938 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007939
7940 wpa_s->global->cross_connection = enabled;
7941 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7942
7943 if (!enabled) {
7944 struct wpa_supplicant *iface;
7945
7946 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7947 {
7948 if (iface->cross_connect_enabled == 0)
7949 continue;
7950
7951 iface->cross_connect_enabled = 0;
7952 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007953 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007954 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7955 iface->ifname,
7956 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007957 }
7958 }
7959
7960 return 0;
7961}
7962
7963
7964static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7965{
7966 struct wpa_supplicant *iface;
7967
7968 if (!uplink->global->cross_connection)
7969 return;
7970
7971 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7972 if (!iface->cross_connect_enabled)
7973 continue;
7974 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7975 0)
7976 continue;
7977 if (iface->ap_iface == NULL)
7978 continue;
7979 if (iface->cross_connect_in_use)
7980 continue;
7981
7982 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007983 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007984 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7985 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007986 }
7987}
7988
7989
7990static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7991{
7992 struct wpa_supplicant *iface;
7993
7994 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7995 if (!iface->cross_connect_enabled)
7996 continue;
7997 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7998 0)
7999 continue;
8000 if (!iface->cross_connect_in_use)
8001 continue;
8002
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008003 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008004 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8005 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008006 iface->cross_connect_in_use = 0;
8007 }
8008}
8009
8010
8011void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8012{
8013 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8014 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8015 wpa_s->cross_connect_disallowed)
8016 wpas_p2p_disable_cross_connect(wpa_s);
8017 else
8018 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008019 if (!wpa_s->ap_iface &&
8020 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8021 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008022}
8023
8024
8025void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8026{
8027 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008028 if (!wpa_s->ap_iface &&
8029 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8030 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008031 wpas_p2p_set_group_idle_timeout(wpa_s);
8032}
8033
8034
8035static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8036{
8037 struct wpa_supplicant *iface;
8038
8039 if (!wpa_s->global->cross_connection)
8040 return;
8041
8042 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8043 if (iface == wpa_s)
8044 continue;
8045 if (iface->drv_flags &
8046 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8047 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008048 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8049 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008050 continue;
8051
8052 wpa_s->cross_connect_enabled = 1;
8053 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8054 sizeof(wpa_s->cross_connect_uplink));
8055 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8056 "%s to %s whenever uplink is available",
8057 wpa_s->ifname, wpa_s->cross_connect_uplink);
8058
8059 if (iface->ap_iface || iface->current_ssid == NULL ||
8060 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8061 iface->cross_connect_disallowed ||
8062 iface->wpa_state != WPA_COMPLETED)
8063 break;
8064
8065 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008066 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008067 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8068 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008069 break;
8070 }
8071}
8072
8073
8074int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8075{
8076 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8077 !wpa_s->p2p_in_provisioning)
8078 return 0; /* not P2P client operation */
8079
8080 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8081 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008082 if (wpa_s != wpa_s->p2pdev)
8083 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008084 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008085 return 1;
8086}
8087
8088
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008089void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8090{
8091 struct wpa_supplicant *wpa_s = eloop_ctx;
8092 wpas_p2p_notif_pbc_overlap(wpa_s);
8093}
8094
8095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008096void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8097 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008098{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008099 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008100 struct wpa_used_freq_data *freqs = NULL;
8101 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008102
8103 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8104 return;
8105
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008106 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8107 if (!freqs)
8108 return;
8109
8110 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008112 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008113 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008114 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8115 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008116 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8117 "channel list");
8118 return;
8119 }
8120
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008121 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008122
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008123 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008124
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008125 /*
8126 * The used frequencies map changed, so it is possible that a GO is
8127 * using a channel that is no longer valid for P2P use. It is also
8128 * possible that due to policy consideration, it would be preferable to
8129 * move it to a frequency already used by other station interfaces.
8130 */
8131 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8132
8133 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008134}
8135
8136
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008137static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8138 struct wpa_scan_results *scan_res)
8139{
8140 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8141}
8142
8143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008144int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8145{
8146 struct wpa_global *global = wpa_s->global;
8147 int found = 0;
8148 const u8 *peer;
8149
8150 if (global->p2p == NULL)
8151 return -1;
8152
8153 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8154
8155 if (wpa_s->pending_interface_name[0] &&
8156 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8157 found = 1;
8158
8159 peer = p2p_get_go_neg_peer(global->p2p);
8160 if (peer) {
8161 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8162 MACSTR, MAC2STR(peer));
8163 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008164 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008165 }
8166
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008167 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8168 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8169 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8170 found = 1;
8171 }
8172
8173 if (wpa_s->pending_pd_before_join) {
8174 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8175 wpa_s->pending_pd_before_join = 0;
8176 found = 1;
8177 }
8178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008179 wpas_p2p_stop_find(wpa_s);
8180
8181 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8182 if (wpa_s == global->p2p_group_formation &&
8183 (wpa_s->p2p_in_provisioning ||
8184 wpa_s->parent->pending_interface_type ==
8185 WPA_IF_P2P_CLIENT)) {
8186 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8187 "formation found - cancelling",
8188 wpa_s->ifname);
8189 found = 1;
8190 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008191 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008192 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008193 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008194 break;
8195 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008196 wpas_p2p_group_delete(wpa_s,
8197 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008198 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008199 } else if (wpa_s->p2p_in_invitation) {
8200 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8201 wpa_s->ifname);
8202 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008203 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008204 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008205 }
8206 }
8207
8208 if (!found) {
8209 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8210 return -1;
8211 }
8212
8213 return 0;
8214}
8215
8216
8217void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8218{
8219 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8220 return;
8221
8222 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8223 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008224 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008225}
8226
8227
8228void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8229 int freq_24, int freq_5, int freq_overall)
8230{
8231 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008232 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008233 return;
8234 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8235}
8236
8237
8238int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8239{
8240 u8 peer[ETH_ALEN];
8241 struct p2p_data *p2p = wpa_s->global->p2p;
8242
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008243 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008244 return -1;
8245
8246 if (hwaddr_aton(addr, peer))
8247 return -1;
8248
8249 return p2p_unauthorize(p2p, peer);
8250}
8251
8252
8253/**
8254 * wpas_p2p_disconnect - Disconnect from a P2P Group
8255 * @wpa_s: Pointer to wpa_supplicant data
8256 * Returns: 0 on success, -1 on failure
8257 *
8258 * This can be used to disconnect from a group in which the local end is a P2P
8259 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8260 * virtual network interface was created for this group, that interface will be
8261 * removed. Otherwise, only the configured P2P group network will be removed
8262 * from the interface.
8263 */
8264int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8265{
8266
8267 if (wpa_s == NULL)
8268 return -1;
8269
Jouni Malinen2b89da82012-08-31 22:04:41 +03008270 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8271 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008272}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008273
8274
8275int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8276{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008277 int ret;
8278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008279 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8280 return 0;
8281
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008282 ret = p2p_in_progress(wpa_s->global->p2p);
8283 if (ret == 0) {
8284 /*
8285 * Check whether there is an ongoing WPS provisioning step (or
8286 * other parts of group formation) on another interface since
8287 * p2p_in_progress() does not report this to avoid issues for
8288 * scans during such provisioning step.
8289 */
8290 if (wpa_s->global->p2p_group_formation &&
8291 wpa_s->global->p2p_group_formation != wpa_s) {
8292 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8293 "in group formation",
8294 wpa_s->global->p2p_group_formation->ifname);
8295 ret = 1;
8296 }
8297 }
8298
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008299 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008300 struct os_reltime now;
8301 os_get_reltime(&now);
8302 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8303 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008304 /* Wait for the first client has expired */
8305 wpa_s->global->p2p_go_wait_client.sec = 0;
8306 } else {
8307 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8308 ret = 1;
8309 }
8310 }
8311
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008312 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008313}
8314
8315
8316void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8317 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008318{
8319 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8320 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008321 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008322 /**
8323 * Remove the network by scheduling the group formation
8324 * timeout to happen immediately. The teardown code
8325 * needs to be scheduled to run asynch later so that we
8326 * don't delete data from under ourselves unexpectedly.
8327 * Calling wpas_p2p_group_formation_timeout directly
8328 * causes a series of crashes in WPS failure scenarios.
8329 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008330 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8331 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008332 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008333 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008334 }
8335}
8336
8337
8338struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008339 const u8 *addr, const u8 *ssid,
8340 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008341{
8342 struct wpa_ssid *s;
8343 size_t i;
8344
8345 for (s = wpa_s->conf->ssid; s; s = s->next) {
8346 if (s->disabled != 2)
8347 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008348 if (ssid &&
8349 (ssid_len != s->ssid_len ||
8350 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8351 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008352 if (addr == NULL) {
8353 if (s->mode == WPAS_MODE_P2P_GO)
8354 return s;
8355 continue;
8356 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008357 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8358 return s; /* peer is GO in the persistent group */
8359 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8360 continue;
8361 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008362 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008363 addr, ETH_ALEN) == 0)
8364 return s; /* peer is P2P client in persistent
8365 * group */
8366 }
8367 }
8368
8369 return NULL;
8370}
8371
8372
8373void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8374 const u8 *addr)
8375{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008376 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008377 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008378 /*
8379 * This can happen if WPS provisioning step is not terminated
8380 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8381 * peer was able to connect, there is no need to time out group
8382 * formation after this, though. In addition, this is used with
8383 * the initial connection wait on the GO as a separate formation
8384 * timeout and as such, expected to be hit after the initial WPS
8385 * provisioning step.
8386 */
8387 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008388
8389 if (!wpa_s->p2p_go_group_formation_completed &&
8390 !wpa_s->group_formation_reported) {
8391 /*
8392 * GO has not yet notified group formation success since
8393 * the WPS step was not completed cleanly. Do that
8394 * notification now since the P2P Client was able to
8395 * connect and as such, must have received the
8396 * credential from the WPS step.
8397 */
8398 if (wpa_s->global->p2p)
8399 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008400 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008401 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008402 }
8403 if (!wpa_s->p2p_go_group_formation_completed) {
8404 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8405 wpa_s->p2p_go_group_formation_completed = 1;
8406 wpa_s->global->p2p_group_formation = NULL;
8407 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008408 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008409 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008410 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008411 if (addr == NULL)
8412 return;
8413 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8414}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008415
Dmitry Shmidt04949592012-07-19 12:16:46 -07008416
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008417static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8418 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008419{
8420 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008421 int ret = 0;
8422
Dmitry Shmidt04949592012-07-19 12:16:46 -07008423 if (wpa_s->global->p2p_group_formation)
8424 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008425 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008426 offchannel_send_action_done(wpa_s);
8427 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008428 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8430 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8431 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8432 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008433 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008434 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008435 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008436 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008437 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008438 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008439 wpa_s->p2p_go_he,
8440 wpa_s->p2p_go_edmg,
8441 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008442 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008443}
8444
8445
8446int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8447{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008448 int res;
8449
Dmitry Shmidt04949592012-07-19 12:16:46 -07008450 if (!wpa_s->p2p_fallback_to_go_neg ||
8451 wpa_s->p2p_in_provisioning <= 5)
8452 return 0;
8453
8454 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8455 return 0; /* peer operating as a GO */
8456
8457 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8458 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008459 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008460 "reason=GO-not-found");
8461 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008462
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008463 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008464}
8465
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008466
8467unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8468{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008469 struct wpa_supplicant *ifs;
8470
8471 if (wpa_s->wpa_state > WPA_SCANNING) {
8472 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8473 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008474 wpa_s->conf->p2p_search_delay);
8475 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008476 }
8477
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008478 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8479 radio_list) {
8480 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008481 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8482 "delay due to concurrent operation on "
8483 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008484 wpa_s->conf->p2p_search_delay,
8485 ifs->ifname);
8486 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008487 }
8488 }
8489
8490 return 0;
8491}
8492
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008493
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008494static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8495 struct wpa_ssid *s, const u8 *addr,
8496 int iface_addr)
8497{
8498 struct psk_list_entry *psk, *tmp;
8499 int changed = 0;
8500
8501 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8502 list) {
8503 if ((iface_addr && !psk->p2p &&
8504 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8505 (!iface_addr && psk->p2p &&
8506 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8507 wpa_dbg(wpa_s, MSG_DEBUG,
8508 "P2P: Remove persistent group PSK list entry for "
8509 MACSTR " p2p=%u",
8510 MAC2STR(psk->addr), psk->p2p);
8511 dl_list_del(&psk->list);
8512 os_free(psk);
8513 changed++;
8514 }
8515 }
8516
8517 return changed;
8518}
8519
8520
8521void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8522 const u8 *p2p_dev_addr,
8523 const u8 *psk, size_t psk_len)
8524{
8525 struct wpa_ssid *ssid = wpa_s->current_ssid;
8526 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008527 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008528
8529 if (psk_len != sizeof(p->psk))
8530 return;
8531
8532 if (p2p_dev_addr) {
8533 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8534 " p2p_dev_addr=" MACSTR,
8535 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8536 if (is_zero_ether_addr(p2p_dev_addr))
8537 p2p_dev_addr = NULL;
8538 } else {
8539 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8540 MAC2STR(mac_addr));
8541 }
8542
8543 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8544 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8545 /* To be added to persistent group once created */
8546 if (wpa_s->global->add_psk == NULL) {
8547 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8548 if (wpa_s->global->add_psk == NULL)
8549 return;
8550 }
8551 p = wpa_s->global->add_psk;
8552 if (p2p_dev_addr) {
8553 p->p2p = 1;
8554 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8555 } else {
8556 p->p2p = 0;
8557 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8558 }
8559 os_memcpy(p->psk, psk, psk_len);
8560 return;
8561 }
8562
8563 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8564 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8565 return;
8566 }
8567
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008568 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008569 ssid->ssid_len);
8570 if (!persistent) {
8571 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8572 return;
8573 }
8574
8575 p = os_zalloc(sizeof(*p));
8576 if (p == NULL)
8577 return;
8578 if (p2p_dev_addr) {
8579 p->p2p = 1;
8580 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8581 } else {
8582 p->p2p = 0;
8583 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8584 }
8585 os_memcpy(p->psk, psk, psk_len);
8586
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008587 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8588 (last = dl_list_last(&persistent->psk_list,
8589 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008590 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8591 MACSTR " (p2p=%u) to make room for a new one",
8592 MAC2STR(last->addr), last->p2p);
8593 dl_list_del(&last->list);
8594 os_free(last);
8595 }
8596
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008597 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008598 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8599 p2p_dev_addr == NULL);
8600 if (p2p_dev_addr) {
8601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8602 MACSTR, MAC2STR(p2p_dev_addr));
8603 } else {
8604 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8605 MAC2STR(mac_addr));
8606 }
8607 dl_list_add(&persistent->psk_list, &p->list);
8608
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008609 if (wpa_s->p2pdev->conf->update_config &&
8610 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008611 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008612}
8613
8614
8615static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8616 struct wpa_ssid *s, const u8 *addr,
8617 int iface_addr)
8618{
8619 int res;
8620
8621 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008622 if (res > 0 && wpa_s->conf->update_config &&
8623 wpa_config_write(wpa_s->confname, wpa_s->conf))
8624 wpa_dbg(wpa_s, MSG_DEBUG,
8625 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008626}
8627
8628
8629static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8630 const u8 *peer, int iface_addr)
8631{
8632 struct hostapd_data *hapd;
8633 struct hostapd_wpa_psk *psk, *prev, *rem;
8634 struct sta_info *sta;
8635
8636 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8637 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8638 return;
8639
8640 /* Remove per-station PSK entry */
8641 hapd = wpa_s->ap_iface->bss[0];
8642 prev = NULL;
8643 psk = hapd->conf->ssid.wpa_psk;
8644 while (psk) {
8645 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8646 (!iface_addr &&
8647 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8648 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8649 MACSTR " iface_addr=%d",
8650 MAC2STR(peer), iface_addr);
8651 if (prev)
8652 prev->next = psk->next;
8653 else
8654 hapd->conf->ssid.wpa_psk = psk->next;
8655 rem = psk;
8656 psk = psk->next;
8657 os_free(rem);
8658 } else {
8659 prev = psk;
8660 psk = psk->next;
8661 }
8662 }
8663
8664 /* Disconnect from group */
8665 if (iface_addr)
8666 sta = ap_get_sta(hapd, peer);
8667 else
8668 sta = ap_get_sta_p2p(hapd, peer);
8669 if (sta) {
8670 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8671 " (iface_addr=%d) from group",
8672 MAC2STR(peer), iface_addr);
8673 hostapd_drv_sta_deauth(hapd, sta->addr,
8674 WLAN_REASON_DEAUTH_LEAVING);
8675 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8676 }
8677}
8678
8679
8680void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8681 int iface_addr)
8682{
8683 struct wpa_ssid *s;
8684 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008685 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008686
8687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8688
8689 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008690 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008691 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8692 continue;
8693 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008694 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8695 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008696 }
8697
8698 /* Remove from any operating group */
8699 for (w = wpa_s->global->ifaces; w; w = w->next)
8700 wpas_p2p_remove_client_go(w, peer, iface_addr);
8701}
8702
8703
8704static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8705{
8706 struct wpa_supplicant *wpa_s = eloop_ctx;
8707 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8708}
8709
8710
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008711static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8712{
8713 struct wpa_supplicant *wpa_s = eloop_ctx;
8714
8715 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8716 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8717}
8718
8719
8720int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8721 struct wpa_ssid *ssid)
8722{
8723 struct wpa_supplicant *iface;
8724
8725 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8726 if (!iface->current_ssid ||
8727 iface->current_ssid->frequency == freq ||
8728 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8729 !iface->current_ssid->p2p_group))
8730 continue;
8731
8732 /* Remove the connection with least priority */
8733 if (!wpas_is_p2p_prioritized(iface)) {
8734 /* STA connection has priority over existing
8735 * P2P connection, so remove the interface. */
8736 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8737 eloop_register_timeout(0, 0,
8738 wpas_p2p_group_freq_conflict,
8739 iface, NULL);
8740 /* If connection in progress is P2P connection, do not
8741 * proceed for the connection. */
8742 if (wpa_s == iface)
8743 return -1;
8744 else
8745 return 0;
8746 } else {
8747 /* P2P connection has priority, disable the STA network
8748 */
8749 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8750 ssid);
8751 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8752 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8753 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8754 ETH_ALEN);
8755 /* If P2P connection is in progress, continue
8756 * connecting...*/
8757 if (wpa_s == iface)
8758 return 0;
8759 else
8760 return -1;
8761 }
8762 }
8763
8764 return 0;
8765}
8766
8767
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008768int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8769{
8770 struct wpa_ssid *ssid = wpa_s->current_ssid;
8771
8772 if (ssid == NULL || !ssid->p2p_group)
8773 return 0;
8774
8775 if (wpa_s->p2p_last_4way_hs_fail &&
8776 wpa_s->p2p_last_4way_hs_fail == ssid) {
8777 u8 go_dev_addr[ETH_ALEN];
8778 struct wpa_ssid *persistent;
8779
8780 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8781 ssid->ssid,
8782 ssid->ssid_len) <= 0) {
8783 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8784 goto disconnect;
8785 }
8786
8787 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8788 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008789 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008790 ssid->ssid,
8791 ssid->ssid_len);
8792 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8793 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8794 goto disconnect;
8795 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008796 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008797 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8798 persistent->id);
8799 disconnect:
8800 wpa_s->p2p_last_4way_hs_fail = NULL;
8801 /*
8802 * Remove the group from a timeout to avoid issues with caller
8803 * continuing to use the interface if this is on a P2P group
8804 * interface.
8805 */
8806 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8807 wpa_s, NULL);
8808 return 1;
8809 }
8810
8811 wpa_s->p2p_last_4way_hs_fail = ssid;
8812 return 0;
8813}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008814
8815
8816#ifdef CONFIG_WPS_NFC
8817
8818static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8819 struct wpabuf *p2p)
8820{
8821 struct wpabuf *ret;
8822 size_t wsc_len;
8823
8824 if (p2p == NULL) {
8825 wpabuf_free(wsc);
8826 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8827 return NULL;
8828 }
8829
8830 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8831 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8832 if (ret == NULL) {
8833 wpabuf_free(wsc);
8834 wpabuf_free(p2p);
8835 return NULL;
8836 }
8837
8838 wpabuf_put_be16(ret, wsc_len);
8839 if (wsc)
8840 wpabuf_put_buf(ret, wsc);
8841 wpabuf_put_be16(ret, wpabuf_len(p2p));
8842 wpabuf_put_buf(ret, p2p);
8843
8844 wpabuf_free(wsc);
8845 wpabuf_free(p2p);
8846 wpa_hexdump_buf(MSG_DEBUG,
8847 "P2P: Generated NFC connection handover message", ret);
8848
8849 if (ndef && ret) {
8850 struct wpabuf *tmp;
8851 tmp = ndef_build_p2p(ret);
8852 wpabuf_free(ret);
8853 if (tmp == NULL) {
8854 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8855 return NULL;
8856 }
8857 ret = tmp;
8858 }
8859
8860 return ret;
8861}
8862
8863
8864static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8865 struct wpa_ssid **ssid, u8 *go_dev_addr)
8866{
8867 struct wpa_supplicant *iface;
8868
8869 if (go_dev_addr)
8870 os_memset(go_dev_addr, 0, ETH_ALEN);
8871 if (ssid)
8872 *ssid = NULL;
8873 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8874 if (iface->wpa_state < WPA_ASSOCIATING ||
8875 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8876 !iface->current_ssid->p2p_group ||
8877 iface->current_ssid->mode != WPAS_MODE_INFRA)
8878 continue;
8879 if (ssid)
8880 *ssid = iface->current_ssid;
8881 if (go_dev_addr)
8882 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8883 return iface->assoc_freq;
8884 }
8885 return 0;
8886}
8887
8888
8889struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8890 int ndef)
8891{
8892 struct wpabuf *wsc, *p2p;
8893 struct wpa_ssid *ssid;
8894 u8 go_dev_addr[ETH_ALEN];
8895 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8896
8897 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8898 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8899 return NULL;
8900 }
8901
8902 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8903 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8904 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8905 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8906 return NULL;
8907 }
8908
8909 if (cli_freq == 0) {
8910 wsc = wps_build_nfc_handover_req_p2p(
8911 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8912 } else
8913 wsc = NULL;
8914 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8915 go_dev_addr, ssid ? ssid->ssid : NULL,
8916 ssid ? ssid->ssid_len : 0);
8917
8918 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8919}
8920
8921
8922struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8923 int ndef, int tag)
8924{
8925 struct wpabuf *wsc, *p2p;
8926 struct wpa_ssid *ssid;
8927 u8 go_dev_addr[ETH_ALEN];
8928 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8929
8930 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8931 return NULL;
8932
8933 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8934 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8935 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8936 return NULL;
8937
8938 if (cli_freq == 0) {
8939 wsc = wps_build_nfc_handover_sel_p2p(
8940 wpa_s->parent->wps,
8941 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8942 DEV_PW_NFC_CONNECTION_HANDOVER,
8943 wpa_s->conf->wps_nfc_dh_pubkey,
8944 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8945 } else
8946 wsc = NULL;
8947 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8948 go_dev_addr, ssid ? ssid->ssid : NULL,
8949 ssid ? ssid->ssid_len : 0);
8950
8951 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8952}
8953
8954
8955static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8956 struct p2p_nfc_params *params)
8957{
8958 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8959 "connection handover (freq=%d)",
8960 params->go_freq);
8961
8962 if (params->go_freq && params->go_ssid_len) {
8963 wpa_s->p2p_wps_method = WPS_NFC;
8964 wpa_s->pending_join_wps_method = WPS_NFC;
8965 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8966 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8967 ETH_ALEN);
8968 return wpas_p2p_join_start(wpa_s, params->go_freq,
8969 params->go_ssid,
8970 params->go_ssid_len);
8971 }
8972
8973 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8974 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008975 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008976 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008977 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008978 params->go_ssid_len ? params->go_ssid : NULL,
8979 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008980}
8981
8982
8983static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8984 struct p2p_nfc_params *params, int tag)
8985{
8986 int res, persistent;
8987 struct wpa_ssid *ssid;
8988
8989 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8990 "connection handover");
8991 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8992 ssid = wpa_s->current_ssid;
8993 if (ssid == NULL)
8994 continue;
8995 if (ssid->mode != WPAS_MODE_P2P_GO)
8996 continue;
8997 if (wpa_s->ap_iface == NULL)
8998 continue;
8999 break;
9000 }
9001 if (wpa_s == NULL) {
9002 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9003 return -1;
9004 }
9005
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009006 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009007 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009008 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009009 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9010 return -1;
9011 }
9012 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009013 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9014 wpa_s->p2pdev->p2p_oob_dev_pw,
9015 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9016 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009017 if (res)
9018 return res;
9019
9020 if (!tag) {
9021 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9022 return 0;
9023 }
9024
9025 if (!params->peer ||
9026 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9027 return 0;
9028
9029 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9030 " to join", MAC2STR(params->peer->p2p_device_addr));
9031
9032 wpa_s->global->p2p_invite_group = wpa_s;
9033 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009034 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009035 params->peer->p2p_device_addr,
9036 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009037 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009038
9039 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9040 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9041 ssid->ssid, ssid->ssid_len, ssid->frequency,
9042 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009043 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009044}
9045
9046
9047static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9048 struct p2p_nfc_params *params,
9049 int forced_freq)
9050{
9051 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9052 "connection handover");
9053 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9054 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009055 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009056 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009057 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9058 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009059}
9060
9061
9062static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9063 struct p2p_nfc_params *params,
9064 int forced_freq)
9065{
9066 int res;
9067
9068 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9069 "connection handover");
9070 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9071 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009072 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009073 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009074 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9075 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009076 if (res)
9077 return res;
9078
9079 res = wpas_p2p_listen(wpa_s, 60);
9080 if (res) {
9081 p2p_unauthorize(wpa_s->global->p2p,
9082 params->peer->p2p_device_addr);
9083 }
9084
9085 return res;
9086}
9087
9088
9089static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9090 const struct wpabuf *data,
9091 int sel, int tag, int forced_freq)
9092{
9093 const u8 *pos, *end;
9094 u16 len, id;
9095 struct p2p_nfc_params params;
9096 int res;
9097
9098 os_memset(&params, 0, sizeof(params));
9099 params.sel = sel;
9100
9101 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9102
9103 pos = wpabuf_head(data);
9104 end = pos + wpabuf_len(data);
9105
9106 if (end - pos < 2) {
9107 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9108 "attributes");
9109 return -1;
9110 }
9111 len = WPA_GET_BE16(pos);
9112 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009113 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009114 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9115 "attributes");
9116 return -1;
9117 }
9118 params.wsc_attr = pos;
9119 params.wsc_len = len;
9120 pos += len;
9121
9122 if (end - pos < 2) {
9123 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
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 P2P "
9131 "attributes");
9132 return -1;
9133 }
9134 params.p2p_attr = pos;
9135 params.p2p_len = len;
9136 pos += len;
9137
9138 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9139 params.wsc_attr, params.wsc_len);
9140 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9141 params.p2p_attr, params.p2p_len);
9142 if (pos < end) {
9143 wpa_hexdump(MSG_DEBUG,
9144 "P2P: Ignored extra data after P2P attributes",
9145 pos, end - pos);
9146 }
9147
9148 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9149 if (res)
9150 return res;
9151
9152 if (params.next_step == NO_ACTION)
9153 return 0;
9154
9155 if (params.next_step == BOTH_GO) {
9156 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9157 MAC2STR(params.peer->p2p_device_addr));
9158 return 0;
9159 }
9160
9161 if (params.next_step == PEER_CLIENT) {
9162 if (!is_zero_ether_addr(params.go_dev_addr)) {
9163 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9164 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9165 " ssid=\"%s\"",
9166 MAC2STR(params.peer->p2p_device_addr),
9167 params.go_freq,
9168 MAC2STR(params.go_dev_addr),
9169 wpa_ssid_txt(params.go_ssid,
9170 params.go_ssid_len));
9171 } else {
9172 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9173 "peer=" MACSTR " freq=%d",
9174 MAC2STR(params.peer->p2p_device_addr),
9175 params.go_freq);
9176 }
9177 return 0;
9178 }
9179
9180 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9181 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9182 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9183 return 0;
9184 }
9185
9186 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9187 wpa_s->p2p_oob_dev_pw = NULL;
9188
9189 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9190 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9191 "received");
9192 return -1;
9193 }
9194
9195 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9196 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9197 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9198 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9199 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9200 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9201 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9202
9203 if (tag) {
9204 if (id < 0x10) {
9205 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9206 "peer OOB Device Password Id %u", id);
9207 return -1;
9208 }
9209 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9210 "Device Password Id %u", id);
9211 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9212 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9213 params.oob_dev_pw_len -
9214 WPS_OOB_PUBKEY_HASH_LEN - 2);
9215 wpa_s->p2p_oob_dev_pw_id = id;
9216 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9217 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9218 params.oob_dev_pw_len -
9219 WPS_OOB_PUBKEY_HASH_LEN - 2);
9220 if (wpa_s->p2p_oob_dev_pw == NULL)
9221 return -1;
9222
9223 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9224 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9225 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9226 return -1;
9227 } else {
9228 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9229 "without Device Password");
9230 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9231 }
9232
9233 switch (params.next_step) {
9234 case NO_ACTION:
9235 case BOTH_GO:
9236 case PEER_CLIENT:
9237 /* already covered above */
9238 return 0;
9239 case JOIN_GROUP:
9240 return wpas_p2p_nfc_join_group(wpa_s, &params);
9241 case AUTH_JOIN:
9242 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9243 case INIT_GO_NEG:
9244 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9245 case RESP_GO_NEG:
9246 /* TODO: use own OOB Dev Pw */
9247 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9248 }
9249
9250 return -1;
9251}
9252
9253
9254int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9255 const struct wpabuf *data, int forced_freq)
9256{
9257 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9258 return -1;
9259
9260 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9261}
9262
9263
9264int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9265 const struct wpabuf *req,
9266 const struct wpabuf *sel, int forced_freq)
9267{
9268 struct wpabuf *tmp;
9269 int ret;
9270
9271 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9272 return -1;
9273
9274 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9275
9276 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9277 wpabuf_head(req), wpabuf_len(req));
9278 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9279 wpabuf_head(sel), wpabuf_len(sel));
9280 if (forced_freq)
9281 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9282 tmp = ndef_parse_p2p(init ? sel : req);
9283 if (tmp == NULL) {
9284 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9285 return -1;
9286 }
9287
9288 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9289 forced_freq);
9290 wpabuf_free(tmp);
9291
9292 return ret;
9293}
9294
9295
9296int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9297{
9298 const u8 *if_addr;
9299 int go_intent = wpa_s->conf->p2p_go_intent;
9300 struct wpa_supplicant *iface;
9301
9302 if (wpa_s->global->p2p == NULL)
9303 return -1;
9304
9305 if (!enabled) {
9306 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9307 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9308 {
9309 if (!iface->ap_iface)
9310 continue;
9311 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9312 }
9313 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9314 0, NULL);
9315 if (wpa_s->p2p_nfc_tag_enabled)
9316 wpas_p2p_remove_pending_group_interface(wpa_s);
9317 wpa_s->p2p_nfc_tag_enabled = 0;
9318 return 0;
9319 }
9320
9321 if (wpa_s->global->p2p_disabled)
9322 return -1;
9323
9324 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9325 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9326 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9327 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9328 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9329 "to allow static handover cases");
9330 return -1;
9331 }
9332
9333 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9334
9335 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9336 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9337 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9338 if (wpa_s->p2p_oob_dev_pw == NULL)
9339 return -1;
9340 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9341
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009342 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9343 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9344 /*
9345 * P2P Group Interface present and the command came on group
9346 * interface, so enable the token for the current interface.
9347 */
9348 wpa_s->create_p2p_iface = 0;
9349 } else {
9350 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9351 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009352
9353 if (wpa_s->create_p2p_iface) {
9354 enum wpa_driver_if_type iftype;
9355 /* Prepare to add a new interface for the group */
9356 iftype = WPA_IF_P2P_GROUP;
9357 if (go_intent == 15)
9358 iftype = WPA_IF_P2P_GO;
9359 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9360 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9361 "interface for the group");
9362 return -1;
9363 }
9364
9365 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009366 } else if (wpa_s->p2p_mgmt)
9367 if_addr = wpa_s->parent->own_addr;
9368 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009369 if_addr = wpa_s->own_addr;
9370
9371 wpa_s->p2p_nfc_tag_enabled = enabled;
9372
9373 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9374 struct hostapd_data *hapd;
9375 if (iface->ap_iface == NULL)
9376 continue;
9377 hapd = iface->ap_iface->bss[0];
9378 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9379 hapd->conf->wps_nfc_dh_pubkey =
9380 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9381 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9382 hapd->conf->wps_nfc_dh_privkey =
9383 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9384 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9385 hapd->conf->wps_nfc_dev_pw =
9386 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9387 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9388
9389 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9390 wpa_dbg(iface, MSG_DEBUG,
9391 "P2P: Failed to enable NFC Tag for GO");
9392 }
9393 }
9394 p2p_set_authorized_oob_dev_pw_id(
9395 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9396 if_addr);
9397
9398 return 0;
9399}
9400
9401#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009402
9403
9404static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9405 struct wpa_used_freq_data *freqs,
9406 unsigned int num)
9407{
9408 u8 curr_chan, cand, chan;
9409 unsigned int i;
9410
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009411 /*
9412 * If possible, optimize the Listen channel to be a channel that is
9413 * already used by one of the other interfaces.
9414 */
9415 if (!wpa_s->conf->p2p_optimize_listen_chan)
9416 return;
9417
9418 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9419 return;
9420
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009421 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9422 for (i = 0, cand = 0; i < num; i++) {
9423 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9424 if (curr_chan == chan) {
9425 cand = 0;
9426 break;
9427 }
9428
9429 if (chan == 1 || chan == 6 || chan == 11)
9430 cand = chan;
9431 }
9432
9433 if (cand) {
9434 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009435 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009436 cand);
9437 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9438 }
9439}
9440
9441
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009442static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009443{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009444 struct hostapd_config *conf;
9445 struct p2p_go_neg_results params;
9446 struct csa_settings csa_settings;
9447 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9448 int old_freq = current_ssid->frequency;
9449 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009450
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009451 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9452 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9453 return -1;
9454 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009455
9456 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009457 * TODO: This function may not always work correctly. For example,
9458 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009459 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009460 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9461 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009462 wpa_dbg(wpa_s, MSG_DEBUG,
9463 "P2P CSA: Failed to select new frequency for GO");
9464 return -1;
9465 }
9466
9467 if (current_ssid->frequency == params.freq) {
9468 wpa_dbg(wpa_s, MSG_DEBUG,
9469 "P2P CSA: Selected same frequency - not moving GO");
9470 return 0;
9471 }
9472
9473 conf = hostapd_config_defaults();
9474 if (!conf) {
9475 wpa_dbg(wpa_s, MSG_DEBUG,
9476 "P2P CSA: Failed to allocate default config");
9477 return -1;
9478 }
9479
9480 current_ssid->frequency = params.freq;
9481 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9482 wpa_dbg(wpa_s, MSG_DEBUG,
9483 "P2P CSA: Failed to create new GO config");
9484 ret = -1;
9485 goto out;
9486 }
9487
9488 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9489 wpa_dbg(wpa_s, MSG_DEBUG,
9490 "P2P CSA: CSA to a different band is not supported");
9491 ret = -1;
9492 goto out;
9493 }
9494
9495 os_memset(&csa_settings, 0, sizeof(csa_settings));
9496 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9497 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9498 csa_settings.freq_params.freq = params.freq;
9499 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9500 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9501 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9502
9503 if (conf->ieee80211ac) {
9504 int freq1 = 0, freq2 = 0;
9505 u8 chan, opclass;
9506
9507 if (ieee80211_freq_to_channel_ext(params.freq,
9508 conf->secondary_channel,
9509 conf->vht_oper_chwidth,
9510 &opclass, &chan) ==
9511 NUM_HOSTAPD_MODES) {
9512 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9513 ret = -1;
9514 goto out;
9515 }
9516
9517 if (conf->vht_oper_centr_freq_seg0_idx)
9518 freq1 = ieee80211_chan_to_freq(
9519 NULL, opclass,
9520 conf->vht_oper_centr_freq_seg0_idx);
9521
9522 if (conf->vht_oper_centr_freq_seg1_idx)
9523 freq2 = ieee80211_chan_to_freq(
9524 NULL, opclass,
9525 conf->vht_oper_centr_freq_seg1_idx);
9526
9527 if (freq1 < 0 || freq2 < 0) {
9528 wpa_dbg(wpa_s, MSG_DEBUG,
9529 "P2P CSA: Selected invalid VHT center freqs");
9530 ret = -1;
9531 goto out;
9532 }
9533
9534 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9535 csa_settings.freq_params.center_freq1 = freq1;
9536 csa_settings.freq_params.center_freq2 = freq2;
9537
9538 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009539 case CHANWIDTH_80MHZ:
9540 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009541 csa_settings.freq_params.bandwidth = 80;
9542 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009543 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009544 csa_settings.freq_params.bandwidth = 160;
9545 break;
9546 }
9547 }
9548
9549 ret = ap_switch_channel(wpa_s, &csa_settings);
9550out:
9551 current_ssid->frequency = old_freq;
9552 hostapd_config_free(conf);
9553 return ret;
9554}
9555
9556
9557static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9558{
9559 struct p2p_go_neg_results params;
9560 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009561 void (*ap_configured_cb)(void *ctx, void *data);
9562 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009563
9564 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9565
9566 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9567 current_ssid->frequency);
9568
9569 /* Stop the AP functionality */
9570 /* TODO: Should do this in a way that does not indicated to possible
9571 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009572 /* If this action occurs before a group is started, the callback should
9573 * be preserved, or GROUP-STARTED event would be lost. If this action
9574 * occurs after a group is started, these pointers are all NULL and
9575 * harmless. */
9576 ap_configured_cb = wpa_s->ap_configured_cb;
9577 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9578 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009579 wpa_supplicant_ap_deinit(wpa_s);
9580
9581 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009582 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9583 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009584 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9585 wpas_p2p_group_delete(wpa_s,
9586 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9587 return;
9588 }
9589 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9590 params.freq);
9591
9592 if (params.freq &&
9593 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9594 wpa_printf(MSG_DEBUG,
9595 "P2P: Selected freq (%u MHz) is not valid for P2P",
9596 params.freq);
9597 wpas_p2p_group_delete(wpa_s,
9598 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9599 return;
9600 }
9601
Hai Shalom899fcc72020-10-19 14:38:18 -07009602 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009603 wpa_s->ap_configured_cb = ap_configured_cb;
9604 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9605 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009606
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009607 /* Update the frequency */
9608 current_ssid->frequency = params.freq;
9609 wpa_s->connect_without_scan = current_ssid;
9610 wpa_s->reassociate = 1;
9611 wpa_s->disconnected = 0;
9612 wpa_supplicant_req_scan(wpa_s, 0, 0);
9613}
9614
9615
9616static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9617{
9618 struct wpa_supplicant *wpa_s = eloop_ctx;
9619
9620 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009621 return;
9622
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009623 wpas_p2p_go_update_common_freqs(wpa_s);
9624
9625 /* Do not move GO in the middle of a CSA */
9626 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9627 wpa_printf(MSG_DEBUG,
9628 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009629 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009630 }
9631
9632 /*
9633 * First, try a channel switch flow. If it is not supported or fails,
9634 * take down the GO and bring it up again.
9635 */
9636 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9637 wpas_p2p_move_go_no_csa(wpa_s);
9638}
9639
9640
9641static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9642{
9643 struct wpa_supplicant *wpa_s = eloop_ctx;
9644 struct wpa_used_freq_data *freqs = NULL;
9645 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009646
9647 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9648 if (!freqs)
9649 return;
9650
9651 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9652
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009653 /* Previous attempt to move a GO was not possible -- try again. */
9654 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9655 WPAS_P2P_CHANNEL_UPDATE_ANY);
9656
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009657 os_free(freqs);
9658}
9659
9660
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009661/*
9662 * Consider moving a GO from its currently used frequency:
9663 * 1. It is possible that due to regulatory consideration the frequency
9664 * can no longer be used and there is a need to evacuate the GO.
9665 * 2. It is possible that due to MCC considerations, it would be preferable
9666 * to move the GO to a channel that is currently used by some other
9667 * station interface.
9668 *
9669 * In case a frequency that became invalid is once again valid, cancel a
9670 * previously initiated GO frequency change.
9671 */
9672static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9673 struct wpa_used_freq_data *freqs,
9674 unsigned int num)
9675{
9676 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9677 unsigned int timeout;
9678 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009679 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009680
9681 wpas_p2p_go_update_common_freqs(wpa_s);
9682
9683 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009684 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009685 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009686 for (i = 0, invalid_freq = 0; i < num; i++) {
9687 if (freqs[i].freq == freq) {
9688 flags = freqs[i].flags;
9689
9690 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009691 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9692 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009693 wpa_dbg(wpa_s, MSG_DEBUG,
9694 "P2P: Freq=%d MHz no longer valid for GO",
9695 freq);
9696 invalid_freq = 1;
9697 }
9698 } else if (freqs[i].flags == 0) {
9699 /* Freq is not used by any other station interface */
9700 continue;
9701 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009702 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009703 /* Freq is not valid for P2P use cases */
9704 continue;
9705 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9706 P2P_GO_FREQ_MOVE_SCM) {
9707 policy_move = 1;
9708 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9709 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9710 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9711 policy_move = 1;
9712 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9713 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9714 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9715 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9716 policy_move = 1;
9717 } else if ((wpa_s->drv_flags &
9718 WPA_DRIVER_FLAGS_AP_CSA) &&
9719 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9720 u8 chan;
9721
9722 /*
9723 * We do not support CSA between bands, so move
9724 * GO only within the same band.
9725 */
9726 if (wpa_s->ap_iface->current_mode->mode ==
9727 ieee80211_freq_to_chan(freqs[i].freq,
9728 &chan))
9729 policy_move = 1;
9730 }
9731 }
9732 }
9733
9734 wpa_dbg(wpa_s, MSG_DEBUG,
9735 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9736 invalid_freq, policy_move, flags);
9737
9738 /*
9739 * The channel is valid, or we are going to have a policy move, so
9740 * cancel timeout.
9741 */
9742 if (!invalid_freq || policy_move) {
9743 wpa_dbg(wpa_s, MSG_DEBUG,
9744 "P2P: Cancel a GO move from freq=%d MHz", freq);
9745 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9746
9747 if (wpas_p2p_in_progress(wpa_s)) {
9748 wpa_dbg(wpa_s, MSG_DEBUG,
9749 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9750 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9751 wpa_s, NULL);
9752 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9753 wpas_p2p_reconsider_moving_go,
9754 wpa_s, NULL);
9755 return;
9756 }
9757 }
9758
9759 if (!invalid_freq && (!policy_move || flags != 0)) {
9760 wpa_dbg(wpa_s, MSG_DEBUG,
9761 "P2P: Not initiating a GO frequency change");
9762 return;
9763 }
9764
9765 /*
9766 * Do not consider moving GO if it is in the middle of a CSA. When the
9767 * CSA is finished this flow should be retriggered.
9768 */
9769 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9770 wpa_dbg(wpa_s, MSG_DEBUG,
9771 "P2P: Not initiating a GO frequency change - CSA is in progress");
9772 return;
9773 }
9774
9775 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9776 timeout = P2P_GO_FREQ_CHANGE_TIME;
9777 else
9778 timeout = 0;
9779
9780 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9781 freq, timeout);
9782 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9783 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9784}
9785
9786
9787static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9788 struct wpa_used_freq_data *freqs,
9789 unsigned int num,
9790 enum wpas_p2p_channel_update_trig trig)
9791{
9792 struct wpa_supplicant *ifs;
9793
9794 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9795 NULL);
9796
9797 /*
9798 * Travers all the radio interfaces, and for each GO interface, check
9799 * if there is a need to move the GO from the frequency it is using,
9800 * or in case the frequency is valid again, cancel the evacuation flow.
9801 */
9802 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9803 radio_list) {
9804 if (ifs->current_ssid == NULL ||
9805 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9806 continue;
9807
9808 /*
9809 * The GO was just started or completed channel switch, no need
9810 * to move it.
9811 */
9812 if (wpa_s == ifs &&
9813 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9814 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9815 wpa_dbg(wpa_s, MSG_DEBUG,
9816 "P2P: GO move - schedule re-consideration");
9817 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9818 wpas_p2p_reconsider_moving_go,
9819 wpa_s, NULL);
9820 continue;
9821 }
9822
9823 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9824 }
9825}
9826
9827
9828void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9829{
9830 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9831 return;
9832
9833 wpas_p2p_update_channel_list(wpa_s,
9834 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9835}
9836
9837
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009838void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9839{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009840 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9841 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9842 "the management interface is being removed");
9843 wpas_p2p_deinit_global(wpa_s->global);
9844 }
9845}
9846
9847
9848void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9849{
9850 if (wpa_s->ap_iface->bss)
9851 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9852 wpas_p2p_group_deinit(wpa_s);
9853}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009854
9855
9856int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9857 unsigned int period, unsigned int interval,
9858 unsigned int count)
9859{
9860 struct p2p_data *p2p = wpa_s->global->p2p;
9861 u8 *device_types;
9862 size_t dev_types_len;
9863 struct wpabuf *buf;
9864 int ret;
9865
9866 if (wpa_s->p2p_lo_started) {
9867 wpa_dbg(wpa_s, MSG_DEBUG,
9868 "P2P Listen offload is already started");
9869 return 0;
9870 }
9871
9872 if (wpa_s->global->p2p == NULL ||
9873 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9874 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9875 return -1;
9876 }
9877
9878 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9879 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9880 freq);
9881 return -1;
9882 }
9883
9884 /* Get device type */
9885 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9886 WPS_DEV_TYPE_LEN;
9887 device_types = os_malloc(dev_types_len);
9888 if (!device_types)
9889 return -1;
9890 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9891 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9892 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9893
9894 /* Get Probe Response IE(s) */
9895 buf = p2p_build_probe_resp_template(p2p, freq);
9896 if (!buf) {
9897 os_free(device_types);
9898 return -1;
9899 }
9900
9901 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9902 device_types, dev_types_len,
9903 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9904 if (ret < 0)
9905 wpa_dbg(wpa_s, MSG_DEBUG,
9906 "P2P: Failed to start P2P listen offload");
9907
9908 os_free(device_types);
9909 wpabuf_free(buf);
9910
9911 if (ret == 0) {
9912 wpa_s->p2p_lo_started = 1;
9913
9914 /* Stop current P2P listen if any */
9915 wpas_stop_listen(wpa_s);
9916 }
9917
9918 return ret;
9919}
9920
9921
9922int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9923{
9924 int ret;
9925
9926 if (!wpa_s->p2p_lo_started)
9927 return 0;
9928
9929 ret = wpa_drv_p2p_lo_stop(wpa_s);
9930 if (ret < 0)
9931 wpa_dbg(wpa_s, MSG_DEBUG,
9932 "P2P: Failed to stop P2P listen offload");
9933
9934 wpa_s->p2p_lo_started = 0;
9935 return ret;
9936}