blob: bc5516113a8616aed6b2e7e32390620a8880fad1 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
132 unsigned int *pref_freq_list,
133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
169
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170/*
171 * Get the number of concurrent channels that the HW can operate, but that are
172 * currently not in use by any of the wpa_supplicant interfaces.
173 */
174static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
175{
176 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800177 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700178
179 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
180 if (!freqs)
181 return -1;
182
183 num = get_shared_radio_freqs(wpa_s, freqs,
184 wpa_s->num_multichan_concurrent);
185 os_free(freqs);
186
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800187 unused = wpa_s->num_multichan_concurrent - num;
188 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
189 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700190}
191
192
193/*
194 * Get the frequencies that are currently in use by one or more of the virtual
195 * interfaces, and that are also valid for P2P operation.
196 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700197static unsigned int
198wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
199 struct wpa_used_freq_data *p2p_freqs,
200 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700201{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700203 unsigned int num, i, j;
204
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 freqs = os_calloc(wpa_s->num_multichan_concurrent,
206 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700207 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700208 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 num = get_shared_radio_freqs_data(wpa_s, freqs,
211 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700212
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214
215 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700216 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700217 p2p_freqs[j++] = freqs[i];
218 }
219
220 os_free(freqs);
221
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700222 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800223
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700224 return j;
225}
226
227
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700228static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
229 int freq)
230{
231 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
232 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700233
234 /* Use the wpa_s used to control the P2P Device operation */
235 wpa_s = wpa_s->global->p2p_init_wpa_s;
236
237 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800238 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
239 wpas_p2p_num_unused_channels(wpa_s) > 0) {
240 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
241 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700242 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800243 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700244 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
245}
246
247
Hai Shalom60840252021-02-19 19:02:11 -0800248static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
249{
250 unsigned int delay = wpas_p2p_search_delay(wpa_s);
251
252 /* In case of concurrent P2P and external scans, delay P2P search. */
253 if (external_scan_running(wpa_s->radio)) {
254 delay = wpa_s->conf->p2p_search_delay;
255 wpa_printf(MSG_DEBUG,
256 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
257 delay);
258 }
259
260 p2p_scan_res_handled(wpa_s->global->p2p, delay);
261}
262
263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
265 struct wpa_scan_results *scan_res)
266{
267 size_t i;
268
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800269 if (wpa_s->p2p_scan_work) {
270 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
271 wpa_s->p2p_scan_work = NULL;
272 radio_work_done(work);
273 }
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
276 return;
277
278 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
279 (int) scan_res->num);
280
281 for (i = 0; i < scan_res->num; i++) {
282 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700284 const u8 *ies;
285 size_t ies_len;
286
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800287 time_tmp_age.sec = bss->age / 1000;
288 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800289 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700290
291 ies = (const u8 *) (bss + 1);
292 ies_len = bss->ie_len;
293 if (bss->beacon_ie_len > 0 &&
294 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
295 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
296 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
297 MACSTR, MAC2STR(bss->bssid));
298 ies = ies + ies_len;
299 ies_len = bss->beacon_ie_len;
300 }
301
302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800304 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700305 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 break;
307 }
308
Hai Shalom60840252021-02-19 19:02:11 -0800309 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310}
311
312
Hai Shalom60840252021-02-19 19:02:11 -0800313static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700314{
Hai Shalom60840252021-02-19 19:02:11 -0800315 if (wpa_s->p2p_scan_work) {
316 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700317
Hai Shalom60840252021-02-19 19:02:11 -0800318 wpa_s->p2p_scan_work = NULL;
319 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700320 }
321
Hai Shalom60840252021-02-19 19:02:11 -0800322 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
323 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700324
Hai Shalom60840252021-02-19 19:02:11 -0800325 wpa_dbg(wpa_s, MSG_DEBUG,
326 "P2P: Failed to get scan results - try to continue");
327 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700328}
329
330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
332{
333 struct wpa_supplicant *wpa_s = work->wpa_s;
334 struct wpa_driver_scan_params *params = work->ctx;
335 int ret;
336
337 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800338 if (!work->started) {
339 wpa_scan_free_params(params);
340 return;
341 }
342
343 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800344 return;
345 }
346
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800347 if (wpa_s->clear_driver_scan_cache) {
348 wpa_printf(MSG_DEBUG,
349 "Request driver to clear scan cache due to local BSS flush");
350 params->only_new_results = 1;
351 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700352
353 if (wpa_s->conf->p2p_6ghz_disable && !params->freqs) {
354 wpa_printf(MSG_DEBUG,
355 "P2P: 6 GHz disabled - update the scan frequency list");
Hai Shalom60840252021-02-19 19:02:11 -0800356 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params,
357 0);
358 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params,
359 0);
Hai Shalom899fcc72020-10-19 14:38:18 -0700360 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800362 if (ret == 0)
363 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800364 wpa_scan_free_params(params);
365 work->ctx = NULL;
366 if (ret) {
367 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800368 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800369 return;
370 }
371
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800372 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800373 os_get_reltime(&wpa_s->scan_trigger_time);
374 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800375 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800376 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800377 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800378 wpa_s->p2p_scan_work = work;
379}
380
381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800382static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
383 int freq)
384{
385 if (wpa_s->global->p2p_24ghz_social_channels &&
386 (freq == 2412 || freq == 2437 || freq == 2462)) {
387 /*
388 * Search all social channels regardless of whether these have
389 * been disabled for P2P operating channel use to avoid missing
390 * peers.
391 */
392 return 1;
393 }
394 return p2p_supported_freq(wpa_s->global->p2p, freq);
395}
396
397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
399 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700400 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401{
402 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800403 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700405 unsigned int num_channels = 0;
406 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800407 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700408 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800409 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410
411 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
412 return -1;
413
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800414 if (wpa_s->p2p_scan_work) {
415 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
416 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 }
418
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800419 params = os_zalloc(sizeof(*params));
420 if (params == NULL)
421 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422
423 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800424 params->num_ssids = 1;
425 n = os_malloc(P2P_WILDCARD_SSID_LEN);
426 if (n == NULL)
427 goto fail;
428 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
429 params->ssids[0].ssid = n;
430 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
432 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700433 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
434 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 num_req_dev_types, req_dev_types);
436 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800437 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 switch (type) {
440 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700441 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
442 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800443 if (params->freqs == NULL)
444 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700445 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800446 if (wpas_p2p_search_social_channel(
447 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700448 params->freqs[num_channels++] =
449 social_channels_freq[i];
450 }
451 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 break;
453 case P2P_SCAN_FULL:
454 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800455 case P2P_SCAN_SPECIFIC:
456 params->freqs = os_calloc(2, sizeof(int));
457 if (params->freqs == NULL)
458 goto fail;
459 params->freqs[0] = freq;
460 params->freqs[1] = 0;
461 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700463 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
464 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800465 if (params->freqs == NULL)
466 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700467 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800468 if (wpas_p2p_search_social_channel(
469 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700470 params->freqs[num_channels++] =
471 social_channels_freq[i];
472 }
473 if (p2p_supported_freq(wpa_s->global->p2p, freq))
474 params->freqs[num_channels++] = freq;
475 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 break;
477 }
478
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800479 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
480 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
481 if (ies == NULL) {
482 wpabuf_free(wps_ie);
483 goto fail;
484 }
485 wpabuf_put_buf(ies, wps_ie);
486 wpabuf_free(wps_ie);
487
488 bands = wpas_get_bands(wpa_s, params->freqs);
489 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
490
491 params->p2p_probe = 1;
492 n = os_malloc(wpabuf_len(ies));
493 if (n == NULL) {
494 wpabuf_free(ies);
495 goto fail;
496 }
497 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
498 params->extra_ies = n;
499 params->extra_ies_len = wpabuf_len(ies);
500 wpabuf_free(ies);
501
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800502 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800503 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
504 params) < 0)
505 goto fail;
506 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800508fail:
509 wpa_scan_free_params(params);
510 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511}
512
513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
515{
516 switch (p2p_group_interface) {
517 case P2P_GROUP_INTERFACE_PENDING:
518 return WPA_IF_P2P_GROUP;
519 case P2P_GROUP_INTERFACE_GO:
520 return WPA_IF_P2P_GO;
521 case P2P_GROUP_INTERFACE_CLIENT:
522 return WPA_IF_P2P_CLIENT;
523 }
524
525 return WPA_IF_P2P_GROUP;
526}
527
528
529static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
530 const u8 *ssid,
531 size_t ssid_len, int *go)
532{
533 struct wpa_ssid *s;
534
535 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
536 for (s = wpa_s->conf->ssid; s; s = s->next) {
537 if (s->disabled != 0 || !s->p2p_group ||
538 s->ssid_len != ssid_len ||
539 os_memcmp(ssid, s->ssid, ssid_len) != 0)
540 continue;
541 if (s->mode == WPAS_MODE_P2P_GO &&
542 s != wpa_s->current_ssid)
543 continue;
544 if (go)
545 *go = s->mode == WPAS_MODE_P2P_GO;
546 return wpa_s;
547 }
548 }
549
550 return NULL;
551}
552
553
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800554static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
555{
556 struct wpa_supplicant *wpa_s = eloop_ctx;
557 wpa_printf(MSG_DEBUG,
558 "P2P: Complete previously requested removal of %s",
559 wpa_s->ifname);
560 wpas_p2p_disconnect(wpa_s);
561}
562
563
564static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
565 struct wpa_supplicant *calling_wpa_s)
566{
567 if (calling_wpa_s == wpa_s && wpa_s &&
568 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
569 /*
570 * The calling wpa_s instance is going to be removed. Do that
571 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700572 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800573 * control interface responses on the per-interface socket.
574 */
575 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
576 wpa_s, NULL) < 0)
577 return -1;
578 return 0;
579 }
580
581 return wpas_p2p_disconnect(wpa_s);
582}
583
584
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800585/* Determine total number of clients in active groups where we are the GO */
586static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
587{
588 unsigned int count = 0;
589 struct wpa_ssid *s;
590
591 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
592 for (s = wpa_s->conf->ssid; s; s = s->next) {
593 wpa_printf(MSG_DEBUG,
594 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
595 wpa_s, s, s->disabled, s->p2p_group,
596 s->mode);
597 if (!s->disabled && s->p2p_group &&
598 s->mode == WPAS_MODE_P2P_GO) {
599 count += p2p_get_group_num_members(
600 wpa_s->p2p_group);
601 }
602 }
603 }
604
605 return count;
606}
607
608
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800609static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
610{
611 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
612 !wpa_s->current_ssid->disabled &&
613 wpa_s->current_ssid->p2p_group &&
614 wpa_s->current_ssid->p2p_persistent_group;
615}
616
617
618static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
619{
620 return p2p_is_active_persistent_group(wpa_s) &&
621 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
622}
623
624
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800625/* Find an interface for a P2P group where we are the GO */
626static struct wpa_supplicant *
627wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
628{
629 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800630
631 if (!wpa_s)
632 return NULL;
633
634 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 if (!p2p_is_active_persistent_go(wpa_s))
636 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800637
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800638 /* Prefer a group with connected clients */
639 if (p2p_get_group_num_members(wpa_s->p2p_group))
640 return wpa_s;
641 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800642 }
643
644 /* No group with connected clients, so pick the one without (if any) */
645 return save;
646}
647
648
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800649static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800650{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800651 return p2p_is_active_persistent_group(wpa_s) &&
652 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
653}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800654
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800655
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800656/* Find an interface for a P2P group where we are the P2P Client */
657static struct wpa_supplicant *
658wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
659{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800661 if (p2p_is_active_persistent_cli(wpa_s))
662 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800663 }
664
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800665 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800666}
667
668
669/* Find a persistent group where we are the GO */
670static struct wpa_ssid *
671wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
672{
673 struct wpa_ssid *s;
674
675 for (s = wpa_s->conf->ssid; s; s = s->next) {
676 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
677 return s;
678 }
679
680 return NULL;
681}
682
683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800684static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
685 unsigned int *force_freq,
686 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800687{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800688 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800689 struct wpa_ssid *s;
690 u8 conncap = P2PS_SETUP_NONE;
691 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800692 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800693 struct wpa_ssid *persistent_go;
694 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800695 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800696
697 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
698
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800699 if (force_freq)
700 *force_freq = 0;
701 if (pref_freq)
702 *pref_freq = 0;
703
704 size = P2P_MAX_PREF_CHANNELS;
705 if (force_freq && pref_freq &&
706 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
707 (int *) pref_freq, 0, pref_freq_list, &size))
708 wpas_p2p_set_own_freq_preference(wpa_s,
709 *force_freq ? *force_freq :
710 *pref_freq);
711
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800712 /*
713 * For non-concurrent capable devices:
714 * If persistent_go, then no new.
715 * If GO, then no client.
716 * If client, then no GO.
717 */
718 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719 if (go_wpa_s)
720 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800721 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800722 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
723 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800725 wpa_printf(MSG_DEBUG,
726 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
727 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800728
729 /* If not concurrent, restrict our choices */
730 if (p2p_no_group_iface) {
731 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800733 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800734 return P2PS_SETUP_NONE;
735
736 if (go_wpa_s) {
737 if (role == P2PS_SETUP_CLIENT ||
738 incoming == P2PS_SETUP_GROUP_OWNER ||
739 p2p_client_limit_reached(go_wpa_s->p2p_group))
740 return P2PS_SETUP_NONE;
741
742 return P2PS_SETUP_GROUP_OWNER;
743 }
744
745 if (persistent_go) {
746 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
747 if (!incoming)
748 return P2PS_SETUP_GROUP_OWNER |
749 P2PS_SETUP_CLIENT;
750 if (incoming == P2PS_SETUP_NEW) {
751 u8 r;
752
753 if (os_get_random(&r, sizeof(r)) < 0 ||
754 (r & 1))
755 return P2PS_SETUP_CLIENT;
756 return P2PS_SETUP_GROUP_OWNER;
757 }
758 }
759 }
760 }
761
762 /* If a required role has been specified, handle it here */
763 if (role && role != P2PS_SETUP_NEW) {
764 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800765 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
766 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
767 /*
768 * Peer has an active GO, so if the role allows it and
769 * we do not have any active roles, become client.
770 */
771 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
772 !cli_wpa_s)
773 return P2PS_SETUP_CLIENT;
774
775 /* fall through */
776
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800777 case P2PS_SETUP_NONE:
778 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800779 conncap = role;
780 goto grp_owner;
781
782 case P2PS_SETUP_GROUP_OWNER:
783 /*
784 * Must be a complimentary role - cannot be a client to
785 * more than one peer.
786 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800787 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800788 return P2PS_SETUP_NONE;
789
790 return P2PS_SETUP_CLIENT;
791
792 case P2PS_SETUP_CLIENT:
793 /* Must be a complimentary role */
794 if (incoming != role) {
795 conncap = P2PS_SETUP_GROUP_OWNER;
796 goto grp_owner;
797 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700798 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800799
800 default:
801 return P2PS_SETUP_NONE;
802 }
803 }
804
805 /*
806 * For now, we only will support ownership of one group, and being a
807 * client of one group. Therefore, if we have either an existing GO
808 * group, or an existing client group, we will not do a new GO
809 * negotiation, but rather try to re-use the existing groups.
810 */
811 switch (incoming) {
812 case P2PS_SETUP_NONE:
813 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800814 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 conncap = P2PS_SETUP_GROUP_OWNER;
816 else if (!owned_members)
817 conncap = P2PS_SETUP_NEW;
818 else if (incoming == P2PS_SETUP_NONE)
819 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
820 else
821 conncap = P2PS_SETUP_CLIENT;
822 break;
823
824 case P2PS_SETUP_CLIENT:
825 conncap = P2PS_SETUP_GROUP_OWNER;
826 break;
827
828 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800829 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800830 conncap = P2PS_SETUP_CLIENT;
831 break;
832
833 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
834 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800835 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800836 conncap = P2PS_SETUP_GROUP_OWNER;
837 else {
838 u8 r;
839
840 if (os_get_random(&r, sizeof(r)) < 0 ||
841 (r & 1))
842 conncap = P2PS_SETUP_CLIENT;
843 else
844 conncap = P2PS_SETUP_GROUP_OWNER;
845 }
846 break;
847
848 default:
849 return P2PS_SETUP_NONE;
850 }
851
852grp_owner:
853 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
854 (!incoming && (conncap & P2PS_SETUP_NEW))) {
855 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
856 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800857
858 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800859 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800860 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800861 wpa_s->p2p_mgmt ?
862 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800863 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800864 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800865 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800866 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800867 wpa_printf(MSG_ERROR,
868 "P2P: Failed to allocate a new interface for the group");
869 return P2PS_SETUP_NONE;
870 }
871 wpa_s->global->pending_group_iface_for_p2ps = 1;
872 p2p_set_intended_addr(wpa_s->global->p2p,
873 wpa_s->pending_interface_addr);
874 }
875 }
876
877 return conncap;
878}
879
880
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700881static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
882 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883{
884 struct wpa_ssid *ssid;
885 char *gtype;
886 const char *reason;
887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 ssid = wpa_s->current_ssid;
889 if (ssid == NULL) {
890 /*
891 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700892 * pending P2P group interface waiting for provisioning or a
893 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 */
895 ssid = wpa_s->conf->ssid;
896 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700897 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 break;
899 ssid = ssid->next;
900 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300901 if (ssid == NULL &&
902 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
903 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700904 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
905 "not found");
906 return -1;
907 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 }
909 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
910 gtype = "GO";
911 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
912 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
913 wpa_s->reassociate = 0;
914 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 gtype = "client";
916 } else
917 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700918
919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
920 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
921
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800922 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700923 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800924 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
925 wpa_s, NULL)) {
926 wpa_printf(MSG_DEBUG,
927 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
928 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
929 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
930 wpa_s, NULL);
931 }
932 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700933
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 if (wpa_s->cross_connect_in_use) {
935 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800936 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700937 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
938 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 case P2P_GROUP_REMOVAL_REQUESTED:
942 reason = " reason=REQUESTED";
943 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700944 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
945 reason = " reason=FORMATION_FAILED";
946 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
948 reason = " reason=IDLE";
949 break;
950 case P2P_GROUP_REMOVAL_UNAVAILABLE:
951 reason = " reason=UNAVAILABLE";
952 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700953 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
954 reason = " reason=GO_ENDING_SESSION";
955 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700956 case P2P_GROUP_REMOVAL_PSK_FAILURE:
957 reason = " reason=PSK_FAILURE";
958 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800959 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
960 reason = " reason=FREQ_CONFLICT";
961 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 default:
963 reason = "";
964 break;
965 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700966 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800967 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700968 P2P_EVENT_GROUP_REMOVED "%s %s%s",
969 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800972 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
973 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700974 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
975 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800976 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800977 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800978 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
979 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700980 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800981 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700982 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983
Dmitry Shmidt15907092014-03-25 10:42:57 -0700984 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800985 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
986 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700987
Dmitry Shmidt96571392013-10-14 12:54:46 -0700988 /*
989 * Make sure wait for the first client does not remain active after the
990 * group has been removed.
991 */
992 wpa_s->global->p2p_go_wait_client.sec = 0;
993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
995 struct wpa_global *global;
996 char *ifname;
997 enum wpa_driver_if_type type;
998 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
999 wpa_s->ifname);
1000 global = wpa_s->global;
1001 ifname = os_strdup(wpa_s->ifname);
1002 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001003 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001004 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 wpa_s = global->ifaces;
1006 if (wpa_s && ifname)
1007 wpa_drv_if_remove(wpa_s, type, ifname);
1008 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001009 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 }
1011
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001012 /*
1013 * The primary interface was used for P2P group operations, so
1014 * need to reset its p2pdev.
1015 */
1016 wpa_s->p2pdev = wpa_s->parent;
1017
Dmitry Shmidt96571392013-10-14 12:54:46 -07001018 if (!wpa_s->p2p_go_group_formation_completed) {
1019 wpa_s->global->p2p_group_formation = NULL;
1020 wpa_s->p2p_in_provisioning = 0;
1021 }
1022
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001023 wpa_s->show_group_started = 0;
1024 os_free(wpa_s->go_params);
1025 wpa_s->go_params = NULL;
1026
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001027 os_free(wpa_s->p2p_group_common_freqs);
1028 wpa_s->p2p_group_common_freqs = NULL;
1029 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001030 wpa_s->p2p_go_do_acs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001031
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001032 wpa_s->waiting_presence_resp = 0;
1033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1035 if (ssid && (ssid->p2p_group ||
1036 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1037 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1038 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001039 if (ssid == wpa_s->current_ssid) {
1040 wpa_sm_set_config(wpa_s->wpa, NULL);
1041 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001043 }
1044 /*
1045 * Networks objects created during any P2P activities are not
1046 * exposed out as they might/will confuse certain non-P2P aware
1047 * applications since these network objects won't behave like
1048 * regular ones.
1049 *
1050 * Likewise, we don't send out network removed signals for such
1051 * network objects.
1052 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 wpa_config_remove_network(wpa_s->conf, id);
1054 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001055 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 } else {
1057 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1058 "found");
1059 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001060 if (wpa_s->ap_iface)
1061 wpa_supplicant_ap_deinit(wpa_s);
1062 else
1063 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001064
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001065 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1066
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001067 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068}
1069
1070
1071static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1072 u8 *go_dev_addr,
1073 const u8 *ssid, size_t ssid_len)
1074{
1075 struct wpa_bss *bss;
1076 const u8 *bssid;
1077 struct wpabuf *p2p;
1078 u8 group_capab;
1079 const u8 *addr;
1080
1081 if (wpa_s->go_params)
1082 bssid = wpa_s->go_params->peer_interface_addr;
1083 else
1084 bssid = wpa_s->bssid;
1085
1086 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001087 if (bss == NULL && wpa_s->go_params &&
1088 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1089 bss = wpa_bss_get_p2p_dev_addr(
1090 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 if (bss == NULL) {
1092 u8 iface_addr[ETH_ALEN];
1093 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1094 iface_addr) == 0)
1095 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1096 }
1097 if (bss == NULL) {
1098 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1099 "group is persistent - BSS " MACSTR " not found",
1100 MAC2STR(bssid));
1101 return 0;
1102 }
1103
1104 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001105 if (p2p == NULL)
1106 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1107 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 if (p2p == NULL) {
1109 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1110 "group is persistent - BSS " MACSTR
1111 " did not include P2P IE", MAC2STR(bssid));
1112 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001113 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001115 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 bss->beacon_ie_len);
1117 return 0;
1118 }
1119
1120 group_capab = p2p_get_group_capab(p2p);
1121 addr = p2p_get_go_dev_addr(p2p);
1122 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1123 "group_capab=0x%x", group_capab);
1124 if (addr) {
1125 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1126 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1127 MAC2STR(addr));
1128 } else
1129 os_memset(go_dev_addr, 0, ETH_ALEN);
1130 wpabuf_free(p2p);
1131
1132 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1133 "go_dev_addr=" MACSTR,
1134 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1135
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001136 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137}
1138
1139
Jouni Malinen75ecf522011-06-27 15:19:46 -07001140static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1141 struct wpa_ssid *ssid,
1142 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143{
1144 struct wpa_ssid *s;
1145 int changed = 0;
1146
1147 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1148 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1149 for (s = wpa_s->conf->ssid; s; s = s->next) {
1150 if (s->disabled == 2 &&
1151 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1152 s->ssid_len == ssid->ssid_len &&
1153 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1154 break;
1155 }
1156
1157 if (s) {
1158 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1159 "entry");
1160 if (ssid->passphrase && !s->passphrase)
1161 changed = 1;
1162 else if (ssid->passphrase && s->passphrase &&
1163 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1164 changed = 1;
1165 } else {
1166 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1167 "entry");
1168 changed = 1;
1169 s = wpa_config_add_network(wpa_s->conf);
1170 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001171 return -1;
1172
1173 /*
1174 * Instead of network_added we emit persistent_group_added
1175 * notification. Also to keep the defense checks in
1176 * persistent_group obj registration method, we set the
1177 * relevant flags in s to designate it as a persistent group.
1178 */
1179 s->p2p_group = 1;
1180 s->p2p_persistent_group = 1;
1181 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 wpa_config_set_network_defaults(s);
1183 }
1184
1185 s->p2p_group = 1;
1186 s->p2p_persistent_group = 1;
1187 s->disabled = 2;
1188 s->bssid_set = 1;
1189 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1190 s->mode = ssid->mode;
1191 s->auth_alg = WPA_AUTH_ALG_OPEN;
1192 s->key_mgmt = WPA_KEY_MGMT_PSK;
1193 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001194 s->pbss = ssid->pbss;
1195 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 s->export_keys = 1;
1197 if (ssid->passphrase) {
1198 os_free(s->passphrase);
1199 s->passphrase = os_strdup(ssid->passphrase);
1200 }
1201 if (ssid->psk_set) {
1202 s->psk_set = 1;
1203 os_memcpy(s->psk, ssid->psk, 32);
1204 }
1205 if (s->passphrase && !s->psk_set)
1206 wpa_config_update_psk(s);
1207 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1208 os_free(s->ssid);
1209 s->ssid = os_malloc(ssid->ssid_len);
1210 }
1211 if (s->ssid) {
1212 s->ssid_len = ssid->ssid_len;
1213 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1214 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001215 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1216 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1217 wpa_s->global->add_psk = NULL;
1218 changed = 1;
1219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 if (changed && wpa_s->conf->update_config &&
1222 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1223 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1224 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001225
1226 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227}
1228
1229
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001230static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1231 const u8 *addr)
1232{
1233 struct wpa_ssid *ssid, *s;
1234 u8 *n;
1235 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001236 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001237 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238
1239 ssid = wpa_s->current_ssid;
1240 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1241 !ssid->p2p_persistent_group)
1242 return;
1243
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001244 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1246 continue;
1247
1248 if (s->ssid_len == ssid->ssid_len &&
1249 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1250 break;
1251 }
1252
1253 if (s == NULL)
1254 return;
1255
1256 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001257 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001258 ETH_ALEN) != 0)
1259 continue;
1260
1261 if (i == s->num_p2p_clients - 1)
1262 return; /* already the most recent entry */
1263
1264 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001265 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1266 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1267 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001268 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001269 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1270 ETH_ALEN);
1271 os_memset(s->p2p_client_list +
1272 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1273 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001274 found = 1;
1275 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001276 }
1277
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001278 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1279 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001280 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001281 if (n == NULL)
1282 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001283 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1284 ETH_ALEN);
1285 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1286 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001287 s->p2p_client_list = n;
1288 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001289 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001290 /* Not enough room for an additional entry - drop the oldest
1291 * entry */
1292 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001293 s->p2p_client_list + 2 * ETH_ALEN,
1294 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001295 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001296 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001297 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001298 os_memset(s->p2p_client_list +
1299 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1300 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001301 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001302
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001303 if (p2p_wpa_s->conf->update_config &&
1304 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001306}
1307
1308
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001309static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1310 int go, struct wpa_ssid *ssid, int freq,
1311 const u8 *psk, const char *passphrase,
1312 const u8 *go_dev_addr, int persistent,
1313 const char *extra)
1314{
1315 const char *ssid_txt;
1316 char psk_txt[65];
1317
1318 if (psk)
1319 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1320 else
1321 psk_txt[0] = '\0';
1322
1323 if (ssid)
1324 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1325 else
1326 ssid_txt = "";
1327
1328 if (passphrase && passphrase[0] == '\0')
1329 passphrase = NULL;
1330
1331 /*
1332 * Include PSK/passphrase only in the control interface message and
1333 * leave it out from the debug log entry.
1334 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001335 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001336 P2P_EVENT_GROUP_STARTED
1337 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1338 MACSTR "%s%s",
1339 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1340 psk ? " psk=" : "", psk_txt,
1341 passphrase ? " passphrase=\"" : "",
1342 passphrase ? passphrase : "",
1343 passphrase ? "\"" : "",
1344 MAC2STR(go_dev_addr),
1345 persistent ? " [PERSISTENT]" : "", extra);
1346 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1347 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1348 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1349 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1350 extra);
1351}
1352
1353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001355 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356{
1357 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 int client;
1359 int persistent;
1360 u8 go_dev_addr[ETH_ALEN];
1361
1362 /*
1363 * This callback is likely called for the main interface. Update wpa_s
1364 * to use the group interface if a new interface was created for the
1365 * group.
1366 */
1367 if (wpa_s->global->p2p_group_formation)
1368 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001369 if (wpa_s->p2p_go_group_formation_completed) {
1370 wpa_s->global->p2p_group_formation = NULL;
1371 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001372 } else if (wpa_s->p2p_in_provisioning && !success) {
1373 wpa_msg(wpa_s, MSG_DEBUG,
1374 "P2P: Stop provisioning state due to failure");
1375 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001376 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001377 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001378 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379
1380 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001381 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001382 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001383 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001384 if (already_deleted)
1385 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001386 wpas_p2p_group_delete(wpa_s,
1387 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 return;
1389 }
1390
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001391 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001392 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393
1394 ssid = wpa_s->current_ssid;
1395 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1396 ssid->mode = WPAS_MODE_P2P_GO;
1397 p2p_group_notif_formation_done(wpa_s->p2p_group);
1398 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1399 }
1400
1401 persistent = 0;
1402 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 client = ssid->mode == WPAS_MODE_INFRA;
1404 if (ssid->mode == WPAS_MODE_P2P_GO) {
1405 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001406 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1407 ETH_ALEN);
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 Yadawad8db34572021-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 Yadawad8db34572021-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 Yadawad8db34572021-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 Yadawad8db34572021-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 Yadawad8db34572021-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;
Jimmy Chen801bf462021-11-09 23:08:48 +08006106 int possible_2g_freqs[] = {
6107 /* operating class 81 */
6108 2412, 2437, 2462,
6109 };
6110 int possible_2g_freqs_num =
6111 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6112 int i;
6113 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6114 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6115 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6116 break;
6117 }
6118 }
6119
6120 if (i >= possible_2g_freqs_num) {
6121 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6122 "2.4 GHz channel for P2P group");
6123 return -1;
6124 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006125 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6126 "channel: %d MHz", freq);
6127 }
6128 }
6129
6130 if (freq == 5) {
6131 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6132 "band");
6133 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006134 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006135 wpa_s->best_5_freq)) {
6136 freq = wpa_s->best_5_freq;
6137 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6138 "channel: %d MHz", freq);
6139 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006140 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6141 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006142
6143 /*
6144 * most of 5G channels are DFS, only operating class 115 and 124
6145 * are available possibly, randomly pick a start to check them.
6146 */
6147 int possible_5g_freqs[] = {
6148 /* operating class 115 */
6149 5180, 5200, 5220, 5240,
6150 /* operating class 124 */
6151 5745, 5765, 5785, 5805,
6152 };
6153 int possible_5g_freqs_num =
6154 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6155
6156 int i;
6157 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6158 if (p2p_supported_freq_go(
6159 wpa_s->global->p2p,
6160 possible_5g_freqs[r % possible_5g_freqs_num])) {
6161 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6162 break;
6163 }
6164 }
6165
6166 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006167 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6168 "5 GHz channel for P2P group");
6169 return -1;
6170 }
6171 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6172 "channel: %d MHz", freq);
6173 }
6174 }
6175
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006176 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006177 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006178 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6179 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006180 /*
6181 * If freq is a DFS channel and DFS is offloaded to the
6182 * driver, allow P2P GO to use it.
6183 */
6184 wpa_printf(MSG_DEBUG, "P2P: "
6185 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6186 __func__, freq);
6187 return freq;
6188 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006189 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6190 "(%u MHz) is not supported for P2P uses",
6191 freq);
6192 return -1;
6193 }
6194
6195 return freq;
6196}
6197
6198
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006199static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6200 const struct p2p_channels *channels,
6201 int freq)
6202{
6203 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6204 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6205 freq_included(wpa_s, channels, freq))
6206 return 1;
6207 return 0;
6208}
6209
6210
6211static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6212 struct p2p_go_neg_results *params,
6213 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006214{
6215 unsigned int i, r;
6216
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006217 /* try all channels in operating class 115 */
6218 for (i = 0; i < 4; i++) {
6219 params->freq = 5180 + i * 20;
6220 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006221 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006222 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6223 goto out;
6224 }
6225
6226 /* try all channels in operating class 124 */
6227 for (i = 0; i < 4; i++) {
6228 params->freq = 5745 + i * 20;
6229 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006230 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006231 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6232 goto out;
6233 }
6234
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006235 /* try social channel class 180 channel 2 */
6236 params->freq = 58320 + 1 * 2160;
6237 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006238 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006239 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6240 goto out;
6241
6242 /* try all channels in reg. class 180 */
6243 for (i = 0; i < 4; i++) {
6244 params->freq = 58320 + i * 2160;
6245 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006246 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006247 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6248 goto out;
6249 }
6250
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006251 /* try some random selection of the social channels */
6252 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6253 return;
6254
6255 for (i = 0; i < 3; i++) {
6256 params->freq = 2412 + ((r + i) % 3) * 25;
6257 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6258 goto out;
6259 }
6260
6261 /* try all other channels in operating class 81 */
6262 for (i = 0; i < 11; i++) {
6263 params->freq = 2412 + i * 5;
6264
6265 /* skip social channels; covered in the previous loop */
6266 if (params->freq == 2412 ||
6267 params->freq == 2437 ||
6268 params->freq == 2462)
6269 continue;
6270
6271 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6272 goto out;
6273 }
6274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006275 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006276 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006277 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006278out:
6279 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6280 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006281}
6282
6283
Roshan Pius3a1667e2018-07-03 15:17:14 -07006284static int wpas_same_band(int freq1, int freq2)
6285{
6286 enum hostapd_hw_mode mode1, mode2;
6287 u8 chan1, chan2;
6288
6289 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6290 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6291 if (mode1 == NUM_HOSTAPD_MODES)
6292 return 0;
6293 return mode1 == mode2;
6294}
6295
6296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006297static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6298 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006299 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006300 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006301 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006302 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006304 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006305 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006306 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006307 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006308 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006309
6310 os_memset(params, 0, sizeof(*params));
6311 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006312 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006313 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006314 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006315 params->max_oper_chwidth = max_oper_chwidth;
6316 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006317 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006319 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6320 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006321 if (!freqs)
6322 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006323
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006324 num = get_shared_radio_freqs_data(wpa_s, freqs,
6325 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006326
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006327 if (wpa_s->current_ssid &&
6328 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6329 wpa_s->wpa_state == WPA_COMPLETED) {
6330 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6331 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006332
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006333 /*
6334 * If the frequency selection is done for an active P2P GO that
6335 * is not sharing a frequency, allow to select a new frequency
6336 * even if there are no unused frequencies as we are about to
6337 * move the P2P GO so its frequency can be re-used.
6338 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006339 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006340 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6341 freqs[i].flags == 0) {
6342 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006343 break;
6344 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006345 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006346 }
6347
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006348 /* Try to use EDMG channel */
6349 if (params->edmg) {
6350 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6351 goto success;
6352 params->edmg = 0;
6353 }
6354
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006355 /* try using the forced freq */
6356 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006357 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6358 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006359 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006360 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006361 freq);
6362 goto fail;
6363 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006364 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6365 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006366 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6367 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006368 /*
6369 * If freq is a DFS channel and DFS is offloaded
6370 * to the driver, allow P2P GO to use it.
6371 */
6372 wpa_printf(MSG_DEBUG,
6373 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6374 __func__, freq);
6375 } else {
6376 wpa_printf(MSG_DEBUG,
6377 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6378 freq);
6379 goto fail;
6380 }
6381 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006382
6383 for (i = 0; i < num; i++) {
6384 if (freqs[i].freq == freq) {
6385 wpa_printf(MSG_DEBUG,
6386 "P2P: forced freq (%d MHz) is also shared",
6387 freq);
6388 params->freq = freq;
6389 goto success;
6390 }
6391 }
6392
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006393 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006394 wpa_printf(MSG_DEBUG,
6395 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6396 freq);
6397 goto fail;
6398 }
6399
6400 wpa_printf(MSG_DEBUG,
6401 "P2P: force GO freq (%d MHz) on a free channel",
6402 freq);
6403 params->freq = freq;
6404 goto success;
6405 }
6406
6407 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006408 if (num &&
6409 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006410 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6411 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6412 wpa_printf(MSG_DEBUG,
6413 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006414 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006415 params->freq = cand;
6416 goto success;
6417 }
6418
6419 /* try using one of the shared freqs */
6420 for (i = 0; i < num; i++) {
6421 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6422 freqs[i].freq)) {
6423 wpa_printf(MSG_DEBUG,
6424 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006425 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006426 params->freq = freqs[i].freq;
6427 goto success;
6428 }
6429 }
6430 }
6431
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006432 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006433 wpa_printf(MSG_DEBUG,
6434 "P2P: Cannot force GO on any of the channels we are already using");
6435 goto fail;
6436 }
6437
6438 /* try using the setting from the configuration file */
6439 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6440 wpa_s->conf->p2p_oper_channel >= 1 &&
6441 wpa_s->conf->p2p_oper_channel <= 11 &&
6442 wpas_p2p_supported_freq_go(
6443 wpa_s, channels,
6444 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6445 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6446 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6447 "frequency %d MHz", params->freq);
6448 goto success;
6449 }
6450
6451 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6452 wpa_s->conf->p2p_oper_reg_class == 116 ||
6453 wpa_s->conf->p2p_oper_reg_class == 117 ||
6454 wpa_s->conf->p2p_oper_reg_class == 124 ||
6455 wpa_s->conf->p2p_oper_reg_class == 125 ||
6456 wpa_s->conf->p2p_oper_reg_class == 126 ||
6457 wpa_s->conf->p2p_oper_reg_class == 127) &&
6458 wpas_p2p_supported_freq_go(wpa_s, channels,
6459 5000 +
6460 5 * wpa_s->conf->p2p_oper_channel)) {
6461 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6462 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6463 "frequency %d MHz", params->freq);
6464 goto success;
6465 }
6466
6467 /* Try using best channels */
6468 if (wpa_s->conf->p2p_oper_channel == 0 &&
6469 wpa_s->best_overall_freq > 0 &&
6470 wpas_p2p_supported_freq_go(wpa_s, channels,
6471 wpa_s->best_overall_freq)) {
6472 params->freq = wpa_s->best_overall_freq;
6473 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6474 "channel %d MHz", params->freq);
6475 goto success;
6476 }
6477
6478 if (wpa_s->conf->p2p_oper_channel == 0 &&
6479 wpa_s->best_24_freq > 0 &&
6480 wpas_p2p_supported_freq_go(wpa_s, channels,
6481 wpa_s->best_24_freq)) {
6482 params->freq = wpa_s->best_24_freq;
6483 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6484 "channel %d MHz", params->freq);
6485 goto success;
6486 }
6487
6488 if (wpa_s->conf->p2p_oper_channel == 0 &&
6489 wpa_s->best_5_freq > 0 &&
6490 wpas_p2p_supported_freq_go(wpa_s, channels,
6491 wpa_s->best_5_freq)) {
6492 params->freq = wpa_s->best_5_freq;
6493 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6494 "channel %d MHz", params->freq);
6495 goto success;
6496 }
6497
6498 /* try using preferred channels */
6499 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6500 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6501 params->freq = cand;
6502 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6503 "channels", params->freq);
6504 goto success;
6505 }
6506
Roshan Pius3a1667e2018-07-03 15:17:14 -07006507 /* Try using a channel that allows VHT to be used with 80 MHz */
6508 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6509 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6510 enum hostapd_hw_mode mode;
6511 struct hostapd_hw_modes *hwmode;
6512 u8 chan;
6513
6514 cand = wpa_s->p2p_group_common_freqs[i];
6515 mode = ieee80211_freq_to_chan(cand, &chan);
6516 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006517 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006518 if (!hwmode ||
6519 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6520 BW80) != ALLOWED)
6521 continue;
6522 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6523 params->freq = cand;
6524 wpa_printf(MSG_DEBUG,
6525 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6526 params->freq);
6527 goto success;
6528 }
6529 }
6530 }
6531
6532 /* Try using a channel that allows HT to be used with 40 MHz on the same
6533 * band so that CSA can be used */
6534 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6535 wpa_s->p2p_group_common_freqs) {
6536 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6537 enum hostapd_hw_mode mode;
6538 struct hostapd_hw_modes *hwmode;
6539 u8 chan;
6540
6541 cand = wpa_s->p2p_group_common_freqs[i];
6542 mode = ieee80211_freq_to_chan(cand, &chan);
6543 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006544 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006545 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6546 cand) ||
6547 !hwmode ||
6548 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6549 BW40MINUS) != ALLOWED &&
6550 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6551 BW40PLUS) != ALLOWED))
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, allowing HT40, and maintaining same band",
6557 params->freq);
6558 goto success;
6559 }
6560 }
6561 }
6562
6563 /* Try using one of the group common freqs on the same band so that CSA
6564 * can be used */
6565 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6566 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6567 cand = wpa_s->p2p_group_common_freqs[i];
6568 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6569 cand))
6570 continue;
6571 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6572 params->freq = cand;
6573 wpa_printf(MSG_DEBUG,
6574 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6575 params->freq);
6576 goto success;
6577 }
6578 }
6579 }
6580
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006581 /* Try using one of the group common freqs */
6582 if (wpa_s->p2p_group_common_freqs) {
6583 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6584 cand = wpa_s->p2p_group_common_freqs[i];
6585 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6586 params->freq = cand;
6587 wpa_printf(MSG_DEBUG,
6588 "P2P: Use freq %d MHz common with the peer",
6589 params->freq);
6590 goto success;
6591 }
6592 }
6593 }
6594
6595 /* no preference, select some channel */
6596 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6597
6598 if (params->freq == 0) {
6599 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6600 goto fail;
6601 }
6602
6603success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006604 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006605 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006606fail:
6607 os_free(freqs);
6608 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006609}
6610
6611
6612static struct wpa_supplicant *
6613wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6614 int go)
6615{
6616 struct wpa_supplicant *group_wpa_s;
6617
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006618 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006619 if (wpa_s->p2p_mgmt) {
6620 /*
6621 * We may be called on the p2p_dev interface which
6622 * cannot be used for group operations, so always use
6623 * the primary interface.
6624 */
6625 wpa_s->parent->p2pdev = wpa_s;
6626 wpa_s = wpa_s->parent;
6627 }
6628 wpa_dbg(wpa_s, MSG_DEBUG,
6629 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006630 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006631 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006632 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635
6636 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006637 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006638 wpa_msg_global(wpa_s, MSG_ERROR,
6639 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006640 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006642 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6643 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006644 wpa_msg_global(wpa_s, MSG_ERROR,
6645 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006646 wpas_p2p_remove_pending_group_interface(wpa_s);
6647 return NULL;
6648 }
6649
Roshan Pius3a1667e2018-07-03 15:17:14 -07006650 if (go && wpa_s->p2p_go_do_acs) {
6651 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6652 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6653 wpa_s->p2p_go_do_acs = 0;
6654 }
6655
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006656 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6657 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006658 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006659 return group_wpa_s;
6660}
6661
6662
6663/**
6664 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6665 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6666 * @persistent_group: Whether to create a persistent group
6667 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006668 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006669 * @ht40: Start GO with 40 MHz channel width
6670 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006671 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006672 * @edmg: Start GO with EDMG support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006673 * Returns: 0 on success, -1 on failure
6674 *
6675 * This function creates a new P2P group with the local end as the Group Owner,
6676 * i.e., without using Group Owner Negotiation.
6677 */
6678int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006679 int freq, int vht_center_freq2, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006680 int max_oper_chwidth, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681{
6682 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006683
6684 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6685 return -1;
6686
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006687 os_free(wpa_s->global->add_psk);
6688 wpa_s->global->add_psk = NULL;
6689
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006690 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006691 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006692 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006693
Roshan Pius3a1667e2018-07-03 15:17:14 -07006694 if (!wpa_s->p2p_go_do_acs) {
6695 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6696 if (freq < 0)
6697 return -1;
6698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006700 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006701 ht40, vht, max_oper_chwidth, he, edmg,
6702 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006703 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006705 p2p_go_params(wpa_s->global->p2p, &params);
6706 params.persistent_group = persistent_group;
6707
6708 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6709 if (wpa_s == NULL)
6710 return -1;
6711 wpas_start_wps_go(wpa_s, &params, 0);
6712
6713 return 0;
6714}
6715
6716
6717static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006718 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006719 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006720{
6721 struct wpa_ssid *ssid;
6722
6723 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6724 if (wpa_s == NULL)
6725 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006726 if (force_scan)
6727 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006728 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006729
6730 wpa_supplicant_ap_deinit(wpa_s);
6731
6732 ssid = wpa_config_add_network(wpa_s->conf);
6733 if (ssid == NULL)
6734 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006735 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006736 wpa_config_set_network_defaults(ssid);
6737 ssid->temporary = 1;
6738 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006739 ssid->pbss = params->pbss;
6740 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6741 WPA_CIPHER_CCMP;
6742 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006743 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6744 ssid->ssid = os_malloc(params->ssid_len);
6745 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006746 wpa_config_remove_network(wpa_s->conf, ssid->id);
6747 return -1;
6748 }
6749 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6750 ssid->ssid_len = params->ssid_len;
6751 ssid->p2p_group = 1;
6752 ssid->export_keys = 1;
6753 if (params->psk_set) {
6754 os_memcpy(ssid->psk, params->psk, 32);
6755 ssid->psk_set = 1;
6756 }
6757 if (params->passphrase)
6758 ssid->passphrase = os_strdup(params->passphrase);
6759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006760 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006761 wpa_s->p2p_in_invitation = 1;
6762 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006763 wpa_s->p2p_go_group_formation_completed = 0;
6764 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006765
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006766 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006767 NULL);
6768 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6769 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006770 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006771 wpa_supplicant_select_network(wpa_s, ssid);
6772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006773 return 0;
6774}
6775
6776
6777int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6778 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006779 int force_freq, int neg_freq,
6780 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006781 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006782 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006783 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006784 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006785{
6786 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006787 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006788
6789 if (ssid->disabled != 2 || ssid->ssid == NULL)
6790 return -1;
6791
6792 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6793 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6794 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6795 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006796 if (go == 0 &&
6797 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006798 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006799 /*
6800 * This can happen if Invitation Response frame was lost
6801 * and the peer (GO of a persistent group) tries to
6802 * invite us again. Reschedule the timeout to avoid
6803 * terminating the wait for the connection too early
6804 * since we now know that the peer is still trying to
6805 * invite us instead of having already started the GO.
6806 */
6807 wpa_printf(MSG_DEBUG,
6808 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6809 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6810 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006811 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006813 return 0;
6814 }
6815
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006816 os_free(wpa_s->global->add_psk);
6817 wpa_s->global->add_psk = NULL;
6818
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006820 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006821
Dmitry Shmidt04949592012-07-19 12:16:46 -07006822 wpa_s->p2p_fallback_to_go_neg = 0;
6823
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006824 if (ssid->mode == WPAS_MODE_P2P_GO) {
6825 if (force_freq > 0) {
6826 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6827 if (freq < 0)
6828 return -1;
6829 } else {
6830 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6831 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006832 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006833 freq = 0;
6834 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006835 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006836 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006837 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006838 struct os_reltime now;
6839 struct wpa_bss *bss =
6840 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006841
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006842 os_get_reltime(&now);
6843 if (bss &&
6844 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006845 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006846 freq = bss->freq;
6847 else
6848 freq = 0;
6849 }
6850
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006851 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6852 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006853 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006854 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006855 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006856
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006857 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006858 ht40, vht, max_oper_chwidth, he, edmg,
6859 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006860 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006861
6862 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006863 params.psk_set = ssid->psk_set;
6864 if (params.psk_set)
6865 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006866 if (ssid->passphrase) {
6867 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6868 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6869 "persistent group");
6870 return -1;
6871 }
6872 os_strlcpy(params.passphrase, ssid->passphrase,
6873 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006874 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006875 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6876 params.ssid_len = ssid->ssid_len;
6877 params.persistent_group = 1;
6878
6879 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6880 if (wpa_s == NULL)
6881 return -1;
6882
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006883 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6884
Dmitry Shmidt56052862013-10-04 10:23:25 -07006885 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886 wpas_start_wps_go(wpa_s, &params, 0);
6887
6888 return 0;
6889}
6890
6891
6892static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6893 struct wpabuf *proberesp_ies)
6894{
6895 struct wpa_supplicant *wpa_s = ctx;
6896 if (wpa_s->ap_iface) {
6897 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006898 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6899 wpabuf_free(beacon_ies);
6900 wpabuf_free(proberesp_ies);
6901 return;
6902 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006903 if (beacon_ies) {
6904 wpabuf_free(hapd->p2p_beacon_ie);
6905 hapd->p2p_beacon_ie = beacon_ies;
6906 }
6907 wpabuf_free(hapd->p2p_probe_resp_ie);
6908 hapd->p2p_probe_resp_ie = proberesp_ies;
6909 } else {
6910 wpabuf_free(beacon_ies);
6911 wpabuf_free(proberesp_ies);
6912 }
6913 wpa_supplicant_ap_update_beacon(wpa_s);
6914}
6915
6916
6917static void wpas_p2p_idle_update(void *ctx, int idle)
6918{
6919 struct wpa_supplicant *wpa_s = ctx;
6920 if (!wpa_s->ap_iface)
6921 return;
6922 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006923 if (idle) {
6924 if (wpa_s->global->p2p_fail_on_wps_complete &&
6925 wpa_s->p2p_in_provisioning) {
6926 wpas_p2p_grpform_fail_after_wps(wpa_s);
6927 return;
6928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006929 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006930 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6932}
6933
6934
6935struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006936 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937{
6938 struct p2p_group *group;
6939 struct p2p_group_config *cfg;
6940
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006941 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6942 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006943 return NULL;
6944
6945 cfg = os_zalloc(sizeof(*cfg));
6946 if (cfg == NULL)
6947 return NULL;
6948
Dmitry Shmidt04949592012-07-19 12:16:46 -07006949 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006950 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006951 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006952 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006953 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6954 if (wpa_s->max_stations &&
6955 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6956 cfg->max_clients = wpa_s->max_stations;
6957 else
6958 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006959 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6960 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006961 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962 cfg->cb_ctx = wpa_s;
6963 cfg->ie_update = wpas_p2p_ie_update;
6964 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006965 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6966 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006967
6968 group = p2p_group_init(wpa_s->global->p2p, cfg);
6969 if (group == NULL)
6970 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006971 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 p2p_group_notif_formation_done(group);
6973 wpa_s->p2p_group = group;
6974 return group;
6975}
6976
6977
6978void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6979 int registrar)
6980{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006981 struct wpa_ssid *ssid = wpa_s->current_ssid;
6982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006983 if (!wpa_s->p2p_in_provisioning) {
6984 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6985 "provisioning not in progress");
6986 return;
6987 }
6988
Dmitry Shmidt04949592012-07-19 12:16:46 -07006989 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6990 u8 go_dev_addr[ETH_ALEN];
6991 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6992 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6993 ssid->ssid_len);
6994 /* Clear any stored provisioning info */
6995 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6996 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006997
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006998 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006999 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007000 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007001 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7002 /*
7003 * Use a separate timeout for initial data connection to
7004 * complete to allow the group to be removed automatically if
7005 * something goes wrong in this step before the P2P group idle
7006 * timeout mechanism is taken into use.
7007 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007008 wpa_dbg(wpa_s, MSG_DEBUG,
7009 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7010 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007011 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7012 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007013 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007014 /* Complete group formation on successful data connection. */
7015 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007016 } else if (ssid) {
7017 /*
7018 * Use a separate timeout for initial data connection to
7019 * complete to allow the group to be removed automatically if
7020 * the client does not complete data connection successfully.
7021 */
7022 wpa_dbg(wpa_s, MSG_DEBUG,
7023 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7024 P2P_MAX_INITIAL_CONN_WAIT_GO);
7025 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7026 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007027 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007028 /*
7029 * Complete group formation on first successful data connection
7030 */
7031 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007032 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033 if (wpa_s->global->p2p)
7034 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007035 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007036}
7037
7038
Jouni Malinen75ecf522011-06-27 15:19:46 -07007039void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7040 struct wps_event_fail *fail)
7041{
7042 if (!wpa_s->p2p_in_provisioning) {
7043 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7044 "provisioning not in progress");
7045 return;
7046 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007047
7048 if (wpa_s->go_params) {
7049 p2p_clear_provisioning_info(
7050 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007051 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007052 }
7053
Jouni Malinen75ecf522011-06-27 15:19:46 -07007054 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007055
7056 if (wpa_s == wpa_s->global->p2p_group_formation) {
7057 /*
7058 * Allow some time for the failed WPS negotiation exchange to
7059 * complete, but remove the group since group formation cannot
7060 * succeed after provisioning failure.
7061 */
7062 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7063 wpa_s->global->p2p_fail_on_wps_complete = 1;
7064 eloop_deplete_timeout(0, 50000,
7065 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007066 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007067 }
7068}
7069
7070
7071int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7072{
7073 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7074 !wpa_s->p2p_in_provisioning)
7075 return 0;
7076
7077 wpas_p2p_grpform_fail_after_wps(wpa_s);
7078
7079 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007080}
7081
7082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007083int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007084 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007085 enum wpas_p2p_prov_disc_use use,
7086 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007087{
7088 u16 config_methods;
7089
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007090 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007091 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007092 wpa_s->p2p_fallback_to_go_neg = 0;
7093 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007094 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7095 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007096 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7097 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7098
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007099 wpa_printf(MSG_DEBUG,
7100 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7101 __func__, p2ps_prov->conncap,
7102 p2ps_prov->adv_id, p2ps_prov->conncap,
7103 p2ps_prov->status, p2ps_prov->info);
7104
7105 config_methods = 0;
7106 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007108 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007110 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7111 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007112 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007113 else {
7114 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007115 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007116 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007118
Dmitry Shmidt04949592012-07-19 12:16:46 -07007119 if (use == WPAS_P2P_PD_AUTO) {
7120 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7121 wpa_s->pending_pd_config_methods = config_methods;
7122 wpa_s->p2p_auto_pd = 1;
7123 wpa_s->p2p_auto_join = 0;
7124 wpa_s->pending_pd_before_join = 0;
7125 wpa_s->auto_pd_scan_retry = 0;
7126 wpas_p2p_stop_find(wpa_s);
7127 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007128 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007129 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7130 wpa_s->p2p_auto_started.sec,
7131 wpa_s->p2p_auto_started.usec);
7132 wpas_p2p_join_scan(wpa_s, NULL);
7133 return 0;
7134 }
7135
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007136 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7137 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007138 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007141 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007142 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007143 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007144}
7145
7146
7147int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7148 char *end)
7149{
7150 return p2p_scan_result_text(ies, ies_len, buf, end);
7151}
7152
7153
7154static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
7155{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007156 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007157 return;
7158
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007159 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007160 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007161 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7162 wpa_s, NULL);
7163 offchannel_send_action_done(wpa_s);
7164 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007166 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7167 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007168 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007169}
7170
7171
7172int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7173 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007174 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007175 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007176 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007177{
7178 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007179 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007180
Dmitry Shmidt04949592012-07-19 12:16:46 -07007181 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007182 wpa_s->p2p_in_provisioning) {
7183 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7184 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7185 " (P2P disabled)" : "",
7186 wpa_s->p2p_in_provisioning ?
7187 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007188 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007190
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007191 wpa_supplicant_cancel_sched_scan(wpa_s);
7192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007193 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007194 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007195 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007196}
7197
7198
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007199static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7200 struct wpa_scan_results *scan_res)
7201{
7202 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7203
7204 if (wpa_s->p2p_scan_work) {
7205 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7206 wpa_s->p2p_scan_work = NULL;
7207 radio_work_done(work);
7208 }
7209
7210 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7211 return;
7212
7213 /*
7214 * Indicate that results have been processed so that the P2P module can
7215 * continue pending tasks.
7216 */
Hai Shalom60840252021-02-19 19:02:11 -08007217 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007218}
7219
7220
7221static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007222{
7223 wpas_p2p_clear_pending_action_tx(wpa_s);
Hai Shalomfdcde762020-04-02 11:19:20 -07007224 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007225 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7226 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007227
7228 if (wpa_s->global->p2p)
7229 p2p_stop_find(wpa_s->global->p2p);
7230
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007231 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7232 wpa_printf(MSG_DEBUG,
7233 "P2P: Do not consider the scan results after stop_find");
7234 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7235 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007236}
7237
7238
7239void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7240{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007241 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007242 if (!wpa_s->global->pending_group_iface_for_p2ps)
7243 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007244}
7245
7246
7247static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7248{
7249 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007250 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007251}
7252
7253
7254int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7255{
7256 int res;
7257
7258 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7259 return -1;
7260
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007261 if (wpa_s->p2p_lo_started) {
7262 wpa_printf(MSG_DEBUG,
7263 "P2P: Cannot start P2P listen, it is offloaded");
7264 return -1;
7265 }
7266
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007267 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007268 wpas_p2p_clear_pending_action_tx(wpa_s);
7269
7270 if (timeout == 0) {
7271 /*
7272 * This is a request for unlimited Listen state. However, at
7273 * least for now, this is mapped to a Listen state for one
7274 * hour.
7275 */
7276 timeout = 3600;
7277 }
7278 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007279 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007280
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007281 /*
7282 * Stop previous find/listen operation to avoid trying to request a new
7283 * remain-on-channel operation while the driver is still running the
7284 * previous one.
7285 */
7286 if (wpa_s->global->p2p)
7287 p2p_stop_find(wpa_s->global->p2p);
7288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007289 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7290 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007291 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007292 eloop_register_timeout(timeout, 0,
7293 wpas_p2p_long_listen_timeout,
7294 wpa_s, NULL);
7295 }
7296
7297 return res;
7298}
7299
7300
7301int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7302 u8 *buf, size_t len, int p2p_group)
7303{
7304 struct wpabuf *p2p_ie;
7305 int ret;
7306
7307 if (wpa_s->global->p2p_disabled)
7308 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007309 /*
7310 * Advertize mandatory cross connection capability even on
7311 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7312 */
7313 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007314 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007315 if (wpa_s->global->p2p == NULL)
7316 return -1;
7317 if (bss == NULL)
7318 return -1;
7319
7320 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7321 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7322 p2p_group, p2p_ie);
7323 wpabuf_free(p2p_ie);
7324
7325 return ret;
7326}
7327
7328
7329int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007330 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007331 const u8 *ie, size_t ie_len,
7332 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007333{
7334 if (wpa_s->global->p2p_disabled)
7335 return 0;
7336 if (wpa_s->global->p2p == NULL)
7337 return 0;
7338
Dmitry Shmidt04949592012-07-19 12:16:46 -07007339 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007340 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007341 case P2P_PREQ_NOT_P2P:
7342 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7343 ssi_signal);
7344 /* fall through */
7345 case P2P_PREQ_MALFORMED:
7346 case P2P_PREQ_NOT_LISTEN:
7347 case P2P_PREQ_NOT_PROCESSED:
7348 default: /* make gcc happy */
7349 return 0;
7350 case P2P_PREQ_PROCESSED:
7351 return 1;
7352 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007353}
7354
7355
7356void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7357 const u8 *sa, const u8 *bssid,
7358 u8 category, const u8 *data, size_t len, int freq)
7359{
7360 if (wpa_s->global->p2p_disabled)
7361 return;
7362 if (wpa_s->global->p2p == NULL)
7363 return;
7364
7365 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7366 freq);
7367}
7368
7369
7370void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7371{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007372 unsigned int bands;
7373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007374 if (wpa_s->global->p2p_disabled)
7375 return;
7376 if (wpa_s->global->p2p == NULL)
7377 return;
7378
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007379 bands = wpas_get_bands(wpa_s, NULL);
7380 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007381}
7382
7383
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007384static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385{
7386 p2p_group_deinit(wpa_s->p2p_group);
7387 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007388
7389 wpa_s->ap_configured_cb = NULL;
7390 wpa_s->ap_configured_cb_ctx = NULL;
7391 wpa_s->ap_configured_cb_data = NULL;
7392 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007393}
7394
7395
7396int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7397{
Hai Shalomfdcde762020-04-02 11:19:20 -07007398 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007400 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7401 return -1;
7402
7403 return p2p_reject(wpa_s->global->p2p, addr);
7404}
7405
7406
7407/* Invite to reinvoke a persistent group */
7408int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007409 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007410 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007411 int pref_freq, int he, int edmg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007412{
7413 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007414 u8 *bssid = NULL;
7415 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007416 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007417 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007418 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007419
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007420 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007421 if (peer_addr)
7422 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7423 else
7424 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007425
Jouni Malinen31be0a42012-08-31 21:20:51 +03007426 wpa_s->p2p_persistent_go_freq = freq;
7427 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007428 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007429 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007430 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7431 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007432 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 if (ssid->mode == WPAS_MODE_P2P_GO) {
7434 role = P2P_INVITE_ROLE_GO;
7435 if (peer_addr == NULL) {
7436 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7437 "address in invitation command");
7438 return -1;
7439 }
7440 if (wpas_p2p_create_iface(wpa_s)) {
7441 if (wpas_p2p_add_group_interface(wpa_s,
7442 WPA_IF_P2P_GO) < 0) {
7443 wpa_printf(MSG_ERROR, "P2P: Failed to "
7444 "allocate a new interface for the "
7445 "group");
7446 return -1;
7447 }
7448 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007449 } else if (wpa_s->p2p_mgmt)
7450 bssid = wpa_s->parent->own_addr;
7451 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007452 bssid = wpa_s->own_addr;
7453 } else {
7454 role = P2P_INVITE_ROLE_CLIENT;
7455 peer_addr = ssid->bssid;
7456 }
7457 wpa_s->pending_invite_ssid_id = ssid->id;
7458
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007459 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007460 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007461 role == P2P_INVITE_ROLE_GO,
7462 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007463 if (res)
7464 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7467 return -1;
7468
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007469 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7470
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007471 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7472 no_pref_freq_given && pref_freq > 0 &&
7473 wpa_s->num_multichan_concurrent > 1 &&
7474 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7475 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7476 pref_freq);
7477 pref_freq = 0;
7478 }
7479
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007480 /*
7481 * Stop any find/listen operations before invitation and possibly
7482 * connection establishment.
7483 */
7484 wpas_p2p_stop_find_oper(wpa_s);
7485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007486 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007487 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007488 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007489}
7490
7491
7492/* Invite to join an active group */
7493int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7494 const u8 *peer_addr, const u8 *go_dev_addr)
7495{
7496 struct wpa_global *global = wpa_s->global;
7497 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007498 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007499 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007500 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007501 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007502 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007503 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007504
Jouni Malinen31be0a42012-08-31 21:20:51 +03007505 wpa_s->p2p_persistent_go_freq = 0;
7506 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007507 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007508 wpa_s->p2p_go_vht_center_freq2 = 0;
7509 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007510 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007512 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7513 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7514 break;
7515 }
7516 if (wpa_s == NULL) {
7517 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7518 return -1;
7519 }
7520
7521 ssid = wpa_s->current_ssid;
7522 if (ssid == NULL) {
7523 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7524 "invitation");
7525 return -1;
7526 }
7527
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007528 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007529 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007530 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007531 ssid->ssid, ssid->ssid_len);
7532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007533 if (ssid->mode == WPAS_MODE_P2P_GO) {
7534 role = P2P_INVITE_ROLE_ACTIVE_GO;
7535 bssid = wpa_s->own_addr;
7536 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007537 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007538 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007539 } else {
7540 role = P2P_INVITE_ROLE_CLIENT;
7541 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7542 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7543 "invite to current group");
7544 return -1;
7545 }
7546 bssid = wpa_s->bssid;
7547 if (go_dev_addr == NULL &&
7548 !is_zero_ether_addr(wpa_s->go_dev_addr))
7549 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007550 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7551 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007553 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007555 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7556 return -1;
7557
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007558 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007559 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007560 role == P2P_INVITE_ROLE_ACTIVE_GO,
7561 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007562 if (res)
7563 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007564 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007566 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007567 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007568 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007569}
7570
7571
7572void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7573{
7574 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007575 u8 go_dev_addr[ETH_ALEN];
7576 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007577 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007578 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007579 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007580
Dmitry Shmidt04949592012-07-19 12:16:46 -07007581 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7582 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007583 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007584 }
7585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007586 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007587 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007588
7589 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007590 if (!wpa_s->p2p_go_group_formation_completed &&
7591 wpa_s->global->p2p_group_formation == wpa_s) {
7592 wpa_dbg(wpa_s, MSG_DEBUG,
7593 "P2P: Marking group formation completed on client on data connection");
7594 wpa_s->p2p_go_group_formation_completed = 1;
7595 wpa_s->global->p2p_group_formation = NULL;
7596 wpa_s->p2p_in_provisioning = 0;
7597 wpa_s->p2p_in_invitation = 0;
7598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007600 os_memset(go_dev_addr, 0, ETH_ALEN);
7601 if (ssid->bssid_set)
7602 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7603 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7604 ssid->ssid_len);
7605 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7606
7607 if (wpa_s->global->p2p_group_formation == wpa_s)
7608 wpa_s->global->p2p_group_formation = NULL;
7609
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007610 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7611 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007612
7613 ip_addr[0] = '\0';
7614 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007615 int res;
7616
7617 res = os_snprintf(ip_addr, sizeof(ip_addr),
7618 " ip_addr=%u.%u.%u.%u "
7619 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7620 ip[0], ip[1], ip[2], ip[3],
7621 ip[4], ip[5], ip[6], ip[7],
7622 ip[8], ip[9], ip[10], ip[11]);
7623 if (os_snprintf_error(sizeof(ip_addr), res))
7624 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007625 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007626 }
7627
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007628 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7629 ssid->passphrase == NULL && ssid->psk_set ?
7630 ssid->psk : NULL,
7631 ssid->passphrase, go_dev_addr, persistent,
7632 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007633
7634 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007635 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7636 ssid, go_dev_addr);
7637
Hai Shalom5f92bc92019-04-18 11:54:11 -07007638 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007639}
7640
7641
7642int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7643 u32 interval1, u32 duration2, u32 interval2)
7644{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007645 int ret;
7646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7648 return -1;
7649
7650 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7651 wpa_s->current_ssid == NULL ||
7652 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7653 return -1;
7654
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007655 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7656 wpa_s->own_addr, wpa_s->assoc_freq,
7657 duration1, interval1, duration2, interval2);
7658 if (ret == 0)
7659 wpa_s->waiting_presence_resp = 1;
7660
7661 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007662}
7663
7664
7665int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7666 unsigned int interval)
7667{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007668 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7669 return -1;
7670
7671 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7672}
7673
7674
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007675static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7676{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007677 if (wpa_s->current_ssid == NULL) {
7678 /*
7679 * current_ssid can be cleared when P2P client interface gets
7680 * disconnected, so assume this interface was used as P2P
7681 * client.
7682 */
7683 return 1;
7684 }
7685 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007686 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7687}
7688
7689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007690static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7691{
7692 struct wpa_supplicant *wpa_s = eloop_ctx;
7693
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007694 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007695 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7696 "disabled");
7697 return;
7698 }
7699
Dmitry Shmidt04949592012-07-19 12:16:46 -07007700 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7701 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007702 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007703}
7704
7705
7706static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7707{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007708 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007709
Dmitry Shmidt04949592012-07-19 12:16:46 -07007710 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7711 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007713 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7714 return;
7715
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007716 timeout = wpa_s->conf->p2p_group_idle;
7717 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7718 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7719 timeout = P2P_MAX_CLIENT_IDLE;
7720
7721 if (timeout == 0)
7722 return;
7723
Dmitry Shmidt04949592012-07-19 12:16:46 -07007724 if (timeout < 0) {
7725 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7726 timeout = 0; /* special client mode no-timeout */
7727 else
7728 return;
7729 }
7730
7731 if (wpa_s->p2p_in_provisioning) {
7732 /*
7733 * Use the normal group formation timeout during the
7734 * provisioning phase to avoid terminating this process too
7735 * early due to group idle timeout.
7736 */
7737 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7738 "during provisioning");
7739 return;
7740 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307741
Dmitry Shmidt04949592012-07-19 12:16:46 -07007742 if (wpa_s->show_group_started) {
7743 /*
7744 * Use the normal group formation timeout between the end of
7745 * the provisioning phase and completion of 4-way handshake to
7746 * avoid terminating this process too early due to group idle
7747 * timeout.
7748 */
7749 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7750 "while waiting for initial 4-way handshake to "
7751 "complete");
7752 return;
7753 }
7754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007756 timeout);
7757 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7758 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007759}
7760
7761
Jouni Malinen2b89da82012-08-31 22:04:41 +03007762/* Returns 1 if the interface was removed */
7763int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7764 u16 reason_code, const u8 *ie, size_t ie_len,
7765 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007766{
7767 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007768 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007769
Dmitry Shmidt04949592012-07-19 12:16:46 -07007770 if (!locally_generated)
7771 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7772 ie_len);
7773
7774 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7775 wpa_s->current_ssid &&
7776 wpa_s->current_ssid->p2p_group &&
7777 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7778 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7779 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007780 if (wpas_p2p_group_delete(wpa_s,
7781 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7782 > 0)
7783 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007784 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007785
7786 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007787}
7788
7789
7790void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007791 u16 reason_code, const u8 *ie, size_t ie_len,
7792 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793{
7794 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7795 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007796
Dmitry Shmidt04949592012-07-19 12:16:46 -07007797 if (!locally_generated)
7798 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7799 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007800}
7801
7802
7803void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7804{
7805 struct p2p_data *p2p = wpa_s->global->p2p;
7806
7807 if (p2p == NULL)
7808 return;
7809
7810 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7811 return;
7812
7813 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7814 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7815
7816 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7817 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7818
7819 if (wpa_s->wps &&
7820 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7821 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7822
7823 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7824 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7825
7826 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7827 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7828 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7829 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7830 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7831 }
7832
7833 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7834 p2p_set_sec_dev_types(p2p,
7835 (void *) wpa_s->conf->sec_device_type,
7836 wpa_s->conf->num_sec_device_types);
7837
7838 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7839 int i;
7840 p2p_remove_wps_vendor_extensions(p2p);
7841 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7842 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7843 continue;
7844 p2p_add_wps_vendor_extension(
7845 p2p, wpa_s->conf->wps_vendor_ext[i]);
7846 }
7847 }
7848
7849 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7850 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7851 char country[3];
7852 country[0] = wpa_s->conf->country[0];
7853 country[1] = wpa_s->conf->country[1];
7854 country[2] = 0x04;
7855 p2p_set_country(p2p, country);
7856 }
7857
7858 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7859 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7860 wpa_s->conf->p2p_ssid_postfix ?
7861 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7862 0);
7863 }
7864
7865 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7866 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007867
7868 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7869 u8 reg_class, channel;
7870 int ret;
7871 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007872 u8 channel_forced;
7873
Jouni Malinen75ecf522011-06-27 15:19:46 -07007874 if (wpa_s->conf->p2p_listen_reg_class &&
7875 wpa_s->conf->p2p_listen_channel) {
7876 reg_class = wpa_s->conf->p2p_listen_reg_class;
7877 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007878 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007879 } else {
7880 reg_class = 81;
7881 /*
7882 * Pick one of the social channels randomly as the
7883 * listen channel.
7884 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007885 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7886 channel = 1;
7887 else
7888 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007889 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007890 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007891 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7892 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007893 if (ret)
7894 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7895 "failed: %d", ret);
7896 }
7897 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7898 u8 op_reg_class, op_channel, cfg_op_channel;
7899 int ret = 0;
7900 unsigned int r;
7901 if (wpa_s->conf->p2p_oper_reg_class &&
7902 wpa_s->conf->p2p_oper_channel) {
7903 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7904 op_channel = wpa_s->conf->p2p_oper_channel;
7905 cfg_op_channel = 1;
7906 } else {
7907 op_reg_class = 81;
7908 /*
7909 * Use random operation channel from (1, 6, 11)
7910 *if no other preference is indicated.
7911 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007912 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7913 op_channel = 1;
7914 else
7915 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007916 cfg_op_channel = 0;
7917 }
7918 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7919 cfg_op_channel);
7920 if (ret)
7921 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7922 "failed: %d", ret);
7923 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007924
7925 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7926 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7927 wpa_s->conf->p2p_pref_chan) < 0) {
7928 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7929 "update failed");
7930 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007931
7932 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7933 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7934 "update failed");
7935 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007936 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007937
7938 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7939 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007940}
7941
7942
7943int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7944 int duration)
7945{
7946 if (!wpa_s->ap_iface)
7947 return -1;
7948 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7949 duration);
7950}
7951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007952
7953int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7954{
7955 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7956 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007957
7958 wpa_s->global->cross_connection = enabled;
7959 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7960
7961 if (!enabled) {
7962 struct wpa_supplicant *iface;
7963
7964 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7965 {
7966 if (iface->cross_connect_enabled == 0)
7967 continue;
7968
7969 iface->cross_connect_enabled = 0;
7970 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007971 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007972 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7973 iface->ifname,
7974 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007975 }
7976 }
7977
7978 return 0;
7979}
7980
7981
7982static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7983{
7984 struct wpa_supplicant *iface;
7985
7986 if (!uplink->global->cross_connection)
7987 return;
7988
7989 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7990 if (!iface->cross_connect_enabled)
7991 continue;
7992 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7993 0)
7994 continue;
7995 if (iface->ap_iface == NULL)
7996 continue;
7997 if (iface->cross_connect_in_use)
7998 continue;
7999
8000 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008001 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008002 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8003 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008004 }
8005}
8006
8007
8008static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8009{
8010 struct wpa_supplicant *iface;
8011
8012 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8013 if (!iface->cross_connect_enabled)
8014 continue;
8015 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8016 0)
8017 continue;
8018 if (!iface->cross_connect_in_use)
8019 continue;
8020
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008021 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008022 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8023 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008024 iface->cross_connect_in_use = 0;
8025 }
8026}
8027
8028
8029void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8030{
8031 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8032 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8033 wpa_s->cross_connect_disallowed)
8034 wpas_p2p_disable_cross_connect(wpa_s);
8035 else
8036 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008037 if (!wpa_s->ap_iface &&
8038 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8039 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008040}
8041
8042
8043void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8044{
8045 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008046 if (!wpa_s->ap_iface &&
8047 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8048 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008049 wpas_p2p_set_group_idle_timeout(wpa_s);
8050}
8051
8052
8053static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8054{
8055 struct wpa_supplicant *iface;
8056
8057 if (!wpa_s->global->cross_connection)
8058 return;
8059
8060 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8061 if (iface == wpa_s)
8062 continue;
8063 if (iface->drv_flags &
8064 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8065 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008066 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8067 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008068 continue;
8069
8070 wpa_s->cross_connect_enabled = 1;
8071 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8072 sizeof(wpa_s->cross_connect_uplink));
8073 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8074 "%s to %s whenever uplink is available",
8075 wpa_s->ifname, wpa_s->cross_connect_uplink);
8076
8077 if (iface->ap_iface || iface->current_ssid == NULL ||
8078 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8079 iface->cross_connect_disallowed ||
8080 iface->wpa_state != WPA_COMPLETED)
8081 break;
8082
8083 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008084 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008085 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8086 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008087 break;
8088 }
8089}
8090
8091
8092int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
8093{
8094 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8095 !wpa_s->p2p_in_provisioning)
8096 return 0; /* not P2P client operation */
8097
8098 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8099 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008100 if (wpa_s != wpa_s->p2pdev)
8101 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008102 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008103 return 1;
8104}
8105
8106
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008107void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8108{
8109 struct wpa_supplicant *wpa_s = eloop_ctx;
8110 wpas_p2p_notif_pbc_overlap(wpa_s);
8111}
8112
8113
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008114void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8115 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008116{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008117 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008118 struct wpa_used_freq_data *freqs = NULL;
8119 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008120
8121 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8122 return;
8123
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008124 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8125 if (!freqs)
8126 return;
8127
8128 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008130 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008131 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008132 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8133 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008134 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8135 "channel list");
8136 return;
8137 }
8138
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008139 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008140
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008141 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008142
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008143 /*
8144 * The used frequencies map changed, so it is possible that a GO is
8145 * using a channel that is no longer valid for P2P use. It is also
8146 * possible that due to policy consideration, it would be preferable to
8147 * move it to a frequency already used by other station interfaces.
8148 */
8149 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8150
8151 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008152}
8153
8154
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008155static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8156 struct wpa_scan_results *scan_res)
8157{
8158 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8159}
8160
8161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008162int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8163{
8164 struct wpa_global *global = wpa_s->global;
8165 int found = 0;
8166 const u8 *peer;
8167
8168 if (global->p2p == NULL)
8169 return -1;
8170
8171 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8172
8173 if (wpa_s->pending_interface_name[0] &&
8174 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8175 found = 1;
8176
8177 peer = p2p_get_go_neg_peer(global->p2p);
8178 if (peer) {
8179 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8180 MACSTR, MAC2STR(peer));
8181 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008182 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008183 }
8184
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008185 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8186 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8187 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8188 found = 1;
8189 }
8190
8191 if (wpa_s->pending_pd_before_join) {
8192 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8193 wpa_s->pending_pd_before_join = 0;
8194 found = 1;
8195 }
8196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008197 wpas_p2p_stop_find(wpa_s);
8198
8199 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8200 if (wpa_s == global->p2p_group_formation &&
8201 (wpa_s->p2p_in_provisioning ||
8202 wpa_s->parent->pending_interface_type ==
8203 WPA_IF_P2P_CLIENT)) {
8204 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8205 "formation found - cancelling",
8206 wpa_s->ifname);
8207 found = 1;
8208 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008209 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008210 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008211 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008212 break;
8213 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008214 wpas_p2p_group_delete(wpa_s,
8215 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008216 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008217 } else if (wpa_s->p2p_in_invitation) {
8218 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8219 wpa_s->ifname);
8220 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008221 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008222 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008223 }
8224 }
8225
8226 if (!found) {
8227 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8228 return -1;
8229 }
8230
8231 return 0;
8232}
8233
8234
8235void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8236{
8237 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8238 return;
8239
8240 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8241 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008242 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008243}
8244
8245
8246void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8247 int freq_24, int freq_5, int freq_overall)
8248{
8249 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008250 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008251 return;
8252 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8253}
8254
8255
8256int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8257{
8258 u8 peer[ETH_ALEN];
8259 struct p2p_data *p2p = wpa_s->global->p2p;
8260
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008261 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008262 return -1;
8263
8264 if (hwaddr_aton(addr, peer))
8265 return -1;
8266
8267 return p2p_unauthorize(p2p, peer);
8268}
8269
8270
8271/**
8272 * wpas_p2p_disconnect - Disconnect from a P2P Group
8273 * @wpa_s: Pointer to wpa_supplicant data
8274 * Returns: 0 on success, -1 on failure
8275 *
8276 * This can be used to disconnect from a group in which the local end is a P2P
8277 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8278 * virtual network interface was created for this group, that interface will be
8279 * removed. Otherwise, only the configured P2P group network will be removed
8280 * from the interface.
8281 */
8282int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8283{
8284
8285 if (wpa_s == NULL)
8286 return -1;
8287
Jouni Malinen2b89da82012-08-31 22:04:41 +03008288 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8289 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008290}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008291
8292
8293int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8294{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008295 int ret;
8296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008297 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8298 return 0;
8299
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008300 ret = p2p_in_progress(wpa_s->global->p2p);
8301 if (ret == 0) {
8302 /*
8303 * Check whether there is an ongoing WPS provisioning step (or
8304 * other parts of group formation) on another interface since
8305 * p2p_in_progress() does not report this to avoid issues for
8306 * scans during such provisioning step.
8307 */
8308 if (wpa_s->global->p2p_group_formation &&
8309 wpa_s->global->p2p_group_formation != wpa_s) {
8310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8311 "in group formation",
8312 wpa_s->global->p2p_group_formation->ifname);
8313 ret = 1;
8314 }
8315 }
8316
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008317 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008318 struct os_reltime now;
8319 os_get_reltime(&now);
8320 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8321 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008322 /* Wait for the first client has expired */
8323 wpa_s->global->p2p_go_wait_client.sec = 0;
8324 } else {
8325 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8326 ret = 1;
8327 }
8328 }
8329
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008330 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008331}
8332
8333
8334void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8335 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008336{
8337 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8338 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008339 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008340 /**
8341 * Remove the network by scheduling the group formation
8342 * timeout to happen immediately. The teardown code
8343 * needs to be scheduled to run asynch later so that we
8344 * don't delete data from under ourselves unexpectedly.
8345 * Calling wpas_p2p_group_formation_timeout directly
8346 * causes a series of crashes in WPS failure scenarios.
8347 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008348 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8349 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008350 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008351 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008352 }
8353}
8354
8355
8356struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008357 const u8 *addr, const u8 *ssid,
8358 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008359{
8360 struct wpa_ssid *s;
8361 size_t i;
8362
8363 for (s = wpa_s->conf->ssid; s; s = s->next) {
8364 if (s->disabled != 2)
8365 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008366 if (ssid &&
8367 (ssid_len != s->ssid_len ||
8368 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8369 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008370 if (addr == NULL) {
8371 if (s->mode == WPAS_MODE_P2P_GO)
8372 return s;
8373 continue;
8374 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008375 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8376 return s; /* peer is GO in the persistent group */
8377 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8378 continue;
8379 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008380 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008381 addr, ETH_ALEN) == 0)
8382 return s; /* peer is P2P client in persistent
8383 * group */
8384 }
8385 }
8386
8387 return NULL;
8388}
8389
8390
8391void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8392 const u8 *addr)
8393{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008394 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008395 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008396 /*
8397 * This can happen if WPS provisioning step is not terminated
8398 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8399 * peer was able to connect, there is no need to time out group
8400 * formation after this, though. In addition, this is used with
8401 * the initial connection wait on the GO as a separate formation
8402 * timeout and as such, expected to be hit after the initial WPS
8403 * provisioning step.
8404 */
8405 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008406
8407 if (!wpa_s->p2p_go_group_formation_completed &&
8408 !wpa_s->group_formation_reported) {
8409 /*
8410 * GO has not yet notified group formation success since
8411 * the WPS step was not completed cleanly. Do that
8412 * notification now since the P2P Client was able to
8413 * connect and as such, must have received the
8414 * credential from the WPS step.
8415 */
8416 if (wpa_s->global->p2p)
8417 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008418 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008419 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008420 }
8421 if (!wpa_s->p2p_go_group_formation_completed) {
8422 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8423 wpa_s->p2p_go_group_formation_completed = 1;
8424 wpa_s->global->p2p_group_formation = NULL;
8425 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008426 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008427 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008428 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008429 if (addr == NULL)
8430 return;
8431 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8432}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008433
Dmitry Shmidt04949592012-07-19 12:16:46 -07008434
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008435static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8436 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008437{
8438 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008439 int ret = 0;
8440
Dmitry Shmidt04949592012-07-19 12:16:46 -07008441 if (wpa_s->global->p2p_group_formation)
8442 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008443 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008444 offchannel_send_action_done(wpa_s);
8445 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008446 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008447 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8448 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8449 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8450 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008451 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008452 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008453 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008454 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008455 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008456 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008457 wpa_s->p2p_go_he,
8458 wpa_s->p2p_go_edmg,
8459 NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008460 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008461}
8462
8463
8464int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8465{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008466 int res;
8467
Dmitry Shmidt04949592012-07-19 12:16:46 -07008468 if (!wpa_s->p2p_fallback_to_go_neg ||
8469 wpa_s->p2p_in_provisioning <= 5)
8470 return 0;
8471
8472 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8473 return 0; /* peer operating as a GO */
8474
8475 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8476 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008477 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008478 "reason=GO-not-found");
8479 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008480
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008481 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008482}
8483
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008484
8485unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8486{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008487 struct wpa_supplicant *ifs;
8488
8489 if (wpa_s->wpa_state > WPA_SCANNING) {
8490 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8491 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008492 wpa_s->conf->p2p_search_delay);
8493 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008494 }
8495
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008496 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8497 radio_list) {
8498 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008499 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8500 "delay due to concurrent operation on "
8501 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008502 wpa_s->conf->p2p_search_delay,
8503 ifs->ifname);
8504 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008505 }
8506 }
8507
8508 return 0;
8509}
8510
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008511
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008512static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8513 struct wpa_ssid *s, const u8 *addr,
8514 int iface_addr)
8515{
8516 struct psk_list_entry *psk, *tmp;
8517 int changed = 0;
8518
8519 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8520 list) {
8521 if ((iface_addr && !psk->p2p &&
8522 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8523 (!iface_addr && psk->p2p &&
8524 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8525 wpa_dbg(wpa_s, MSG_DEBUG,
8526 "P2P: Remove persistent group PSK list entry for "
8527 MACSTR " p2p=%u",
8528 MAC2STR(psk->addr), psk->p2p);
8529 dl_list_del(&psk->list);
8530 os_free(psk);
8531 changed++;
8532 }
8533 }
8534
8535 return changed;
8536}
8537
8538
8539void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8540 const u8 *p2p_dev_addr,
8541 const u8 *psk, size_t psk_len)
8542{
8543 struct wpa_ssid *ssid = wpa_s->current_ssid;
8544 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008545 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008546
8547 if (psk_len != sizeof(p->psk))
8548 return;
8549
8550 if (p2p_dev_addr) {
8551 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8552 " p2p_dev_addr=" MACSTR,
8553 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8554 if (is_zero_ether_addr(p2p_dev_addr))
8555 p2p_dev_addr = NULL;
8556 } else {
8557 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8558 MAC2STR(mac_addr));
8559 }
8560
8561 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8563 /* To be added to persistent group once created */
8564 if (wpa_s->global->add_psk == NULL) {
8565 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8566 if (wpa_s->global->add_psk == NULL)
8567 return;
8568 }
8569 p = wpa_s->global->add_psk;
8570 if (p2p_dev_addr) {
8571 p->p2p = 1;
8572 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8573 } else {
8574 p->p2p = 0;
8575 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8576 }
8577 os_memcpy(p->psk, psk, psk_len);
8578 return;
8579 }
8580
8581 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8582 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8583 return;
8584 }
8585
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008586 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008587 ssid->ssid_len);
8588 if (!persistent) {
8589 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8590 return;
8591 }
8592
8593 p = os_zalloc(sizeof(*p));
8594 if (p == NULL)
8595 return;
8596 if (p2p_dev_addr) {
8597 p->p2p = 1;
8598 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8599 } else {
8600 p->p2p = 0;
8601 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8602 }
8603 os_memcpy(p->psk, psk, psk_len);
8604
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008605 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8606 (last = dl_list_last(&persistent->psk_list,
8607 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008608 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8609 MACSTR " (p2p=%u) to make room for a new one",
8610 MAC2STR(last->addr), last->p2p);
8611 dl_list_del(&last->list);
8612 os_free(last);
8613 }
8614
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008615 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008616 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8617 p2p_dev_addr == NULL);
8618 if (p2p_dev_addr) {
8619 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8620 MACSTR, MAC2STR(p2p_dev_addr));
8621 } else {
8622 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8623 MAC2STR(mac_addr));
8624 }
8625 dl_list_add(&persistent->psk_list, &p->list);
8626
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008627 if (wpa_s->p2pdev->conf->update_config &&
8628 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008629 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008630}
8631
8632
8633static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8634 struct wpa_ssid *s, const u8 *addr,
8635 int iface_addr)
8636{
8637 int res;
8638
8639 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008640 if (res > 0 && wpa_s->conf->update_config &&
8641 wpa_config_write(wpa_s->confname, wpa_s->conf))
8642 wpa_dbg(wpa_s, MSG_DEBUG,
8643 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008644}
8645
8646
8647static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8648 const u8 *peer, int iface_addr)
8649{
8650 struct hostapd_data *hapd;
8651 struct hostapd_wpa_psk *psk, *prev, *rem;
8652 struct sta_info *sta;
8653
8654 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8655 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8656 return;
8657
8658 /* Remove per-station PSK entry */
8659 hapd = wpa_s->ap_iface->bss[0];
8660 prev = NULL;
8661 psk = hapd->conf->ssid.wpa_psk;
8662 while (psk) {
8663 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8664 (!iface_addr &&
8665 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8666 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8667 MACSTR " iface_addr=%d",
8668 MAC2STR(peer), iface_addr);
8669 if (prev)
8670 prev->next = psk->next;
8671 else
8672 hapd->conf->ssid.wpa_psk = psk->next;
8673 rem = psk;
8674 psk = psk->next;
8675 os_free(rem);
8676 } else {
8677 prev = psk;
8678 psk = psk->next;
8679 }
8680 }
8681
8682 /* Disconnect from group */
8683 if (iface_addr)
8684 sta = ap_get_sta(hapd, peer);
8685 else
8686 sta = ap_get_sta_p2p(hapd, peer);
8687 if (sta) {
8688 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8689 " (iface_addr=%d) from group",
8690 MAC2STR(peer), iface_addr);
8691 hostapd_drv_sta_deauth(hapd, sta->addr,
8692 WLAN_REASON_DEAUTH_LEAVING);
8693 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8694 }
8695}
8696
8697
8698void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8699 int iface_addr)
8700{
8701 struct wpa_ssid *s;
8702 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008703 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008704
8705 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8706
8707 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008708 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008709 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8710 continue;
8711 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008712 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8713 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008714 }
8715
8716 /* Remove from any operating group */
8717 for (w = wpa_s->global->ifaces; w; w = w->next)
8718 wpas_p2p_remove_client_go(w, peer, iface_addr);
8719}
8720
8721
8722static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8723{
8724 struct wpa_supplicant *wpa_s = eloop_ctx;
8725 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8726}
8727
8728
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008729static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8730{
8731 struct wpa_supplicant *wpa_s = eloop_ctx;
8732
8733 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8734 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8735}
8736
8737
8738int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8739 struct wpa_ssid *ssid)
8740{
8741 struct wpa_supplicant *iface;
8742
8743 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8744 if (!iface->current_ssid ||
8745 iface->current_ssid->frequency == freq ||
8746 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8747 !iface->current_ssid->p2p_group))
8748 continue;
8749
8750 /* Remove the connection with least priority */
8751 if (!wpas_is_p2p_prioritized(iface)) {
8752 /* STA connection has priority over existing
8753 * P2P connection, so remove the interface. */
8754 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8755 eloop_register_timeout(0, 0,
8756 wpas_p2p_group_freq_conflict,
8757 iface, NULL);
8758 /* If connection in progress is P2P connection, do not
8759 * proceed for the connection. */
8760 if (wpa_s == iface)
8761 return -1;
8762 else
8763 return 0;
8764 } else {
8765 /* P2P connection has priority, disable the STA network
8766 */
8767 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8768 ssid);
8769 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8770 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8771 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8772 ETH_ALEN);
8773 /* If P2P connection is in progress, continue
8774 * connecting...*/
8775 if (wpa_s == iface)
8776 return 0;
8777 else
8778 return -1;
8779 }
8780 }
8781
8782 return 0;
8783}
8784
8785
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008786int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8787{
8788 struct wpa_ssid *ssid = wpa_s->current_ssid;
8789
8790 if (ssid == NULL || !ssid->p2p_group)
8791 return 0;
8792
8793 if (wpa_s->p2p_last_4way_hs_fail &&
8794 wpa_s->p2p_last_4way_hs_fail == ssid) {
8795 u8 go_dev_addr[ETH_ALEN];
8796 struct wpa_ssid *persistent;
8797
8798 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8799 ssid->ssid,
8800 ssid->ssid_len) <= 0) {
8801 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8802 goto disconnect;
8803 }
8804
8805 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8806 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008807 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008808 ssid->ssid,
8809 ssid->ssid_len);
8810 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8811 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8812 goto disconnect;
8813 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008814 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008815 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8816 persistent->id);
8817 disconnect:
8818 wpa_s->p2p_last_4way_hs_fail = NULL;
8819 /*
8820 * Remove the group from a timeout to avoid issues with caller
8821 * continuing to use the interface if this is on a P2P group
8822 * interface.
8823 */
8824 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8825 wpa_s, NULL);
8826 return 1;
8827 }
8828
8829 wpa_s->p2p_last_4way_hs_fail = ssid;
8830 return 0;
8831}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008832
8833
8834#ifdef CONFIG_WPS_NFC
8835
8836static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8837 struct wpabuf *p2p)
8838{
8839 struct wpabuf *ret;
8840 size_t wsc_len;
8841
8842 if (p2p == NULL) {
8843 wpabuf_free(wsc);
8844 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8845 return NULL;
8846 }
8847
8848 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8849 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8850 if (ret == NULL) {
8851 wpabuf_free(wsc);
8852 wpabuf_free(p2p);
8853 return NULL;
8854 }
8855
8856 wpabuf_put_be16(ret, wsc_len);
8857 if (wsc)
8858 wpabuf_put_buf(ret, wsc);
8859 wpabuf_put_be16(ret, wpabuf_len(p2p));
8860 wpabuf_put_buf(ret, p2p);
8861
8862 wpabuf_free(wsc);
8863 wpabuf_free(p2p);
8864 wpa_hexdump_buf(MSG_DEBUG,
8865 "P2P: Generated NFC connection handover message", ret);
8866
8867 if (ndef && ret) {
8868 struct wpabuf *tmp;
8869 tmp = ndef_build_p2p(ret);
8870 wpabuf_free(ret);
8871 if (tmp == NULL) {
8872 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8873 return NULL;
8874 }
8875 ret = tmp;
8876 }
8877
8878 return ret;
8879}
8880
8881
8882static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8883 struct wpa_ssid **ssid, u8 *go_dev_addr)
8884{
8885 struct wpa_supplicant *iface;
8886
8887 if (go_dev_addr)
8888 os_memset(go_dev_addr, 0, ETH_ALEN);
8889 if (ssid)
8890 *ssid = NULL;
8891 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8892 if (iface->wpa_state < WPA_ASSOCIATING ||
8893 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8894 !iface->current_ssid->p2p_group ||
8895 iface->current_ssid->mode != WPAS_MODE_INFRA)
8896 continue;
8897 if (ssid)
8898 *ssid = iface->current_ssid;
8899 if (go_dev_addr)
8900 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8901 return iface->assoc_freq;
8902 }
8903 return 0;
8904}
8905
8906
8907struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8908 int ndef)
8909{
8910 struct wpabuf *wsc, *p2p;
8911 struct wpa_ssid *ssid;
8912 u8 go_dev_addr[ETH_ALEN];
8913 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8914
8915 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8916 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8917 return NULL;
8918 }
8919
8920 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8921 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8922 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8924 return NULL;
8925 }
8926
8927 if (cli_freq == 0) {
8928 wsc = wps_build_nfc_handover_req_p2p(
8929 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8930 } else
8931 wsc = NULL;
8932 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8933 go_dev_addr, ssid ? ssid->ssid : NULL,
8934 ssid ? ssid->ssid_len : 0);
8935
8936 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8937}
8938
8939
8940struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8941 int ndef, int tag)
8942{
8943 struct wpabuf *wsc, *p2p;
8944 struct wpa_ssid *ssid;
8945 u8 go_dev_addr[ETH_ALEN];
8946 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8947
8948 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8949 return NULL;
8950
8951 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8952 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8953 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8954 return NULL;
8955
8956 if (cli_freq == 0) {
8957 wsc = wps_build_nfc_handover_sel_p2p(
8958 wpa_s->parent->wps,
8959 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8960 DEV_PW_NFC_CONNECTION_HANDOVER,
8961 wpa_s->conf->wps_nfc_dh_pubkey,
8962 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8963 } else
8964 wsc = NULL;
8965 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8966 go_dev_addr, ssid ? ssid->ssid : NULL,
8967 ssid ? ssid->ssid_len : 0);
8968
8969 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8970}
8971
8972
8973static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8974 struct p2p_nfc_params *params)
8975{
8976 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8977 "connection handover (freq=%d)",
8978 params->go_freq);
8979
8980 if (params->go_freq && params->go_ssid_len) {
8981 wpa_s->p2p_wps_method = WPS_NFC;
8982 wpa_s->pending_join_wps_method = WPS_NFC;
8983 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8984 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8985 ETH_ALEN);
8986 return wpas_p2p_join_start(wpa_s, params->go_freq,
8987 params->go_ssid,
8988 params->go_ssid_len);
8989 }
8990
8991 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8992 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008993 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008994 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008995 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008996 params->go_ssid_len ? params->go_ssid : NULL,
8997 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008998}
8999
9000
9001static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9002 struct p2p_nfc_params *params, int tag)
9003{
9004 int res, persistent;
9005 struct wpa_ssid *ssid;
9006
9007 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9008 "connection handover");
9009 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9010 ssid = wpa_s->current_ssid;
9011 if (ssid == NULL)
9012 continue;
9013 if (ssid->mode != WPAS_MODE_P2P_GO)
9014 continue;
9015 if (wpa_s->ap_iface == NULL)
9016 continue;
9017 break;
9018 }
9019 if (wpa_s == NULL) {
9020 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9021 return -1;
9022 }
9023
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009024 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009025 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009026 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009027 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9028 return -1;
9029 }
9030 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009031 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9032 wpa_s->p2pdev->p2p_oob_dev_pw,
9033 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9034 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009035 if (res)
9036 return res;
9037
9038 if (!tag) {
9039 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9040 return 0;
9041 }
9042
9043 if (!params->peer ||
9044 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9045 return 0;
9046
9047 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9048 " to join", MAC2STR(params->peer->p2p_device_addr));
9049
9050 wpa_s->global->p2p_invite_group = wpa_s;
9051 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009052 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009053 params->peer->p2p_device_addr,
9054 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009055 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009056
9057 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9058 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9059 ssid->ssid, ssid->ssid_len, ssid->frequency,
9060 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009061 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009062}
9063
9064
9065static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9066 struct p2p_nfc_params *params,
9067 int forced_freq)
9068{
9069 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9070 "connection handover");
9071 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9072 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009073 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009074 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009075 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9076 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009077}
9078
9079
9080static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9081 struct p2p_nfc_params *params,
9082 int forced_freq)
9083{
9084 int res;
9085
9086 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9087 "connection handover");
9088 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9089 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009090 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009091 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009092 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
9093 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009094 if (res)
9095 return res;
9096
9097 res = wpas_p2p_listen(wpa_s, 60);
9098 if (res) {
9099 p2p_unauthorize(wpa_s->global->p2p,
9100 params->peer->p2p_device_addr);
9101 }
9102
9103 return res;
9104}
9105
9106
9107static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9108 const struct wpabuf *data,
9109 int sel, int tag, int forced_freq)
9110{
9111 const u8 *pos, *end;
9112 u16 len, id;
9113 struct p2p_nfc_params params;
9114 int res;
9115
9116 os_memset(&params, 0, sizeof(params));
9117 params.sel = sel;
9118
9119 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9120
9121 pos = wpabuf_head(data);
9122 end = pos + wpabuf_len(data);
9123
9124 if (end - pos < 2) {
9125 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9126 "attributes");
9127 return -1;
9128 }
9129 len = WPA_GET_BE16(pos);
9130 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009131 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009132 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9133 "attributes");
9134 return -1;
9135 }
9136 params.wsc_attr = pos;
9137 params.wsc_len = len;
9138 pos += len;
9139
9140 if (end - pos < 2) {
9141 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9142 "attributes");
9143 return -1;
9144 }
9145 len = WPA_GET_BE16(pos);
9146 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009147 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009148 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9149 "attributes");
9150 return -1;
9151 }
9152 params.p2p_attr = pos;
9153 params.p2p_len = len;
9154 pos += len;
9155
9156 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9157 params.wsc_attr, params.wsc_len);
9158 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9159 params.p2p_attr, params.p2p_len);
9160 if (pos < end) {
9161 wpa_hexdump(MSG_DEBUG,
9162 "P2P: Ignored extra data after P2P attributes",
9163 pos, end - pos);
9164 }
9165
9166 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9167 if (res)
9168 return res;
9169
9170 if (params.next_step == NO_ACTION)
9171 return 0;
9172
9173 if (params.next_step == BOTH_GO) {
9174 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9175 MAC2STR(params.peer->p2p_device_addr));
9176 return 0;
9177 }
9178
9179 if (params.next_step == PEER_CLIENT) {
9180 if (!is_zero_ether_addr(params.go_dev_addr)) {
9181 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9182 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9183 " ssid=\"%s\"",
9184 MAC2STR(params.peer->p2p_device_addr),
9185 params.go_freq,
9186 MAC2STR(params.go_dev_addr),
9187 wpa_ssid_txt(params.go_ssid,
9188 params.go_ssid_len));
9189 } else {
9190 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9191 "peer=" MACSTR " freq=%d",
9192 MAC2STR(params.peer->p2p_device_addr),
9193 params.go_freq);
9194 }
9195 return 0;
9196 }
9197
9198 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9199 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9200 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9201 return 0;
9202 }
9203
9204 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9205 wpa_s->p2p_oob_dev_pw = NULL;
9206
9207 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9208 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9209 "received");
9210 return -1;
9211 }
9212
9213 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9214 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9215 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9216 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9217 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9218 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9219 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9220
9221 if (tag) {
9222 if (id < 0x10) {
9223 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9224 "peer OOB Device Password Id %u", id);
9225 return -1;
9226 }
9227 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9228 "Device Password Id %u", id);
9229 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9230 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9231 params.oob_dev_pw_len -
9232 WPS_OOB_PUBKEY_HASH_LEN - 2);
9233 wpa_s->p2p_oob_dev_pw_id = id;
9234 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9235 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9236 params.oob_dev_pw_len -
9237 WPS_OOB_PUBKEY_HASH_LEN - 2);
9238 if (wpa_s->p2p_oob_dev_pw == NULL)
9239 return -1;
9240
9241 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9242 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9243 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9244 return -1;
9245 } else {
9246 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9247 "without Device Password");
9248 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9249 }
9250
9251 switch (params.next_step) {
9252 case NO_ACTION:
9253 case BOTH_GO:
9254 case PEER_CLIENT:
9255 /* already covered above */
9256 return 0;
9257 case JOIN_GROUP:
9258 return wpas_p2p_nfc_join_group(wpa_s, &params);
9259 case AUTH_JOIN:
9260 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9261 case INIT_GO_NEG:
9262 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9263 case RESP_GO_NEG:
9264 /* TODO: use own OOB Dev Pw */
9265 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9266 }
9267
9268 return -1;
9269}
9270
9271
9272int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9273 const struct wpabuf *data, int forced_freq)
9274{
9275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9276 return -1;
9277
9278 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9279}
9280
9281
9282int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9283 const struct wpabuf *req,
9284 const struct wpabuf *sel, int forced_freq)
9285{
9286 struct wpabuf *tmp;
9287 int ret;
9288
9289 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9290 return -1;
9291
9292 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9293
9294 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9295 wpabuf_head(req), wpabuf_len(req));
9296 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9297 wpabuf_head(sel), wpabuf_len(sel));
9298 if (forced_freq)
9299 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9300 tmp = ndef_parse_p2p(init ? sel : req);
9301 if (tmp == NULL) {
9302 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9303 return -1;
9304 }
9305
9306 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9307 forced_freq);
9308 wpabuf_free(tmp);
9309
9310 return ret;
9311}
9312
9313
9314int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9315{
9316 const u8 *if_addr;
9317 int go_intent = wpa_s->conf->p2p_go_intent;
9318 struct wpa_supplicant *iface;
9319
9320 if (wpa_s->global->p2p == NULL)
9321 return -1;
9322
9323 if (!enabled) {
9324 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9325 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9326 {
9327 if (!iface->ap_iface)
9328 continue;
9329 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9330 }
9331 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9332 0, NULL);
9333 if (wpa_s->p2p_nfc_tag_enabled)
9334 wpas_p2p_remove_pending_group_interface(wpa_s);
9335 wpa_s->p2p_nfc_tag_enabled = 0;
9336 return 0;
9337 }
9338
9339 if (wpa_s->global->p2p_disabled)
9340 return -1;
9341
9342 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9343 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9344 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9345 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9346 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9347 "to allow static handover cases");
9348 return -1;
9349 }
9350
9351 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9352
9353 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9354 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9355 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9356 if (wpa_s->p2p_oob_dev_pw == NULL)
9357 return -1;
9358 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9359
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009360 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9361 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9362 /*
9363 * P2P Group Interface present and the command came on group
9364 * interface, so enable the token for the current interface.
9365 */
9366 wpa_s->create_p2p_iface = 0;
9367 } else {
9368 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9369 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009370
9371 if (wpa_s->create_p2p_iface) {
9372 enum wpa_driver_if_type iftype;
9373 /* Prepare to add a new interface for the group */
9374 iftype = WPA_IF_P2P_GROUP;
9375 if (go_intent == 15)
9376 iftype = WPA_IF_P2P_GO;
9377 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9378 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9379 "interface for the group");
9380 return -1;
9381 }
9382
9383 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009384 } else if (wpa_s->p2p_mgmt)
9385 if_addr = wpa_s->parent->own_addr;
9386 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009387 if_addr = wpa_s->own_addr;
9388
9389 wpa_s->p2p_nfc_tag_enabled = enabled;
9390
9391 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9392 struct hostapd_data *hapd;
9393 if (iface->ap_iface == NULL)
9394 continue;
9395 hapd = iface->ap_iface->bss[0];
9396 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9397 hapd->conf->wps_nfc_dh_pubkey =
9398 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9399 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9400 hapd->conf->wps_nfc_dh_privkey =
9401 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9402 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9403 hapd->conf->wps_nfc_dev_pw =
9404 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9405 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9406
9407 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9408 wpa_dbg(iface, MSG_DEBUG,
9409 "P2P: Failed to enable NFC Tag for GO");
9410 }
9411 }
9412 p2p_set_authorized_oob_dev_pw_id(
9413 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9414 if_addr);
9415
9416 return 0;
9417}
9418
9419#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009420
9421
9422static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9423 struct wpa_used_freq_data *freqs,
9424 unsigned int num)
9425{
9426 u8 curr_chan, cand, chan;
9427 unsigned int i;
9428
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009429 /*
9430 * If possible, optimize the Listen channel to be a channel that is
9431 * already used by one of the other interfaces.
9432 */
9433 if (!wpa_s->conf->p2p_optimize_listen_chan)
9434 return;
9435
9436 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9437 return;
9438
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009439 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9440 for (i = 0, cand = 0; i < num; i++) {
9441 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9442 if (curr_chan == chan) {
9443 cand = 0;
9444 break;
9445 }
9446
9447 if (chan == 1 || chan == 6 || chan == 11)
9448 cand = chan;
9449 }
9450
9451 if (cand) {
9452 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009453 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009454 cand);
9455 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9456 }
9457}
9458
9459
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009460static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009461{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009462 struct hostapd_config *conf;
9463 struct p2p_go_neg_results params;
9464 struct csa_settings csa_settings;
9465 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9466 int old_freq = current_ssid->frequency;
9467 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009468
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009469 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9470 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9471 return -1;
9472 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009473
9474 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009475 * TODO: This function may not always work correctly. For example,
9476 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009477 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009478 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9479 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009480 wpa_dbg(wpa_s, MSG_DEBUG,
9481 "P2P CSA: Failed to select new frequency for GO");
9482 return -1;
9483 }
9484
9485 if (current_ssid->frequency == params.freq) {
9486 wpa_dbg(wpa_s, MSG_DEBUG,
9487 "P2P CSA: Selected same frequency - not moving GO");
9488 return 0;
9489 }
9490
9491 conf = hostapd_config_defaults();
9492 if (!conf) {
9493 wpa_dbg(wpa_s, MSG_DEBUG,
9494 "P2P CSA: Failed to allocate default config");
9495 return -1;
9496 }
9497
9498 current_ssid->frequency = params.freq;
9499 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9500 wpa_dbg(wpa_s, MSG_DEBUG,
9501 "P2P CSA: Failed to create new GO config");
9502 ret = -1;
9503 goto out;
9504 }
9505
9506 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9507 wpa_dbg(wpa_s, MSG_DEBUG,
9508 "P2P CSA: CSA to a different band is not supported");
9509 ret = -1;
9510 goto out;
9511 }
9512
9513 os_memset(&csa_settings, 0, sizeof(csa_settings));
9514 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9515 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9516 csa_settings.freq_params.freq = params.freq;
9517 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9518 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9519 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9520
9521 if (conf->ieee80211ac) {
9522 int freq1 = 0, freq2 = 0;
9523 u8 chan, opclass;
9524
9525 if (ieee80211_freq_to_channel_ext(params.freq,
9526 conf->secondary_channel,
9527 conf->vht_oper_chwidth,
9528 &opclass, &chan) ==
9529 NUM_HOSTAPD_MODES) {
9530 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9531 ret = -1;
9532 goto out;
9533 }
9534
9535 if (conf->vht_oper_centr_freq_seg0_idx)
9536 freq1 = ieee80211_chan_to_freq(
9537 NULL, opclass,
9538 conf->vht_oper_centr_freq_seg0_idx);
9539
9540 if (conf->vht_oper_centr_freq_seg1_idx)
9541 freq2 = ieee80211_chan_to_freq(
9542 NULL, opclass,
9543 conf->vht_oper_centr_freq_seg1_idx);
9544
9545 if (freq1 < 0 || freq2 < 0) {
9546 wpa_dbg(wpa_s, MSG_DEBUG,
9547 "P2P CSA: Selected invalid VHT center freqs");
9548 ret = -1;
9549 goto out;
9550 }
9551
9552 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9553 csa_settings.freq_params.center_freq1 = freq1;
9554 csa_settings.freq_params.center_freq2 = freq2;
9555
9556 switch (conf->vht_oper_chwidth) {
Hai Shalom81f62d82019-07-22 12:10:00 -07009557 case CHANWIDTH_80MHZ:
9558 case CHANWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009559 csa_settings.freq_params.bandwidth = 80;
9560 break;
Hai Shalom81f62d82019-07-22 12:10:00 -07009561 case CHANWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009562 csa_settings.freq_params.bandwidth = 160;
9563 break;
9564 }
9565 }
9566
9567 ret = ap_switch_channel(wpa_s, &csa_settings);
9568out:
9569 current_ssid->frequency = old_freq;
9570 hostapd_config_free(conf);
9571 return ret;
9572}
9573
9574
9575static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9576{
9577 struct p2p_go_neg_results params;
9578 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009579 void (*ap_configured_cb)(void *ctx, void *data);
9580 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009581
9582 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9583
9584 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9585 current_ssid->frequency);
9586
9587 /* Stop the AP functionality */
9588 /* TODO: Should do this in a way that does not indicated to possible
9589 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009590 /* If this action occurs before a group is started, the callback should
9591 * be preserved, or GROUP-STARTED event would be lost. If this action
9592 * occurs after a group is started, these pointers are all NULL and
9593 * harmless. */
9594 ap_configured_cb = wpa_s->ap_configured_cb;
9595 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9596 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009597 wpa_supplicant_ap_deinit(wpa_s);
9598
9599 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009600 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9601 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009602 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9603 wpas_p2p_group_delete(wpa_s,
9604 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9605 return;
9606 }
9607 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9608 params.freq);
9609
9610 if (params.freq &&
9611 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9612 wpa_printf(MSG_DEBUG,
9613 "P2P: Selected freq (%u MHz) is not valid for P2P",
9614 params.freq);
9615 wpas_p2p_group_delete(wpa_s,
9616 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9617 return;
9618 }
9619
Hai Shalom899fcc72020-10-19 14:38:18 -07009620 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009621 wpa_s->ap_configured_cb = ap_configured_cb;
9622 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9623 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009624
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009625 /* Update the frequency */
9626 current_ssid->frequency = params.freq;
9627 wpa_s->connect_without_scan = current_ssid;
9628 wpa_s->reassociate = 1;
9629 wpa_s->disconnected = 0;
9630 wpa_supplicant_req_scan(wpa_s, 0, 0);
9631}
9632
9633
9634static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9635{
9636 struct wpa_supplicant *wpa_s = eloop_ctx;
9637
9638 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009639 return;
9640
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009641 wpas_p2p_go_update_common_freqs(wpa_s);
9642
9643 /* Do not move GO in the middle of a CSA */
9644 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9645 wpa_printf(MSG_DEBUG,
9646 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009647 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009648 }
9649
9650 /*
9651 * First, try a channel switch flow. If it is not supported or fails,
9652 * take down the GO and bring it up again.
9653 */
9654 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9655 wpas_p2p_move_go_no_csa(wpa_s);
9656}
9657
9658
9659static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9660{
9661 struct wpa_supplicant *wpa_s = eloop_ctx;
9662 struct wpa_used_freq_data *freqs = NULL;
9663 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009664
9665 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9666 if (!freqs)
9667 return;
9668
9669 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9670
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009671 /* Previous attempt to move a GO was not possible -- try again. */
9672 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9673 WPAS_P2P_CHANNEL_UPDATE_ANY);
9674
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009675 os_free(freqs);
9676}
9677
9678
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009679/*
9680 * Consider moving a GO from its currently used frequency:
9681 * 1. It is possible that due to regulatory consideration the frequency
9682 * can no longer be used and there is a need to evacuate the GO.
9683 * 2. It is possible that due to MCC considerations, it would be preferable
9684 * to move the GO to a channel that is currently used by some other
9685 * station interface.
9686 *
9687 * In case a frequency that became invalid is once again valid, cancel a
9688 * previously initiated GO frequency change.
9689 */
9690static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9691 struct wpa_used_freq_data *freqs,
9692 unsigned int num)
9693{
9694 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9695 unsigned int timeout;
9696 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009697 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009698
9699 wpas_p2p_go_update_common_freqs(wpa_s);
9700
9701 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009702 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009703 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009704 for (i = 0, invalid_freq = 0; i < num; i++) {
9705 if (freqs[i].freq == freq) {
9706 flags = freqs[i].flags;
9707
9708 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009709 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9710 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009711 wpa_dbg(wpa_s, MSG_DEBUG,
9712 "P2P: Freq=%d MHz no longer valid for GO",
9713 freq);
9714 invalid_freq = 1;
9715 }
9716 } else if (freqs[i].flags == 0) {
9717 /* Freq is not used by any other station interface */
9718 continue;
9719 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009720 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009721 /* Freq is not valid for P2P use cases */
9722 continue;
9723 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9724 P2P_GO_FREQ_MOVE_SCM) {
9725 policy_move = 1;
9726 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9727 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9728 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9729 policy_move = 1;
9730 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9731 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9732 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9733 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9734 policy_move = 1;
9735 } else if ((wpa_s->drv_flags &
9736 WPA_DRIVER_FLAGS_AP_CSA) &&
9737 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9738 u8 chan;
9739
9740 /*
9741 * We do not support CSA between bands, so move
9742 * GO only within the same band.
9743 */
9744 if (wpa_s->ap_iface->current_mode->mode ==
9745 ieee80211_freq_to_chan(freqs[i].freq,
9746 &chan))
9747 policy_move = 1;
9748 }
9749 }
9750 }
9751
9752 wpa_dbg(wpa_s, MSG_DEBUG,
9753 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9754 invalid_freq, policy_move, flags);
9755
9756 /*
9757 * The channel is valid, or we are going to have a policy move, so
9758 * cancel timeout.
9759 */
9760 if (!invalid_freq || policy_move) {
9761 wpa_dbg(wpa_s, MSG_DEBUG,
9762 "P2P: Cancel a GO move from freq=%d MHz", freq);
9763 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9764
9765 if (wpas_p2p_in_progress(wpa_s)) {
9766 wpa_dbg(wpa_s, MSG_DEBUG,
9767 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9768 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9769 wpa_s, NULL);
9770 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9771 wpas_p2p_reconsider_moving_go,
9772 wpa_s, NULL);
9773 return;
9774 }
9775 }
9776
9777 if (!invalid_freq && (!policy_move || flags != 0)) {
9778 wpa_dbg(wpa_s, MSG_DEBUG,
9779 "P2P: Not initiating a GO frequency change");
9780 return;
9781 }
9782
9783 /*
9784 * Do not consider moving GO if it is in the middle of a CSA. When the
9785 * CSA is finished this flow should be retriggered.
9786 */
9787 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9788 wpa_dbg(wpa_s, MSG_DEBUG,
9789 "P2P: Not initiating a GO frequency change - CSA is in progress");
9790 return;
9791 }
9792
9793 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9794 timeout = P2P_GO_FREQ_CHANGE_TIME;
9795 else
9796 timeout = 0;
9797
9798 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9799 freq, timeout);
9800 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9801 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9802}
9803
9804
9805static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9806 struct wpa_used_freq_data *freqs,
9807 unsigned int num,
9808 enum wpas_p2p_channel_update_trig trig)
9809{
9810 struct wpa_supplicant *ifs;
9811
9812 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9813 NULL);
9814
9815 /*
9816 * Travers all the radio interfaces, and for each GO interface, check
9817 * if there is a need to move the GO from the frequency it is using,
9818 * or in case the frequency is valid again, cancel the evacuation flow.
9819 */
9820 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9821 radio_list) {
9822 if (ifs->current_ssid == NULL ||
9823 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9824 continue;
9825
9826 /*
9827 * The GO was just started or completed channel switch, no need
9828 * to move it.
9829 */
9830 if (wpa_s == ifs &&
9831 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9832 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9833 wpa_dbg(wpa_s, MSG_DEBUG,
9834 "P2P: GO move - schedule re-consideration");
9835 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9836 wpas_p2p_reconsider_moving_go,
9837 wpa_s, NULL);
9838 continue;
9839 }
9840
9841 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9842 }
9843}
9844
9845
9846void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9847{
9848 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9849 return;
9850
9851 wpas_p2p_update_channel_list(wpa_s,
9852 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9853}
9854
9855
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009856void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9857{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009858 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9860 "the management interface is being removed");
9861 wpas_p2p_deinit_global(wpa_s->global);
9862 }
9863}
9864
9865
9866void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9867{
9868 if (wpa_s->ap_iface->bss)
9869 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9870 wpas_p2p_group_deinit(wpa_s);
9871}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009872
9873
9874int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9875 unsigned int period, unsigned int interval,
9876 unsigned int count)
9877{
9878 struct p2p_data *p2p = wpa_s->global->p2p;
9879 u8 *device_types;
9880 size_t dev_types_len;
9881 struct wpabuf *buf;
9882 int ret;
9883
9884 if (wpa_s->p2p_lo_started) {
9885 wpa_dbg(wpa_s, MSG_DEBUG,
9886 "P2P Listen offload is already started");
9887 return 0;
9888 }
9889
9890 if (wpa_s->global->p2p == NULL ||
9891 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9892 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9893 return -1;
9894 }
9895
9896 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9897 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9898 freq);
9899 return -1;
9900 }
9901
9902 /* Get device type */
9903 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9904 WPS_DEV_TYPE_LEN;
9905 device_types = os_malloc(dev_types_len);
9906 if (!device_types)
9907 return -1;
9908 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9909 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9910 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9911
9912 /* Get Probe Response IE(s) */
9913 buf = p2p_build_probe_resp_template(p2p, freq);
9914 if (!buf) {
9915 os_free(device_types);
9916 return -1;
9917 }
9918
9919 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9920 device_types, dev_types_len,
9921 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9922 if (ret < 0)
9923 wpa_dbg(wpa_s, MSG_DEBUG,
9924 "P2P: Failed to start P2P listen offload");
9925
9926 os_free(device_types);
9927 wpabuf_free(buf);
9928
9929 if (ret == 0) {
9930 wpa_s->p2p_lo_started = 1;
9931
9932 /* Stop current P2P listen if any */
9933 wpas_stop_listen(wpa_s);
9934 }
9935
9936 return ret;
9937}
9938
9939
9940int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9941{
9942 int ret;
9943
9944 if (!wpa_s->p2p_lo_started)
9945 return 0;
9946
9947 ret = wpa_drv_p2p_lo_stop(wpa_s);
9948 if (ret < 0)
9949 wpa_dbg(wpa_s, MSG_DEBUG,
9950 "P2P: Failed to stop P2P listen offload");
9951
9952 wpa_s->p2p_lo_started = 0;
9953 return ret;
9954}